Commit f34db72e40b7e35a60c5df7f2f554328bf890ded

Authored by Georg Hopp
1 parent 7c414e14

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

... ... @@ -10,6 +10,40 @@
10 10
11 11 static
12 12 void
  13 +_updateHashSize(struct BIGPOINT_HASH * _this)
  14 +{
  15 + if (_this->used == _this->size) {
  16 + _this->size += HASH_ENTRY_CHUNK_SIZE;
  17 +
  18 + _this->keys = realloc(_this->keys, sizeof(char*) * _this->size);
  19 + memset(_this->keys + (_this->used * sizeof(char*)),
  20 + HASH_ENTRY_CHUNK_SIZE * sizeof(char*),
  21 + 0);
  22 +
  23 + _this->values = realloc(
  24 + _this->values,
  25 + sizeof(struct BIGPOINT_DYNTYPE *) * _this->size);
  26 + memset(_this->values + (_this->used * sizeof(struct BIGPOINT_DYNTYPE *)),
  27 + HASH_ENTRY_CHUNK_SIZE * sizeof(struct BIGPOINT_DYNTYPE *),
  28 + 0);
  29 + }
  30 +}
  31 +
  32 +static
  33 +size_t
  34 +_getHashIdx(struct BIGPOINT_HASH * _this, const char * key)
  35 +{
  36 + size_t index;
  37 +
  38 + for (index = 0;
  39 + index < _this->used && strcmp(_this->keys[index], key);
  40 + index++);
  41 +
  42 + return index;
  43 +}
  44 +
  45 +static
  46 +void
13 47 __construct(struct BIGPOINT_HASH * _this, va_list * params)
14 48 {
15 49 _this->size = 0;
... ... @@ -54,41 +88,6 @@ struct BIGPOINT_CCLASS _bigpoint_hash = {
54 88
55 89 const struct BIGPOINT_CCLASS * const BIGPOINT_HASH = &_bigpoint_hash;
56 90
57   -static
58   -void
59   -_updateHashSize(struct BIGPOINT_HASH * _this)
60   -{
61   - if (_this->used == _this->size) {
62   - _this->size += HASH_ENTRY_CHUNK_SIZE;
63   -
64   - _this->keys = realloc(_this->keys, sizeof(char*) * _this->size);
65   - memset(_this->keys + (_this->used * sizeof(char*)),
66   - HASH_ENTRY_CHUNK_SIZE * sizeof(char*),
67   - 0);
68   -
69   - _this->values = realloc(
70   - _this->values,
71   - sizeof(struct BIGPOINT_DYNTYPE *) * _this->size,
72   - 0);
73   - memset(_this->values + (_this->used * sizeof(struct BIGPOINT_DYNTYPE *)),
74   - HASH_ENTRY_CHUNK_SIZE * sizeof(struct BIGPOINT_DYNTYPE *),
75   - 0);
76   - }
77   -}
78   -
79   -static
80   -size_t
81   -_getHashIdx(struct BIGPOINT_HASH * _this, const char * key)
82   -{
83   - size_t index;
84   -
85   - for (index = 0;
86   - index < _this->used && strcmp(_this->key[index], key);
87   - index++);
88   -
89   - return index;
90   -}
91   -
92 91 void
93 92 bigpoint_hash_set(struct BIGPOINT_HASH * _this,
94 93 const char * key,
... ... @@ -96,15 +95,15 @@ bigpoint_hash_set(struct BIGPOINT_HASH * _this,
96 95 {
97 96 size_t index = _getHashIdx(_this, key);
98 97
99   - if (index == _this->used) {
100   - index = _this->used = index + 1;
101   - }
102   -
103 98 _this->keys[index] = calloc(strlen(key) + 1, sizeof(char));
104 99 memcpy(_this->keys[index], key, strlen(key));
105 100
106 101 _this->values[index] = value;
107 102
  103 + if (index == _this->used) {
  104 + _this->used += 1;
  105 + }
  106 +
108 107 _updateHashSize(_this);
109 108 }
110 109
... ... @@ -117,7 +116,7 @@ bigpoint_hash_get(struct BIGPOINT_HASH * _this, const char * key)
117 116 return NULL;
118 117 }
119 118
120   - return _this->value[index];
  119 + return _this->values[index];
121 120 }
122 121
123 122 struct BIGPOINT_DYNTYPE *
... ... @@ -130,14 +129,14 @@ bigpoint_hash_del(struct BIGPOINT_HASH * _this, const char * key)
130 129 return NULL;
131 130 }
132 131
133   - free(_this->key[index]);
  132 + free(_this->keys[index]);
134 133
135   - memmove(_this->key + index + 1,
136   - _this->key + index,
  134 + memmove(_this->keys + index + 1,
  135 + _this->keys + index,
137 136 (_this->size - index) * sizeof(char*));
138 137
139   - memmove(_this->value + index + 1,
140   - _this->value + index,
  138 + memmove(_this->values + index + 1,
  139 + _this->values + index,
141 140 (_this->size - index) * sizeof(struct BIGPOINT_DYNTYPE *));
142 141
143 142 _this->used -= 1;
... ...
... ... @@ -15,7 +15,7 @@ struct BIGPOINT_HASH {
15 15 size_t used;
16 16 };
17 17
18   -extern const struct BIGPOINT_CCLASS * const BIGPOINT_DYNTYPE;
  18 +extern const struct BIGPOINT_CCLASS * const BIGPOINT_HASH;
19 19
20 20 void bigpoint_hash_set(struct BIGPOINT_HASH * _this,
21 21 const char * key,
... ...
  1 +#include <stdio.h>
  2 +#include <string.h>
  3 +#include <json/json.h>
  4 +
  5 +#include "../bigpoint_cclass.h"
  6 +#include "../bigpoint_dyntype.h"
  7 +#include "../bigpoint_hash.h"
  8 +
  9 +
  10 +#define TEST_KEY1 "key1"
  11 +#define TEST_KEY2 "key2"
  12 +
  13 +
  14 +int
  15 +main(int argc, char * argv[])
  16 +{
  17 + struct BIGPOINT_DYNTYPE * dyn;
  18 + struct BIGPOINT_HASH * hash = new(BIGPOINT_HASH);
  19 +
  20 + dyn = new(BIGPOINT_DYNTYPE, BIGPOINT_DYNTYPE_INT, sizeof(int), 123);
  21 + bigpoint_hash_set(hash, TEST_KEY1, dyn);
  22 +
  23 + dyn = new(BIGPOINT_DYNTYPE, BIGPOINT_DYNTYPE_INT, sizeof(int), 321);
  24 + bigpoint_hash_set(hash, TEST_KEY2, dyn);
  25 +
  26 + dyn = bigpoint_hash_get(hash, TEST_KEY1);
  27 + printf("%d\n", (dyn->data)._int);
  28 + delete(dyn);
  29 +
  30 + dyn = bigpoint_hash_get(hash, TEST_KEY2);
  31 + printf("%d\n", (dyn->data)._int);
  32 + delete(dyn);
  33 +
  34 + delete(hash);
  35 +
  36 + return 0;
  37 +}
  38 +
  39 +// vim: set et ts=4 sw=4:
... ...
Please register or login to post a comment