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.
22 #include "hash_table.h"
23 #include "opkg_message.h"
24 #include "libbb/libbb.h"
27 static int hash_index(hash_table_t
*hash
, const char *pkg_name
);
28 static int rotating(const char *key
, int len
, int prime
);
30 static int hash_index(hash_table_t
*hash
, const char *pkg_name
)
32 return rotating(pkg_name
, strlen(pkg_name
), hash
->n_entries
);
35 static int rotating(const char *key
, int len
, int prime
)
38 for (hash
=len
, i
=0; i
<len
; ++i
)
39 hash
= (hash
<<4)^(hash
>>28)^key
[i
];
40 return (hash
% prime
);
45 * this is an open table keyed by strings
47 int hash_table_init(const char *name
, hash_table_t
*hash
, int len
)
49 static int primes_table
[] = {
50 379, 761, 983, 1423, 2711, 3361, 3931, 4679, 5519, 6701, 9587,
51 19471, 23143, 33961, 46499, 49727, 99529, 0
55 if (hash
->entries
!= NULL
) {
56 /* we have been here already */
63 hash
->hash_entry_key
= NULL
;
65 picker
= primes_table
;
66 while(*picker
&& (*picker
++ < len
));
68 fprintf(stderr
, "%s: primes table might not be big enough (! << %d)\n", __FUNCTION__
, len
);
71 hash
->n_entries
= *picker
;
72 hash
->entries
= xcalloc(hash
->n_entries
, sizeof(hash_entry_t
));
77 void hash_table_deinit(hash_table_t
*hash
)
83 /* free the reminaing entries */
84 for (i
= 0; i
< hash
->n_entries
; i
++) {
85 hash_entry_t
*hash_entry
= (hash
->entries
+ i
);
86 free (hash_entry
->key
);
87 /* skip the first entry as this is part of the array */
88 hash_entry
= hash_entry
->next
;
91 hash_entry_t
*old
= hash_entry
;
92 hash_entry
= hash_entry
->next
;
100 hash
->entries
= NULL
;
104 void *hash_table_get(hash_table_t
*hash
, const char *key
)
106 int ndx
= hash_index(hash
, key
);
107 hash_entry_t
*hash_entry
= hash
->entries
+ ndx
;
112 if (strcmp(key
, hash_entry
->key
) == 0) {
113 // opkg_message(NULL, OPKG_DEBUG, "Function: %s. Key found for '%s' \n", __FUNCTION__, key);
114 return hash_entry
->data
;
117 hash_entry
= hash_entry
->next
;
122 int hash_table_insert(hash_table_t
*hash
, const char *key
, void *value
)
124 int ndx
= hash_index(hash
, key
);
125 hash_entry_t
*hash_entry
= hash
->entries
+ ndx
;
126 if (hash_entry
->key
) {
127 if (strcmp(hash_entry
->key
, key
) == 0) {
128 /* alread in table, update the value */
129 hash_entry
->data
= value
;
133 * if this is a collision, we have to go to the end of the ll,
134 * then add a new entry
135 * before we can hook up the value
137 while (hash_entry
->next
) {
138 hash_entry
= hash_entry
->next
;
139 if (strcmp(hash_entry
->key
, key
) == 0) {
140 hash_entry
->data
= value
;
144 hash_entry
->next
= xcalloc(1, sizeof(hash_entry_t
));
145 hash_entry
= hash_entry
->next
;
146 hash_entry
->next
= NULL
;
150 hash_entry
->key
= xstrdup(key
);
151 hash_entry
->data
= value
;
156 int hash_table_remove(hash_table_t
*hash
, const char *key
)
158 int ndx
= hash_index(hash
, key
);
159 hash_entry_t
*hash_entry
= hash
->entries
+ ndx
;
160 hash_entry_t
*next_entry
=NULL
, *last_entry
=NULL
;
165 if (strcmp(key
, hash_entry
->key
) == 0) {
166 free(hash_entry
->key
);
168 last_entry
->next
= hash_entry
->next
;
171 next_entry
= hash_entry
->next
;
173 memmove(hash_entry
, next_entry
, sizeof(hash_entry_t
));
176 memset(hash_entry
, 0 , sizeof(hash_entry_t
));
182 last_entry
= hash_entry
;
183 hash_entry
= hash_entry
->next
;
188 void hash_table_foreach(hash_table_t
*hash
, void (*f
)(const char *key
, void *entry
, void *data
), void *data
)
194 for (i
= 0; i
< hash
->n_entries
; i
++) {
195 hash_entry_t
*hash_entry
= (hash
->entries
+ i
);
197 if(hash_entry
->key
) {
198 f(hash_entry
->key
, hash_entry
->data
, data
);
200 } while((hash_entry
= hash_entry
->next
));