mirror of
https://github.com/gryf/wmaker.git
synced 2025-12-26 08:22:37 +01:00
Removed array bag, and restructured the tree bag to be WMBag
This commit is contained in:
@@ -11,6 +11,10 @@ changes since wmaker 0.62.1:
|
|||||||
- added WMCreateTabViewItem()
|
- added WMCreateTabViewItem()
|
||||||
- added W_CreateUnmanagedTopView()
|
- added W_CreateUnmanagedTopView()
|
||||||
- added wtokenize()
|
- added wtokenize()
|
||||||
|
- restructured the directory tree. Added Documentation, Examples and Tests
|
||||||
|
subdirectories
|
||||||
|
- removed WMArrayBag and reorganized WMTreeBag to be WMBag.
|
||||||
|
|
||||||
|
|
||||||
changes since wmaker 0.62.0:
|
changes since wmaker 0.62.0:
|
||||||
............................
|
............................
|
||||||
|
|||||||
@@ -68,7 +68,6 @@ libWINGs_a_SOURCES = \
|
|||||||
wview.c \
|
wview.c \
|
||||||
error.c \
|
error.c \
|
||||||
findfile.c \
|
findfile.c \
|
||||||
bagarray.c \
|
|
||||||
bagtree.c \
|
bagtree.c \
|
||||||
connection.c \
|
connection.c \
|
||||||
data.c \
|
data.c \
|
||||||
@@ -82,7 +81,6 @@ libWINGs_a_SOURCES = \
|
|||||||
libWUtil_a_SOURCES = \
|
libWUtil_a_SOURCES = \
|
||||||
WINGs.h \
|
WINGs.h \
|
||||||
WINGsP.h \
|
WINGsP.h \
|
||||||
bagarray.c \
|
|
||||||
bagtree.c \
|
bagtree.c \
|
||||||
connection.c \
|
connection.c \
|
||||||
data.c \
|
data.c \
|
||||||
|
|||||||
@@ -1,9 +1,13 @@
|
|||||||
#include "WINGs.h"
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#include "WINGs.h"
|
||||||
|
|
||||||
|
|
||||||
WMScreen *scr;
|
WMScreen *scr;
|
||||||
|
|
||||||
|
|
||||||
void
|
void
|
||||||
wAbort()
|
wAbort()
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -161,32 +161,8 @@ typedef struct {
|
|||||||
|
|
||||||
typedef void *WMBagIterator;
|
typedef void *WMBagIterator;
|
||||||
|
|
||||||
typedef struct W_BagFunctions {
|
|
||||||
int (*getItemCount)(WMBag *self);
|
|
||||||
int (*appendBag)(WMBag *self, WMBag *bag);
|
|
||||||
int (*putInBag)(WMBag *self, void *item);
|
|
||||||
int (*insertInBag)(WMBag *self, int index, void *item);
|
|
||||||
int (*removeFromBag)(WMBag *bag, void *item);
|
|
||||||
int (*eraseFromBag)(WMBag *bag, int index);
|
|
||||||
int (*deleteFromBag)(WMBag *bag, int index);
|
|
||||||
void *(*getFromBag)(WMBag *bag, int index);
|
|
||||||
int (*firstInBag)(WMBag *bag, void *item);
|
|
||||||
int (*countInBag)(WMBag *bag, void *item);
|
|
||||||
void *(*replaceInBag)(WMBag *bag, int index, void *item);
|
|
||||||
int (*sortBag)(WMBag *bag, int (*comparer)(const void*, const void*));
|
|
||||||
void (*emptyBag)(WMBag *bag);
|
|
||||||
void (*freeBag)(WMBag *bag);
|
|
||||||
void (*mapBag)(WMBag *bag, void (*function)(void*, void*), void *data);
|
|
||||||
int (*findInBag)(WMBag *bag, int (*match)(void*, void*), void *cdata);
|
|
||||||
void *(*first)(WMBag *bag, WMBagIterator *ptr);
|
|
||||||
void *(*last)(WMBag *bag, WMBagIterator *ptr);
|
|
||||||
void *(*next)(WMBag *bag, WMBagIterator *ptr);
|
|
||||||
void *(*previous)(WMBag *bag, WMBagIterator *ptr);
|
|
||||||
void *(*iteratorAtIndex)(WMBag *bag, int index, WMBagIterator *ptr);
|
|
||||||
int (*indexForIterator)(WMBag *bag, WMBagIterator ptr);
|
|
||||||
} W_BagFunctions;
|
|
||||||
|
|
||||||
|
|
||||||
|
#if 0
|
||||||
struct W_Bag {
|
struct W_Bag {
|
||||||
void *data;
|
void *data;
|
||||||
|
|
||||||
@@ -194,6 +170,7 @@ struct W_Bag {
|
|||||||
|
|
||||||
W_BagFunctions func;
|
W_BagFunctions func;
|
||||||
};
|
};
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -373,74 +350,71 @@ WMBag* WMCreateArrayBagWithDestructor(int initialSize,
|
|||||||
* O(lg n) insertion/deletion/search
|
* O(lg n) insertion/deletion/search
|
||||||
* Slow for storing small numbers of elements
|
* Slow for storing small numbers of elements
|
||||||
*/
|
*/
|
||||||
WMBag *WMCreateTreeBag(void);
|
|
||||||
WMBag *WMCreateTreeBagWithDestructor(void (*destructor)(void*));
|
|
||||||
|
|
||||||
|
|
||||||
#define WMCreateArrayBag(a) WMCreateTreeBag()
|
|
||||||
#define WMCreateArrayBagWithDestructor(a,d) WMCreateTreeBagWithDestructor(d)
|
|
||||||
|
|
||||||
#define WMCreateBag(size) WMCreateTreeBag()
|
#define WMCreateBag(size) WMCreateTreeBag()
|
||||||
|
|
||||||
#define WMCreateBagWithDestructor(size, d) WMCreateTreeBagWithDestructor(d)
|
#define WMCreateBagWithDestructor(size, d) WMCreateTreeBagWithDestructor(d)
|
||||||
|
|
||||||
#define WMGetBagItemCount(bag) bag->func.getItemCount(bag)
|
WMBag* WMCreateTreeBag(void);
|
||||||
|
|
||||||
#define WMAppendBag(bag, other) bag->func.appendBag(bag, other)
|
WMBag* WMCreateTreeBagWithDestructor(void (*destructor)(void*));
|
||||||
|
|
||||||
#define WMPutInBag(bag, item) bag->func.putInBag(bag, item)
|
int WMGetBagItemCount(WMBag *self);
|
||||||
|
|
||||||
|
int WMAppendBag(WMBag *self, WMBag *bag);
|
||||||
|
|
||||||
|
int WMPutInBag(WMBag *self, void *item);
|
||||||
|
|
||||||
/* insert will increment the index of elements after it by 1 */
|
/* insert will increment the index of elements after it by 1 */
|
||||||
#define WMInsertInBag(bag, index, item) bag->func.insertInBag(bag, index, item)
|
int WMInsertInBag(WMBag *self, int index, void *item);
|
||||||
|
|
||||||
/* this is slow */
|
/* this is slow */
|
||||||
/* erase will remove the element from the bag,
|
/* erase will remove the element from the bag,
|
||||||
* but will keep the index of the other elements unchanged */
|
* but will keep the index of the other elements unchanged */
|
||||||
#define WMEraseFromBag(bag, index) bag->func.eraseFromBag(bag, index)
|
int WMEraseFromBag(WMBag *self, int index);
|
||||||
|
|
||||||
/* delete and remove will remove the elements and cause the elements
|
/* delete and remove will remove the elements and cause the elements
|
||||||
* after them to decrement their indexes by 1 */
|
* after them to decrement their indexes by 1 */
|
||||||
#define WMRemoveFromBag(bag, item) bag->func.removeFromBag(bag, item)
|
int WMRemoveFromBag(WMBag *self, void *item);
|
||||||
|
|
||||||
#define WMDeleteFromBag(bag, index) bag->func.deleteFromBag(bag, index)
|
int WMDeleteFromBag(WMBag *self, int index);
|
||||||
|
|
||||||
#define WMGetFromBag(bag, index) bag->func.getFromBag(bag, index)
|
void* WMGetFromBag(WMBag *self, int index);
|
||||||
|
|
||||||
#define WMCountInBag(bag, item) bag->func.countInBag(bag, item)
|
void* WMReplaceInBag(WMBag *self, int index, void *item);
|
||||||
|
|
||||||
#define WMReplaceInBag(bag, index, item) bag->func.replaceInBag(bag, index, item)
|
#define WMSetInBag(bag, index, item) WMReplaceInBag(bag, index, item)
|
||||||
#define WMSetInBag(bag, index, item) bag->func.replaceInBag(bag, index, item)
|
|
||||||
|
|
||||||
/* comparer must return:
|
/* comparer must return:
|
||||||
* < 0 if a < b
|
* < 0 if a < b
|
||||||
* > 0 if a > b
|
* > 0 if a > b
|
||||||
* = 0 if a = b
|
* = 0 if a = b
|
||||||
*/
|
*/
|
||||||
#define WMSortBag(bag, comparer) bag->func.sortBag(bag, comparer)
|
int WMSortBag(WMBag *self, int (*comparer)(const void*, const void*));
|
||||||
|
|
||||||
#define WMEmptyBag(bag) bag->func.emptyBag(bag)
|
void WMEmptyBag(WMBag *self);
|
||||||
|
|
||||||
#define WMFreeBag(bag) bag->func.freeBag(bag)
|
void WMFreeBag(WMBag *self);
|
||||||
|
|
||||||
#define WMMapBag(bag, function, cdata) bag->func.mapBag(bag, function, cdata)
|
void WMMapBag(WMBag *self, void (*function)(void*, void*), void *data);
|
||||||
|
|
||||||
#define WMGetFirstInBag(bag, item) bag->func.firstInBag(bag, item)
|
int WMGetFirstInBag(WMBag *self, void *item);
|
||||||
|
|
||||||
#define WMCountInBag(bag, item) bag->func.countInBag(bag, item)
|
int WMCountInBag(WMBag *self, void *item);
|
||||||
|
|
||||||
#define WMFindInBag(bag, match, cdata) bag->func.findInBag(bag, match, cdata)
|
int WMFindInBag(WMBag *self, int (*match)(void*,void*), void *cdata);
|
||||||
|
|
||||||
#define WMBagFirst(bag, ptr) bag->func.first(bag, ptr)
|
void* WMBagFirst(WMBag *self, WMBagIterator *ptr);
|
||||||
|
|
||||||
#define WMBagLast(bag, ptr) bag->func.last(bag, ptr)
|
void* WMBagLast(WMBag *self, WMBagIterator *ptr);
|
||||||
|
|
||||||
#define WMBagNext(bag, ptr) bag->func.next(bag, ptr)
|
void* WMBagNext(WMBag *self, WMBagIterator *ptr);
|
||||||
|
|
||||||
#define WMBagPrevious(bag, ptr) bag->func.previous(bag, ptr)
|
void* WMBagPrevious(WMBag *self, WMBagIterator *ptr);
|
||||||
|
|
||||||
#define WMBagIteratorAtIndex(bag, index, ptr) bag->func.iteratorAtIndex(bag, index, ptr)
|
void* WMBagIteratorAtIndex(WMBag *self, int index, WMBagIterator *ptr);
|
||||||
|
|
||||||
#define WMBagIndexForIterator(bag, ptr) bag->func.indexForIterator(bag, ptr)
|
int WMBagIndexForIterator(WMBag *bag, WMBagIterator ptr);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
152
WINGs/array.c
Normal file
152
WINGs/array.c
Normal file
@@ -0,0 +1,152 @@
|
|||||||
|
/*
|
||||||
|
* Dynamically Resized Array
|
||||||
|
*
|
||||||
|
* Authors: Alfredo K. Kojima <kojima@windowmaker.org>
|
||||||
|
* Dan Pascu <dan@windowmaker.org>
|
||||||
|
*
|
||||||
|
* This code is released to the Public Domain, but
|
||||||
|
* proper credit is always appreciated :)
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#include "WUtil.h"
|
||||||
|
|
||||||
|
|
||||||
|
#define INITIAL_SIZE 8
|
||||||
|
#define RESIZE_INCREMENT 8
|
||||||
|
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
void **items; /* the array data */
|
||||||
|
unsigned length; /* # of items in array */
|
||||||
|
unsigned allocSize; /* allocated size of array */
|
||||||
|
} W_Array;
|
||||||
|
|
||||||
|
|
||||||
|
WMArray*
|
||||||
|
WMCreateArray(unsigned initialSize)
|
||||||
|
{
|
||||||
|
Array *a;
|
||||||
|
|
||||||
|
a = malloc(sizeof(Array));
|
||||||
|
sassertrv(a!=NULL, NULL);
|
||||||
|
|
||||||
|
if (initialSize == 0) {
|
||||||
|
initialSize = INITIAL_SIZE;
|
||||||
|
}
|
||||||
|
|
||||||
|
a->items = malloc(sizeof(void*)*initialSize);
|
||||||
|
sassertdo(a->items!=NULL,
|
||||||
|
free(a);
|
||||||
|
return NULL;
|
||||||
|
);
|
||||||
|
a->length = 0;
|
||||||
|
a->allocSize = initialSize;
|
||||||
|
|
||||||
|
return a;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void ArrayFree(Array *array)
|
||||||
|
{
|
||||||
|
free(array->items);
|
||||||
|
free(array);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void ArrayClear(Array *array)
|
||||||
|
{
|
||||||
|
memset(array->items, 0, array->length*sizeof(void*));
|
||||||
|
array->length = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int ArraySet(Array *array, unsigned index, void *data)
|
||||||
|
{
|
||||||
|
sassertrv(index > array->length, 0);
|
||||||
|
|
||||||
|
if (index == array->length)
|
||||||
|
return ArrayAppend(array, data);
|
||||||
|
|
||||||
|
array->items[index] = data;
|
||||||
|
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#if 0
|
||||||
|
void *ArrayGet(Array *array, unsigned index)
|
||||||
|
{
|
||||||
|
return array->items[index];
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
int ArrayAppend(Array *array, void *data)
|
||||||
|
{
|
||||||
|
if (array->length >= array->allocSize) {
|
||||||
|
array->allocSize += RESIZE_INCREMENT;
|
||||||
|
array->items = realloc(array->items, sizeof(void*)*array->allocSize);
|
||||||
|
sassertrv(array->items != NULL, 0);
|
||||||
|
}
|
||||||
|
array->items[array->length++] = data;
|
||||||
|
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int ArrayInsert(Array *array, unsigned index, void *data)
|
||||||
|
{
|
||||||
|
sassertrv(index < array->length, 0);
|
||||||
|
|
||||||
|
if (array->length >= array->allocSize) {
|
||||||
|
array->allocSize += RESIZE_INCREMENT;
|
||||||
|
array->items = realloc(array->items, sizeof(void*)*array->allocSize);
|
||||||
|
sassertrv(array->items != NULL, 0);
|
||||||
|
}
|
||||||
|
if (index < array->length-1)
|
||||||
|
memmove(array->items+index+1, array->items+index,
|
||||||
|
sizeof(void*)*(array->length-index));
|
||||||
|
array->items[index] = data;
|
||||||
|
|
||||||
|
array->length++;
|
||||||
|
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void ArrayDelete(Array *array, unsigned index)
|
||||||
|
{
|
||||||
|
sassertr(index < array->length);
|
||||||
|
|
||||||
|
memmove(array->items+index, array->items+index+1,
|
||||||
|
sizeof(void*)*(array->length-index-1));
|
||||||
|
|
||||||
|
array->length--;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
void *ArrayPop(Array *array)
|
||||||
|
{
|
||||||
|
void *d = ArrayGet(array, array->length-1);
|
||||||
|
|
||||||
|
ArrayDelete(array, array->length-1);
|
||||||
|
|
||||||
|
return d;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int ArrayIndex(Array *array, void *data)
|
||||||
|
{
|
||||||
|
unsigned i;
|
||||||
|
|
||||||
|
for (i = 0; i < array->length; i++)
|
||||||
|
if (array->items[i] == data)
|
||||||
|
return i;
|
||||||
|
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
418
WINGs/bagarray.c
418
WINGs/bagarray.c
@@ -1,418 +0,0 @@
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <string.h>
|
|
||||||
|
|
||||||
#include "WUtil.h"
|
|
||||||
|
|
||||||
#if 0
|
|
||||||
|
|
||||||
typedef struct W_ArrayBag {
|
|
||||||
void **items;
|
|
||||||
int size;
|
|
||||||
int count;
|
|
||||||
|
|
||||||
int base;
|
|
||||||
int first;
|
|
||||||
int last;
|
|
||||||
} W_ArrayBag;
|
|
||||||
|
|
||||||
|
|
||||||
static int getItemCount(WMBag *bag);
|
|
||||||
static int appendBag(WMBag *bag, WMBag *appendedBag);
|
|
||||||
static int putInBag(WMBag *bag, void *item);
|
|
||||||
static int insertInBag(WMBag *bag, int index, void *item);
|
|
||||||
static int removeFromBag(WMBag *bag, void *item);
|
|
||||||
static int deleteFromBag(WMBag *bag, int index);
|
|
||||||
static void* getFromBag(WMBag *bag, int index);
|
|
||||||
static int firstInBag(WMBag *bag, void *item);
|
|
||||||
static int countInBag(WMBag *bag, void *item);
|
|
||||||
static void* replaceInBag(WMBag *bag, int index, void *item);
|
|
||||||
static int sortBag(WMBag *bag, int (*comparer)(const void*, const void*));
|
|
||||||
static void emptyBag(WMBag *bag);
|
|
||||||
static void freeBag(WMBag *bag);
|
|
||||||
static void mapBag(WMBag *bag, void (*function)(void*, void*), void *data);
|
|
||||||
static int findInBag(WMBag *bag, int (*match)(void*));
|
|
||||||
static void* first(WMBag *bag, void **ptr);
|
|
||||||
static void* last(WMBag *bag, void **ptr);
|
|
||||||
static void* next(WMBag *bag, void **ptr);
|
|
||||||
static void* previous(WMBag *bag, void **ptr);
|
|
||||||
static void* iteratorAtIndex(WMBag *bag, int index, WMBagIterator *ptr);
|
|
||||||
static int indexForIterator(WMBag *bag, WMBagIterator ptr);
|
|
||||||
|
|
||||||
|
|
||||||
static W_BagFunctions arrayFunctions = {
|
|
||||||
getItemCount,
|
|
||||||
appendBag,
|
|
||||||
putInBag,
|
|
||||||
insertInBag,
|
|
||||||
removeFromBag,
|
|
||||||
deleteFromBag,
|
|
||||||
deleteFromBag,
|
|
||||||
getFromBag,
|
|
||||||
firstInBag,
|
|
||||||
countInBag,
|
|
||||||
replaceInBag,
|
|
||||||
sortBag,
|
|
||||||
emptyBag,
|
|
||||||
freeBag,
|
|
||||||
mapBag,
|
|
||||||
findInBag,
|
|
||||||
first,
|
|
||||||
last,
|
|
||||||
next,
|
|
||||||
previous,
|
|
||||||
iteratorAtIndex,
|
|
||||||
indexForIterator
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
#define ARRAY ((W_ArrayBag*)(bag->data))
|
|
||||||
|
|
||||||
|
|
||||||
#define I2O(a, i) ((a)->base + i)
|
|
||||||
|
|
||||||
|
|
||||||
WMBag*
|
|
||||||
WMCreateArrayBagWithDestructor(int initialSize, void (*destructor)(void*))
|
|
||||||
{
|
|
||||||
WMBag *bag;
|
|
||||||
W_ArrayBag *array;
|
|
||||||
int size;
|
|
||||||
|
|
||||||
assert(0&&"array bag is not working");
|
|
||||||
bag = wmalloc(sizeof(WMBag));
|
|
||||||
|
|
||||||
array = wmalloc(sizeof(W_ArrayBag));
|
|
||||||
|
|
||||||
bag->data = array;
|
|
||||||
|
|
||||||
array->items = wmalloc(sizeof(void*) * size);
|
|
||||||
array->size = size;
|
|
||||||
array->count = 0;
|
|
||||||
array->first = 0;
|
|
||||||
array->last = 0;
|
|
||||||
array->base = 0;
|
|
||||||
|
|
||||||
bag->func = arrayFunctions;
|
|
||||||
|
|
||||||
bag->destructor = destructor;
|
|
||||||
|
|
||||||
return bag;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
WMBag*
|
|
||||||
WMCreateArrayBag(int initialSize)
|
|
||||||
{
|
|
||||||
return WMCreateArrayBagWithDestructor(initialSize, NULL);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
static int
|
|
||||||
getItemCount(WMBag *bag)
|
|
||||||
{
|
|
||||||
return ARRAY->count;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
static int
|
|
||||||
appendBag(WMBag *bag, WMBag *appendedBag)
|
|
||||||
{
|
|
||||||
W_ArrayBag *array = (W_ArrayBag*)appendedBag->data;
|
|
||||||
int ok;
|
|
||||||
int i;
|
|
||||||
|
|
||||||
for (i = array->first; i <= array->last; i++) {
|
|
||||||
if (array->items[array->base+i]) {
|
|
||||||
ok = putInBag(bag, array->items[array->base+i]);
|
|
||||||
if (!ok)
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
static int
|
|
||||||
putInBag(WMBag *bag, void *item)
|
|
||||||
{
|
|
||||||
return insertInBag(bag, ARRAY->last+1, item);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
static int
|
|
||||||
insertInBag(WMBag *bag, int index, void *item)
|
|
||||||
{
|
|
||||||
W_ArrayBag *array = ARRAY;
|
|
||||||
|
|
||||||
if (I2O(array, index) >= array->size) {
|
|
||||||
array->size = WMAX(array->size + 16, I2O(array, index));
|
|
||||||
array->items = wrealloc(array->items, sizeof(void*) * array->size);
|
|
||||||
memset(array->items + I2O(array, array->last), 0,
|
|
||||||
sizeof(void*) * (array->size - I2O(array, array->last)));
|
|
||||||
}
|
|
||||||
|
|
||||||
if (index > array->last) {
|
|
||||||
array->last = index;
|
|
||||||
} else if (index >= array->first) {
|
|
||||||
memmove(array->items + I2O(array, index),
|
|
||||||
array->items + (I2O(array, index) + 1), sizeof(void*));
|
|
||||||
array->last++;
|
|
||||||
} else {
|
|
||||||
memmove(array->items,
|
|
||||||
array->items + (abs(index) - array->base),
|
|
||||||
sizeof(void*) * (abs(index) - array->base));
|
|
||||||
memset(array->items, 0, sizeof(void*) * (abs(index) - array->base));
|
|
||||||
array->first = index;
|
|
||||||
array->base = abs(index);
|
|
||||||
}
|
|
||||||
|
|
||||||
array->items[array->base + index] = item;
|
|
||||||
array->count++;
|
|
||||||
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
static int
|
|
||||||
removeFromBag(WMBag *bag, void *item)
|
|
||||||
{
|
|
||||||
int i;
|
|
||||||
W_ArrayBag *array = ARRAY;
|
|
||||||
|
|
||||||
for (i = 0; i < array->count; i++) {
|
|
||||||
if (array->items[i] == item) {
|
|
||||||
if (bag->destructor)
|
|
||||||
bag->destructor(array->items[i]);
|
|
||||||
|
|
||||||
memmove(&array->items[i], &array->items[i + 1],
|
|
||||||
(array->count - i - 1) * sizeof(void*));
|
|
||||||
array->count--;
|
|
||||||
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
static int
|
|
||||||
deleteFromBag(WMBag *bag, int index)
|
|
||||||
{
|
|
||||||
W_ArrayBag *array = ARRAY;
|
|
||||||
|
|
||||||
if (index < 0 || index >= array->count)
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
if (index < array->count-1) {
|
|
||||||
if (bag->destructor)
|
|
||||||
bag->destructor(array->items[index]);
|
|
||||||
|
|
||||||
memmove(&array->items[index], &array->items[index + 1],
|
|
||||||
(array->count - index - 1) * sizeof(void*));
|
|
||||||
}
|
|
||||||
array->count--;
|
|
||||||
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
static void*
|
|
||||||
getFromBag(WMBag *bag, int index)
|
|
||||||
{
|
|
||||||
if (index < 0 || index >= ARRAY->count)
|
|
||||||
return NULL;
|
|
||||||
|
|
||||||
return ARRAY->items[index];
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
static int
|
|
||||||
firstInBag(WMBag *bag, void *item)
|
|
||||||
{
|
|
||||||
int j;
|
|
||||||
int count = ARRAY->count;
|
|
||||||
W_ArrayBag *array = ARRAY;
|
|
||||||
|
|
||||||
for (j = 0; j < count; j++) {
|
|
||||||
if (array->items[j] == item)
|
|
||||||
return j;
|
|
||||||
}
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
static int
|
|
||||||
countInBag(WMBag *bag, void *item)
|
|
||||||
{
|
|
||||||
int i, j;
|
|
||||||
int count = ARRAY->count;
|
|
||||||
W_ArrayBag *array = ARRAY;
|
|
||||||
|
|
||||||
for (j = 0, i = 0; j < count; j++) {
|
|
||||||
if (array->items[j] == item)
|
|
||||||
i++;
|
|
||||||
}
|
|
||||||
return i;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
static void*
|
|
||||||
replaceInBag(WMBag *bag, int index, void *item)
|
|
||||||
{
|
|
||||||
void *old;
|
|
||||||
|
|
||||||
if (index < 0 || index >= ARRAY->count)
|
|
||||||
return NULL;
|
|
||||||
|
|
||||||
old = ARRAY->items[index];
|
|
||||||
|
|
||||||
ARRAY->items[index] = item;
|
|
||||||
|
|
||||||
return old;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
static int
|
|
||||||
sortBag(WMBag *bag, int (*comparer)(const void*, const void*))
|
|
||||||
{
|
|
||||||
qsort(ARRAY->items, ARRAY->count, sizeof(void*), comparer);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
static void
|
|
||||||
emptyBag(WMBag *bag)
|
|
||||||
{
|
|
||||||
W_ArrayBag *array = ARRAY;
|
|
||||||
|
|
||||||
if (bag->destructor) {
|
|
||||||
while (--array->count) {
|
|
||||||
bag->destructor(array->items[array->count]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
ARRAY->count=0;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
static void
|
|
||||||
freeBag(WMBag *bag)
|
|
||||||
{
|
|
||||||
emptyBag(bag);
|
|
||||||
|
|
||||||
wfree(ARRAY->items);
|
|
||||||
wfree(ARRAY);
|
|
||||||
wfree(bag);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
static void
|
|
||||||
mapBag(WMBag *bag, void (*function)(void *, void *), void *clientData)
|
|
||||||
{
|
|
||||||
int i;
|
|
||||||
|
|
||||||
for (i = 0; i < ARRAY->last; i++) {
|
|
||||||
if (ARRAY->items[i])
|
|
||||||
(*function)(ARRAY->items[i], clientData);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
static int
|
|
||||||
findInBag(WMBag *bag, int (*match)(void*))
|
|
||||||
{
|
|
||||||
int i;
|
|
||||||
|
|
||||||
for (i = 0; i < ARRAY->last; i++) {
|
|
||||||
if ((*match)(ARRAY->items[i]))
|
|
||||||
return i;
|
|
||||||
}
|
|
||||||
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
static void*
|
|
||||||
first(WMBag *bag, void **ptr)
|
|
||||||
{
|
|
||||||
*ptr = NULL;
|
|
||||||
|
|
||||||
if (ARRAY->count == 0)
|
|
||||||
return NULL;
|
|
||||||
|
|
||||||
*(int**)ptr = 0;
|
|
||||||
return ARRAY->items[0];
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
static void*
|
|
||||||
last(WMBag *bag, void **ptr)
|
|
||||||
{
|
|
||||||
*ptr = NULL;
|
|
||||||
|
|
||||||
if (ARRAY->count == 0)
|
|
||||||
return NULL;
|
|
||||||
|
|
||||||
*(int*)ptr = ARRAY->count-1;
|
|
||||||
|
|
||||||
return ARRAY->items[ARRAY->count-1];
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
static void*
|
|
||||||
next(WMBag *bag, void **ptr)
|
|
||||||
{
|
|
||||||
if (!*ptr)
|
|
||||||
return NULL;
|
|
||||||
|
|
||||||
(*(int*)ptr)++;
|
|
||||||
if (*(int*)ptr >= ARRAY->count) {
|
|
||||||
*ptr = NULL;
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
return ARRAY->items[*(int*)ptr];
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
static void*
|
|
||||||
previous(WMBag *bag, void **ptr)
|
|
||||||
{
|
|
||||||
if (!*ptr)
|
|
||||||
return NULL;
|
|
||||||
|
|
||||||
(*(int*)ptr)--;
|
|
||||||
if (*(int*)ptr < 0) {
|
|
||||||
*ptr = NULL;
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
return ARRAY->items[*(int*)ptr];
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
static void*
|
|
||||||
iteratorAtIndex(WMBag *bag, int index, WMBagIterator *ptr)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
static int
|
|
||||||
indexForIterator(WMBag *bag, WMBagIterator ptr)
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif
|
|
||||||
355
WINGs/bagtree.c
355
WINGs/bagtree.c
@@ -19,16 +19,18 @@ typedef struct W_Node {
|
|||||||
} W_Node;
|
} W_Node;
|
||||||
|
|
||||||
|
|
||||||
typedef struct W_TreeBag {
|
typedef struct W_Bag {
|
||||||
W_Node *root;
|
W_Node *root;
|
||||||
|
|
||||||
W_Node *nil; /* sentinel */
|
W_Node *nil; /* sentinel */
|
||||||
|
|
||||||
int count;
|
int count;
|
||||||
} W_TreeBag;
|
|
||||||
|
void (*destructor)(void *item);
|
||||||
|
} W_Bag;
|
||||||
|
|
||||||
|
|
||||||
|
#if 0
|
||||||
static int getItemCount(WMBag *self);
|
static int getItemCount(WMBag *self);
|
||||||
static int appendBag(WMBag *self, WMBag *bag);
|
static int appendBag(WMBag *self, WMBag *bag);
|
||||||
static int putInBag(WMBag *self, void *item);
|
static int putInBag(WMBag *self, void *item);
|
||||||
@@ -51,34 +53,7 @@ static void *next(WMBag *bag, WMBagIterator *ptr);
|
|||||||
static void *previous(WMBag *bag, WMBagIterator *ptr);
|
static void *previous(WMBag *bag, WMBagIterator *ptr);
|
||||||
static void *iteratorAtIndex(WMBag *bag, int index, WMBagIterator *ptr);
|
static void *iteratorAtIndex(WMBag *bag, int index, WMBagIterator *ptr);
|
||||||
static int indexForIterator(WMBag *bag, WMBagIterator ptr);
|
static int indexForIterator(WMBag *bag, WMBagIterator ptr);
|
||||||
|
#endif
|
||||||
|
|
||||||
static W_BagFunctions bagFunctions = {
|
|
||||||
getItemCount,
|
|
||||||
appendBag,
|
|
||||||
putInBag,
|
|
||||||
insertInBag,
|
|
||||||
removeFromBag,
|
|
||||||
eraseFromBag,
|
|
||||||
deleteFromBag,
|
|
||||||
getFromBag,
|
|
||||||
firstInBag,
|
|
||||||
countInBag,
|
|
||||||
replaceInBag,
|
|
||||||
sortBag,
|
|
||||||
emptyBag,
|
|
||||||
freeBag,
|
|
||||||
mapBag,
|
|
||||||
findInBag,
|
|
||||||
first,
|
|
||||||
last,
|
|
||||||
next,
|
|
||||||
previous,
|
|
||||||
iteratorAtIndex,
|
|
||||||
indexForIterator
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#define IS_LEFT(node) (node == node->parent->left)
|
#define IS_LEFT(node) (node == node->parent->left)
|
||||||
@@ -86,7 +61,8 @@ static W_BagFunctions bagFunctions = {
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
static void leftRotate(W_TreeBag *tree, W_Node *node)
|
static void
|
||||||
|
leftRotate(W_Bag *tree, W_Node *node)
|
||||||
{
|
{
|
||||||
W_Node *node2;
|
W_Node *node2;
|
||||||
|
|
||||||
@@ -112,7 +88,8 @@ static void leftRotate(W_TreeBag *tree, W_Node *node)
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
static void rightRotate(W_TreeBag *tree, W_Node *node)
|
static void
|
||||||
|
rightRotate(W_Bag *tree, W_Node *node)
|
||||||
{
|
{
|
||||||
W_Node *node2;
|
W_Node *node2;
|
||||||
|
|
||||||
@@ -137,8 +114,8 @@ static void rightRotate(W_TreeBag *tree, W_Node *node)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static void
|
||||||
static void treeInsert(W_TreeBag *tree, W_Node *node)
|
treeInsert(W_Bag *tree, W_Node *node)
|
||||||
{
|
{
|
||||||
W_Node *y = tree->nil;
|
W_Node *y = tree->nil;
|
||||||
W_Node *x = tree->root;
|
W_Node *x = tree->root;
|
||||||
@@ -160,7 +137,8 @@ static void treeInsert(W_TreeBag *tree, W_Node *node)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static void rbTreeInsert(W_TreeBag *tree, W_Node *node)
|
static void
|
||||||
|
rbTreeInsert(W_Bag *tree, W_Node *node)
|
||||||
{
|
{
|
||||||
W_Node *y;
|
W_Node *y;
|
||||||
|
|
||||||
@@ -213,8 +191,8 @@ static void rbTreeInsert(W_TreeBag *tree, W_Node *node)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static void
|
||||||
static void rbDeleteFixup(W_TreeBag *tree, W_Node *node)
|
rbDeleteFixup(W_Bag *tree, W_Node *node)
|
||||||
{
|
{
|
||||||
W_Node *w;
|
W_Node *w;
|
||||||
|
|
||||||
@@ -274,7 +252,8 @@ static void rbDeleteFixup(W_TreeBag *tree, W_Node *node)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static W_Node *treeMinimum(W_Node *node, W_Node *nil)
|
static W_Node*
|
||||||
|
treeMinimum(W_Node *node, W_Node *nil)
|
||||||
{
|
{
|
||||||
while (node->left != nil)
|
while (node->left != nil)
|
||||||
node = node->left;
|
node = node->left;
|
||||||
@@ -282,7 +261,8 @@ static W_Node *treeMinimum(W_Node *node, W_Node *nil)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static W_Node *treeMaximum(W_Node *node, W_Node *nil)
|
static W_Node*
|
||||||
|
treeMaximum(W_Node *node, W_Node *nil)
|
||||||
{
|
{
|
||||||
while (node->right != nil)
|
while (node->right != nil)
|
||||||
node = node->right;
|
node = node->right;
|
||||||
@@ -290,7 +270,8 @@ static W_Node *treeMaximum(W_Node *node, W_Node *nil)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static W_Node *treeSuccessor(W_Node *node, W_Node *nil)
|
static W_Node*
|
||||||
|
treeSuccessor(W_Node *node, W_Node *nil)
|
||||||
{
|
{
|
||||||
W_Node *y;
|
W_Node *y;
|
||||||
|
|
||||||
@@ -306,7 +287,8 @@ static W_Node *treeSuccessor(W_Node *node, W_Node *nil)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static W_Node *treePredecessor(W_Node *node, W_Node *nil)
|
static W_Node*
|
||||||
|
treePredecessor(W_Node *node, W_Node *nil)
|
||||||
{
|
{
|
||||||
W_Node *y;
|
W_Node *y;
|
||||||
|
|
||||||
@@ -322,7 +304,8 @@ static W_Node *treePredecessor(W_Node *node, W_Node *nil)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static W_Node *rbTreeDelete(W_TreeBag *tree, W_Node *node)
|
static W_Node*
|
||||||
|
rbTreeDelete(W_Bag *tree, W_Node *node)
|
||||||
{
|
{
|
||||||
W_Node *nil = tree->nil;
|
W_Node *nil = tree->nil;
|
||||||
W_Node *x, *y;
|
W_Node *x, *y;
|
||||||
@@ -362,8 +345,8 @@ static W_Node *rbTreeDelete(W_TreeBag *tree, W_Node *node)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static W_Node*
|
||||||
static W_Node *treeSearch(W_Node *root, W_Node *nil, int index)
|
treeSearch(W_Node *root, W_Node *nil, int index)
|
||||||
{
|
{
|
||||||
if (root == nil || root->index == index) {
|
if (root == nil || root->index == index) {
|
||||||
return root;
|
return root;
|
||||||
@@ -377,7 +360,8 @@ static W_Node *treeSearch(W_Node *root, W_Node *nil, int index)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static W_Node *treeFind(W_Node *root, W_Node *nil, void *data)
|
static W_Node*
|
||||||
|
treeFind(W_Node *root, W_Node *nil, void *data)
|
||||||
{
|
{
|
||||||
W_Node *tmp;
|
W_Node *tmp;
|
||||||
|
|
||||||
@@ -395,8 +379,6 @@ static W_Node *treeFind(W_Node *root, W_Node *nil, void *data)
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#if 0
|
#if 0
|
||||||
static char buf[512];
|
static char buf[512];
|
||||||
|
|
||||||
@@ -428,63 +410,62 @@ void PrintTree(WMBag *bag)
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
//#define SELF ((W_TreeBag*)self->data)
|
||||||
|
|
||||||
|
WMBag*
|
||||||
#define SELF ((W_TreeBag*)self->data)
|
WMCreateTreeBag(void)
|
||||||
|
|
||||||
WMBag *WMCreateTreeBag(void)
|
|
||||||
{
|
{
|
||||||
return WMCreateTreeBagWithDestructor(NULL);
|
return WMCreateTreeBagWithDestructor(NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
WMBag *WMCreateTreeBagWithDestructor(void (*destructor)(void*))
|
WMBag*
|
||||||
|
WMCreateTreeBagWithDestructor(void (*destructor)(void*))
|
||||||
{
|
{
|
||||||
WMBag *bag;
|
WMBag *bag;
|
||||||
W_TreeBag *tree;
|
|
||||||
|
|
||||||
bag = wmalloc(sizeof(WMBag));
|
bag = wmalloc(sizeof(WMBag));
|
||||||
|
|
||||||
bag->data = tree = wmalloc(sizeof(W_TreeBag));
|
memset(bag, 0, sizeof(WMBag));
|
||||||
memset(tree, 0, sizeof(W_TreeBag));
|
|
||||||
|
|
||||||
tree->nil = wmalloc(sizeof(W_Node));
|
bag->nil = wmalloc(sizeof(W_Node));
|
||||||
memset(tree->nil, 0, sizeof(W_Node));
|
memset(bag->nil, 0, sizeof(W_Node));
|
||||||
tree->nil->left = tree->nil->right = tree->nil->parent = tree->nil;
|
bag->nil->left = bag->nil->right = bag->nil->parent = bag->nil;
|
||||||
tree->nil->index = WBNotFound;
|
bag->nil->index = WBNotFound;
|
||||||
|
|
||||||
tree->root = tree->nil;
|
bag->root = bag->nil;
|
||||||
|
|
||||||
bag->destructor = destructor;
|
bag->destructor = destructor;
|
||||||
|
|
||||||
bag->func = bagFunctions;
|
|
||||||
|
|
||||||
return bag;
|
return bag;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static int getItemCount(WMBag *self)
|
int
|
||||||
|
WMGetBagItemCount(WMBag *self)
|
||||||
{
|
{
|
||||||
return SELF->count;
|
return self->count;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static int appendBag(WMBag *self, WMBag *bag)
|
int
|
||||||
|
WMAppendBag(WMBag *self, WMBag *bag)
|
||||||
{
|
{
|
||||||
WMBagIterator ptr;
|
WMBagIterator ptr;
|
||||||
void *data;
|
void *data;
|
||||||
|
|
||||||
for (data = first(bag, &ptr); data != NULL; data = next(bag, &ptr)) {
|
for (data = WMBagFirst(bag, &ptr); data != NULL; data = WMBagNext(bag, &ptr)) {
|
||||||
if (!putInBag(self, data))
|
if (!WMPutInBag(self, data))
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
extern WMBag *bla;
|
//extern WMBag *bla;
|
||||||
|
|
||||||
static int putInBag(WMBag *self, void *item)
|
int
|
||||||
|
WMPutInBag(WMBag *self, void *item)
|
||||||
{
|
{
|
||||||
W_Node *ptr;
|
W_Node *ptr;
|
||||||
|
|
||||||
@@ -492,20 +473,21 @@ static int putInBag(WMBag *self, void *item)
|
|||||||
ptr = wmalloc(sizeof(W_Node));
|
ptr = wmalloc(sizeof(W_Node));
|
||||||
|
|
||||||
ptr->data = item;
|
ptr->data = item;
|
||||||
ptr->index = SELF->count;
|
ptr->index = self->count;
|
||||||
ptr->left = SELF->nil;
|
ptr->left = self->nil;
|
||||||
ptr->right = SELF->nil;
|
ptr->right = self->nil;
|
||||||
ptr->parent = SELF->nil;
|
ptr->parent = self->nil;
|
||||||
|
|
||||||
rbTreeInsert(SELF, ptr);
|
rbTreeInsert(self, ptr);
|
||||||
|
|
||||||
SELF->count++;
|
self->count++;
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static int insertInBag(WMBag *self, int index, void *item)
|
int
|
||||||
|
WMInsertInBag(WMBag *self, int index, void *item)
|
||||||
{
|
{
|
||||||
W_Node *ptr;
|
W_Node *ptr;
|
||||||
|
|
||||||
@@ -513,40 +495,40 @@ static int insertInBag(WMBag *self, int index, void *item)
|
|||||||
|
|
||||||
ptr->data = item;
|
ptr->data = item;
|
||||||
ptr->index = index;
|
ptr->index = index;
|
||||||
ptr->left = SELF->nil;
|
ptr->left = self->nil;
|
||||||
ptr->right = SELF->nil;
|
ptr->right = self->nil;
|
||||||
ptr->parent = SELF->nil;
|
ptr->parent = self->nil;
|
||||||
|
|
||||||
rbTreeInsert(SELF, ptr);
|
rbTreeInsert(self, ptr);
|
||||||
|
|
||||||
while ((ptr = treeSuccessor(ptr, SELF->nil)) != SELF->nil) {
|
while ((ptr = treeSuccessor(ptr, self->nil)) != self->nil) {
|
||||||
ptr->index++;
|
ptr->index++;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
SELF->count++;
|
self->count++;
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int
|
||||||
static int removeFromBag(WMBag *self, void *item)
|
WMRemoveFromBag(WMBag *self, void *item)
|
||||||
{
|
{
|
||||||
W_Node *ptr = treeFind(SELF->root, SELF->nil, item);
|
W_Node *ptr = treeFind(self->root, self->nil, item);
|
||||||
|
|
||||||
if (ptr != SELF->nil) {
|
if (ptr != self->nil) {
|
||||||
W_Node *tmp;
|
W_Node *tmp;
|
||||||
|
|
||||||
SELF->count--;
|
self->count--;
|
||||||
|
|
||||||
tmp = treeSuccessor(ptr, SELF->nil);
|
tmp = treeSuccessor(ptr, self->nil);
|
||||||
while (tmp != SELF->nil) {
|
while (tmp != self->nil) {
|
||||||
tmp->index--;
|
tmp->index--;
|
||||||
tmp = treeSuccessor(tmp, SELF->nil);
|
tmp = treeSuccessor(tmp, self->nil);
|
||||||
}
|
}
|
||||||
|
|
||||||
ptr = rbTreeDelete(SELF, ptr);
|
ptr = rbTreeDelete(self, ptr);
|
||||||
if (self->destructor)
|
if (self->destructor)
|
||||||
self->destructor(ptr->data);
|
self->destructor(ptr->data);
|
||||||
free(ptr);
|
free(ptr);
|
||||||
@@ -558,21 +540,21 @@ static int removeFromBag(WMBag *self, void *item)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int
|
||||||
static int eraseFromBag(WMBag *self, int index)
|
WMEraseFromBag(WMBag *self, int index)
|
||||||
{
|
{
|
||||||
W_Node *ptr = treeSearch(SELF->root, SELF->nil, index);
|
W_Node *ptr = treeSearch(self->root, self->nil, index);
|
||||||
|
|
||||||
if (ptr != SELF->nil) {
|
if (ptr != self->nil) {
|
||||||
|
|
||||||
SELF->count--;
|
self->count--;
|
||||||
|
|
||||||
ptr = rbTreeDelete(SELF, ptr);
|
ptr = rbTreeDelete(self, ptr);
|
||||||
if (self->destructor)
|
if (self->destructor)
|
||||||
self->destructor(ptr->data);
|
self->destructor(ptr->data);
|
||||||
free(ptr);
|
free(ptr);
|
||||||
|
|
||||||
wassertrv(SELF->count == 0||SELF->root->index >= 0, 1);
|
wassertrv(self->count == 0||self->root->index >= 0, 1);
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
} else {
|
} else {
|
||||||
@@ -581,27 +563,28 @@ static int eraseFromBag(WMBag *self, int index)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static int deleteFromBag(WMBag *self, int index)
|
int
|
||||||
|
WMDeleteFromBag(WMBag *self, int index)
|
||||||
{
|
{
|
||||||
W_Node *ptr = treeSearch(SELF->root, SELF->nil, index);
|
W_Node *ptr = treeSearch(self->root, self->nil, index);
|
||||||
|
|
||||||
if (ptr != SELF->nil) {
|
if (ptr != self->nil) {
|
||||||
W_Node *tmp;
|
W_Node *tmp;
|
||||||
|
|
||||||
SELF->count--;
|
self->count--;
|
||||||
|
|
||||||
tmp = treeSuccessor(ptr, SELF->nil);
|
tmp = treeSuccessor(ptr, self->nil);
|
||||||
while (tmp != SELF->nil) {
|
while (tmp != self->nil) {
|
||||||
tmp->index--;
|
tmp->index--;
|
||||||
tmp = treeSuccessor(tmp, SELF->nil);
|
tmp = treeSuccessor(tmp, self->nil);
|
||||||
}
|
}
|
||||||
|
|
||||||
ptr = rbTreeDelete(SELF, ptr);
|
ptr = rbTreeDelete(self, ptr);
|
||||||
if (self->destructor)
|
if (self->destructor)
|
||||||
self->destructor(ptr->data);
|
self->destructor(ptr->data);
|
||||||
free(ptr);
|
free(ptr);
|
||||||
|
|
||||||
wassertrv(SELF->count == 0||SELF->root->index >= 0, 1);
|
wassertrv(self->count == 0||self->root->index >= 0, 1);
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
} else {
|
} else {
|
||||||
@@ -610,25 +593,26 @@ static int deleteFromBag(WMBag *self, int index)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static void *getFromBag(WMBag *self, int index)
|
void*
|
||||||
|
WMGetFromBag(WMBag *self, int index)
|
||||||
{
|
{
|
||||||
W_Node *node;
|
W_Node *node;
|
||||||
|
|
||||||
node = treeSearch(SELF->root, SELF->nil, index);
|
node = treeSearch(self->root, self->nil, index);
|
||||||
if (node != SELF->nil)
|
if (node != self->nil)
|
||||||
return node->data;
|
return node->data;
|
||||||
else
|
else
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int
|
||||||
static int firstInBag(WMBag *self, void *item)
|
WMGetFirstInBag(WMBag *self, void *item)
|
||||||
{
|
{
|
||||||
W_Node *node;
|
W_Node *node;
|
||||||
|
|
||||||
node = treeFind(SELF->root, SELF->nil, item);
|
node = treeFind(self->root, self->nil, item);
|
||||||
if (node != SELF->nil)
|
if (node != self->nil)
|
||||||
return node->index;
|
return node->index;
|
||||||
else
|
else
|
||||||
return WBNotFound;
|
return WBNotFound;
|
||||||
@@ -636,7 +620,8 @@ static int firstInBag(WMBag *self, void *item)
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
static int treeCount(W_Node *root, W_Node *nil, void *item)
|
static int
|
||||||
|
treeCount(W_Node *root, W_Node *nil, void *item)
|
||||||
{
|
{
|
||||||
int count = 0;
|
int count = 0;
|
||||||
|
|
||||||
@@ -657,24 +642,26 @@ static int treeCount(W_Node *root, W_Node *nil, void *item)
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
static int countInBag(WMBag *self, void *item)
|
int
|
||||||
|
WMCountInBag(WMBag *self, void *item)
|
||||||
{
|
{
|
||||||
return treeCount(SELF->root, SELF->nil, item);
|
return treeCount(self->root, self->nil, item);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static void *replaceInBag(WMBag *self, int index, void *item)
|
void*
|
||||||
|
WMReplaceInBag(WMBag *self, int index, void *item)
|
||||||
{
|
{
|
||||||
W_Node *ptr = treeSearch(SELF->root, SELF->nil, index);
|
W_Node *ptr = treeSearch(self->root, self->nil, index);
|
||||||
void *old = NULL;
|
void *old = NULL;
|
||||||
|
|
||||||
if (item == NULL) {
|
if (item == NULL) {
|
||||||
SELF->count--;
|
self->count--;
|
||||||
ptr = rbTreeDelete(SELF, ptr);
|
ptr = rbTreeDelete(self, ptr);
|
||||||
if (self->destructor)
|
if (self->destructor)
|
||||||
self->destructor(ptr->data);
|
self->destructor(ptr->data);
|
||||||
free(ptr);
|
free(ptr);
|
||||||
} else if (ptr != SELF->nil) {
|
} else if (ptr != self->nil) {
|
||||||
old = ptr->data;
|
old = ptr->data;
|
||||||
ptr->data = item;
|
ptr->data = item;
|
||||||
} else {
|
} else {
|
||||||
@@ -684,47 +671,46 @@ static void *replaceInBag(WMBag *self, int index, void *item)
|
|||||||
|
|
||||||
ptr->data = item;
|
ptr->data = item;
|
||||||
ptr->index = index;
|
ptr->index = index;
|
||||||
ptr->left = SELF->nil;
|
ptr->left = self->nil;
|
||||||
ptr->right = SELF->nil;
|
ptr->right = self->nil;
|
||||||
ptr->parent = SELF->nil;
|
ptr->parent = self->nil;
|
||||||
|
|
||||||
rbTreeInsert(SELF, ptr);
|
rbTreeInsert(self, ptr);
|
||||||
|
|
||||||
SELF->count++;
|
self->count++;
|
||||||
}
|
}
|
||||||
|
|
||||||
return old;
|
return old;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int
|
||||||
|
WMSortBag(WMBag *self, int (*comparer)(const void*, const void*))
|
||||||
static int sortBag(WMBag *self, int (*comparer)(const void*, const void*))
|
|
||||||
{
|
{
|
||||||
void **items;
|
void **items;
|
||||||
W_Node *tmp;
|
W_Node *tmp;
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
if (SELF->count == 0)
|
if (self->count == 0)
|
||||||
return 1;
|
return 1;
|
||||||
|
|
||||||
items = wmalloc(sizeof(void*)*SELF->count);
|
items = wmalloc(sizeof(void*)*self->count);
|
||||||
i = 0;
|
i = 0;
|
||||||
|
|
||||||
tmp = treeMinimum(SELF->root, SELF->nil);
|
tmp = treeMinimum(self->root, self->nil);
|
||||||
while (tmp != SELF->nil) {
|
while (tmp != self->nil) {
|
||||||
items[i++] = tmp->data;
|
items[i++] = tmp->data;
|
||||||
tmp = treeSuccessor(tmp, SELF->nil);
|
tmp = treeSuccessor(tmp, self->nil);
|
||||||
}
|
}
|
||||||
|
|
||||||
qsort(&items[0], SELF->count, sizeof(void*), comparer);
|
qsort(&items[0], self->count, sizeof(void*), comparer);
|
||||||
|
|
||||||
i = 0;
|
i = 0;
|
||||||
tmp = treeMinimum(SELF->root, SELF->nil);
|
tmp = treeMinimum(self->root, self->nil);
|
||||||
while (tmp != SELF->nil) {
|
while (tmp != self->nil) {
|
||||||
tmp->index = i;
|
tmp->index = i;
|
||||||
tmp->data = items[i++];
|
tmp->data = items[i++];
|
||||||
tmp = treeSuccessor(tmp, SELF->nil);
|
tmp = treeSuccessor(tmp, self->nil);
|
||||||
}
|
}
|
||||||
|
|
||||||
wfree(items);
|
wfree(items);
|
||||||
@@ -733,11 +719,10 @@ static int sortBag(WMBag *self, int (*comparer)(const void*, const void*))
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static void
|
||||||
|
deleteTree(WMBag *self, W_Node *node)
|
||||||
static void deleteTree(WMBag *self, W_Node *node)
|
|
||||||
{
|
{
|
||||||
if (node == SELF->nil)
|
if (node == self->nil)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
deleteTree(self, node->left);
|
deleteTree(self, node->left);
|
||||||
@@ -751,28 +736,28 @@ static void deleteTree(WMBag *self, W_Node *node)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static void emptyBag(WMBag *self)
|
void
|
||||||
|
WMEmptyBag(WMBag *self)
|
||||||
{
|
{
|
||||||
deleteTree(self, SELF->root);
|
deleteTree(self, self->root);
|
||||||
SELF->root = SELF->nil;
|
self->root = self->nil;
|
||||||
SELF->count = 0;
|
self->count = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static void freeBag(WMBag *self)
|
void
|
||||||
|
WMFreeBag(WMBag *self)
|
||||||
{
|
{
|
||||||
emptyBag(self);
|
WMEmptyBag(self);
|
||||||
|
|
||||||
free(SELF->nil);
|
free(self->nil);
|
||||||
free(self->data);
|
|
||||||
|
|
||||||
free(self);
|
free(self);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static void
|
||||||
static void mapTree(W_TreeBag *tree, W_Node *node,
|
mapTree(W_Bag *tree, W_Node *node, void (*function)(void*, void*), void *data)
|
||||||
void (*function)(void*, void*), void *data)
|
|
||||||
{
|
{
|
||||||
if (node == tree->nil)
|
if (node == tree->nil)
|
||||||
return;
|
return;
|
||||||
@@ -785,15 +770,16 @@ static void mapTree(W_TreeBag *tree, W_Node *node,
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static void mapBag(WMBag *self, void (*function)(void*, void*), void *data)
|
void
|
||||||
|
WMMapBag(WMBag *self, void (*function)(void*, void*), void *data)
|
||||||
{
|
{
|
||||||
mapTree(SELF, SELF->root, function, data);
|
mapTree(self, self->root, function, data);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
static int findInTree(W_TreeBag *tree, W_Node *node,
|
static int
|
||||||
int (*function)(void*,void*), void *cdata)
|
findInTree(W_Bag *tree, W_Node *node, int (*function)(void*,void*), void *cdata)
|
||||||
{
|
{
|
||||||
int index;
|
int index;
|
||||||
|
|
||||||
@@ -812,21 +798,21 @@ static int findInTree(W_TreeBag *tree, W_Node *node,
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static int findInBag(WMBag *self, int (*match)(void*,void*), void *cdata)
|
int
|
||||||
|
WMFindInBag(WMBag *self, int (*match)(void*,void*), void *cdata)
|
||||||
{
|
{
|
||||||
return findInTree(SELF, SELF->root, match, cdata);
|
return findInTree(self, self->root, match, cdata);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void*
|
||||||
|
WMBagFirst(WMBag *self, WMBagIterator *ptr)
|
||||||
static void *first(WMBag *self, WMBagIterator *ptr)
|
|
||||||
{
|
{
|
||||||
W_Node *node;
|
W_Node *node;
|
||||||
|
|
||||||
node = treeMinimum(SELF->root, SELF->nil);
|
node = treeMinimum(self->root, self->nil);
|
||||||
|
|
||||||
if (node == SELF->nil) {
|
if (node == self->nil) {
|
||||||
*ptr = NULL;
|
*ptr = NULL;
|
||||||
|
|
||||||
return NULL;
|
return NULL;
|
||||||
@@ -838,15 +824,15 @@ static void *first(WMBag *self, WMBagIterator *ptr)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void*
|
||||||
static void *last(WMBag *self, WMBagIterator *ptr)
|
WMBagLast(WMBag *self, WMBagIterator *ptr)
|
||||||
{
|
{
|
||||||
|
|
||||||
W_Node *node;
|
W_Node *node;
|
||||||
|
|
||||||
node = treeMaximum(SELF->root, SELF->nil);
|
node = treeMaximum(self->root, self->nil);
|
||||||
|
|
||||||
if (node == SELF->nil) {
|
if (node == self->nil) {
|
||||||
*ptr = NULL;
|
*ptr = NULL;
|
||||||
|
|
||||||
return NULL;
|
return NULL;
|
||||||
@@ -858,17 +844,17 @@ static void *last(WMBag *self, WMBagIterator *ptr)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void*
|
||||||
static void *next(WMBag *self, WMBagIterator *ptr)
|
WMBagNext(WMBag *self, WMBagIterator *ptr)
|
||||||
{
|
{
|
||||||
W_Node *node;
|
W_Node *node;
|
||||||
|
|
||||||
if (*ptr == NULL)
|
if (*ptr == NULL)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
node = treeSuccessor(*ptr, SELF->nil);
|
node = treeSuccessor(*ptr, self->nil);
|
||||||
|
|
||||||
if (node == SELF->nil) {
|
if (node == self->nil) {
|
||||||
*ptr = NULL;
|
*ptr = NULL;
|
||||||
|
|
||||||
return NULL;
|
return NULL;
|
||||||
@@ -880,18 +866,17 @@ static void *next(WMBag *self, WMBagIterator *ptr)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void*
|
||||||
static void *previous(WMBag *self, WMBagIterator *ptr)
|
WMBagPrevious(WMBag *self, WMBagIterator *ptr)
|
||||||
{
|
{
|
||||||
W_Node *node;
|
W_Node *node;
|
||||||
|
|
||||||
if (*ptr == NULL)
|
if (*ptr == NULL)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
node = treePredecessor(*ptr, SELF->nil);
|
node = treePredecessor(*ptr, self->nil);
|
||||||
|
|
||||||
|
if (node == self->nil) {
|
||||||
if (node == SELF->nil) {
|
|
||||||
*ptr = NULL;
|
*ptr = NULL;
|
||||||
|
|
||||||
return NULL;
|
return NULL;
|
||||||
@@ -903,14 +888,14 @@ static void *previous(WMBag *self, WMBagIterator *ptr)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void*
|
||||||
static void *iteratorAtIndex(WMBag *self, int index, WMBagIterator *ptr)
|
WMBagIteratorAtIndex(WMBag *self, int index, WMBagIterator *ptr)
|
||||||
{
|
{
|
||||||
W_Node *node;
|
W_Node *node;
|
||||||
|
|
||||||
node = treeSearch(SELF->root, SELF->nil, index);
|
node = treeSearch(self->root, self->nil, index);
|
||||||
|
|
||||||
if (node == SELF->nil) {
|
if (node == self->nil) {
|
||||||
*ptr = NULL;
|
*ptr = NULL;
|
||||||
return NULL;
|
return NULL;
|
||||||
} else {
|
} else {
|
||||||
@@ -920,8 +905,10 @@ static void *iteratorAtIndex(WMBag *self, int index, WMBagIterator *ptr)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static int indexForIterator(WMBag *bag, WMBagIterator ptr)
|
int
|
||||||
|
WMBagIndexForIterator(WMBag *bag, WMBagIterator ptr)
|
||||||
{
|
{
|
||||||
return ((W_Node*)ptr)->index;
|
return ((W_Node*)ptr)->index;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -2158,7 +2158,7 @@ getStreamIntoBag(WMText *tPtr, int sel)
|
|||||||
if(!stream)
|
if(!stream)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
bag = WMCreateArrayBagWithDestructor(4, releaseBagData);
|
bag = WMCreateBagWithDestructor(4, releaseBagData);
|
||||||
|
|
||||||
start = stream;
|
start = stream;
|
||||||
while (start) {
|
while (start) {
|
||||||
@@ -2265,7 +2265,7 @@ WMCreateText(WMWidget *parent)
|
|||||||
tPtr->currentTextBlock = NULL;
|
tPtr->currentTextBlock = NULL;
|
||||||
tPtr->tpos = 0;
|
tPtr->tpos = 0;
|
||||||
|
|
||||||
tPtr->gfxItems = WMCreateArrayBag(4);
|
tPtr->gfxItems = WMCreateBag(4);
|
||||||
|
|
||||||
tPtr->parser = NULL;
|
tPtr->parser = NULL;
|
||||||
tPtr->writer = NULL;
|
tPtr->writer = NULL;
|
||||||
|
|||||||
Reference in New Issue
Block a user