Rewrite of GArray gboolean type changed to int

Rewrite of GArray
gboolean type changed to int

-Yosh
This commit is contained in:
Manish Singh 1998-03-18 00:08:31 +00:00
parent b141619b50
commit 467e4e73c1
10 changed files with 135 additions and 92 deletions

View File

@ -1,3 +1,8 @@
Tue Mar 17 15:51:30 PST 1998 Manish Singh <yosh@gimp.org>
* glib.h: gboolean type changed to int
* garray.c: complete redo by Josh MacDonald
Tue Mar 17 22:45:39 1998 Tim Janik <timj@gtk.org>
* gtk/gtkwidget.c (gtk_widget_get_arg): return real boolean values

View File

@ -1,3 +1,8 @@
Tue Mar 17 15:51:30 PST 1998 Manish Singh <yosh@gimp.org>
* glib.h: gboolean type changed to int
* garray.c: complete redo by Josh MacDonald
Tue Mar 17 22:45:39 1998 Tim Janik <timj@gtk.org>
* gtk/gtkwidget.c (gtk_widget_get_arg): return real boolean values

View File

@ -1,3 +1,8 @@
Tue Mar 17 15:51:30 PST 1998 Manish Singh <yosh@gimp.org>
* glib.h: gboolean type changed to int
* garray.c: complete redo by Josh MacDonald
Tue Mar 17 22:45:39 1998 Tim Janik <timj@gtk.org>
* gtk/gtkwidget.c (gtk_widget_get_arg): return real boolean values

View File

@ -1,3 +1,8 @@
Tue Mar 17 15:51:30 PST 1998 Manish Singh <yosh@gimp.org>
* glib.h: gboolean type changed to int
* garray.c: complete redo by Josh MacDonald
Tue Mar 17 22:45:39 1998 Tim Janik <timj@gtk.org>
* gtk/gtkwidget.c (gtk_widget_get_arg): return real boolean values

View File

@ -1,3 +1,8 @@
Tue Mar 17 15:51:30 PST 1998 Manish Singh <yosh@gimp.org>
* glib.h: gboolean type changed to int
* garray.c: complete redo by Josh MacDonald
Tue Mar 17 22:45:39 1998 Tim Janik <timj@gtk.org>
* gtk/gtkwidget.c (gtk_widget_get_arg): return real boolean values

View File

@ -1,3 +1,8 @@
Tue Mar 17 15:51:30 PST 1998 Manish Singh <yosh@gimp.org>
* glib.h: gboolean type changed to int
* garray.c: complete redo by Josh MacDonald
Tue Mar 17 22:45:39 1998 Tim Janik <timj@gtk.org>
* gtk/gtkwidget.c (gtk_widget_get_arg): return real boolean values

View File

@ -1,3 +1,8 @@
Tue Mar 17 15:51:30 PST 1998 Manish Singh <yosh@gimp.org>
* glib.h: gboolean type changed to int
* garray.c: complete redo by Josh MacDonald
Tue Mar 17 22:45:39 1998 Tim Janik <timj@gtk.org>
* gtk/gtkwidget.c (gtk_widget_get_arg): return real boolean values

View File

@ -26,13 +26,11 @@ typedef struct _GRealArray GRealArray;
struct _GRealArray
{
guint8 *data;
gpointer *data;
guint len;
guint alloc;
guint zero_terminated;
};
static gint g_nearest_pow (gint num);
static void g_array_maybe_expand (GRealArray *array,
gint len);
@ -41,8 +39,9 @@ static void g_array_maybe_expand (GRealArray *array,
static GMemChunk *array_mem_chunk = NULL;
GArray*
g_array_new (gint zero_terminated)
g_array_new ()
{
GRealArray *array;
@ -56,62 +55,22 @@ g_array_new (gint zero_terminated)
array->data = NULL;
array->len = 0;
array->alloc = 0;
array->zero_terminated = (zero_terminated ? 1 : 0);
return (GArray*) array;
}
void
g_array_free (GArray *array,
gint free_segment)
gboolean free_segment)
{
g_return_if_fail (array);
if (free_segment)
g_free (array->data);
g_mem_chunk_free (array_mem_chunk, array);
}
GArray*
g_rarray_append (GArray *array,
gpointer data,
gint size)
{
g_array_maybe_expand ((GRealArray*) array, size);
memcpy (array->data + array->len, data, size);
array->len += size;
return array;
}
GArray*
g_rarray_prepend (GArray *array,
gpointer data,
gint size)
{
g_array_maybe_expand ((GRealArray*) array, size);
g_memmove (array->data + size, array->data, array->len);
memcpy (array->data, data, size);
array->len += size;
return array;
}
GArray*
g_rarray_truncate (GArray *array,
gint length,
gint size)
{
if (array->data)
memset (array->data + length * size, 0, size);
array->len = length;
return array;
}
static gint
g_nearest_pow (gint num)
{
@ -133,10 +92,78 @@ g_array_maybe_expand (GRealArray *array,
{
old_alloc = array->alloc;
array->alloc = g_nearest_pow (array->len + array->zero_terminated + len);
array->alloc = g_nearest_pow (array->len + len);
array->alloc = MAX (array->alloc, MIN_ARRAY_SIZE);
array->data = g_realloc (array->data, array->alloc);
if (array->data)
array->data = g_realloc (array->data, sizeof(gpointer) * array->alloc);
else
array->data = g_new0 (gpointer, array->alloc);
memset (array->data + old_alloc, 0, array->alloc - old_alloc);
}
}
void
g_array_set_size (GArray *farray,
gint length)
{
GRealArray* array = (GRealArray*) farray;
g_return_if_fail (array);
if (length > array->len)
g_array_maybe_expand (array, (length - array->len));
array->len = length;
}
void
g_array_remove_index (GArray* farray,
gint index)
{
GRealArray* array = (GRealArray*) farray;
g_return_if_fail (array);
g_return_if_fail (index >= array->len);
array->data[index] = array->data[array->len - 1];
array->data[array->len - 1] = NULL;
array->len -= 1;
}
gboolean
g_array_remove (GArray* farray,
gpointer data)
{
GRealArray* array = (GRealArray*) farray;
int i;
g_return_val_if_fail (array, FALSE);
for (i = 0; i < array->len; i += 1)
{
if (array->data[i] == data)
{
g_array_remove_index (farray, i);
return TRUE;
}
}
return FALSE;
}
void
g_array_add (GArray* farray,
gpointer data)
{
GRealArray* array = (GRealArray*) farray;
g_return_if_fail (array);
g_array_maybe_expand (array, 1);
array->data[array->len++] = data;
}

View File

@ -295,7 +295,7 @@ typedef char gchar;
typedef short gshort;
typedef long glong;
typedef int gint;
typedef char gboolean;
typedef int gboolean; /* Josh says. */
typedef unsigned char guchar;
typedef unsigned short gushort;
@ -390,7 +390,7 @@ struct _GString
struct _GArray
{
gchar *data;
gpointer *data;
guint len;
};
@ -513,7 +513,6 @@ void g_hash_table_foreach (GHashTable *hash_table,
GHFunc func,
gpointer user_data);
/* Caches
*/
GCache* g_cache_new (GCacheNewFunc value_new_func,
@ -745,33 +744,25 @@ void g_string_sprintfa (GString *string,
...);
#endif
/* Resizable arrays
*/
#define g_array_append_val(array,type,val) \
g_rarray_append (array, (gpointer) &val, sizeof (type))
#define g_array_append_vals(array,type,vals,nvals) \
g_rarray_append (array, (gpointer) vals, sizeof (type) * nvals)
#define g_array_prepend_val(array,type,val) \
g_rarray_prepend (array, (gpointer) &val, sizeof (type))
#define g_array_prepend_vals(array,type,vals,nvals) \
g_rarray_prepend (array, (gpointer) vals, sizeof (type) * nvals)
#define g_array_truncate(array,type,length) \
g_rarray_truncate (array, length, sizeof (type))
#define g_array_index(array,type,index) \
((type*) array->data)[index]
/* Resizable pointer array. There used to be a more complicated
* interface that dealt with arbitrary sizes. It was found to be
* too ugly to use. Add appends appends a pointer. Remove fills
* any cleared spot and shortens the array.
*/
#define g_array_index(array,index) (array->data)[index]
GArray* g_array_new ();
void g_array_free (GArray *array,
gboolean free_seg);
void g_array_set_size (GArray *array,
gint length);
void g_array_remove_index (GArray *array,
gint index);
gboolean g_array_remove (GArray *array,
gpointer data);
void g_array_add (GArray *array,
gpointer data);
GArray* g_array_new (gint zero_terminated);
void g_array_free (GArray *array,
gint free_segment);
GArray* g_rarray_append (GArray *array,
gpointer data,
gint size);
GArray* g_rarray_prepend (GArray *array,
gpointer data,
gint size);
GArray* g_rarray_truncate (GArray *array,
gint length,
gint size);
/* Hash Functions
*/

View File

@ -339,23 +339,13 @@ main (int argc,
g_print ("checking arrays...");
garray = g_array_new (FALSE);
garray = g_array_new ();
for (i = 0; i < 10000; i++)
g_array_append_val (garray, gint, i);
g_array_add (garray, (void*)i);
for (i = 0; i < 10000; i++)
if (g_array_index (garray, gint, i) != i)
g_print ("uh oh: %d ( %d )\n", g_array_index (garray, gint, i), i);
g_array_free (garray, TRUE);
garray = g_array_new (FALSE);
for (i = 0; i < 10000; i++)
g_array_prepend_val (garray, gint, i);
for (i = 0; i < 10000; i++)
if (g_array_index (garray, gint, i) != (10000 - i - 1))
g_print ("uh oh: %d ( %d )\n", g_array_index (garray, gint, i), 10000 - i - 1);
if (g_array_index (garray, i) != (void*)i)
g_print ("array fails: %p ( %p )\n", g_array_index (garray, i), (void*)i);
g_array_free (garray, TRUE);