1 /* hash.c - hash tables for opkg
3 Steven M. Ayer, Jamey Hicks
5 Copyright (C) 2002 Compaq Computer Corporation
7 This program is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public License as
9 published by the Free Software Foundation; either version 2, or (at
10 your option) any later version.
12 This program is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
21 #include "hash_table.h"
22 #include "opkg_message.h"
23 #include "libbb/libbb.h"
27 djb2_hash(const unsigned char *str)
29 unsigned long hash = 5381;
32 hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
37 hash_index(hash_table_t *hash, const char *key)
39 return djb2_hash((const unsigned char *)key) % hash->n_buckets;
43 * this is an open table keyed by strings
46 hash_table_init(const char *name, hash_table_t *hash, int len)
48 if (hash->entries != NULL) {
49 opkg_msg(ERROR, "Internal error: non empty hash table.\n");
53 memset(hash, 0, sizeof(hash_table_t));
56 hash->n_buckets = len;
57 hash->entries = xcalloc(hash->n_buckets, sizeof(hash_entry_t));
61 hash_print_stats(hash_table_t *hash)
63 printf("hash_table: %s, %d bytes\n"
64 "\tn_buckets=%d, n_elements=%d, n_collisions=%d\n"
65 "\tmax_bucket_len=%d, n_used_buckets=%d, ave_bucket_len=%.2f\n"
66 "\tn_hits=%d, n_misses=%d\n",
68 hash->n_buckets*(int)sizeof(hash_entry_t),
74 (hash->n_used_buckets ?
75 ((float)hash->n_elements)/hash->n_used_buckets : 0.0f),
80 void hash_table_deinit(hash_table_t *hash)
86 /* free the reminaing entries */
87 for (i = 0; i < hash->n_buckets; i++) {
88 hash_entry_t *hash_entry = (hash->entries + i);
89 free (hash_entry->key);
90 /* skip the first entry as this is part of the array */
91 hash_entry = hash_entry->next;
94 hash_entry_t *old = hash_entry;
95 hash_entry = hash_entry->next;
101 free (hash->entries);
103 hash->entries = NULL;
107 void *hash_table_get(hash_table_t *hash, const char *key)
109 int ndx= hash_index(hash, key);
110 hash_entry_t *hash_entry = hash->entries + ndx;
115 if (strcmp(key, hash_entry->key) == 0) {
117 return hash_entry->data;
120 hash_entry = hash_entry->next;
126 int hash_table_insert(hash_table_t *hash, const char *key, void *value)
129 int ndx= hash_index(hash, key);
130 hash_entry_t *hash_entry = hash->entries + ndx;
131 if (hash_entry->key) {
132 if (strcmp(hash_entry->key, key) == 0) {
133 /* alread in table, update the value */
134 hash_entry->data = value;
138 * if this is a collision, we have to go to the end of the ll,
139 * then add a new entry
140 * before we can hook up the value
142 while (hash_entry->next) {
143 hash_entry = hash_entry->next;
144 if (strcmp(hash_entry->key, key) == 0) {
145 hash_entry->data = value;
150 hash_entry->next = xcalloc(1, sizeof(hash_entry_t));
151 hash_entry = hash_entry->next;
152 hash_entry->next = NULL;
154 hash->n_collisions++;
155 if (++bucket_len > hash->max_bucket_len)
156 hash->max_bucket_len = bucket_len;
159 hash->n_used_buckets++;
162 hash_entry->key = xstrdup(key);
163 hash_entry->data = value;
168 int hash_table_remove(hash_table_t *hash, const char *key)
170 int ndx= hash_index(hash, key);
171 hash_entry_t *hash_entry = hash->entries + ndx;
172 hash_entry_t *next_entry=NULL, *last_entry=NULL;
177 if (strcmp(key, hash_entry->key) == 0) {
178 free(hash_entry->key);
180 last_entry->next = hash_entry->next;
183 next_entry = hash_entry->next;
185 memmove(hash_entry, next_entry, sizeof(hash_entry_t));
188 memset(hash_entry, 0 , sizeof(hash_entry_t));
194 last_entry = hash_entry;
195 hash_entry = hash_entry->next;
200 void hash_table_foreach(hash_table_t *hash, void (*f)(const char *key, void *entry, void *data), void *data)
206 for (i = 0; i < hash->n_buckets; i++) {
207 hash_entry_t *hash_entry = (hash->entries + i);
209 if(hash_entry->key) {
210 f(hash_entry->key, hash_entry->data, data);
212 } while((hash_entry = hash_entry->next));