source: mainline/common/include/adt/hash_table.h

Last change on this file was 45226285, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 3 months ago

Clarify the purpose and use of hash_table_foreach()

  • Property mode set to 100644
File size: 4.3 KB
RevLine 
[ee7736e]1/*
[df4ed85]2 * Copyright (c) 2006 Jakub Jermar
[062d900]3 * Copyright (c) 2012 Adam Hraska
[1b20da0]4 *
[ee7736e]5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * - Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * - Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * - The name of the author may not be used to endorse or promote products
17 * derived from this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
[fadd381]31/** @addtogroup libc
[b2951e2]32 * @{
33 */
34/** @file
35 */
36
[4805495]37#ifndef _LIBC_HASH_TABLE_H_
38#define _LIBC_HASH_TABLE_H_
[ee7736e]39
[d9c8c81]40#include <adt/list.h>
[3e6a98c5]41#include <stdbool.h>
[062d900]42#include <macros.h>
[36795edf]43#include <member.h>
[ee7736e]44
[062d900]45/** Opaque hash table link type. */
46typedef struct ht_link {
47 link_t link;
48} ht_link_t;
[ee7736e]49
50/** Set of operations for hash table. */
[9f3864a]51typedef struct {
[062d900]52 /** Returns the hash of the key stored in the item (ie its lookup key). */
53 size_t (*hash)(const ht_link_t *item);
[a35b458]54
[062d900]55 /** Returns the hash of the key. */
[5e801dc]56 size_t (*key_hash)(const void *key);
[a35b458]57
[062d900]58 /** True if the items are equal (have the same lookup keys). */
59 bool (*equal)(const ht_link_t *item1, const ht_link_t *item2);
60
61 /** Returns true if the key is equal to the item's lookup key. */
[0db0df2]62 bool (*key_equal)(const void *key, size_t hash, const ht_link_t *item);
[062d900]63
[ee7736e]64 /** Hash table item removal callback.
[1b20da0]65 *
[062d900]66 * Must not invoke any mutating functions of the hash table.
[ee7736e]67 *
[9f3864a]68 * @param item Item that was removed from the hash table.
[ee7736e]69 */
[062d900]70 void (*remove_callback)(ht_link_t *item);
71} hash_table_ops_t;
[9f3864a]72
73/** Hash table structure. */
74typedef struct {
[61eb2ce2]75 const hash_table_ops_t *op;
[062d900]76 list_t *bucket;
77 size_t bucket_cnt;
78 size_t full_item_cnt;
79 size_t item_cnt;
80 size_t max_load;
81 bool apply_ongoing;
[9f3864a]82} hash_table_t;
[ee7736e]83
[062d900]84#define hash_table_get_inst(item, type, member) \
85 member_to_inst((item), type, member)
[ee7736e]86
[45226285]87/** Iterate over all entries associated with a given key.
88 * For iterating over all entries regardless of key, use hash_table_apply().
89 *
90 * Example:
91 * struct ht_entry {
92 * ht_link_t my_link_member;
93 * void *data;
94 * int my_key;
95 * }
96 *
97 * hash_table_t *table = ...;
98 * int key = ...;
99 *
100 * hash_table_foreach(table, &key, my_link_member, struct ht_entry, item) {
101 * _print_entry_data(item->data);
102 * }
103 */
[0db0df2]104#define hash_table_foreach(ht, key, member, itype, iterator) \
105 for (itype *iterator = NULL; \
106 iterator == NULL; iterator = (itype *) INTPTR_MAX) \
107 for (ht_link_t *__link = hash_table_find((ht), (key)); \
108 __link != NULL && ((iterator = member_to_inst(__link, itype, member))); \
109 __link = hash_table_find_next((ht), __link))
110
[5b0cf63]111extern bool hash_table_create(hash_table_t *, size_t, size_t,
[61eb2ce2]112 const hash_table_ops_t *);
[739d00a]113extern void hash_table_destroy(hash_table_t *);
[062d900]114
115extern bool hash_table_empty(hash_table_t *);
116extern size_t hash_table_size(hash_table_t *);
117
118extern void hash_table_clear(hash_table_t *);
119extern void hash_table_insert(hash_table_t *, ht_link_t *);
120extern bool hash_table_insert_unique(hash_table_t *, ht_link_t *);
[5e801dc]121extern ht_link_t *hash_table_find(const hash_table_t *, const void *);
[0db0df2]122extern ht_link_t *hash_table_find_next(const hash_table_t *, ht_link_t *);
[5e801dc]123extern size_t hash_table_remove(hash_table_t *, const void *);
[062d900]124extern void hash_table_remove_item(hash_table_t *, ht_link_t *);
[5b0cf63]125extern void hash_table_apply(hash_table_t *, bool (*)(ht_link_t *, void *),
126 void *);
[062d900]127
[ee7736e]128#endif
[b2951e2]129
[fadd381]130/** @}
[b2951e2]131 */
Note: See TracBrowser for help on using the repository browser.