1 /* GLIB - Library of useful routines for C programming
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 * Modified by the GLib Team and others 1997-2000. See the AUTHORS
22 * file for a list of people on the GLib Team. See the ChangeLog
23 * files for a list of changes. These files are distributed with
24 * GLib at ftp://ftp.gtk.org/pub/gtk/.
40 #define MIN_ARRAY_SIZE 16
42 typedef struct _GRealArray GRealArray;
50 guint zero_terminated : 1;
54 #define g_array_elt_len(array,i) ((array)->elt_size * (i))
55 #define g_array_elt_pos(array,i) ((array)->data + g_array_elt_len((array),(i)))
56 #define g_array_elt_zero(array, pos, len) \
57 (memset (g_array_elt_pos ((array), pos), 0, g_array_elt_len ((array), len)))
58 #define g_array_zero_terminate(array) G_STMT_START{ \
59 if ((array)->zero_terminated) \
60 g_array_elt_zero ((array), (array)->len, 1); \
63 static gint g_nearest_pow (gint num) G_GNUC_CONST;
64 static void g_array_maybe_expand (GRealArray *array,
67 static GMemChunk *array_mem_chunk = NULL;
68 G_LOCK_DEFINE_STATIC (array_mem_chunk);
71 g_array_new (gboolean zero_terminated,
75 return (GArray*) g_array_sized_new (zero_terminated, clear, elt_size, 0);
78 GArray* g_array_sized_new (gboolean zero_terminated,
85 G_LOCK (array_mem_chunk);
87 array_mem_chunk = g_mem_chunk_new ("array mem chunk",
89 1024, G_ALLOC_AND_FREE);
91 array = g_chunk_new (GRealArray, array_mem_chunk);
92 G_UNLOCK (array_mem_chunk);
97 array->zero_terminated = (zero_terminated ? 1 : 0);
98 array->clear = (clear ? 1 : 0);
99 array->elt_size = elt_size;
101 if (array->zero_terminated || reserved_size != 0)
103 g_array_maybe_expand (array, reserved_size);
104 g_array_zero_terminate(array);
107 return (GArray*) array;
111 g_array_free (GArray *array,
112 gboolean free_segment)
116 g_return_val_if_fail (array, NULL);
120 g_free (array->data);
124 segment = array->data;
126 G_LOCK (array_mem_chunk);
127 g_mem_chunk_free (array_mem_chunk, array);
128 G_UNLOCK (array_mem_chunk);
134 g_array_append_vals (GArray *farray,
138 GRealArray *array = (GRealArray*) farray;
140 g_array_maybe_expand (array, len);
142 memcpy (g_array_elt_pos (array, array->len), data,
143 g_array_elt_len (array, len));
147 g_array_zero_terminate (array);
153 g_array_prepend_vals (GArray *farray,
157 GRealArray *array = (GRealArray*) farray;
159 g_array_maybe_expand (array, len);
161 g_memmove (g_array_elt_pos (array, len), g_array_elt_pos (array, 0),
162 g_array_elt_len (array, array->len));
164 memcpy (g_array_elt_pos (array, 0), data, g_array_elt_len (array, len));
168 g_array_zero_terminate (array);
174 g_array_insert_vals (GArray *farray,
179 GRealArray *array = (GRealArray*) farray;
181 g_array_maybe_expand (array, len);
183 g_memmove (g_array_elt_pos (array, len + index),
184 g_array_elt_pos (array, index),
185 g_array_elt_len (array, array->len - index));
187 memcpy (g_array_elt_pos (array, index), data, g_array_elt_len (array, len));
191 g_array_zero_terminate (array);
197 g_array_set_size (GArray *farray,
200 GRealArray *array = (GRealArray*) farray;
201 if (length > array->len)
203 g_array_maybe_expand (array, length - array->len);
206 g_array_elt_zero (array, array->len, length - array->len);
208 #ifdef ENABLE_GC_FRIENDLY
209 else if (length < array->len)
210 g_array_elt_zero (array, length, array->len - length);
211 #endif /* ENABLE_GC_FRIENDLY */
215 g_array_zero_terminate (array);
221 g_array_remove_index (GArray* farray,
224 GRealArray* array = (GRealArray*) farray;
226 g_return_val_if_fail (array, NULL);
228 g_return_val_if_fail (index < array->len, NULL);
230 if (index != array->len - 1)
231 g_memmove (g_array_elt_pos (array, index),
232 g_array_elt_pos (array, index + 1),
233 g_array_elt_len (array, array->len - index - 1));
237 #ifdef ENABLE_GC_FRIENDLY
238 g_array_elt_zero (array, array->len, 1);
239 #else /* !ENABLE_GC_FRIENDLY */
240 g_array_zero_terminate (array);
241 #endif /* ENABLE_GC_FRIENDLY */
247 g_array_remove_index_fast (GArray* farray,
250 GRealArray* array = (GRealArray*) farray;
252 g_return_val_if_fail (array, NULL);
254 g_return_val_if_fail (index < array->len, NULL);
256 if (index != array->len - 1)
257 memcpy (g_array_elt_pos (array, index),
258 g_array_elt_pos (array, array->len - 1),
259 g_array_elt_len (array, 1));
263 #ifdef ENABLE_GC_FRIENDLY
264 g_array_elt_zero (array, array->len, 1);
265 #else /* !ENABLE_GC_FRIENDLY */
266 g_array_zero_terminate (array);
267 #endif /* ENABLE_GC_FRIENDLY */
273 g_array_sort (GArray *farray,
274 GCompareFunc compare_func)
276 GRealArray *array = (GRealArray*) farray;
278 g_return_if_fail (array != NULL);
287 g_array_sort_with_data (GArray *farray,
288 GCompareDataFunc compare_func,
291 GRealArray *array = (GRealArray*) farray;
293 g_return_if_fail (array != NULL);
295 g_qsort_with_data (array->data,
304 g_nearest_pow (gint num)
315 g_array_maybe_expand (GRealArray *array,
318 guint want_alloc = g_array_elt_len (array, array->len + len +
319 array->zero_terminated);
321 if (want_alloc > array->alloc)
323 want_alloc = g_nearest_pow (want_alloc);
324 want_alloc = MAX (want_alloc, MIN_ARRAY_SIZE);
326 array->data = g_realloc (array->data, want_alloc);
328 #ifdef ENABLE_GC_FRIENDLY
329 memset (array->data + array->alloc, 0, want_alloc - array->alloc);
330 #endif /* ENABLE_GC_FRIENDLY */
332 array->alloc = want_alloc;
339 typedef struct _GRealPtrArray GRealPtrArray;
341 struct _GRealPtrArray
348 static void g_ptr_array_maybe_expand (GRealPtrArray *array,
351 static GMemChunk *ptr_array_mem_chunk = NULL;
352 G_LOCK_DEFINE_STATIC (ptr_array_mem_chunk);
356 g_ptr_array_new (void)
358 return g_ptr_array_sized_new (0);
362 g_ptr_array_sized_new (guint reserved_size)
364 GRealPtrArray *array;
366 G_LOCK (ptr_array_mem_chunk);
367 if (!ptr_array_mem_chunk)
368 ptr_array_mem_chunk = g_mem_chunk_new ("array mem chunk",
369 sizeof (GRealPtrArray),
370 1024, G_ALLOC_AND_FREE);
372 array = g_chunk_new (GRealPtrArray, ptr_array_mem_chunk);
373 G_UNLOCK (ptr_array_mem_chunk);
379 if (reserved_size != 0)
380 g_ptr_array_maybe_expand (array, reserved_size);
382 return (GPtrArray*) array;
386 g_ptr_array_free (GPtrArray *array,
387 gboolean free_segment)
391 g_return_val_if_fail (array, NULL);
395 g_free (array->pdata);
399 segment = array->pdata;
401 G_LOCK (ptr_array_mem_chunk);
402 g_mem_chunk_free (ptr_array_mem_chunk, array);
403 G_UNLOCK (ptr_array_mem_chunk);
409 g_ptr_array_maybe_expand (GRealPtrArray *array,
412 if ((array->len + len) > array->alloc)
414 #ifdef ENABLE_GC_FRIENDLY
415 guint old_alloc = array->alloc;
416 #endif /* ENABLE_GC_FRIENDLY */
417 array->alloc = g_nearest_pow (array->len + len);
418 array->alloc = MAX (array->alloc, MIN_ARRAY_SIZE);
419 array->pdata = g_realloc (array->pdata, sizeof(gpointer) * array->alloc);
420 #ifdef ENABLE_GC_FRIENDLY
421 for ( ; old_alloc < array->alloc; old_alloc++)
422 array->pdata [old_alloc] = NULL;
423 #endif /* ENABLE_GC_FRIENDLY */
428 g_ptr_array_set_size (GPtrArray *farray,
431 GRealPtrArray* array = (GRealPtrArray*) farray;
433 g_return_if_fail (array);
435 if (length > array->len)
438 g_ptr_array_maybe_expand (array, (length - array->len));
440 * memset (array->pdata + array->len, 0,
441 * sizeof (gpointer) * (length - array->len));
442 * to make it really portable. Remember (void*)NULL needn't be
443 * bitwise zero. It of course is silly not to use memset (..,0,..).
445 for (i = array->len; i < length; i++)
446 array->pdata[i] = NULL;
448 #ifdef ENABLE_GC_FRIENDLY
449 else if (length < array->len)
452 for (i = length; i < array->len; i++)
453 array->pdata[i] = NULL;
455 #endif /* ENABLE_GC_FRIENDLY */
461 g_ptr_array_remove_index (GPtrArray* farray,
464 GRealPtrArray* array = (GRealPtrArray*) farray;
467 g_return_val_if_fail (array, NULL);
469 g_return_val_if_fail (index < array->len, NULL);
471 result = array->pdata[index];
473 if (index != array->len - 1)
474 g_memmove (array->pdata + index, array->pdata + index + 1,
475 sizeof (gpointer) * (array->len - index - 1));
479 #ifdef ENABLE_GC_FRIENDLY
480 array->pdata[array->len] = NULL;
481 #endif /* ENABLE_GC_FRIENDLY */
487 g_ptr_array_remove_index_fast (GPtrArray* farray,
490 GRealPtrArray* array = (GRealPtrArray*) farray;
493 g_return_val_if_fail (array, NULL);
495 g_return_val_if_fail (index < array->len, NULL);
497 result = array->pdata[index];
499 if (index != array->len - 1)
500 array->pdata[index] = array->pdata[array->len - 1];
504 #ifdef ENABLE_GC_FRIENDLY
505 array->pdata[array->len] = NULL;
506 #endif /* ENABLE_GC_FRIENDLY */
512 g_ptr_array_remove (GPtrArray* farray,
515 GRealPtrArray* array = (GRealPtrArray*) farray;
518 g_return_val_if_fail (array, FALSE);
520 for (i = 0; i < array->len; i += 1)
522 if (array->pdata[i] == data)
524 g_ptr_array_remove_index (farray, i);
533 g_ptr_array_remove_fast (GPtrArray* farray,
536 GRealPtrArray* array = (GRealPtrArray*) farray;
539 g_return_val_if_fail (array, FALSE);
541 for (i = 0; i < array->len; i += 1)
543 if (array->pdata[i] == data)
545 g_ptr_array_remove_index_fast (farray, i);
554 g_ptr_array_add (GPtrArray* farray,
557 GRealPtrArray* array = (GRealPtrArray*) farray;
559 g_return_if_fail (array);
561 g_ptr_array_maybe_expand (array, 1);
563 array->pdata[array->len++] = data;
567 g_ptr_array_sort (GPtrArray *array,
568 GCompareFunc compare_func)
570 g_return_if_fail (array != NULL);
579 g_ptr_array_sort_with_data (GPtrArray *array,
580 GCompareDataFunc compare_func,
583 g_return_if_fail (array != NULL);
585 g_qsort_with_data (array->pdata,
595 GByteArray* g_byte_array_new (void)
597 return (GByteArray*) g_array_sized_new (FALSE, FALSE, 1, 0);
600 GByteArray* g_byte_array_sized_new (guint reserved_size)
602 return (GByteArray*) g_array_sized_new (FALSE, FALSE, 1, reserved_size);
605 guint8* g_byte_array_free (GByteArray *array,
606 gboolean free_segment)
608 return (guint8*) g_array_free ((GArray*) array, free_segment);
611 GByteArray* g_byte_array_append (GByteArray *array,
615 g_array_append_vals ((GArray*) array, (guint8*)data, len);
620 GByteArray* g_byte_array_prepend (GByteArray *array,
624 g_array_prepend_vals ((GArray*) array, (guint8*)data, len);
629 GByteArray* g_byte_array_set_size (GByteArray *array,
632 g_array_set_size ((GArray*) array, length);
637 GByteArray* g_byte_array_remove_index (GByteArray *array,
640 g_array_remove_index((GArray*) array, index);
645 GByteArray* g_byte_array_remove_index_fast (GByteArray *array,
648 g_array_remove_index_fast((GArray*) array, index);
654 g_byte_array_sort (GByteArray *array,
655 GCompareFunc compare_func)
657 g_array_sort ((GArray *) array, compare_func);
661 g_byte_array_sort_with_data (GByteArray *array,
662 GCompareDataFunc compare_func,
665 g_array_sort_with_data ((GArray *) array, compare_func, user_data);