Lines Matching defs:hash

31  *   This is a very simplified and adapted version of the hash tables I am
32 * using in a personal project. It was added to try to have a single hash
42 * also, all code traversing hash tables is now using
48 * Also, this hash table implementation was added mainly for the tags
55 static int hash_equal(hash_table *hash, hash_key *left, hash_key *right);
63 hash_equal(hash_table *hash, hash_key *left, hash_key *right)
68 if (hash->compare)
69 return (hash->compare(left, right));
97 hash_table *hash;
99 hash = calloc(1, sizeof(hash_table));
100 if (hash) {
101 hash->entries = calloc(length, sizeof(hash_entry *));
102 if (hash->entries) {
103 hash->length = length;
104 hash->compare = compare;
105 hash->iter.offset = -1;
108 free(hash);
109 hash = (hash_table *)0;
113 return (hash);
117 hash_put(hash_table *hash, hash_entry *entry)
122 /* Offset in hash table vector for this entry */
123 key = hash_value(entry->key) % hash->length;
126 ptr = hash->entries[key];
131 if (hash_equal(hash, entry->key, ptr->key)) {
135 hash->entries[key] = entry;
150 hash->entries[key] = entry;
157 ++hash->count;
161 * again an entry already in the hash table */
166 hash_get(hash_table *hash, hash_key *name)
171 key = hash_value(name) % hash->length;
172 for (entry = hash->entries[key]; entry; entry = entry->next) {
173 if (hash_equal(hash, name, entry->key)) {
183 hash_check(hash_table *hash, const char *name, unsigned int length)
188 key = hash_data(name, length) % hash->length;
189 for (entry = hash->entries[key]; entry; entry = entry->next) {
201 hash_rem_no_free(hash_table *hash, hash_entry *entry)
206 key = hash_value(entry->key) % hash->length;
207 for (ptr = prev = hash->entries[key]; ptr; prev = ptr, ptr = ptr->next) {
209 --hash->count;
211 hash->entries[key] = ptr->next;
218 if (ptr && ptr == hash->iter.entry)
219 hash->iter.entry = ptr->next;
221 /* If entry wasn't in hash table ptr will be nil */
226 hash_rem(hash_table *hash, hash_entry *entry)
228 entry = hash_rem_no_free(hash, entry);
237 hash_rehash(hash_table *hash, unsigned int length)
245 for (i = 0; i < hash->length; i++) {
246 for (entry = hash->entries[i]; entry; entry = next) {
254 /* Finish updating hash table */
255 free(hash->entries);
256 hash->entries = entries;
257 hash->length = length;
259 hash->iter.offset = -1;
263 hash_iter_first(hash_table *hash)
265 hash->iter.offset = 0;
266 hash->iter.entry = (hash_entry *)0;
268 return (hash_iter_next(hash));
272 hash_iter_next(hash_table *hash)
274 if (hash->iter.offset >= 0) {
275 if (hash->iter.entry) {
276 if ((hash->iter.entry = hash->iter.entry->next))
277 return (hash->iter.entry);
278 ++hash->iter.offset;
280 for (; hash->iter.offset < hash->length; hash->iter.offset++) {
281 if ((hash->iter.entry = hash->entries[hash->iter.offset]))
282 return (hash->iter.entry);
284 hash->iter.entry = (hash_entry *)0;
285 hash->iter.offset = -1;
292 hash_clr(hash_table *hash)
298 for (i = 0; i < hash->length; i++) {
299 entry = hash->entries[i];
307 hash->entries[i] = (hash_entry *)0;
311 hash->count = 0;
312 hash->iter.offset = -1;
316 hash_del(hash_table *hash)
318 hash_clr(hash);
319 free(hash->entries);
320 free(hash);