Browse Source

fixed hash implementation...this one should be subject of optimizing in future

master
Georg Hopp 14 years ago
parent
commit
f34db72e40
  1. 89
      bigpoint_hash.c
  2. 2
      bigpoint_hash.h
  3. 39
      tests/hash_test.c

89
bigpoint_hash.c

@ -8,6 +8,40 @@
#define HASH_ENTRY_CHUNK_SIZE 128 #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 static
void void
__construct(struct BIGPOINT_HASH * _this, va_list * params) __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; 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 void
bigpoint_hash_set(struct BIGPOINT_HASH * _this, bigpoint_hash_set(struct BIGPOINT_HASH * _this,
const char * key, const char * key,
@ -96,15 +95,15 @@ bigpoint_hash_set(struct BIGPOINT_HASH * _this,
{ {
size_t index = _getHashIdx(_this, key); size_t index = _getHashIdx(_this, key);
if (index == _this->used) {
index = _this->used = index + 1;
}
_this->keys[index] = calloc(strlen(key) + 1, sizeof(char)); _this->keys[index] = calloc(strlen(key) + 1, sizeof(char));
memcpy(_this->keys[index], key, strlen(key)); memcpy(_this->keys[index], key, strlen(key));
_this->values[index] = value; _this->values[index] = value;
if (index == _this->used) {
_this->used += 1;
}
_updateHashSize(_this); _updateHashSize(_this);
} }
@ -117,7 +116,7 @@ bigpoint_hash_get(struct BIGPOINT_HASH * _this, const char * key)
return NULL; return NULL;
} }
return _this->value[index];
return _this->values[index];
} }
struct BIGPOINT_DYNTYPE * struct BIGPOINT_DYNTYPE *
@ -130,14 +129,14 @@ bigpoint_hash_del(struct BIGPOINT_HASH * _this, const char * key)
return NULL; 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*)); (_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->size - index) * sizeof(struct BIGPOINT_DYNTYPE *));
_this->used -= 1; _this->used -= 1;

2
bigpoint_hash.h

@ -15,7 +15,7 @@ struct BIGPOINT_HASH {
size_t used; 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, void bigpoint_hash_set(struct BIGPOINT_HASH * _this,
const char * key, const char * key,

39
tests/hash_test.c

@ -0,0 +1,39 @@
#include <stdio.h>
#include <string.h>
#include <json/json.h>
#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:
Loading…
Cancel
Save