libdict 1.0.7
Data Structure C Library
Loading...
Searching...
No Matches
hashtable2.h
Go to the documentation of this file.
1/*
2 * libdict -- open-addressing 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_HASHTABLE2_H__
29#define LIBDICT_HASHTABLE2_H__
30
31#include "dict.h"
32
45
48typedef struct hashtable2 hashtable2;
49
64
76void** hashtable2_search(hashtable2* table, const void* key);
82 hashtable2_remove(hashtable2* table, const void* key);
91size_t hashtable2_traverse(hashtable2* table, dict_visit_func visit, void* user_data);
93size_t hashtable2_count(const hashtable2* table);
95size_t hashtable2_size(const hashtable2* table);
109bool hashtable2_resize(hashtable2* table, unsigned size);
110
114
123
126bool hashtable2_itor_valid(const hashtable2_itor* itor);
139bool hashtable2_itor_nextn(hashtable2_itor* itor, size_t count);
142bool hashtable2_itor_prevn(hashtable2_itor* itor, size_t count);
151bool hashtable2_itor_search(hashtable2_itor* itor, const void* key);
155const void* hashtable2_itor_key(const hashtable2_itor* itor);
164
166
167#endif /* !LIBDICT_HASHTABLE2_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
bool hashtable2_verify(const hashtable2 *table)
Check the internal invariants of the table and return true if they hold, or print the first failed ch...
Definition hashtable2.c:425
hashtable2_itor * hashtable2_itor_new(hashtable2 *table)
Create an iterator over |table| that is not positioned on any element yet.
Definition hashtable2.c:442
dict_itor * hashtable2_dict_itor_new(hashtable2 *table)
Like hashtable2_itor_new(), but wrapped in a generic dict_itor for use with the dict_itor_* macros.
Definition hashtable2.c:454
void ** hashtable2_search(hashtable2 *table, const void *key)
Return the address of the datum stored for |key|, or NULL if |key| is not present.
Definition hashtable2.c:245
bool hashtable2_itor_nextn(hashtable2_itor *itor, size_t count)
Move |itor| |count| elements forward and return whether it is still positioned.
Definition hashtable2.c:535
size_t hashtable2_free(hashtable2 *table, dict_delete_func delete_func)
Remove every element, calling |delete_func|(key, datum) for each unless |delete_func| is NULL,...
Definition hashtable2.c:183
bool hashtable2_itor_valid(const hashtable2_itor *itor)
Return whether |itor| is positioned on an element.
Definition hashtable2.c:491
bool hashtable2_itor_prev(hashtable2_itor *itor)
Move |itor| to the previous element in table order (unspecified) and return true, or unposition it an...
Definition hashtable2.c:521
void ** hashtable2_itor_datum(hashtable2_itor *itor)
Return the address of the datum |itor| is positioned on, through which it can be read or replaced,...
Definition hashtable2.c:609
bool hashtable2_itor_remove(hashtable2_itor *itor)
Remove the element |itor| is positioned on and unposition |itor|; return false if it was not position...
Definition hashtable2.c:615
size_t hashtable2_clear(hashtable2 *table, dict_delete_func delete_func)
Remove every element, calling |delete_func|(key, datum) for each unless |delete_func| is NULL.
Definition hashtable2.c:329
dict_insert_result hashtable2_insert(hashtable2 *table, void *key)
Insert |key| unless an equal key is present.
Definition hashtable2.c:224
void hashtable2_itor_invalidate(hashtable2_itor *itor)
Unposition |itor|, so that it is not positioned on any element.
Definition hashtable2.c:501
hashtable2 * hashtable2_new(dict_compare_func cmp_func, dict_hash_func hash_func, unsigned initial_size)
Create an empty hash table that compares keys with |cmp_func| and hashes them with |hash_func| (neith...
Definition hashtable2.c:140
size_t hashtable2_count(const hashtable2 *table)
Return the number of elements.
Definition hashtable2.c:362
size_t hashtable2_slots_used(const hashtable2 *table)
Return the number of occupied slots, which for open addressing equals the number of elements.
Definition hashtable2.c:374
size_t hashtable2_size(const hashtable2 *table)
Return the current number of slots in the table.
Definition hashtable2.c:368
void hashtable2_itor_free(hashtable2_itor *itor)
Free an iterator.
Definition hashtable2.c:468
bool hashtable2_itor_prevn(hashtable2_itor *itor, size_t count)
Move |itor| |count| elements back and return whether it is still positioned.
Definition hashtable2.c:544
bool hashtable2_itor_first(hashtable2_itor *itor)
Position |itor| on the first element in table order (unspecified) and return true,...
Definition hashtable2.c:553
const void * hashtable2_itor_key(const hashtable2_itor *itor)
Return the key |itor| is positioned on, or NULL if it is not positioned.
Definition hashtable2.c:603
bool hashtable2_resize(hashtable2 *table, unsigned size)
Rehash the table into at least |size| slots, rounded up as in hashtable2_new().
Definition hashtable2.c:380
bool hashtable2_itor_next(hashtable2_itor *itor)
Move |itor| to the next element in table order (unspecified) and return true, or unposition it and re...
Definition hashtable2.c:507
dict_remove_result hashtable2_remove(hashtable2 *table, const void *key)
Remove the element with key |key|.
Definition hashtable2.c:303
size_t hashtable2_traverse(hashtable2 *table, dict_visit_func visit, void *user_data)
Call |visit|(key, datum, |user_data|) for each element in table order (unspecified),...
Definition hashtable2.c:348
bool hashtable2_itor_search(hashtable2_itor *itor, const void *key)
Position |itor| on |key| and return true, or unposition it and return false if |key| is not present.
Definition hashtable2.c:581
bool hashtable2_itor_last(hashtable2_itor *itor)
Position |itor| on the last element in table order (unspecified) and return true, or unposition it an...
Definition hashtable2.c:567
dict * hashtable2_dict_new(dict_compare_func cmp_func, dict_hash_func hash_func, unsigned initial_size)
Like hashtable2_new(), but wrapped in a generic dict for use with the dict_* macros.
Definition hashtable2.c:164
Definition dict.h:164
hashtable2 * table
Definition hashtable2.c:67
unsigned size
Definition hashtable2.c:58
dict_compare_func cmp_func
Definition hashtable2.c:55
hash_node * table
Definition hashtable2.c:57
dict_hash_func hash_func
Definition hashtable2.c:56