WPrefs: set default to 1st color in gradient texture
[wmaker-crm.git] / WINGs / bagtree.c
blob4b3062ca4b65a0d9a88d14248e8900d92bdc7fe1
2 #include <stdlib.h>
3 #include <string.h>
5 #include "WUtil.h"
7 typedef struct W_Node {
8 struct W_Node *parent;
9 struct W_Node *left;
10 struct W_Node *right;
11 int color;
13 void *data;
14 int index;
15 } W_Node;
17 typedef struct W_Bag {
18 W_Node *root;
20 W_Node *nil; /* sentinel */
22 int count;
24 void (*destructor) (void *item);
25 } W_Bag;
27 #define IS_LEFT(node) (node == node->parent->left)
28 #define IS_RIGHT(node) (node == node->parent->right)
30 static void leftRotate(W_Bag * tree, W_Node * node)
32 W_Node *node2;
34 node2 = node->right;
35 node->right = node2->left;
37 node2->left->parent = node;
39 node2->parent = node->parent;
41 if (node->parent == tree->nil) {
42 tree->root = node2;
43 } else {
44 if (IS_LEFT(node)) {
45 node->parent->left = node2;
46 } else {
47 node->parent->right = node2;
50 node2->left = node;
51 node->parent = node2;
54 static void rightRotate(W_Bag * tree, W_Node * node)
56 W_Node *node2;
58 node2 = node->left;
59 node->left = node2->right;
61 node2->right->parent = node;
63 node2->parent = node->parent;
65 if (node->parent == tree->nil) {
66 tree->root = node2;
67 } else {
68 if (IS_LEFT(node)) {
69 node->parent->left = node2;
70 } else {
71 node->parent->right = node2;
74 node2->right = node;
75 node->parent = node2;
78 static void treeInsert(W_Bag * tree, W_Node * node)
80 W_Node *y = tree->nil;
81 W_Node *x = tree->root;
83 while (x != tree->nil) {
84 y = x;
85 if (node->index <= x->index)
86 x = x->left;
87 else
88 x = x->right;
90 node->parent = y;
91 if (y == tree->nil)
92 tree->root = node;
93 else if (node->index <= y->index)
94 y->left = node;
95 else
96 y->right = node;
99 static void rbTreeInsert(W_Bag * tree, W_Node * node)
101 W_Node *y;
103 treeInsert(tree, node);
105 node->color = 'R';
107 while (node != tree->root && node->parent->color == 'R') {
108 if (IS_LEFT(node->parent)) {
109 y = node->parent->parent->right;
111 if (y->color == 'R') {
113 node->parent->color = 'B';
114 y->color = 'B';
115 node->parent->parent->color = 'R';
116 node = node->parent->parent;
118 } else {
119 if (IS_RIGHT(node)) {
120 node = node->parent;
121 leftRotate(tree, node);
123 node->parent->color = 'B';
124 node->parent->parent->color = 'R';
125 rightRotate(tree, node->parent->parent);
127 } else {
128 y = node->parent->parent->left;
130 if (y->color == 'R') {
132 node->parent->color = 'B';
133 y->color = 'B';
134 node->parent->parent->color = 'R';
135 node = node->parent->parent;
137 } else {
138 if (IS_LEFT(node)) {
139 node = node->parent;
140 rightRotate(tree, node);
142 node->parent->color = 'B';
143 node->parent->parent->color = 'R';
144 leftRotate(tree, node->parent->parent);
148 tree->root->color = 'B';
151 static void rbDeleteFixup(W_Bag * tree, W_Node * node)
153 W_Node *w;
155 while (node != tree->root && node->color == 'B') {
156 if (IS_LEFT(node)) {
157 w = node->parent->right;
158 if (w->color == 'R') {
159 w->color = 'B';
160 node->parent->color = 'R';
161 leftRotate(tree, node->parent);
162 w = node->parent->right;
164 if (w->left->color == 'B' && w->right->color == 'B') {
165 w->color = 'R';
166 node = node->parent;
167 } else {
168 if (w->right->color == 'B') {
169 w->left->color = 'B';
170 w->color = 'R';
171 rightRotate(tree, w);
172 w = node->parent->right;
174 w->color = node->parent->color;
175 node->parent->color = 'B';
176 w->right->color = 'B';
177 leftRotate(tree, node->parent);
178 node = tree->root;
180 } else {
181 w = node->parent->left;
182 if (w->color == 'R') {
183 w->color = 'B';
184 node->parent->color = 'R';
185 rightRotate(tree, node->parent);
186 w = node->parent->left;
188 if (w->left->color == 'B' && w->right->color == 'B') {
189 w->color = 'R';
190 node = node->parent;
191 } else {
192 if (w->left->color == 'B') {
193 w->right->color = 'B';
194 w->color = 'R';
195 leftRotate(tree, w);
196 w = node->parent->left;
198 w->color = node->parent->color;
199 node->parent->color = 'B';
200 w->left->color = 'B';
201 rightRotate(tree, node->parent);
202 node = tree->root;
206 node->color = 'B';
210 static W_Node *treeMinimum(W_Node * node, W_Node * nil)
212 while (node->left != nil)
213 node = node->left;
214 return node;
217 static W_Node *treeMaximum(W_Node * node, W_Node * nil)
219 while (node->right != nil)
220 node = node->right;
221 return node;
224 static W_Node *treeSuccessor(W_Node * node, W_Node * nil)
226 W_Node *y;
228 if (node->right != nil) {
229 return treeMinimum(node->right, nil);
231 y = node->parent;
232 while (y != nil && node == y->right) {
233 node = y;
234 y = y->parent;
236 return y;
239 static W_Node *treePredecessor(W_Node * node, W_Node * nil)
241 W_Node *y;
243 if (node->left != nil) {
244 return treeMaximum(node->left, nil);
246 y = node->parent;
247 while (y != nil && node == y->left) {
248 node = y;
249 y = y->parent;
251 return y;
254 static W_Node *rbTreeDelete(W_Bag * tree, W_Node * node)
256 W_Node *nil = tree->nil;
257 W_Node *x, *y;
259 if (node->left == nil || node->right == nil) {
260 y = node;
261 } else {
262 y = treeSuccessor(node, nil);
265 if (y->left != nil) {
266 x = y->left;
267 } else {
268 x = y->right;
271 x->parent = y->parent;
273 if (y->parent == nil) {
274 tree->root = x;
275 } else {
276 if (IS_LEFT(y)) {
277 y->parent->left = x;
278 } else {
279 y->parent->right = x;
282 if (y != node) {
283 node->index = y->index;
284 node->data = y->data;
286 if (y->color == 'B') {
287 rbDeleteFixup(tree, x);
290 return y;
293 static W_Node *treeSearch(W_Node * root, W_Node * nil, int index)
295 if (root == nil || root->index == index) {
296 return root;
299 if (index < root->index) {
300 return treeSearch(root->left, nil, index);
301 } else {
302 return treeSearch(root->right, nil, index);
306 static W_Node *treeFind(W_Node * root, W_Node * nil, void *data)
308 W_Node *tmp;
310 if (root == nil || root->data == data)
311 return root;
313 tmp = treeFind(root->left, nil, data);
314 if (tmp != nil)
315 return tmp;
317 tmp = treeFind(root->right, nil, data);
319 return tmp;
322 #if 0
323 static char buf[512];
325 static void printNodes(W_Node * node, W_Node * nil, int depth)
327 if (node == nil) {
328 return;
331 printNodes(node->left, nil, depth + 1);
333 memset(buf, ' ', depth * 2);
334 buf[depth * 2] = 0;
335 if (IS_LEFT(node))
336 printf("%s/(%2i\n", buf, node->index);
337 else
338 printf("%s\\(%2i\n", buf, node->index);
340 printNodes(node->right, nil, depth + 1);
343 void PrintTree(WMBag * bag)
345 W_TreeBag *tree = (W_TreeBag *) bag->data;
347 printNodes(tree->root, tree->nil, 0);
349 #endif
351 WMBag *WMCreateTreeBag(void)
353 return WMCreateTreeBagWithDestructor(NULL);
356 WMBag *WMCreateTreeBagWithDestructor(WMFreeDataProc * destructor)
358 WMBag *bag;
360 bag = wmalloc(sizeof(WMBag));
361 bag->nil = wmalloc(sizeof(W_Node));
362 bag->nil->left = bag->nil->right = bag->nil->parent = bag->nil;
363 bag->nil->index = WBNotFound;
364 bag->root = bag->nil;
365 bag->destructor = destructor;
367 return bag;
370 int WMGetBagItemCount(WMBag * self)
372 return self->count;
375 void WMAppendBag(WMBag * self, WMBag * bag)
377 WMBagIterator ptr;
378 void *data;
380 for (data = WMBagFirst(bag, &ptr); data != NULL; data = WMBagNext(bag, &ptr)) {
381 WMPutInBag(self, data);
385 void WMPutInBag(WMBag * self, void *item)
387 W_Node *ptr;
389 ptr = wmalloc(sizeof(W_Node));
391 ptr->data = item;
392 ptr->index = self->count;
393 ptr->left = self->nil;
394 ptr->right = self->nil;
395 ptr->parent = self->nil;
397 rbTreeInsert(self, ptr);
399 self->count++;
402 void WMInsertInBag(WMBag * self, int index, void *item)
404 W_Node *ptr;
406 ptr = wmalloc(sizeof(W_Node));
408 ptr->data = item;
409 ptr->index = index;
410 ptr->left = self->nil;
411 ptr->right = self->nil;
412 ptr->parent = self->nil;
414 rbTreeInsert(self, ptr);
416 while ((ptr = treeSuccessor(ptr, self->nil)) != self->nil) {
417 ptr->index++;
420 self->count++;
423 static int treeDeleteNode(WMBag * self, W_Node *ptr)
425 if (ptr != self->nil) {
426 W_Node *tmp;
428 self->count--;
430 tmp = treeSuccessor(ptr, self->nil);
431 while (tmp != self->nil) {
432 tmp->index--;
433 tmp = treeSuccessor(tmp, self->nil);
436 ptr = rbTreeDelete(self, ptr);
437 if (self->destructor)
438 self->destructor(ptr->data);
439 wfree(ptr);
440 return 1;
442 return 0;
445 int WMRemoveFromBag(WMBag * self, void *item)
447 W_Node *ptr = treeFind(self->root, self->nil, item);
448 return treeDeleteNode(self, ptr);
451 int WMEraseFromBag(WMBag * self, int index)
453 W_Node *ptr = treeSearch(self->root, self->nil, index);
455 if (ptr != self->nil) {
457 self->count--;
459 ptr = rbTreeDelete(self, ptr);
460 if (self->destructor)
461 self->destructor(ptr->data);
462 wfree(ptr);
464 wassertrv(self->count == 0 || self->root->index >= 0, 1);
466 return 1;
467 } else {
468 return 0;
472 int WMDeleteFromBag(WMBag * self, int index)
474 W_Node *ptr = treeSearch(self->root, self->nil, index);
475 return treeDeleteNode(self, ptr);
478 void *WMGetFromBag(WMBag * self, int index)
480 W_Node *node;
482 node = treeSearch(self->root, self->nil, index);
483 if (node != self->nil)
484 return node->data;
485 else
486 return NULL;
489 int WMGetFirstInBag(WMBag * self, void *item)
491 W_Node *node;
493 node = treeFind(self->root, self->nil, item);
494 if (node != self->nil)
495 return node->index;
496 else
497 return WBNotFound;
500 static int treeCount(W_Node * root, W_Node * nil, void *item)
502 int count = 0;
504 if (root == nil)
505 return 0;
507 if (root->data == item)
508 count++;
510 if (root->left != nil)
511 count += treeCount(root->left, nil, item);
513 if (root->right != nil)
514 count += treeCount(root->right, nil, item);
516 return count;
519 int WMCountInBag(WMBag * self, void *item)
521 return treeCount(self->root, self->nil, item);
524 void *WMReplaceInBag(WMBag * self, int index, void *item)
526 W_Node *ptr = treeSearch(self->root, self->nil, index);
527 void *old = NULL;
529 if (item == NULL) {
530 self->count--;
531 ptr = rbTreeDelete(self, ptr);
532 if (self->destructor)
533 self->destructor(ptr->data);
534 wfree(ptr);
535 } else if (ptr != self->nil) {
536 old = ptr->data;
537 ptr->data = item;
538 } else {
539 W_Node *ptr;
541 ptr = wmalloc(sizeof(W_Node));
543 ptr->data = item;
544 ptr->index = index;
545 ptr->left = self->nil;
546 ptr->right = self->nil;
547 ptr->parent = self->nil;
549 rbTreeInsert(self, ptr);
551 self->count++;
554 return old;
557 void WMSortBag(WMBag * self, WMCompareDataProc * comparer)
559 void **items;
560 W_Node *tmp;
561 int i;
563 if (self->count == 0)
564 return;
566 items = wmalloc(sizeof(void *) * self->count);
567 i = 0;
569 tmp = treeMinimum(self->root, self->nil);
570 while (tmp != self->nil) {
571 items[i++] = tmp->data;
572 tmp = treeSuccessor(tmp, self->nil);
575 qsort(&items[0], self->count, sizeof(void *), comparer);
577 i = 0;
578 tmp = treeMinimum(self->root, self->nil);
579 while (tmp != self->nil) {
580 tmp->index = i;
581 tmp->data = items[i++];
582 tmp = treeSuccessor(tmp, self->nil);
585 wfree(items);
588 static void deleteTree(WMBag * self, W_Node * node)
590 if (node == self->nil)
591 return;
593 deleteTree(self, node->left);
595 if (self->destructor)
596 self->destructor(node->data);
598 deleteTree(self, node->right);
600 wfree(node);
603 void WMEmptyBag(WMBag * self)
605 deleteTree(self, self->root);
606 self->root = self->nil;
607 self->count = 0;
610 void WMFreeBag(WMBag * self)
612 WMEmptyBag(self);
613 wfree(self->nil);
614 wfree(self);
617 static void mapTree(W_Bag * tree, W_Node * node, void (*function) (void *, void *), void *data)
619 if (node == tree->nil)
620 return;
622 mapTree(tree, node->left, function, data);
624 (*function) (node->data, data);
626 mapTree(tree, node->right, function, data);
629 void WMMapBag(WMBag * self, void (*function) (void *, void *), void *data)
631 mapTree(self, self->root, function, data);
634 static int findInTree(W_Bag * tree, W_Node * node, WMMatchDataProc * function, void *cdata)
636 int index;
638 if (node == tree->nil)
639 return WBNotFound;
641 index = findInTree(tree, node->left, function, cdata);
642 if (index != WBNotFound)
643 return index;
645 if ((*function) (node->data, cdata)) {
646 return node->index;
649 return findInTree(tree, node->right, function, cdata);
652 int WMFindInBag(WMBag * self, WMMatchDataProc * match, void *cdata)
654 return findInTree(self, self->root, match, cdata);
657 void *WMBagFirst(WMBag * self, WMBagIterator * ptr)
659 W_Node *node;
661 node = treeMinimum(self->root, self->nil);
663 if (node == self->nil) {
664 *ptr = NULL;
665 return NULL;
666 } else {
667 *ptr = node;
668 return node->data;
672 void *WMBagLast(WMBag * self, WMBagIterator * ptr)
675 W_Node *node;
677 node = treeMaximum(self->root, self->nil);
679 if (node == self->nil) {
680 *ptr = NULL;
681 return NULL;
682 } else {
683 *ptr = node;
684 return node->data;
688 void *WMBagNext(WMBag * self, WMBagIterator * ptr)
690 W_Node *node;
692 if (*ptr == NULL)
693 return NULL;
695 node = treeSuccessor(*ptr, self->nil);
697 if (node == self->nil) {
698 *ptr = NULL;
699 return NULL;
700 } else {
701 *ptr = node;
702 return node->data;
706 void *WMBagPrevious(WMBag * self, WMBagIterator * ptr)
708 W_Node *node;
710 if (*ptr == NULL)
711 return NULL;
713 node = treePredecessor(*ptr, self->nil);
715 if (node == self->nil) {
716 *ptr = NULL;
717 return NULL;
718 } else {
719 *ptr = node;
720 return node->data;
724 void *WMBagIteratorAtIndex(WMBag * self, int index, WMBagIterator * ptr)
726 W_Node *node;
728 node = treeSearch(self->root, self->nil, index);
730 if (node == self->nil) {
731 *ptr = NULL;
732 return NULL;
733 } else {
734 *ptr = node;
735 return node->data;
739 int WMBagIndexForIterator(WMBag * bag, WMBagIterator ptr)
741 /* Parameter not used, but tell the compiler that it is ok */
742 (void) bag;
744 return ((W_Node *) ptr)->index;