Coding style cleanup in dock.c
[wmaker-crm.git] / WINGs / array.c
blobae1e17c925e32b772d828dc892f5264336143610
1 /*
2 * Dynamically Resized Array
4 * Authors: Alfredo K. Kojima <kojima@windowmaker.info>
5 * Dan Pascu <dan@windowmaker.info>
7 * This code is released to the Public Domain, but
8 * proper credit is always appreciated :)
9 */
11 #include <stdlib.h>
12 #include <string.h>
14 #include "WUtil.h"
16 #define INITIAL_SIZE 8
17 #define RESIZE_INCREMENT 8
19 typedef struct W_Array {
20 void **items; /* the array data */
21 int itemCount; /* # of items in array */
22 int allocSize; /* allocated size of array */
23 WMFreeDataProc *destructor; /* the destructor to free elements */
24 } W_Array;
26 WMArray *WMCreateArray(int initialSize)
28 return WMCreateArrayWithDestructor(initialSize, NULL);
31 WMArray *WMCreateArrayWithDestructor(int initialSize, WMFreeDataProc * destructor)
33 WMArray *array;
35 array = wmalloc(sizeof(WMArray));
37 if (initialSize <= 0) {
38 initialSize = INITIAL_SIZE;
41 array->items = wmalloc(sizeof(void *) * initialSize);
43 array->itemCount = 0;
44 array->allocSize = initialSize;
45 array->destructor = destructor;
47 return array;
50 WMArray *WMCreateArrayWithArray(WMArray * array)
52 WMArray *newArray;
54 newArray = wmalloc(sizeof(WMArray));
56 newArray->items = wmalloc(sizeof(void *) * array->allocSize);
57 memcpy(newArray->items, array->items, sizeof(void *) * array->itemCount);
59 newArray->itemCount = array->itemCount;
60 newArray->allocSize = array->allocSize;
61 newArray->destructor = NULL;
63 return newArray;
66 void WMEmptyArray(WMArray * array)
68 if (array->destructor) {
69 while (array->itemCount > 0) {
70 array->itemCount--;
71 array->destructor(array->items[array->itemCount]);
74 /*memset(array->items, 0, array->itemCount * sizeof(void*)); */
75 array->itemCount = 0;
78 void WMFreeArray(WMArray * array)
80 WMEmptyArray(array);
81 wfree(array->items);
82 wfree(array);
85 int WMGetArrayItemCount(WMArray * array)
87 return array->itemCount;
90 void WMAppendArray(WMArray * array, WMArray * other)
92 if (other->itemCount == 0)
93 return;
95 if (array->itemCount + other->itemCount > array->allocSize) {
96 array->allocSize += other->allocSize;
97 array->items = wrealloc(array->items, sizeof(void *) * array->allocSize);
100 memcpy(array->items + array->itemCount, other->items, sizeof(void *) * other->itemCount);
101 array->itemCount += other->itemCount;
104 void WMAddToArray(WMArray * array, void *item)
106 if (array->itemCount >= array->allocSize) {
107 array->allocSize += RESIZE_INCREMENT;
108 array->items = wrealloc(array->items, sizeof(void *) * array->allocSize);
110 array->items[array->itemCount] = item;
112 array->itemCount++;
115 void WMInsertInArray(WMArray * array, int index, void *item)
117 wassertr(index >= 0 && index <= array->itemCount);
119 if (array->itemCount >= array->allocSize) {
120 array->allocSize += RESIZE_INCREMENT;
121 array->items = wrealloc(array->items, sizeof(void *) * array->allocSize);
123 if (index < array->itemCount) {
124 memmove(array->items + index + 1, array->items + index,
125 sizeof(void *) * (array->itemCount - index));
127 array->items[index] = item;
129 array->itemCount++;
132 void *WMReplaceInArray(WMArray * array, int index, void *item)
134 void *old;
136 wassertrv(index >= 0 && index <= array->itemCount, NULL);
138 /* is it really useful to perform append if index == array->itemCount ? -Dan */
139 if (index == array->itemCount) {
140 WMAddToArray(array, item);
141 return NULL;
144 old = array->items[index];
145 array->items[index] = item;
147 return old;
150 int WMDeleteFromArray(WMArray * array, int index)
152 wassertrv(index >= 0 && index < array->itemCount, 0);
154 if (array->destructor) {
155 array->destructor(array->items[index]);
158 if (index < array->itemCount - 1) {
159 memmove(array->items + index, array->items + index + 1,
160 sizeof(void *) * (array->itemCount - index - 1));
163 array->itemCount--;
165 return 1;
168 int WMRemoveFromArrayMatching(WMArray * array, WMMatchDataProc * match, void *cdata)
170 int i;
172 if (match != NULL) {
173 for (i = 0; i < array->itemCount; i++) {
174 if ((*match) (array->items[i], cdata)) {
175 WMDeleteFromArray(array, i);
176 return 1;
179 } else {
180 for (i = 0; i < array->itemCount; i++) {
181 if (array->items[i] == cdata) {
182 WMDeleteFromArray(array, i);
183 return 1;
188 return 0;
191 void *WMGetFromArray(WMArray * array, int index)
193 if (index < 0 || index >= array->itemCount)
194 return NULL;
196 return array->items[index];
199 void *WMPopFromArray(WMArray * array)
201 if (array->itemCount <= 0)
202 return NULL;
204 array->itemCount--;
206 return array->items[array->itemCount];
209 int WMFindInArray(WMArray * array, WMMatchDataProc * match, void *cdata)
211 int i;
213 if (match != NULL) {
214 for (i = 0; i < array->itemCount; i++) {
215 if ((*match) (array->items[i], cdata))
216 return i;
218 } else {
219 for (i = 0; i < array->itemCount; i++) {
220 if (array->items[i] == cdata)
221 return i;
225 return WANotFound;
228 int WMCountInArray(WMArray * array, void *item)
230 int i, count;
232 for (i = 0, count = 0; i < array->itemCount; i++) {
233 if (array->items[i] == item)
234 count++;
237 return count;
240 void WMSortArray(WMArray * array, WMCompareDataProc * comparer)
242 if (array->itemCount > 1) { /* Don't sort empty or single element arrays */
243 qsort(array->items, array->itemCount, sizeof(void *), comparer);
247 void WMMapArray(WMArray * array, void (*function) (void *, void *), void *data)
249 int i;
251 for (i = 0; i < array->itemCount; i++) {
252 (*function) (array->items[i], data);
256 WMArray *WMGetSubarrayWithRange(WMArray * array, WMRange aRange)
258 WMArray *newArray;
260 if (aRange.count <= 0)
261 return WMCreateArray(0);
263 if (aRange.position < 0)
264 aRange.position = 0;
265 if (aRange.position >= array->itemCount)
266 aRange.position = array->itemCount - 1;
267 if (aRange.position + aRange.count > array->itemCount)
268 aRange.count = array->itemCount - aRange.position;
270 newArray = WMCreateArray(aRange.count);
271 memcpy(newArray->items, array->items + aRange.position, sizeof(void *) * aRange.count);
272 newArray->itemCount = aRange.count;
274 return newArray;
277 void *WMArrayFirst(WMArray * array, WMArrayIterator * iter)
279 if (array->itemCount == 0) {
280 *iter = WANotFound;
281 return NULL;
282 } else {
283 *iter = 0;
284 return array->items[0];
288 void *WMArrayLast(WMArray * array, WMArrayIterator * iter)
290 if (array->itemCount == 0) {
291 *iter = WANotFound;
292 return NULL;
293 } else {
294 *iter = array->itemCount - 1;
295 return array->items[*iter];
299 void *WMArrayNext(WMArray * array, WMArrayIterator * iter)
301 if (*iter >= 0 && *iter < array->itemCount - 1) {
302 return array->items[++(*iter)];
303 } else {
304 *iter = WANotFound;
305 return NULL;
309 void *WMArrayPrevious(WMArray * array, WMArrayIterator * iter)
311 if (*iter > 0 && *iter < array->itemCount) {
312 return array->items[--(*iter)];
313 } else {
314 *iter = WANotFound;
315 return NULL;