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 WMFreeDataProc
*destructor
; /* the destructor to free elements */
31 WMCreateArray(int initialSize
)
33 return 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
>= 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
;
144 WMReplaceInArray(WMArray
*array
, int index
, void *item
)
148 wassertrv(index
>= 0 && index
<= array
->itemCount
, NULL
);
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 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));
183 WMRemoveFromArray(WMArray
*array
, void *item
)
187 for (i
= 0; i
< array
->itemCount
; i
++) {
188 if (array
->items
[i
] == item
) {
189 return WMDeleteFromArray(array
, i
);
198 WMGetFromArray(WMArray
*array
, int index
)
200 if (index
< 0 || index
>= array
->itemCount
)
203 return array
->items
[index
];
208 WMPopFromArray(WMArray
*array
)
212 return array
->items
[array
->itemCount
];
217 WMFindInArray(WMArray
*array
, WMMatchDataProc
*match
, void *cdata
)
222 for (i
= 0; i
< array
->itemCount
; i
++) {
223 if ((*match
)(array
->items
[i
], cdata
))
227 for (i
= 0; i
< array
->itemCount
; i
++) {
228 if (array
->items
[i
] == cdata
)
238 WMCountInArray(WMArray
*array
, void *item
)
242 for (i
=0, count
=0; i
<array
->itemCount
; i
++) {
243 if (array
->items
[i
] == item
)
252 WMSortArray(WMArray
*array
, WMCompareDataProc
*comparer
)
254 qsort(array
->items
, array
->itemCount
, sizeof(void*), comparer
);
259 WMMapArray(WMArray
*array
, void (*function
)(void*, void*), void *data
)
263 for (i
=0; i
<array
->itemCount
; i
++) {
264 (*function
)(array
->items
[i
], data
);