diff --git a/bigpoint_hash.c b/bigpoint_hash.c index 154269b..3be5609 100644 --- a/bigpoint_hash.c +++ b/bigpoint_hash.c @@ -8,6 +8,40 @@ #define HASH_ENTRY_CHUNK_SIZE 128 +static +void +_updateHashSize(struct BIGPOINT_HASH * _this) +{ + if (_this->used == _this->size) { + _this->size += HASH_ENTRY_CHUNK_SIZE; + + _this->keys = realloc(_this->keys, sizeof(char*) * _this->size); + memset(_this->keys + (_this->used * sizeof(char*)), + HASH_ENTRY_CHUNK_SIZE * sizeof(char*), + 0); + + _this->values = realloc( + _this->values, + sizeof(struct BIGPOINT_DYNTYPE *) * _this->size); + memset(_this->values + (_this->used * sizeof(struct BIGPOINT_DYNTYPE *)), + HASH_ENTRY_CHUNK_SIZE * sizeof(struct BIGPOINT_DYNTYPE *), + 0); + } +} + +static +size_t +_getHashIdx(struct BIGPOINT_HASH * _this, const char * key) +{ + size_t index; + + for (index = 0; + index < _this->used && strcmp(_this->keys[index], key); + index++); + + return index; +} + static void __construct(struct BIGPOINT_HASH * _this, va_list * params) @@ -54,41 +88,6 @@ struct BIGPOINT_CCLASS _bigpoint_hash = { const struct BIGPOINT_CCLASS * const BIGPOINT_HASH = &_bigpoint_hash; -static -void -_updateHashSize(struct BIGPOINT_HASH * _this) -{ - if (_this->used == _this->size) { - _this->size += HASH_ENTRY_CHUNK_SIZE; - - _this->keys = realloc(_this->keys, sizeof(char*) * _this->size); - memset(_this->keys + (_this->used * sizeof(char*)), - HASH_ENTRY_CHUNK_SIZE * sizeof(char*), - 0); - - _this->values = realloc( - _this->values, - sizeof(struct BIGPOINT_DYNTYPE *) * _this->size, - 0); - memset(_this->values + (_this->used * sizeof(struct BIGPOINT_DYNTYPE *)), - HASH_ENTRY_CHUNK_SIZE * sizeof(struct BIGPOINT_DYNTYPE *), - 0); - } -} - -static -size_t -_getHashIdx(struct BIGPOINT_HASH * _this, const char * key) -{ - size_t index; - - for (index = 0; - index < _this->used && strcmp(_this->key[index], key); - index++); - - return index; -} - void bigpoint_hash_set(struct BIGPOINT_HASH * _this, const char * key, @@ -96,15 +95,15 @@ bigpoint_hash_set(struct BIGPOINT_HASH * _this, { size_t index = _getHashIdx(_this, key); - if (index == _this->used) { - index = _this->used = index + 1; - } - _this->keys[index] = calloc(strlen(key) + 1, sizeof(char)); memcpy(_this->keys[index], key, strlen(key)); _this->values[index] = value; + if (index == _this->used) { + _this->used += 1; + } + _updateHashSize(_this); } @@ -117,7 +116,7 @@ bigpoint_hash_get(struct BIGPOINT_HASH * _this, const char * key) return NULL; } - return _this->value[index]; + return _this->values[index]; } struct BIGPOINT_DYNTYPE * @@ -130,14 +129,14 @@ bigpoint_hash_del(struct BIGPOINT_HASH * _this, const char * key) return NULL; } - free(_this->key[index]); + free(_this->keys[index]); - memmove(_this->key + index + 1, - _this->key + index, + memmove(_this->keys + index + 1, + _this->keys + index, (_this->size - index) * sizeof(char*)); - memmove(_this->value + index + 1, - _this->value + index, + memmove(_this->values + index + 1, + _this->values + index, (_this->size - index) * sizeof(struct BIGPOINT_DYNTYPE *)); _this->used -= 1; diff --git a/bigpoint_hash.h b/bigpoint_hash.h index bf44116..85ee929 100644 --- a/bigpoint_hash.h +++ b/bigpoint_hash.h @@ -15,7 +15,7 @@ struct BIGPOINT_HASH { size_t used; }; -extern const struct BIGPOINT_CCLASS * const BIGPOINT_DYNTYPE; +extern const struct BIGPOINT_CCLASS * const BIGPOINT_HASH; void bigpoint_hash_set(struct BIGPOINT_HASH * _this, const char * key, diff --git a/tests/hash_test.c b/tests/hash_test.c new file mode 100644 index 0000000..1f54613 --- /dev/null +++ b/tests/hash_test.c @@ -0,0 +1,39 @@ +#include +#include +#include + +#include "../bigpoint_cclass.h" +#include "../bigpoint_dyntype.h" +#include "../bigpoint_hash.h" + + +#define TEST_KEY1 "key1" +#define TEST_KEY2 "key2" + + +int +main(int argc, char * argv[]) +{ + struct BIGPOINT_DYNTYPE * dyn; + struct BIGPOINT_HASH * hash = new(BIGPOINT_HASH); + + dyn = new(BIGPOINT_DYNTYPE, BIGPOINT_DYNTYPE_INT, sizeof(int), 123); + bigpoint_hash_set(hash, TEST_KEY1, dyn); + + dyn = new(BIGPOINT_DYNTYPE, BIGPOINT_DYNTYPE_INT, sizeof(int), 321); + bigpoint_hash_set(hash, TEST_KEY2, dyn); + + dyn = bigpoint_hash_get(hash, TEST_KEY1); + printf("%d\n", (dyn->data)._int); + delete(dyn); + + dyn = bigpoint_hash_get(hash, TEST_KEY2); + printf("%d\n", (dyn->data)._int); + delete(dyn); + + delete(hash); + + return 0; +} + +// vim: set et ts=4 sw=4: