Commit 7c414e14430ca651162ed573fb06f37bbd7c8b98
1 parent
64f2bf41
first creation of simple string indexed hash
Showing
2 changed files
with
178 additions
and
0 deletions
bigpoint_hash.c
0 → 100644
1 | +#include <string.h> | |
2 | +#include <stdlib.h> | |
3 | +#include <json/json.h> | |
4 | + | |
5 | +#include "bigpoint_hash.h" | |
6 | + | |
7 | + | |
8 | +#define HASH_ENTRY_CHUNK_SIZE 128 | |
9 | + | |
10 | + | |
11 | +static | |
12 | +void | |
13 | +__construct(struct BIGPOINT_HASH * _this, va_list * params) | |
14 | +{ | |
15 | + _this->size = 0; | |
16 | + _this->used = 0; | |
17 | + _updateHashSize(_this); | |
18 | +} | |
19 | + | |
20 | +static | |
21 | +void | |
22 | +__jsonConst(struct BIGPOINT_HASH * _this, struct json_object * json) | |
23 | +{ | |
24 | +} | |
25 | + | |
26 | +static | |
27 | +void | |
28 | +__destruct(struct BIGPOINT_HASH * _this) | |
29 | +{ | |
30 | + size_t index; | |
31 | + | |
32 | + for (index = 0; index < _this->used; index ++) { | |
33 | + free(_this->keys[index]); | |
34 | + } | |
35 | + | |
36 | + free(_this->keys); | |
37 | + free(_this->values); | |
38 | +} | |
39 | + | |
40 | +static | |
41 | +struct json_object * | |
42 | +__toJson(struct BIGPOINT_HASH * _this) | |
43 | +{ | |
44 | +} | |
45 | + | |
46 | +static const | |
47 | +struct BIGPOINT_CCLASS _bigpoint_hash = { | |
48 | + sizeof(struct BIGPOINT_HASH), | |
49 | + (ctor)__construct, | |
50 | + (jCtor)__jsonConst, | |
51 | + (dtor)__destruct, | |
52 | + (jTo)__toJson | |
53 | +}; | |
54 | + | |
55 | +const struct BIGPOINT_CCLASS * const BIGPOINT_HASH = &_bigpoint_hash; | |
56 | + | |
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 | +void | |
93 | +bigpoint_hash_set(struct BIGPOINT_HASH * _this, | |
94 | + const char * key, | |
95 | + struct BIGPOINT_DYNTYPE * value) | |
96 | +{ | |
97 | + size_t index = _getHashIdx(_this, key); | |
98 | + | |
99 | + if (index == _this->used) { | |
100 | + index = _this->used = index + 1; | |
101 | + } | |
102 | + | |
103 | + _this->keys[index] = calloc(strlen(key) + 1, sizeof(char)); | |
104 | + memcpy(_this->keys[index], key, strlen(key)); | |
105 | + | |
106 | + _this->values[index] = value; | |
107 | + | |
108 | + _updateHashSize(_this); | |
109 | +} | |
110 | + | |
111 | +struct BIGPOINT_DYNTYPE * | |
112 | +bigpoint_hash_get(struct BIGPOINT_HASH * _this, const char * key) | |
113 | +{ | |
114 | + size_t index = _getHashIdx(_this, key); | |
115 | + | |
116 | + if (index == _this->used) { | |
117 | + return NULL; | |
118 | + } | |
119 | + | |
120 | + return _this->value[index]; | |
121 | +} | |
122 | + | |
123 | +struct BIGPOINT_DYNTYPE * | |
124 | +bigpoint_hash_del(struct BIGPOINT_HASH * _this, const char * key) | |
125 | +{ | |
126 | + struct BIGPOINT_DYNTYPE * found = NULL; | |
127 | + size_t index = _getHashIdx(_this, key); | |
128 | + | |
129 | + if (index == _this->used) { | |
130 | + return NULL; | |
131 | + } | |
132 | + | |
133 | + free(_this->key[index]); | |
134 | + | |
135 | + memmove(_this->key + index + 1, | |
136 | + _this->key + index, | |
137 | + (_this->size - index) * sizeof(char*)); | |
138 | + | |
139 | + memmove(_this->value + index + 1, | |
140 | + _this->value + index, | |
141 | + (_this->size - index) * sizeof(struct BIGPOINT_DYNTYPE *)); | |
142 | + | |
143 | + _this->used -= 1; | |
144 | +} | |
145 | + | |
146 | +// vim: set et ts=4 sw=4: | ... | ... |
bigpoint_hash.h
0 → 100644
1 | +#ifndef __BIGPOINT_HASH_H__ | |
2 | +#define __BIGPOINT_HASH_H__ | |
3 | + | |
4 | +#include <sys/types.h> | |
5 | + | |
6 | +#include "bigpoint_cclass.h" | |
7 | +#include "bigpoint_dyntype.h" | |
8 | + | |
9 | + | |
10 | +struct BIGPOINT_HASH { | |
11 | + const struct BIGPOINT_CCLASS * const class; | |
12 | + char ** keys; | |
13 | + struct BIGPOINT_DYNTYPE ** values; | |
14 | + size_t size; | |
15 | + size_t used; | |
16 | +}; | |
17 | + | |
18 | +extern const struct BIGPOINT_CCLASS * const BIGPOINT_DYNTYPE; | |
19 | + | |
20 | +void bigpoint_hash_set(struct BIGPOINT_HASH * _this, | |
21 | + const char * key, | |
22 | + struct BIGPOINT_DYNTYPE * value); | |
23 | + | |
24 | +struct BIGPOINT_DYNTYPE * | |
25 | +bigpoint_hash_get(struct BIGPOINT_HASH * _this, const char * key); | |
26 | + | |
27 | +struct BIGPOINT_DYNTYPE * | |
28 | +bigpoint_hash_del(struct BIGPOINT_HASH * _this, const char * key); | |
29 | + | |
30 | +#endif//__BIGPOINT_HASH_H__ | |
31 | + | |
32 | +// vim: set et ts=4 sw=4: | ... | ... |
Please
register
or
login
to post a comment