Menu

[r360]: / branches / sew-refactor / postgresql / hash.c  Maximize  Restore  History

Download this file

191 lines (156 with data), 3.7 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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
//
// hash.c
//
// Copyright (c) 2012 TJ Holowaychuk <tj@vision-media.ca>
//
#include "hash.h"
/*
* Set hash `key` to `val`.
*/
inline void
hash_set(hash_t *self, char *key, void *val) {
int ret;
khiter_t k = kh_put(ptr, self, key, &ret);
kh_value(self, k) = val;
}
/*
* Get hash `key`, or NULL.
*/
inline void *
hash_get(hash_t *self, char *key) {
khiter_t k = kh_get(ptr, self, key);
return k == kh_end(self) ? NULL : kh_value(self, k);
}
/*
* Check if hash `key` exists.
*/
inline int
hash_has(hash_t *self, char *key) {
khiter_t k = kh_get(ptr, self, key);
return kh_exist(self, k);
}
/*
* Remove hash `key`.
*/
void
hash_del(hash_t *self, char *key) {
khiter_t k = kh_get(ptr, self, key);
kh_del(ptr, self, k);
}
// tests
#ifdef TEST_HASH
#include <stdio.h>
#include <assert.h>
#include <string.h>
void
test_hash_set() {
hash_t *hash = hash_new();
assert(0 == hash_size(hash));
hash_set(hash, "name", "tobi");
hash_set(hash, "species", "ferret");
assert(2 == hash_size(hash));
assert(0 == strcmp("tobi", hash_get(hash, "name")));
assert(0 == strcmp("ferret", hash_get(hash, "species")));
}
void
test_hash_get() {
hash_t *hash = hash_new();
hash_set(hash, "foo", "bar");
assert(0 == strcmp("bar", hash_get(hash, "foo")));
assert(NULL == hash_get(hash, "bar"));
}
void
test_hash_has() {
hash_t *hash = hash_new();
hash_set(hash, "foo", "bar");
assert(1 == hash_has(hash, "foo"));
assert(0 == hash_has(hash, "bar"));
}
void
test_hash_size() {
hash_t *hash = hash_new();
assert(0 == hash_size(hash));
hash_set(hash, "foo", "bar");
assert(1 == hash_size(hash));
hash_set(hash, "bar", "baz");
assert(2 == hash_size(hash));
}
void
test_hash_del() {
hash_t *hash = hash_new();
hash_set(hash, "foo", "bar");
assert(1 == hash_has(hash, "foo"));
assert(0 == hash_has(hash, "bar"));
hash_del(hash, "foo");
hash_del(hash, "bar");
assert(0 == hash_has(hash, "foo"));
}
void
test_hash_clear() {
hash_t *hash = hash_new();
hash_set(hash, "foo", "bar");
hash_set(hash, "bar", "baz");
hash_set(hash, "raz", "jaz");
assert(3 == hash_size(hash));
hash_clear(hash);
assert(0 == hash_size(hash));
}
void
test_hash_each() {
hash_t *hash = hash_new();
hash_set(hash, "name", "tj");
hash_set(hash, "age", "25");
const char *keys[2];
void *vals[2];
int n = 0;
hash_each(hash, {
keys[n] = key;
vals[n] = val;
n++;
});
assert(0 == strcmp("age", keys[0]) || 0 == strcmp("name", keys[0]));
assert(0 == strcmp("age", keys[1]) || 0 == strcmp("name", keys[1]));
assert(0 == strcmp("25", vals[0]) || 0 == strcmp("tj", vals[0]));
assert(0 == strcmp("25", vals[1]) || 0 == strcmp("tj", vals[1]));
}
void
test_hash_each_key() {
hash_t *hash = hash_new();
hash_set(hash, "name", "tj");
hash_set(hash, "age", "25");
const char *keys[2];
int n = 0;
hash_each_key(hash, {
keys[n++] = key;
});
assert(0 == strcmp("age", keys[0]) || 0 == strcmp("name", keys[0]));
assert(0 == strcmp("age", keys[1]) || 0 == strcmp("name", keys[1]));
}
void
test_hash_each_val() {
hash_t *hash = hash_new();
hash_set(hash, "name", "tj");
hash_set(hash, "age", "25");
void *vals[2];
int n = 0;
hash_each_val(hash, {
vals[n++] = val;
});
assert(0 == strcmp("25", vals[0]) || 0 == strcmp("tj", vals[0]));
assert(0 == strcmp("25", vals[1]) || 0 == strcmp("tj", vals[1]));
}
int
main(){
test_hash_set();
test_hash_get();
test_hash_has();
test_hash_del();
test_hash_size();
test_hash_clear();
test_hash_each();
test_hash_each_key();
test_hash_each_val();
printf("\n \e[32m\u2713 \e[90mok\e[0m\n\n");
return 0;
}
#endif
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.