[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>
|
---|
[ee7736e] | 43 |
|
---|
[062d900] | 44 | /** Opaque hash table link type. */
|
---|
| 45 | typedef struct ht_link {
|
---|
| 46 | link_t link;
|
---|
| 47 | } ht_link_t;
|
---|
[ee7736e] | 48 |
|
---|
| 49 | /** Set of operations for hash table. */
|
---|
[9f3864a] | 50 | typedef struct {
|
---|
[062d900] | 51 | /** Returns the hash of the key stored in the item (ie its lookup key). */
|
---|
| 52 | size_t (*hash)(const ht_link_t *item);
|
---|
[a35b458] | 53 |
|
---|
[062d900] | 54 | /** Returns the hash of the key. */
|
---|
| 55 | size_t (*key_hash)(void *key);
|
---|
[a35b458] | 56 |
|
---|
[062d900] | 57 | /** True if the items are equal (have the same lookup keys). */
|
---|
| 58 | bool (*equal)(const ht_link_t *item1, const ht_link_t *item2);
|
---|
| 59 |
|
---|
| 60 | /** Returns true if the key is equal to the item's lookup key. */
|
---|
| 61 | bool (*key_equal)(void *key, const ht_link_t *item);
|
---|
| 62 |
|
---|
[ee7736e] | 63 | /** Hash table item removal callback.
|
---|
[1b20da0] | 64 | *
|
---|
[062d900] | 65 | * Must not invoke any mutating functions of the hash table.
|
---|
[ee7736e] | 66 | *
|
---|
[9f3864a] | 67 | * @param item Item that was removed from the hash table.
|
---|
[ee7736e] | 68 | */
|
---|
[062d900] | 69 | void (*remove_callback)(ht_link_t *item);
|
---|
| 70 | } hash_table_ops_t;
|
---|
[9f3864a] | 71 |
|
---|
| 72 | /** Hash table structure. */
|
---|
| 73 | typedef struct {
|
---|
[062d900] | 74 | hash_table_ops_t *op;
|
---|
| 75 | list_t *bucket;
|
---|
| 76 | size_t bucket_cnt;
|
---|
| 77 | size_t full_item_cnt;
|
---|
| 78 | size_t item_cnt;
|
---|
| 79 | size_t max_load;
|
---|
| 80 | bool apply_ongoing;
|
---|
[9f3864a] | 81 | } hash_table_t;
|
---|
[ee7736e] | 82 |
|
---|
[062d900] | 83 | #define hash_table_get_inst(item, type, member) \
|
---|
| 84 | member_to_inst((item), type, member)
|
---|
[ee7736e] | 85 |
|
---|
[5b0cf63] | 86 | extern bool hash_table_create(hash_table_t *, size_t, size_t,
|
---|
| 87 | hash_table_ops_t *);
|
---|
[739d00a] | 88 | extern void hash_table_destroy(hash_table_t *);
|
---|
[062d900] | 89 |
|
---|
| 90 | extern bool hash_table_empty(hash_table_t *);
|
---|
| 91 | extern size_t hash_table_size(hash_table_t *);
|
---|
| 92 |
|
---|
| 93 | extern void hash_table_clear(hash_table_t *);
|
---|
| 94 | extern void hash_table_insert(hash_table_t *, ht_link_t *);
|
---|
| 95 | extern bool hash_table_insert_unique(hash_table_t *, ht_link_t *);
|
---|
| 96 | extern ht_link_t *hash_table_find(const hash_table_t *, void *);
|
---|
[30f1a25] | 97 | extern ht_link_t *hash_table_find_next(const hash_table_t *, ht_link_t *,
|
---|
| 98 | ht_link_t *);
|
---|
[062d900] | 99 | extern size_t hash_table_remove(hash_table_t *, void *);
|
---|
| 100 | extern void hash_table_remove_item(hash_table_t *, ht_link_t *);
|
---|
[5b0cf63] | 101 | extern void hash_table_apply(hash_table_t *, bool (*)(ht_link_t *, void *),
|
---|
| 102 | void *);
|
---|
[062d900] | 103 |
|
---|
[ee7736e] | 104 | #endif
|
---|
[b2951e2] | 105 |
|
---|
[fadd381] | 106 | /** @}
|
---|
[b2951e2] | 107 | */
|
---|