Commit 38614a78 authored by Manish Singh's avatar Manish Singh
Browse files

Reverted garray change, and fixed the g_rarray_truncate bug with length.

Can we be one big happy g* family now? :)

-Yosh
parent b44ec526
Tue Mar 17 15:51:30 PST 1998 Manish Singh <yosh@gimp.org>
Wed Mar 18 22:27:08 PST 1998
* glib.h: gboolean type changed to int
* garray.c: complete redo by Josh MacDonald
* garray.c: g_rarray_truncate length done correctly
Sun Mar 15 07:13:34 1998 Tim Janik <timj@gimp.org>
......
......@@ -26,11 +26,13 @@ typedef struct _GRealArray GRealArray;
struct _GRealArray
{
gpointer *data;
guint8 *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);
......@@ -39,9 +41,8 @@ static void g_array_maybe_expand (GRealArray *array,
static GMemChunk *array_mem_chunk = NULL;
GArray*
g_array_new ()
g_array_new (gint zero_terminated)
{
GRealArray *array;
......@@ -55,22 +56,62 @@ g_array_new ()
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,
gboolean free_segment)
gint 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 * size;
return array;
}
static gint
g_nearest_pow (gint num)
{
......@@ -92,78 +133,10 @@ g_array_maybe_expand (GRealArray *array,
{
old_alloc = array->alloc;
array->alloc = g_nearest_pow (array->len + len);
array->alloc = g_nearest_pow (array->len + array->zero_terminated + len);
array->alloc = MAX (array->alloc, MIN_ARRAY_SIZE);
if (array->data)
array->data = g_realloc (array->data, sizeof(gpointer) * array->alloc);
else
array->data = g_new0 (gpointer, array->alloc);
array->data = g_realloc (array->data, 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;
}
......@@ -295,8 +295,7 @@ typedef char gchar;
typedef short gshort;
typedef long glong;
typedef int gint;
typedef unsigned char gboolean; /* Josh says. */
/* #gimp says, and common sense says. Where's Josh? */
typedef char gboolean;
typedef unsigned char guchar;
typedef unsigned short gushort;
......@@ -391,7 +390,7 @@ struct _GString
struct _GArray
{
gpointer *data;
gchar *data;
guint len;
};
......@@ -514,6 +513,7 @@ void g_hash_table_foreach (GHashTable *hash_table,
GHFunc func,
gpointer user_data);
/* Caches
*/
GCache* g_cache_new (GCacheNewFunc value_new_func,
......@@ -745,25 +745,33 @@ void g_string_sprintfa (GString *string,
...);
#endif
/* 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);
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);
/* 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]
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
*/
......
......@@ -339,13 +339,23 @@ main (int argc,
g_print ("checking arrays...");
garray = g_array_new ();
garray = g_array_new (FALSE);
for (i = 0; i < 10000; i++)
g_array_add (garray, (void*)i);
g_array_append_val (garray, gint, i);
for (i = 0; i < 10000; i++)
if (g_array_index (garray, i) != (void*)i)
g_print ("array fails: %p ( %p )\n", g_array_index (garray, i), (void*)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);
g_array_free (garray, TRUE);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment