Fixed some bugs, and compilation warnings.
[wmaker-crm.git] / WINGs / array.c
blob8dd77047974efd631b70483a0cacab75e3eb9482
1 /*
2 * Dynamically Resized Array
4 * Authors: Alfredo K. Kojima <kojima@windowmaker.org>
5 * Dan Pascu <dan@windowmaker.org>
7 * This code is released to the Public Domain, but
8 * proper credit is always appreciated :)
9 */
12 #include <stdlib.h>
13 #include <string.h>
15 #include "WUtil.h"
18 #define INITIAL_SIZE 8
19 #define RESIZE_INCREMENT 8
22 typedef struct W_Array {
23 void **items; /* the array data */
24 int itemCount; /* # of items in array */
25 int allocSize; /* allocated size of array */
26 WMFreeDataProc *destructor; /* the destructor to free elements */
27 } W_Array;
30 WMArray*
31 WMCreateArray(int initialSize)
33 return WMCreateArrayWithDestructor(initialSize, NULL);
37 WMArray*
38 WMCreateArrayWithDestructor(int initialSize, WMFreeDataProc *destructor)
40 WMArray *array;
42 array = wmalloc(sizeof(WMArray));
44 if (initialSize == 0) {
45 initialSize = INITIAL_SIZE;
48 array->items = wmalloc(sizeof(void*) * initialSize);
50 array->itemCount = 0;
51 array->allocSize = initialSize;
52 array->destructor = destructor;
54 return array;
58 void
59 WMEmptyArray(WMArray *array)
61 if (array->destructor) {
62 while (array->itemCount > 0) {
63 array->itemCount--;
64 array->destructor(array->items[array->itemCount]);
67 /*memset(array->items, 0, array->itemCount * sizeof(void*));*/
68 array->itemCount = 0;
72 void
73 WMFreeArray(WMArray *array)
75 WMEmptyArray(array);
76 free(array->items);
77 free(array);
81 int
82 WMGetArrayItemCount(WMArray *array)
84 return array->itemCount;
88 int
89 WMAppendArray(WMArray *array, WMArray *other)
91 if (other->itemCount == 0)
92 return 1;
94 if (array->itemCount + other->itemCount > array->allocSize) {
95 array->allocSize += other->allocSize;
96 array->items = wrealloc(array->items, sizeof(void*)*array->allocSize);
99 memcpy(array->items+array->itemCount, other->items,
100 sizeof(void*)*other->itemCount);
101 array->itemCount += other->itemCount;
103 return 1;
108 WMAddToArray(WMArray *array, void *item)
110 if (array->itemCount >= array->allocSize) {
111 array->allocSize += RESIZE_INCREMENT;
112 array->items = wrealloc(array->items, sizeof(void*)*array->allocSize);
114 array->items[array->itemCount] = item;
116 array->itemCount++;
118 return 1;
123 WMInsertInArray(WMArray *array, int index, void *item)
125 wassertrv(index <= array->itemCount, 0);
127 if (array->itemCount >= array->allocSize) {
128 array->allocSize += RESIZE_INCREMENT;
129 array->items = wrealloc(array->items, sizeof(void*)*array->allocSize);
131 if (index < array->itemCount) {
132 memmove(array->items+index+1, array->items+index,
133 sizeof(void*)*(array->itemCount-index));
135 array->items[index] = item;
137 array->itemCount++;
139 return 1;
143 void*
144 WMReplaceInArray(WMArray *array, int index, void *item)
146 void *old;
148 wassertrv(index > array->itemCount, 0);
150 if (index == array->itemCount) {
151 WMAddToArray(array, item);
152 return NULL;
155 old = array->items[index];
156 array->items[index] = item;
158 return old;
163 WMDeleteFromArray(WMArray *array, int index)
165 if (index >= array->itemCount)
166 return 0;
168 if (array->destructor) {
169 array->destructor(array->items[index]);
172 if (index < array->itemCount-1) {
173 memmove(array->items+index, array->items+index+1,
174 sizeof(void*)*(array->itemCount-index-1));
177 array->itemCount--;
179 return 1;
184 WMRemoveFromArray(WMArray *array, void *item)
186 int i;
188 for (i = 0; i < array->itemCount; i++) {
189 if (array->items[i] == item) {
190 return WMDeleteFromArray(array, i);
194 return 0;
198 void*
199 WMGetFromArray(WMArray *array, int index)
201 if (index >= array->itemCount)
202 return NULL;
204 return array->items[index];
208 void*
209 WMPopFromArray(WMArray *array)
211 array->itemCount--;
213 return array->items[array->itemCount];
218 WMFindInArray(WMArray *array, WMMatchDataProc *match, void *cdata)
220 int i;
222 if (match!=NULL) {
223 for (i = 0; i < array->itemCount; i++) {
224 if ((*match)(array->items[i], cdata))
225 return i;
227 } else {
228 for (i = 0; i < array->itemCount; i++) {
229 if (array->items[i] == cdata)
230 return i;
234 return WANotFound;
239 WMCountInArray(WMArray *array, void *item)
241 int i, count;
243 for (i=0, count=0; i<array->itemCount; i++) {
244 if (array->items[i] == item)
245 count++;
248 return count;
253 WMSortArray(WMArray *array, int (*comparer)(const void*, const void*))
255 qsort(array->items, array->itemCount, sizeof(void*), comparer);
257 return 1;
261 void
262 WMMapArray(WMArray *array, void (*function)(void*, void*), void *data)
264 int i;
266 for (i=0; i<array->itemCount; i++) {
267 (*function)(array->items[i], data);