Changeset b0f00a9 in mainline for uspace/lib/c/generic/adt/hash_table.c
- Timestamp:
- 2011-11-06T22:21:05Z (15 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/fix-logger-deadlock, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- 898e847
- Parents:
- 2bdf8313 (diff), 7b5f4c9 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the(diff)links above to see all the changes relative to each parent. - File:
-
- 1 edited
-
uspace/lib/c/generic/adt/hash_table.c (modified) (9 diffs)
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/c/generic/adt/hash_table.c
r2bdf8313 rb0f00a9 54 54 * 55 55 */ 56 inthash_table_create(hash_table_t *h, hash_count_t m, hash_count_t max_keys,56 bool hash_table_create(hash_table_t *h, hash_count_t m, hash_count_t max_keys, 57 57 hash_table_operations_t *op) 58 58 { … … 61 61 assert(max_keys > 0); 62 62 63 h->entry = malloc(m * sizeof(li nk_t));63 h->entry = malloc(m * sizeof(list_t)); 64 64 if (!h->entry) 65 65 return false; 66 66 67 memset((void *) h->entry, 0, m * sizeof(li nk_t));67 memset((void *) h->entry, 0, m * sizeof(list_t)); 68 68 69 69 hash_count_t i; … … 78 78 } 79 79 80 /** Remove all elements from the hash table 81 * 82 * @param h Hash table to be cleared 83 */ 84 void hash_table_clear(hash_table_t *h) 85 { 86 for (hash_count_t chain = 0; chain < h->entries; ++chain) { 87 link_t *cur; 88 link_t *next; 89 90 for (cur = h->entry[chain].head.next; 91 cur != &h->entry[chain].head; 92 cur = next) { 93 next = cur->next; 94 list_remove(cur); 95 h->op->remove_callback(cur); 96 } 97 } 98 } 99 80 100 /** Destroy a hash table instance. 81 101 * … … 123 143 assert(chain < h->entries); 124 144 125 link_t *cur; 126 for (cur = h->entry[chain].next; cur != &h->entry[chain]; 127 cur = cur->next) { 145 list_foreach(h->entry[chain], cur) { 128 146 if (h->op->compare(key, h->max_keys, cur)) { 129 147 /* … … 152 170 h->op->remove_callback); 153 171 assert(keys <= h->max_keys); 154 155 link_t *cur;156 172 157 173 if (keys == h->max_keys) { … … 161 177 */ 162 178 163 cur = hash_table_find(h, key);179 link_t *cur = hash_table_find(h, key); 164 180 if (cur) { 165 181 list_remove(cur); … … 176 192 hash_index_t chain; 177 193 for (chain = 0; chain < h->entries; chain++) { 178 for (cur = h->entry[chain].next; cur != &h->entry[chain]; 194 for (link_t *cur = h->entry[chain].head.next; 195 cur != &h->entry[chain].head; 179 196 cur = cur->next) { 180 197 if (h->op->compare(key, keys, cur)) { … … 193 210 } 194 211 195 /** Apply fu cntion to all items in hash table.212 /** Apply function to all items in hash table. 196 213 * 197 214 * @param h Hash table. … … 201 218 */ 202 219 void hash_table_apply(hash_table_t *h, void (*f)(link_t *, void *), void *arg) 203 { 204 hash_index_t bucket; 205 link_t *cur; 206 207 for (bucket = 0; bucket < h->entries; bucket++) { 208 for (cur = h->entry[bucket].next; cur != &h->entry[bucket]; 209 cur = cur->next) { 220 { 221 for (hash_index_t bucket = 0; bucket < h->entries; bucket++) { 222 link_t *cur; 223 link_t *next; 224 225 for (cur = h->entry[bucket].head.next; cur != &h->entry[bucket].head; 226 cur = next) { 227 /* 228 * The next pointer must be stored prior to the functor 229 * call to allow using destructor as the functor (the 230 * free function could overwrite the cur->next pointer). 231 */ 232 next = cur->next; 210 233 f(cur, arg); 211 234 }
Note:
See TracChangeset
for help on using the changeset viewer.
