Anyone want STL (C++) Style Iterators+Containers?



Anyone interested in the code that does this?

I've been hacking this up in my spare time, so its hardly production
ready, but I figure if there is interest it can sit in a separate
library (EGG?) until it matures. Maybe one day, around 3.0-ish, it might
make it into Glib?

I'd be happy to explain it at length, but if no one cares, I'll just
hack on it until I get bored with it.

The basics:
- containers are gobjects
- iterators totally separate data types and algorithms
- basically copied right out of an STL reference guide

Cheers,
Ryan
#include <stdio.h>
#include <stdlib.h>
#include "eggalgorithm.h"
#include "egglist.h"

gboolean
cmp_gint (gpointer p, EggIterator *i)
{
	const gint* v1	= p;
	const gint* v2	= g_value_get_pointer (egg_iterator_get_value (i));
	
	return	(*v1 == *v2);
}

int	main()
{
	EggList 		*list;
	EggListNode		*current;
	EggIterator		*iter;
	const EggIterator	*list_begin;
	const EggIterator	*list_end;
	EggUnPredicate		*pred;
	
	gint			*p, i, N=25;

	
	/* Initialize our types */
	g_type_init();

	list 	= g_object_new (EGG_TYPE_LIST, NULL);
	iter 	= egg_collection_new_iterator (EGG_COLLECTION (list));
	pred	= g_new (EggUnPredicate, 1);
	

	/* Do some insertions. */
	egg_iterator_assign (iter, egg_collection_end (EGG_COLLECTION (list)));
	
	for (i=0; i<N; i++)
	{
		p = g_new (gint, 1);
		*p = i;

		egg_list_insert (list, iter, p);
	}
	
	list_begin	= egg_collection_begin (EGG_COLLECTION (list));
	list_end	= egg_collection_end (EGG_COLLECTION (list));
	

	/* Lookup all values */
	egg_iterator_assign (iter, list_begin);

	while (!egg_iterator_equal (iter, list_end))
	{
		p = (gint*) g_value_get_pointer (egg_iterator_get_value (iter));
		g_print ("%d\n", *p);

		egg_iterator_fore (iter);
	}
	
	
	/* Find a value */
	p = g_new (gint, 1);
	*p = 5;
	
	pred-> data = p;
	pred-> function = cmp_gint;
	
	egg_iterator_assign (iter, egg_find (list_begin, list_end, pred));
	
	p = (gint*) g_value_get_pointer (egg_iterator_get_value (iter));
	g_print ("We found %d\n", *p);
	
	
	/* Clean up */
	g_object_unref (G_OBJECT (list));
	g_free (iter);
	g_free (pred);
	
	return	0;
}


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]