1
0
mirror of https://github.com/gryf/wmaker.git synced 2025-12-24 23:22:30 +01:00

Removed array bag, and restructured the tree bag to be WMBag

This commit is contained in:
dan
2000-09-15 04:57:31 +00:00
parent 446e260186
commit 595d2b060b
8 changed files with 670 additions and 969 deletions

View File

@@ -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:
............................ ............................

View File

@@ -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 \

View File

@@ -1,8 +1,12 @@
#include "WINGs.h"
#include <stdio.h> #include <stdio.h>
#include <string.h>
#include "WINGs.h"
WMScreen *scr; WMScreen *scr;
void void
wAbort() wAbort()

View File

@@ -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
View 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;
}

View File

@@ -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

File diff suppressed because it is too large Load Diff

View File

@@ -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;