Update Serbian translation from master branch
[wmaker-crm.git] / WINGs / array.c
blobdf52358de7a1fde6ac84f48bf71035c8b75a8a6c
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 if (array == NULL)
81 return;
83 WMEmptyArray(array);
84 wfree(array->items);
85 wfree(array);
88 int WMGetArrayItemCount(WMArray * array)
90 if (array == NULL)
91 return 0;
93 return array->itemCount;
96 void WMAppendArray(WMArray * array, WMArray * other)
98 if (array == NULL || other == NULL)
99 return;
101 if (other->itemCount == 0)
102 return;
104 if (array->itemCount + other->itemCount > array->allocSize) {
105 array->allocSize += other->allocSize;
106 array->items = wrealloc(array->items, sizeof(void *) * array->allocSize);
109 memcpy(array->items + array->itemCount, other->items, sizeof(void *) * other->itemCount);
110 array->itemCount += other->itemCount;
113 void WMAddToArray(WMArray * array, void *item)
115 if (array == NULL)
116 return;
118 if (array->itemCount >= array->allocSize) {
119 array->allocSize += RESIZE_INCREMENT;
120 array->items = wrealloc(array->items, sizeof(void *) * array->allocSize);
122 array->items[array->itemCount] = item;
124 array->itemCount++;
127 void WMInsertInArray(WMArray * array, int index, void *item)
129 if (array == NULL)
130 return;
132 wassertr(index >= 0 && index <= array->itemCount);
134 if (array->itemCount >= array->allocSize) {
135 array->allocSize += RESIZE_INCREMENT;
136 array->items = wrealloc(array->items, sizeof(void *) * array->allocSize);
138 if (index < array->itemCount) {
139 memmove(array->items + index + 1, array->items + index,
140 sizeof(void *) * (array->itemCount - index));
142 array->items[index] = item;
144 array->itemCount++;
147 void *WMReplaceInArray(WMArray * array, int index, void *item)
149 void *old;
151 if (array == NULL)
152 return NULL;
154 wassertrv(index >= 0 && index <= array->itemCount, NULL);
156 /* is it really useful to perform append if index == array->itemCount ? -Dan */
157 if (index == array->itemCount) {
158 WMAddToArray(array, item);
159 return NULL;
162 old = array->items[index];
163 array->items[index] = item;
165 return old;
168 int WMDeleteFromArray(WMArray * array, int index)
170 if (array == NULL)
171 return 0;
173 wassertrv(index >= 0 && index < array->itemCount, 0);
175 if (array->destructor) {
176 array->destructor(array->items[index]);
179 if (index < array->itemCount - 1) {
180 memmove(array->items + index, array->items + index + 1,
181 sizeof(void *) * (array->itemCount - index - 1));
184 array->itemCount--;
186 return 1;
189 int WMRemoveFromArrayMatching(WMArray * array, WMMatchDataProc * match, void *cdata)
191 int i;
193 if (array == NULL)
194 return 1;
196 if (match != NULL) {
197 for (i = 0; i < array->itemCount; i++) {
198 if ((*match) (array->items[i], cdata)) {
199 WMDeleteFromArray(array, i);
200 return 1;
203 } else {
204 for (i = 0; i < array->itemCount; i++) {
205 if (array->items[i] == cdata) {
206 WMDeleteFromArray(array, i);
207 return 1;
212 return 0;
215 void *WMGetFromArray(WMArray * array, int index)
217 if (index < 0 || array == NULL || index >= array->itemCount)
218 return NULL;
220 return array->items[index];
223 void *WMPopFromArray(WMArray * array)
225 if (array == NULL || array->itemCount <= 0)
226 return NULL;
228 array->itemCount--;
230 return array->items[array->itemCount];
233 int WMFindInArray(WMArray * array, WMMatchDataProc * match, void *cdata)
235 int i;
237 if (array == NULL)
238 return WANotFound;
240 if (match != NULL) {
241 for (i = 0; i < array->itemCount; i++) {
242 if ((*match) (array->items[i], cdata))
243 return i;
245 } else {
246 for (i = 0; i < array->itemCount; i++) {
247 if (array->items[i] == cdata)
248 return i;
252 return WANotFound;
255 int WMCountInArray(WMArray * array, void *item)
257 int i, count;
259 if (array == NULL)
260 return 0;
262 for (i = 0, count = 0; i < array->itemCount; i++) {
263 if (array->items[i] == item)
264 count++;
267 return count;
270 void WMSortArray(WMArray * array, WMCompareDataProc * comparer)
272 if (array == NULL)
273 return;
275 if (array->itemCount > 1) { /* Don't sort empty or single element arrays */
276 qsort(array->items, array->itemCount, sizeof(void *), comparer);
280 void WMMapArray(WMArray * array, void (*function) (void *, void *), void *data)
282 int i;
284 if (array == NULL)
285 return;
287 for (i = 0; i < array->itemCount; i++) {
288 (*function) (array->items[i], data);
292 WMArray *WMGetSubarrayWithRange(WMArray * array, WMRange aRange)
294 WMArray *newArray;
296 if (aRange.count <= 0 || array == NULL)
297 return WMCreateArray(0);
299 if (aRange.position < 0)
300 aRange.position = 0;
301 if (aRange.position >= array->itemCount)
302 aRange.position = array->itemCount - 1;
303 if (aRange.position + aRange.count > array->itemCount)
304 aRange.count = array->itemCount - aRange.position;
306 newArray = WMCreateArray(aRange.count);
307 memcpy(newArray->items, array->items + aRange.position, sizeof(void *) * aRange.count);
308 newArray->itemCount = aRange.count;
310 return newArray;
313 void *WMArrayFirst(WMArray * array, WMArrayIterator * iter)
315 if (array == NULL || array->itemCount == 0) {
316 *iter = WANotFound;
317 return NULL;
318 } else {
319 *iter = 0;
320 return array->items[0];
324 void *WMArrayLast(WMArray * array, WMArrayIterator * iter)
326 if (array == NULL || array->itemCount == 0) {
327 *iter = WANotFound;
328 return NULL;
329 } else {
330 *iter = array->itemCount - 1;
331 return array->items[*iter];
335 void *WMArrayNext(WMArray * array, WMArrayIterator * iter)
337 if (array == NULL) {
338 *iter = WANotFound;
339 return NULL;
342 if (*iter >= 0 && *iter < array->itemCount - 1) {
343 return array->items[++(*iter)];
344 } else {
345 *iter = WANotFound;
346 return NULL;
350 void *WMArrayPrevious(WMArray * array, WMArrayIterator * iter)
352 if (array == NULL) {
353 *iter = WANotFound;
354 return NULL;
357 if (*iter > 0 && *iter < array->itemCount) {
358 return array->items[--(*iter)];
359 } else {
360 *iter = WANotFound;
361 return NULL;