summaryrefslogtreecommitdiff
path: root/src/base/index.c
diff options
context:
space:
mode:
authorTomas Chvatal (scarabeus) <tomas.chvatal@gmail.com>2008-10-22 22:50:54 +0200
committerTomas Chvatal (scarabeus) <tomas.chvatal@gmail.com>2008-10-22 22:50:54 +0200
commite12753d2ec9db83ec08bb9db63902a82fb245cdd (patch)
tree6e4a63b790d6335f969f88b797a927e0fb25f37a /src/base/index.c
parent0b4d9821c7333b8c4bdf7e0eeb750aef502a1137 (diff)
X
Diffstat (limited to 'src/base/index.c')
-rw-r--r--src/base/index.c347
1 files changed, 170 insertions, 177 deletions
diff --git a/src/base/index.c b/src/base/index.c
index 77b0eac..427aad1 100644
--- a/src/base/index.c
+++ b/src/base/index.c
@@ -10,280 +10,273 @@
#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;
+ index_item_t *this;
+ int min, max, point, len;
+
+ len = list->count;
- len = list->count;
+ if (len == 0) {
+ return -1;
+ }
- if( len == 0 )
- {
- return -1;
- }
+ min = 0;
+ max = len - 1;
- min = 0;
- max = len-1;
+ for (;;) {
+ point = min + (max - min) / 2;
- for(;;)
- {
- point = min + ( max - min ) / 2;
+ if (max < 0 || point >= len || max < min) {
+ return -1;
+ }
- if( max < 0 || point >= len || max < min )
- {
- return -1;
- }
-
- this = (index_item_t *)list->list[point];
+ 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);
}