libdict 1.0.7
Data Structure C Library
Loading...
Searching...
No Matches
hashtable.h
Go to the documentation of this file.
1/*
2 * libdict -- hash-value sorted, chained hash-table interface.
3 *
4 * Copyright (c) 2001-2014, Farooq Mela
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 are met:
9 *
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
20 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#ifndef LIBDICT_HASHTABLE_H__
29#define LIBDICT_HASHTABLE_H__
30
31#include "dict.h"
32
45
48typedef struct hashtable hashtable;
49
64
73 hashtable_insert(hashtable* table, void* key);
76void** hashtable_search(hashtable* table, const void* key);
82 hashtable_remove(hashtable* table, const void* key);
91size_t hashtable_traverse(hashtable* table, dict_visit_func visit, void* user_data);
93size_t hashtable_count(const hashtable* table);
95size_t hashtable_size(const hashtable* table);
101bool hashtable_verify(const hashtable* table);
107bool hashtable_resize(hashtable* table, unsigned size);
108
112
121
124bool hashtable_itor_valid(const hashtable_itor* itor);
137bool hashtable_itor_nextn(hashtable_itor* itor, size_t count);
140bool hashtable_itor_prevn(hashtable_itor* itor, size_t count);
149bool hashtable_itor_search(hashtable_itor* itor, const void* key);
153const void* hashtable_itor_key(const hashtable_itor* itor);
162
164
165#endif /* !LIBDICT_HASHTABLE_H__ */
#define BEGIN_DECL
Definition dict.h:42
int(* dict_compare_func)(const void *, const void *)
A pointer to a function that compares two keys.
Definition dict.h:59
void(* dict_delete_func)(void *, void *)
A pointer to a function that is called when a key-value pair gets removed from a dictionary.
Definition dict.h:62
unsigned(* dict_hash_func)(const void *)
A pointer to a function that returns the hash value of a key.
Definition dict.h:66
#define END_DECL
Definition dict.h:43
bool(* dict_visit_func)(const void *, void *, void *)
A pointer to a function used for iterating over dictionary contents.
Definition dict.h:64
size_t hashtable_free(hashtable *table, dict_delete_func delete_func)
Remove every element, calling |delete_func|(key, datum) for each unless |delete_func| is NULL,...
Definition hashtable.c:187
bool hashtable_resize(hashtable *table, unsigned size)
Rehash the table into at least |size| slots, rounded up as in hashtable_new().
Definition hashtable.c:345
bool hashtable_itor_search(hashtable_itor *itor, const void *key)
Position |itor| on |key| and return true, or unposition it and return false if |key| is not present.
Definition hashtable.c:574
dict_remove_result hashtable_remove(hashtable *table, const void *key)
Remove the element with key |key|.
Definition hashtable.c:272
dict_itor * hashtable_dict_itor_new(hashtable *table)
Like hashtable_itor_new(), but wrapped in a generic dict_itor for use with the dict_itor_* macros.
Definition hashtable.c:420
hashtable_itor * hashtable_itor_new(hashtable *table)
Create an iterator over |table| that is not positioned on any element yet.
Definition hashtable.c:407
bool hashtable_verify(const hashtable *table)
Check the internal invariants of the table and return true if they hold, or print the first failed ch...
Definition hashtable.c:386
bool hashtable_itor_last(hashtable_itor *itor)
Position |itor| on the last element in table order (unspecified) and return true, or unposition it an...
Definition hashtable.c:555
bool hashtable_itor_next(hashtable_itor *itor)
Move |itor| to the next element in table order (unspecified) and return true, or unposition it and re...
Definition hashtable.c:472
bool hashtable_itor_first(hashtable_itor *itor)
Position |itor| on the first element in table order (unspecified) and return true,...
Definition hashtable.c:539
size_t hashtable_count(const hashtable *table)
Return the number of elements.
Definition hashtable.c:323
bool hashtable_itor_prevn(hashtable_itor *itor, size_t count)
Move |itor| |count| elements back and return whether it is still positioned.
Definition hashtable.c:529
dict_insert_result hashtable_insert(hashtable *table, void *key)
Insert |key| unless an equal key is present.
Definition hashtable.c:198
void ** hashtable_itor_datum(hashtable_itor *itor)
Return the address of the datum |itor| is positioned on, through which it can be read or replaced,...
Definition hashtable.c:600
bool hashtable_itor_remove(hashtable_itor *itor)
Remove the element |itor| is positioned on and unposition |itor|; return false if it was not position...
Definition hashtable.c:606
void hashtable_itor_free(hashtable_itor *itor)
Free an iterator.
Definition hashtable.c:434
bool hashtable_itor_nextn(hashtable_itor *itor, size_t count)
Move |itor| |count| elements forward and return whether it is still positioned.
Definition hashtable.c:519
size_t hashtable_clear(hashtable *table, dict_delete_func delete_func)
Remove every element, calling |delete_func|(key, datum) for each unless |delete_func| is NULL.
Definition hashtable.c:290
bool hashtable_itor_prev(hashtable_itor *itor)
Move |itor| to the previous element in table order (unspecified) and return true, or unposition it an...
Definition hashtable.c:494
void hashtable_itor_invalidate(hashtable_itor *itor)
Unposition |itor|, so that it is not positioned on any element.
Definition hashtable.c:465
size_t hashtable_traverse(hashtable *table, dict_visit_func visit, void *user_data)
Call |visit|(key, datum, |user_data|) for each element in table order (unspecified),...
Definition hashtable.c:310
dict * hashtable_dict_new(dict_compare_func cmp_func, dict_hash_func hash_func, unsigned size)
Like hashtable_new(), but wrapped in a generic dict for use with the dict_* macros.
Definition hashtable.c:168
bool hashtable_itor_valid(const hashtable_itor *itor)
Return whether |itor| is positioned on an element.
Definition hashtable.c:459
void ** hashtable_search(hashtable *table, const void *key)
Return the address of the datum stored for |key|, or NULL if |key| is not present.
Definition hashtable.c:243
size_t hashtable_size(const hashtable *table)
Return the current number of slots in the table.
Definition hashtable.c:329
size_t hashtable_slots_used(const hashtable *table)
Return the number of slots whose chain is not empty.
Definition hashtable.c:335
const void * hashtable_itor_key(const hashtable_itor *itor)
Return the key |itor| is positioned on, or NULL if it is not positioned.
Definition hashtable.c:594
hashtable * hashtable_new(dict_compare_func cmp_func, dict_hash_func hash_func, unsigned size)
Create an empty hash table that compares keys with |cmp_func| and hashes them with |hash_func| (neith...
Definition hashtable.c:144
Definition dict.h:164
hashtable * table
Definition hashtable.c:70
unsigned size
Definition hashtable.c:62
dict_compare_func cmp_func
Definition hashtable.c:59
hash_node ** table
Definition hashtable.c:58
dict_hash_func hash_func
Definition hashtable.c:60