hash.c
3.79 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
168
169
170
171
172
173
174
/**
* \file
* dyntype/hash.c: very simple string indexed hash mainly to hold json objects
* Copyright (C) 2011 Georg Hopp
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <string.h>
#include <stdlib.h>
#include <json/json.h>
#include "token/cclass.h"
#include "token/dyntype.h"
#include "token/dyntype/hash.h"
#define HASH_ENTRY_CHUNK_SIZE 128
static void _updateHashSize(DYNTYPE_HASH this);
INIT_CLASS(DYNTYPE_HASH);
__construct(DYNTYPE_HASH)
{
this->size = 0;
this->used = 0;
_updateHashSize(this);
}
#undef __construct
__jsonConst(DYNTYPE_HASH)
{
__construct(this, NULL);
if (json_type_object != json_object_get_type(json)) {
return;
}
json_object_object_foreach(json, key, value) {
dyntype_hash_set(this, key, newFromJson(DYNTYPE, value));
}
}
__clear(DYNTYPE_HASH) {}
__destruct(DYNTYPE_HASH)
{
size_t index;
for (index = 0; index < this->used; index ++) {
free(this->keys[index]);
delete(this->values[index]);
}
free(this->keys);
free(this->values);
this->size = this->used = 0;
_updateHashSize(this);
}
__toJson(DYNTYPE_HASH)
{
size_t index;
*json = json_object_new_object();
for (index = 0; index < this->used; index ++) {
struct json_object * values;
toJson(this->values[index], &values);
json_object_object_add(*json, this->keys[index], values);
}
}
static
void
_updateHashSize(DYNTYPE_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(DYNTYPE) * this->size);
memset(this->values + (this->used * sizeof(DYNTYPE)),
0,
HASH_ENTRY_CHUNK_SIZE * sizeof(DYNTYPE));
}
}
static
size_t
_getHashIdx(DYNTYPE_HASH this, const char * key)
{
size_t index;
for (index = 0;
index < this->used && strcmp(this->keys[index], key);
index++);
return index;
}
void
dyntype_hash_set(DYNTYPE_HASH this, const char * key, 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);
}
DYNTYPE
dyntype_hash_get(DYNTYPE_HASH this, const char * key)
{
size_t index = _getHashIdx(this, key);
if (index == this->used) {
return NULL;
}
return this->values[index];
}
void
dyntype_hash_del(DYNTYPE_HASH this, const char * key)
{
size_t index = _getHashIdx(this, key);
if (index == this->used) {
return;
}
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(DYNTYPE));
this->used -= 1;
}
// vim: set et ts=4 sw=4: