Remake of the WMList code to allow multiple selection. Not complete yet.
[wmaker-crm.git] / WINGs / array.c
blob30fa70a7d26612dc4e828e58daa4b6a0131361b8
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 >= 0 && 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 >= 0 && index <= array->itemCount, NULL);
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 wassertrv(index >= 0 && index < array->itemCount, 0);
167 if (array->destructor) {
168 array->destructor(array->items[index]);
171 if (index < array->itemCount-1) {
172 memmove(array->items+index, array->items+index+1,
173 sizeof(void*)*(array->itemCount-index-1));
176 array->itemCount--;
178 return 1;
183 WMRemoveFromArray(WMArray *array, void *item)
185 int i;
187 for (i = 0; i < array->itemCount; i++) {
188 if (array->items[i] == item) {
189 return WMDeleteFromArray(array, i);
193 return 0;
197 void*
198 WMGetFromArray(WMArray *array, int index)
200 if (index < 0 || index >= array->itemCount)
201 return NULL;
203 return array->items[index];
207 void*
208 WMPopFromArray(WMArray *array)
210 array->itemCount--;
212 return array->items[array->itemCount];
217 WMFindInArray(WMArray *array, WMMatchDataProc *match, void *cdata)
219 int i;
221 if (match!=NULL) {
222 for (i = 0; i < array->itemCount; i++) {
223 if ((*match)(array->items[i], cdata))
224 return i;
226 } else {
227 for (i = 0; i < array->itemCount; i++) {
228 if (array->items[i] == cdata)
229 return i;
233 return WANotFound;
238 WMCountInArray(WMArray *array, void *item)
240 int i, count;
242 for (i=0, count=0; i<array->itemCount; i++) {
243 if (array->items[i] == item)
244 count++;
247 return count;
251 void
252 WMSortArray(WMArray *array, WMCompareDataProc *comparer)
254 qsort(array->items, array->itemCount, sizeof(void*), comparer);
258 void
259 WMMapArray(WMArray *array, void (*function)(void*, void*), void *data)
261 int i;
263 for (i=0; i<array->itemCount; i++) {
264 (*function)(array->items[i], data);