bigpoint_hash.c
3.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#include <string.h>
#include <stdlib.h>
#include <json/json.h>
#include "bigpoint/bigpoint_hash.h"
#define HASH_ENTRY_CHUNK_SIZE 128
static void _updateHashSize(struct BIGPOINT_HASH * _this);
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)
{
__construct(_this, NULL);
if (json_type_object != json_object_get_type(json)) {
return;
}
json_object_object_foreach(json, key, value) {
bigpoint_hash_set(_this, key, newFromJson(BIGPOINT_DYNTYPE, value));
}
}
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)
{
size_t index;
struct json_object * json = json_object_new_object();
for (index = 0; index < _this->used; index ++) {
json_object_object_add(
json,
_this->keys[index],
toJson(_this->values[index]));
}
return json;
}
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*)),
0,
HASH_ENTRY_CHUNK_SIZE * sizeof(char*));
_this->values = realloc(
_this->values,
sizeof(struct BIGPOINT_DYNTYPE *) * _this->size);
memset(_this->values + (_this->used * sizeof(struct BIGPOINT_DYNTYPE *)),
0,
HASH_ENTRY_CHUNK_SIZE * sizeof(struct BIGPOINT_DYNTYPE *));
}
}
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;
}
void
bigpoint_hash_set(struct BIGPOINT_HASH * _this,
const char * key,
struct BIGPOINT_DYNTYPE * value)
{
size_t index = _getHashIdx(_this, key);
_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);
}
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->values[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->keys[index]);
memmove(_this->keys + index + 1,
_this->keys + index,
(_this->size - index) * sizeof(char*));
memmove(_this->values + index + 1,
_this->values + index,
(_this->size - index) * sizeof(struct BIGPOINT_DYNTYPE *));
_this->used -= 1;
}
// vim: set et ts=4 sw=4: