From cccd13db92c7b6ca9a12c33f38a84366d5b647ff Mon Sep 17 00:00:00 2001
From: axel <axel@liljencrantz.se>
Date: Tue, 1 Aug 2006 02:16:41 +1000
Subject: [PATCH] Add long and function pointer versions of all array_list_t
 calls

darcs-hash:20060731161641-ac50b-1850b9c2464ce1aa7ff2f1aa74140b526a6825f2.gz
---
 util.c | 140 ++++++++++++++++++++++++++++++++++++++++++++++++---------
 util.h |  59 ++++++++++++++++++++++--
 2 files changed, 175 insertions(+), 24 deletions(-)

diff --git a/util.c b/util.c
index c06d601c5..ace6fa270 100644
--- a/util.c
+++ b/util.c
@@ -310,9 +310,14 @@ void *hash_get( hash_table_t *h,
 	
 	int pos = hash_search( h, (void *)key );	
 	if( h->arr[pos].key == 0 )
+	{
 		return 0;
+	}
 	else
-		return h->arr[pos].data;
+	{
+		void *res =h->arr[pos].data;
+		return res;
+	}
 }
 
 void *hash_get_key( hash_table_t *h,
@@ -680,21 +685,43 @@ void al_destroy( array_list_t *l )
 	free( l->arr );
 }
 
-int al_push( array_list_t *l, const void *o )
+static int al_push_generic( array_list_t *l, anything_t o )
 {
 	if( l->pos >= l->size )
 	{
 		int new_size = l->pos == 0 ? MIN_SIZE : 2 * l->pos;
-		void *tmp = realloc( l->arr, sizeof( void *)*new_size );
+		void *tmp = realloc( l->arr, sizeof( anything_t )*new_size );
 		if( tmp == 0 )
 			return 0;
 		l->arr = tmp;
 		l->size = new_size;		
 	}
-	l->arr[l->pos++] = (void *)o;
+	l->arr[l->pos++] = o;
 	return 1;
 }
 
+int al_push( array_list_t *l, const void *o )
+{
+	anything_t v;
+	v.ptr_val = (void *)o;
+	return al_push_generic( l, v );
+}
+
+int al_push_long( array_list_t *l, long val )
+{
+	anything_t v;
+	v.long_val = val;
+	return al_push_generic( l, v );
+}
+
+int al_push_func( array_list_t *l, func_ptr_t f )
+{
+	anything_t v;
+	v.func_val = f;
+	return al_push_generic( l, v );
+}
+
+
 int al_push_all( array_list_t *a, array_list_t *b )
 {
 	int k;
@@ -707,7 +734,7 @@ int al_push_all( array_list_t *a, array_list_t *b )
 }
 
 
-int al_set( array_list_t *l, int pos, const void *o )
+static int al_set_generic( array_list_t *l, int pos, anything_t v )
 {
 	int old_pos;
 	
@@ -715,48 +742,88 @@ int al_set( array_list_t *l, int pos, const void *o )
 		return 0;
 	if( pos < l->pos )
 	{
-		l->arr[pos] = (void *)o;
+		l->arr[pos]=v;
 		return 1;
 	}
 	old_pos=l->pos;
 	
 	l->pos = pos;
-	if( al_push( l, o ) )
+	if( al_push_generic( l, v ) )
 	{
 /*		fwprintf( stderr, L"Clearing from index %d to index %d\n", 
 				  old_pos, pos );
 */	
 		memset( &l->arr[old_pos], 
 				0,
-				sizeof(void *) * (pos - old_pos) );
+				sizeof(anything_t) * (pos - old_pos) );
 		return 1;		
 	}
 	return 0;	
 }
 
+int al_set( array_list_t *l, int pos, const void *o )
+{
+	anything_t v;
+	v.ptr_val = (void *)o;
+	return al_set_generic( l, pos, v );
+}
+
+int al_set_long( array_list_t *l, int pos, long o )
+{
+	anything_t v;
+	v.long_val = o;
+	return al_set_generic( l, pos, v );
+}
+
+int al_set_func( array_list_t *l, int pos, func_ptr_t o )
+{
+	anything_t v;
+	v.func_val = o;
+	return al_set_generic( l, pos, v );
+}
+
+static anything_t al_get_generic( array_list_t *l, int pos )
+{
+	anything_t res;
+	res.ptr_val=0;
+	
+	if( (pos >= 0) && (pos < l->pos) )
+		res = l->arr[pos];
+
+	return res;
+}
+
 void *al_get( array_list_t *l, int pos )
 {
-	if( pos < 0 )
-		return 0;
-	if( pos >= l->pos )
-		return 0;
-	return l->arr[pos];
+	return al_get_generic(l,pos).ptr_val;
 }
 
+long al_get_long( array_list_t *l, int pos )
+{
+	return al_get_generic(l,pos).long_val;
+}
+
+func_ptr_t al_get_func( array_list_t *l, int pos )
+{
+	return al_get_generic(l,pos).func_val;
+}
+
+
+
 void al_truncate( array_list_t *l, int new_sz )
 {
 	l->pos = new_sz;
 }
 
-void *al_pop( array_list_t *l )
+static anything_t al_pop_generic( array_list_t *l )
 {
-	void *e = l->arr[--l->pos];
+	anything_t e = l->arr[--l->pos];
 	if( (l->pos*3 < l->size) && (l->size < MIN_SIZE) )
 	{
-		void ** old_arr = l->arr;
+		anything_t *old_arr = l->arr;
 		int old_size = l->size;
 		l->size = l->size/2;
-		l->arr = realloc( l->arr, sizeof(void*)*l->size );
+		l->arr = realloc( l->arr, sizeof(anything_t)*l->size );
 		if( l->arr == 0 )
 		{
 			l->arr = old_arr;
@@ -766,10 +833,43 @@ void *al_pop( array_list_t *l )
 	return e;
 }
 
+void *al_pop( array_list_t *l )
+{
+	return al_pop_generic(l).ptr_val;	
+}
+
+long al_pop_long( array_list_t *l )
+{
+	return al_pop_generic(l).long_val;	
+}
+
+func_ptr_t al_pop_func( array_list_t *l )
+{
+	return al_pop_generic(l).func_val;	
+}
+
+static anything_t al_peek_generic( array_list_t *l )
+{
+	anything_t res;
+	res.ptr_val=0;
+	if( l->pos>0)
+		res = l->arr[l->pos-1];
+	return res;
+}
+
 void *al_peek( array_list_t *l )
 {
+	return al_peek_generic(l).ptr_val;	
+}
 
-	return l->pos>0?l->arr[l->pos-1]:0;
+long al_peek_long( array_list_t *l )
+{
+	return al_peek_generic(l).long_val;	
+}
+
+func_ptr_t al_peek_func( array_list_t *l )
+{
+	return al_peek_generic(l).func_val;	
 }
 
 int al_empty( array_list_t *l )
@@ -787,14 +887,14 @@ void al_foreach( array_list_t *l, void (*func)( void * ))
 {
 	int i;
 	for( i=0; i<l->pos; i++ )
-		func( l->arr[i] );
+		func( l->arr[i].ptr_val );
 }
 
 void al_foreach2( array_list_t *l, void (*func)( void *, void *), void *aux)
 {
 	int i;
 	for( i=0; i<l->pos; i++ )
-		func( l->arr[i], aux );
+		func( l->arr[i].ptr_val, aux );
 }
 
 int wcsfilecmp( const wchar_t *a, const wchar_t *b )
diff --git a/util.h b/util.h
index 286486c74..b2c6fec7b 100644
--- a/util.h
+++ b/util.h
@@ -15,6 +15,17 @@
 #include <stdarg.h>
 #include <unistd.h>
 
+typedef void (*func_ptr_t)();
+
+typedef union 
+{
+	long long_val;
+	void *ptr_val;
+	func_ptr_t func_val;
+}
+	anything_t;
+
+
 /**
    Data structure for an automatically resizing dynamically allocated queue,
 */
@@ -29,7 +40,7 @@ typedef struct dyn_queue
 	/** Where to remove elements */
 	void **get_pos;
 }
-dyn_queue_t;
+	dyn_queue_t;
 
 /**
    Internal struct used by hash_table_t.
@@ -41,7 +52,7 @@ typedef struct
 	/** Value */
 	void *data;
 }
-hash_struct_t;
+	hash_struct_t;
 
 /**
    Data structure for the hash table implementaion. A hash table allows for
@@ -71,7 +82,7 @@ typedef struct hash_table
 	/** Comparison function */
 	int (*compare_func)( void *key1, void *key2 );
 }
-hash_table_t;
+	hash_table_t;
 
 /**
    Data structure for an automatically resizing dynamically allocated
@@ -99,7 +110,7 @@ priority_queue_t;
 typedef struct array_list
 {
 	/** Array containing the data */
-	void **arr;
+	anything_t *arr;
 	/** Position to append elements at*/
 	int pos;
 	/** Length of array */
@@ -384,6 +395,24 @@ void al_destroy( array_list_t *l );
    \return 1 if succesfull, 0 otherwise
 */
 int al_push( array_list_t *l, const void *o );
+/**
+   Append element to list
+
+   \param l The list
+   \param o The element
+   \return
+   \return 1 if succesfull, 0 otherwise
+*/
+int al_push_long( array_list_t *l, long o );
+/**
+   Append element to list
+
+   \param l The list
+   \param o The element
+   \return
+   \return 1 if succesfull, 0 otherwise
+*/
+int al_push_func( array_list_t *l, void (*f)() );
 
 /**
    Append all elements of a list to another
@@ -402,6 +431,22 @@ int al_push_all( array_list_t *a, array_list_t *b );
    \param o The element 
 */
 int al_set( array_list_t *l, int pos, const void *o );
+/**
+   Sets the element at the specified index
+
+   \param l The array_list_t
+   \param pos The index 
+   \param o The element 
+*/
+int al_set_long( array_list_t *l, int pos, long v );
+/**
+   Sets the element at the specified index
+
+   \param l The array_list_t
+   \param pos The index 
+   \param o The element 
+*/
+int al_set_func( array_list_t *l, int pos, void (*f)() );
 
 /**
    Returns the element at the specified index
@@ -411,6 +456,8 @@ int al_set( array_list_t *l, int pos, const void *o );
    \return The element 
 */
 void *al_get( array_list_t *l, int pos );
+long al_get_long( array_list_t *l, int pos );
+func_ptr_t al_get_func( array_list_t *l, int pos );
 
 /**
   Truncates the list to new_sz items.
@@ -421,6 +468,8 @@ void al_truncate( array_list_t *l, int new_sz );
   Removes and returns the last entry in the list
 */
 void *al_pop( array_list_t *l );
+long al_pop_long( array_list_t *l );
+func_ptr_t al_pop_func( array_list_t *l );
 
 /**
    Returns the number of elements in the list
@@ -431,6 +480,8 @@ int al_get_count( array_list_t *l );
   Returns the last entry in the list witout removing it.
 */
 void *al_peek( array_list_t *l );
+long al_peek_long( array_list_t *l );
+func_ptr_t al_peek_func( array_list_t *l );
 
 /**
    Returns 1 if the list is empty, 0 otherwise