fixed bug in repainting empty popup buttons
[wmaker-crm.git] / WINGs / bag.c
blobba1975c0013df7aa3ba1a53273ee2d32f6518e4b
3 #include <stdlib.h>
4 #include <string.h>
6 #include "WUtil.h"
10 struct W_Bag {
11 int size;
12 int count;
14 void **items;
20 WMBag*
21 WMCreateBag(int size)
23 WMBag *bag;
25 wassertrv(size > 0, NULL);
27 bag = wmalloc(sizeof(WMBag));
29 bag->items = wmalloc(sizeof(void*) * size);
30 bag->size = size;
31 bag->count = 0;
33 return bag;
38 int
39 WMGetBagItemCount(WMBag *bag)
41 return bag->count;
46 void
47 WMAppendBag(WMBag *bag, WMBag *appendedBag)
49 bag->items = wrealloc(bag->items,
50 sizeof(void*) * (bag->size+appendedBag->count));
52 memcpy(bag->items + bag->count, appendedBag->items,
53 sizeof(void*) * appendedBag->count);
55 bag->count += appendedBag->count;
56 bag->size += appendedBag->count;
61 void
62 WMPutInBag(WMBag *bag, void *item)
64 WMInsertInBag(bag, bag->count, item);
69 void
70 WMInsertInBag(WMBag *bag, int index, void *item)
72 if (bag->count == bag->size) {
73 bag->size += 16;
74 bag->items = wrealloc(bag->items, sizeof(void*) * bag->size);
77 if (index >= 0 && index < bag->count) {
78 memmove(&bag->items[index+1], &bag->items[index],
79 (bag->count - index) * sizeof(void*));
80 } else {
81 /* If index is invalid, place it at end */
82 index = bag->count;
84 bag->items[index] = item;
85 bag->count++;
89 int
90 WMGetFirstInBag(WMBag *bag, void *item)
92 int i;
94 for (i = 0; i < bag->count; i++) {
95 if (bag->items[i] == item) {
96 return i;
99 return -1;
104 WMGetLastInBag(WMBag *bag, void *item)
106 int i;
108 for (i = bag->count-1; i>= 0; i--) {
109 if (bag->items[i] == item) {
110 return i;
113 return -1;
117 void
118 WMRemoveFromBag(WMBag *bag, void *item)
120 int i;
122 i = WMGetFirstInBag(bag, item);
123 if (i >= 0) {
124 WMDeleteFromBag(bag, i);
130 void
131 WMDeleteFromBag(WMBag *bag, int index)
133 wassertr(index >= 0 && index < bag->count);
135 if (index < bag->count-1) {
136 memmove(&bag->items[index], &bag->items[index + 1],
137 (bag->count - index - 1) * sizeof(void*));
139 bag->count--;
143 void*
144 WMGetFromBag(WMBag *bag, int index)
146 wassertrv(index >= 0 && index < bag->count, NULL);
148 return bag->items[index];
153 WMCountInBag(WMBag *bag, void *item)
155 int i, j;
157 for (j = 0, i = 0; j < bag->count; j++) {
158 if (bag->items[j] == item)
159 i++;
161 return i;
166 void
167 WMSortBag(WMBag *bag, int (*comparer)(const void*, const void*))
169 qsort(bag->items, bag->count, sizeof(void*), comparer);
174 void
175 WMFreeBag(WMBag *bag)
177 wfree(bag->items);
178 wfree(bag);
182 void
183 WMEmptyBag(WMBag *bag)
185 bag->count = 0;
190 WMBag*
191 WMMapBag(WMBag *bag, void *(*function)(void *))
193 int i;
194 WMBag *new = WMCreateBag(bag->size);
196 for (i = 0; i < bag->count; i++) {
197 WMPutInBag(new, (*function)(bag->items[i]));
200 return new;
204 void*
205 WMReplaceInBag(WMBag *bag, int index, void *item)
207 void *old;
209 wassertrv(index >= 0 && index < bag->count, NULL);
211 old = bag->items[index];
213 bag->items[index] = item;
215 return old;