summaryrefslogtreecommitdiff
path: root/src/base/index.c
diff options
context:
space:
mode:
authorTomas Chvatal (scarabeus) <tomas.chvatal@gmail.com>2008-10-22 22:51:24 +0200
committerTomas Chvatal (scarabeus) <tomas.chvatal@gmail.com>2008-10-22 22:51:24 +0200
commit44daf03024fbf7cbffa10349044786f6ab784821 (patch)
treefe311eb22e2d853dfc18fde1a9e55d21815f4f82 /src/base/index.c
parente12753d2ec9db83ec08bb9db63902a82fb245cdd (diff)
Revert "X"
This reverts commit e12753d2ec9db83ec08bb9db63902a82fb245cdd.
Diffstat (limited to 'src/base/index.c')
-rw-r--r--src/base/index.c347
1 files changed, 177 insertions, 170 deletions
diff --git a/src/base/index.c b/src/base/index.c
index 427aad1..77b0eac 100644
--- a/src/base/index.c
+++ b/src/base/index.c
@@ -10,273 +10,280 @@
#define DEBUG_INDEX
-static index_item_t *
-newIndexItem(int key, void *data)
+static index_item_t* newIndexItem(int key, void *data)
{
- index_item_t *new;
+ index_item_t *new;
- new = malloc(sizeof(index_item_t));
- new->key = key;
- new->data = data;
+ new = malloc( sizeof(index_item_t) );
+ new->key = key;
+ new->data = data;
- return new;
+ return new;
}
#ifdef DEBUG_INDEX
-static void
-printIndexItem(index_item_t * p)
+static void printIndexItem(index_item_t *p)
{
- printf("key = %d data = %p\n", p->key, p->data);
+ printf("key = %d data = %p\n", p->key, p->data);
}
-static void
-printListIndexItem(list_t * list)
+static void printListIndexItem(list_t *list)
{
- int i;
+ int i;
- printf("list :\n");
- printf("------------------\n");
+ printf("list :\n");
+ printf("------------------\n");
- for (i = 0; i < list->count; i++) {
- index_item_t *this;
+ for( i = 0 ; i < list->count ; i++ )
+ {
+ index_item_t *this;
- this = (index_item_t *) list->list[i];
- printIndexItem(this);
- }
+ this = (index_item_t *)list->list[i];
+ printIndexItem(this);
+ }
}
-static void
-checkList(list_t * list)
+static void checkList(list_t *list)
{
- int i;
- int prev;
- int this;
+ int i;
+ int prev;
+ int this;
- //return;
+ //return;
- if (list->count == 0) {
- printf("nothing\n");
- return;
- }
+ if( list->count == 0 )
+ {
+ printf("nothing\n");
+ return;
+ }
- prev = ((index_item_t *) list->list[0])->key;
+ prev = ( (index_item_t *) list->list[0] )->key;
- for (i = 1; i < list->count; i++) {
- this = ((index_item_t *) list->list[i])->key;
+ for( i = 1 ; i < list->count ; i++ )
+ {
+ this = ( (index_item_t *) list->list[i] )->key;
- if (prev >= this) {
- printListIndexItem(list);
- assert(!"error");
- }
+ if( prev >= this )
+ {
+ printListIndexItem(list);
+ assert( ! "error" );
+ }
- prev = this;
- }
+ prev = this;
+ }
}
#endif
-static void
-destroyIndexItem(index_item_t * p)
+static void destroyIndexItem(index_item_t *p)
{
- free(p);
+ free(p);
}
-list_t *
-newIndex()
+list_t* newIndex()
{
- return newList();
+ return newList();
}
-void
-addToIndex(list_t * list, int key, void *data)
+void addToIndex(list_t *list, int key, void *data)
{
#ifdef DEBUG_INDEX
- int count = 0;
+ int count = 0;
#endif
- index_item_t *item;
- index_item_t *this;
- int min, max, point;
- int len;
+ index_item_t *item;
+ index_item_t *this;
+ int min, max, point;
+ int len;
- item = newIndexItem(key, data);
- len = list->count;
+ item = newIndexItem(key, data);
+ len = list->count;
- min = 0;
- max = len - 1;
+ min = 0;
+ max = len-1;
- for (;;) {
- point = min + (max - min) / 2;
+ for(;;)
+ {
+ point = min + ( max - min ) / 2;
#ifdef DEBUG_INDEX
- if (++count == len * 5) {
- printf("CICLIC ERROR\n");
- printIndexItem(item);
- printf("-------------------\n");
- printListIndexItem(list);
- assert(0);
- }
+ if( ++count == len*5 )
+ {
+ printf("CICLIC ERROR\n");
+ printIndexItem(item);
+ printf("-------------------\n");
+ printListIndexItem(list);
+ assert( 0 );
+ }
#endif
- if (max < 0) {
- insList(list, 0, item);
+ if( max < 0 )
+ {
+ insList(list, 0, item);
#ifdef DEBUG_INDEX
- checkList(list);
+ checkList(list);
#endif
- return;
- }
+ return;
+ }
- if (min >= len) {
- addList(list, item);
+ if( min >= len )
+ {
+ addList(list, item);
#ifdef DEBUG_INDEX
- checkList(list);
+ checkList(list);
#endif
- return;
- }
-
- this = (index_item_t *) list->list[point];
+ return;
+ }
+
+ this = (index_item_t *)list->list[point];
/*
printf("min = %d max = %d point = %d len = %d offset = %d\n",
min, max, point, len, offset);
*/
- if (min > max) {
- insList(list, point, item);
+ if( min > max )
+ {
+ insList(list, point, item);
#ifdef DEBUG_INDEX
- checkList(list);
+ checkList(list);
#endif
- return;
- }
-
- if (item->key > this->key) {
- min = point + 1;
- continue;
- }
-
- if (item->key < this->key) {
- max = point - 1;
- continue;
- }
- }
+ return;
+ }
+
+ if( item->key > this->key )
+ {
+ min = point + 1;
+ continue;
+ }
+
+ if( item->key < this->key )
+ {
+ max = point - 1;
+ continue;
+ }
+ }
}
-static int
-getOffsetFromIndex(list_t * list, int key)
+static int getOffsetFromIndex(list_t *list, int key)
{
- index_item_t *this;
- int min, max, point, len;
-
- len = list->count;
+ index_item_t *this;
+ int min, max, point, len;
- if (len == 0) {
- return -1;
- }
+ len = list->count;
- min = 0;
- max = len - 1;
+ if( len == 0 )
+ {
+ return -1;
+ }
- for (;;) {
- point = min + (max - min) / 2;
+ min = 0;
+ max = len-1;
- if (max < 0 || point >= len || max < min) {
- return -1;
- }
+ for(;;)
+ {
+ point = min + ( max - min ) / 2;
- this = (index_item_t *) list->list[point];
+ if( max < 0 || point >= len || max < min )
+ {
+ return -1;
+ }
+
+ this = (index_item_t *)list->list[point];
/*
printf("min = %d max = %d point = %d len = %d offset = %d\n",
min, max, point, len, offset);
*/
- if (key == this->key) {
- return point;
- }
-
- if (key > this->key) {
- min = point + 1;
- continue;
- }
-
- if (key < this->key) {
- max = point - 1;
- continue;
- }
- }
+ if( key == this->key )
+ {
+ return point;
+ }
+
+ if( key > this->key )
+ {
+ min = point + 1;
+ continue;
+ }
+
+ if( key < this->key )
+ {
+ max = point - 1;
+ continue;
+ }
+ }
}
-void *
-getFromIndex(list_t * list, int key)
+void* getFromIndex(list_t *list, int key)
{
- int offset;
+ int offset;
- offset = getOffsetFromIndex(list, key);
+ offset = getOffsetFromIndex(list, key);
- if (offset != -1) {
- index_item_t *this;
+ if( offset != -1 )
+ {
+ index_item_t *this;
- this = list->list[offset];
- return this->data;
- }
+ this = list->list[offset];
+ return this->data;
+ }
- return NULL;
+ return NULL;
}
-void
-delFromIndex(list_t * list, int key)
+void delFromIndex(list_t *list, int key)
{
- int offset;
+ int offset;
- offset = getOffsetFromIndex(list, key);
+ offset = getOffsetFromIndex(list, key);
- if (offset != -1) {
- delListItem(list, offset, destroyIndexItem);
- }
+ if( offset != -1 )
+ {
+ delListItem(list, offset, destroyIndexItem);
+ }
}
-void
-delFromIndexWithObject(list_t * list, int key, void *f)
+void delFromIndexWithObject(list_t *list, int key, void *f)
{
- int offset;
+ int offset;
- offset = getOffsetFromIndex(list, key);
+ offset = getOffsetFromIndex(list, key);
- if (offset != -1) {
- index_item_t *this;
- void (*fce) (void *p);
+ if( offset != -1 )
+ {
+ index_item_t *this;
+ void (*fce)(void *p);
- this = list->list[offset];
+ this = list->list[offset];
- fce = f;
- fce(this);
+ fce = f;
+ fce(this);
- delListItem(list, offset, destroyIndexItem);
- }
+ delListItem(list, offset, destroyIndexItem);
+ }
}
-void
-actionIndexWithObject(list_t * list, void *f)
+void actionIndexWithObject(list_t *list, void *f)
{
- int i;
+ int i;
- for (i = 0; i < list->count; i++) {
- index_item_t *this;
- void (*fce) (void *p);
+ for( i = 0 ; i < list->count ; i++)
+ {
+ index_item_t *this;
+ void (*fce)(void *p);
- this = list->list[i];
+ this = list->list[i];
- fce = f;
- fce(this);
- }
+ fce = f;
+ fce(this);
+ }
}
-void
-destroyIndex(list_t * list)
+void destroyIndex(list_t *list)
{
- destroyListItem(list, destroyIndexItem);
+ destroyListItem(list, destroyIndexItem);
}
-void
-destroyIndexWithObject(list_t * list, void *f)
+void destroyIndexWithObject(list_t *list, void *f)
{
- actionIndexWithObject(list, f);
- destroyListItem(list, destroyIndexItem);
+ actionIndexWithObject(list, f);
+ destroyListItem(list, destroyIndexItem);
}