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 :)
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 void (*destructor
)(void *item
); /* the destructor to free elements */
31 WMCreateArray(int initialSize
)
33 WMCreateArrayWithDestructor(initialSize
, NULL
);
38 WMCreateArrayWithDestructor(int initialSize
, WMFreeDataProc
*destructor
)
42 array
= wmalloc(sizeof(WMArray
));
44 if (initialSize
== 0) {
45 initialSize
= INITIAL_SIZE
;
48 array
->items
= wmalloc(sizeof(void*) * initialSize
);
51 array
->allocSize
= initialSize
;
52 array
->destructor
= destructor
;
59 WMEmptyArray(WMArray
*array
)
61 if (array
->destructor
) {
62 while (array
->itemCount
> 0) {
64 array
->destructor(array
->items
[array
->itemCount
]);
67 /*memset(array->items, 0, array->itemCount * sizeof(void*));*/
73 WMFreeArray(WMArray
*array
)
82 WMGetArrayItemCount(WMArray
*array
)
84 return array
->itemCount
;
89 WMAppendArray(WMArray
*array
, WMArray
*other
)
91 if (other
->itemCount
== 0)
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
;
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
;
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
;
144 WMReplaceInArray(WMArray
*array
, int index
, void *item
)
148 wassertrv(index
> array
->itemCount
, 0);
150 if (index
== array
->itemCount
) {
151 WMAddToArray(array
, item
);
155 old
= array
->items
[index
];
156 array
->items
[index
] = item
;
163 WMDeleteFromArray(WMArray
*array
, int index
)
165 if (index
>= array
->itemCount
)
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));
184 WMRemoveFromArray(WMArray
*array
, void *item
)
188 for (i
= 0; i
< array
->itemCount
; i
++) {
189 if (array
->items
[i
] == item
) {
190 return WMDeleteFromArray(array
, i
);
199 WMGetFromArray(WMArray
*array
, int index
)
201 if (index
>= array
->itemCount
)
204 return array
->items
[index
];
209 WMPopFromArray(WMArray
*array
)
213 return array
->items
[array
->itemCount
];
218 WMFindInArray(WMArray
*array
, WMMatchDataProc
*match
, void *cdata
)
223 for (i
= 0; i
< array
->itemCount
; i
++) {
224 if ((*match
)(array
->items
[i
], cdata
))
228 for (i
= 0; i
< array
->itemCount
; i
++) {
229 if (array
->items
[i
] == cdata
)
239 WMCountInArray(WMArray
*array
, void *item
)
243 for (i
=0, count
=0; i
<array
->itemCount
; i
++) {
244 if (array
->items
[i
] == item
)
253 WMSortArray(WMArray
*array
, int (*comparer
)(const void*, const void*))
255 qsort(array
->items
, array
->itemCount
, sizeof(void*), comparer
);
262 WMMapArray(WMArray
*array
, void (*function
)(void*, void*), void *data
)
266 for (i
=0; i
<array
->itemCount
; i
++) {
267 (*function
)(array
->items
[i
], data
);