2 changed files with 178 additions and 0 deletions
-
146bigpoint_hash.c
-
32bigpoint_hash.h
@ -0,0 +1,146 @@ |
|||
#include <string.h> |
|||
#include <stdlib.h> |
|||
#include <json/json.h> |
|||
|
|||
#include "bigpoint_hash.h" |
|||
|
|||
|
|||
#define HASH_ENTRY_CHUNK_SIZE 128 |
|||
|
|||
|
|||
static |
|||
void |
|||
__construct(struct BIGPOINT_HASH * _this, va_list * params) |
|||
{ |
|||
_this->size = 0; |
|||
_this->used = 0; |
|||
_updateHashSize(_this); |
|||
} |
|||
|
|||
static |
|||
void |
|||
__jsonConst(struct BIGPOINT_HASH * _this, struct json_object * json) |
|||
{ |
|||
} |
|||
|
|||
static |
|||
void |
|||
__destruct(struct BIGPOINT_HASH * _this) |
|||
{ |
|||
size_t index; |
|||
|
|||
for (index = 0; index < _this->used; index ++) { |
|||
free(_this->keys[index]); |
|||
} |
|||
|
|||
free(_this->keys); |
|||
free(_this->values); |
|||
} |
|||
|
|||
static |
|||
struct json_object * |
|||
__toJson(struct BIGPOINT_HASH * _this) |
|||
{ |
|||
} |
|||
|
|||
static const |
|||
struct BIGPOINT_CCLASS _bigpoint_hash = { |
|||
sizeof(struct BIGPOINT_HASH), |
|||
(ctor)__construct, |
|||
(jCtor)__jsonConst, |
|||
(dtor)__destruct, |
|||
(jTo)__toJson |
|||
}; |
|||
|
|||
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, |
|||
struct BIGPOINT_DYNTYPE * value) |
|||
{ |
|||
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; |
|||
|
|||
_updateHashSize(_this); |
|||
} |
|||
|
|||
struct BIGPOINT_DYNTYPE * |
|||
bigpoint_hash_get(struct BIGPOINT_HASH * _this, const char * key) |
|||
{ |
|||
size_t index = _getHashIdx(_this, key); |
|||
|
|||
if (index == _this->used) { |
|||
return NULL; |
|||
} |
|||
|
|||
return _this->value[index]; |
|||
} |
|||
|
|||
struct BIGPOINT_DYNTYPE * |
|||
bigpoint_hash_del(struct BIGPOINT_HASH * _this, const char * key) |
|||
{ |
|||
struct BIGPOINT_DYNTYPE * found = NULL; |
|||
size_t index = _getHashIdx(_this, key); |
|||
|
|||
if (index == _this->used) { |
|||
return NULL; |
|||
} |
|||
|
|||
free(_this->key[index]); |
|||
|
|||
memmove(_this->key + index + 1, |
|||
_this->key + index, |
|||
(_this->size - index) * sizeof(char*)); |
|||
|
|||
memmove(_this->value + index + 1, |
|||
_this->value + index, |
|||
(_this->size - index) * sizeof(struct BIGPOINT_DYNTYPE *)); |
|||
|
|||
_this->used -= 1; |
|||
} |
|||
|
|||
// vim: set et ts=4 sw=4: |
|||
@ -0,0 +1,32 @@ |
|||
#ifndef __BIGPOINT_HASH_H__ |
|||
#define __BIGPOINT_HASH_H__ |
|||
|
|||
#include <sys/types.h> |
|||
|
|||
#include "bigpoint_cclass.h" |
|||
#include "bigpoint_dyntype.h" |
|||
|
|||
|
|||
struct BIGPOINT_HASH { |
|||
const struct BIGPOINT_CCLASS * const class; |
|||
char ** keys; |
|||
struct BIGPOINT_DYNTYPE ** values; |
|||
size_t size; |
|||
size_t used; |
|||
}; |
|||
|
|||
extern const struct BIGPOINT_CCLASS * const BIGPOINT_DYNTYPE; |
|||
|
|||
void bigpoint_hash_set(struct BIGPOINT_HASH * _this, |
|||
const char * key, |
|||
struct BIGPOINT_DYNTYPE * value); |
|||
|
|||
struct BIGPOINT_DYNTYPE * |
|||
bigpoint_hash_get(struct BIGPOINT_HASH * _this, const char * key); |
|||
|
|||
struct BIGPOINT_DYNTYPE * |
|||
bigpoint_hash_del(struct BIGPOINT_HASH * _this, const char * key); |
|||
|
|||
#endif//__BIGPOINT_HASH_H__ |
|||
|
|||
// vim: set et ts=4 sw=4: |
|||
Write
Preview
Loading…
Cancel
Save
Reference in new issue