diff options
| author | oroborus <oroborus@0ee4d065-81f0-4d1e-b06c-ff20ad07cc3e> | 2008-06-28 16:38:35 +0000 |
|---|---|---|
| committer | oroborus <oroborus@0ee4d065-81f0-4d1e-b06c-ff20ad07cc3e> | 2008-06-28 16:38:35 +0000 |
| commit | 2d776fcd61be168e1ff0ff2e4a5e71835e7cea19 (patch) | |
| tree | 7cf274897bf540d5332d446f583ffd066e4e2e1d /src/hashTable.c | |
| parent | b877ae23ac5d380ab3aa48d7724045cf4883b186 (diff) | |
- prekopanie adresarovej struktury, prva cast
git-svn-id: http://opensvn.csie.org/tuxanci_ng@77 0ee4d065-81f0-4d1e-b06c-ff20ad07cc3e
Diffstat (limited to 'src/hashTable.c')
| -rw-r--r-- | src/hashTable.c | 221 |
1 files changed, 0 insertions, 221 deletions
diff --git a/src/hashTable.c b/src/hashTable.c deleted file mode 100644 index cc67225..0000000 --- a/src/hashTable.c +++ /dev/null @@ -1,221 +0,0 @@ - -#include <stdio.h> -#include <stdlib.h> -#include <string.h> - -#include "hashTable.h" - -hashtable_t* newHashTable() -{ - hashtable_t *new; - - new = malloc( sizeof(hashtable_t) ); - memset(new, 0, sizeof(hashtable_t) ); - - return new; -} - -void addHashTable(hashtable_t *p, char *key, void *item) -{ - hashtable_t *this; - hashtable_t *next; - int len; - int i; - - this = p; - next = p; - - len = strlen(key); - - for( i = 0 ; i < len ; i++ ) - { - //printf("addHashTable key[%d] = %c\n", i , key[i]); - - next = this->table[ (int)key[i] ]; - - if( next == NULL ) - { - next = newHashTable(); - this->table[ (int)key[i] ] = next; - } - - this = next; - } - - next->data = item; -} - -void addHashTableWithIndex(hashtable_t *p, int index, void *item) -{ - char str[16]; - sprintf(str, "%d", index); - addHashTable(p, str, item); -} - -void* getHashTable(hashtable_t *p, char *key) -{ - hashtable_t *this; - hashtable_t *next; - int len; - int i; - - this = p; - next = p; - - len = strlen(key); - - for( i = 0 ; i < len ; i++ ) - { - //printf("getHashTable key[%d] = %c\n", i , key[i]); - - next = this->table[ (int)key[i] ]; - - //printf("next = %p\n", next); - - if( next == NULL ) - { - return NULL; - } - - this = next; - } - - return next->data; -} - -void* getHashTableWithIndex(hashtable_t *p, int index) -{ - char str[16]; - sprintf(str, "%d", index); - return getHashTable(p, str); -} - -void delHashTable(hashtable_t *p, char *key) -{ - hashtable_t *this; - hashtable_t *next; - int len; - int i; - - this = p; - next = p; - - len = strlen(key); - - for( i = 0 ; i < len ; i++ ) - { - //printf("getHashTable key[%d] = %c\n", i , key[i]); - - next = this->table[ (int)key[i] ]; - - //printf("next = %p\n", next); - - if( next == NULL ) - { - return; - } - - this = next; - } - - next->data = NULL; - -} - -void delHashTableWithIndex(hashtable_t *p, int index) -{ - char str[16]; - sprintf(str, "%d", index); - delHashTable(p, str); -} - -void delHashTableWithMem(hashtable_t *p, char *key, void *f) -{ - void (*fce)(void *p); - - delHashTable(p, key); - - fce = f; - fce(key); -} - -void delHashTableWithMemWithIndex(hashtable_t *p, int index) -{ - char str[16]; - sprintf(str, "%d", index); - delHashTable(p, str); -} - -void destroyHashTable(hashtable_t *p) -{ - hashtable_t *this; - hashtable_t *next; - int i; - - this = p; - - for( i = 0 ; i < 256 ; i++ ) - { - next = this->table[i]; - - if( next != NULL ) - { - destroyHashTable(next); - } - } - - free(p); -} - -void destroyHashTableWithMem(hashtable_t *p, void *f) -{ - void (*fce)(void *p); - hashtable_t *this; - hashtable_t *next; - int i; - - this = p; - fce = f; - - for( i = 0 ; i < 256 ; i++ ) - { - next = this->table[i]; - - if( next != NULL ) - { - destroyHashTableWithMem(next, f); - } - } - - if( p->data != NULL ) - { - fce(p->data); - } - - free(p); -} - -int test_main(int argc, char **argv) -{ - hashtable_t *p; - int max = 10000; - int i; - - p = newHashTable(); - - for( i = 0 ; i < max ; i++ ) - { - addHashTableWithIndex(p, i, NULL); - } - - for( i = 0 ; i < max ; i++ ) - { - printf("%d -> %s\n", i, (char *)getHashTableWithIndex(p, i) ); - } - - //printHashTable(p); - - destroyHashTableWithMem(p, free); - - return 0; -} |