libamxc  1.10.3
C Generic Data Containers
amxc_htable.h
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** SPDX-License-Identifier: BSD-2-Clause-Patent
4 **
5 ** SPDX-FileCopyrightText: Copyright (c) 2023 SoftAtHome
6 **
7 ** Redistribution and use in source and binary forms, with or without modification,
8 ** are permitted provided that the following conditions are met:
9 **
10 ** 1. Redistributions of source code must retain the above copyright notice,
11 ** this list of conditions and the following disclaimer.
12 **
13 ** 2. Redistributions in binary form must reproduce the above copyright notice,
14 ** this list of conditions and the following disclaimer in the documentation
15 ** and/or other materials provided with the distribution.
16 **
17 ** Subject to the terms and conditions of this license, each copyright holder
18 ** and contributor hereby grants to those receiving rights under this license
19 ** a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable
20 ** (except for failure to satisfy the conditions of this license) patent license
21 ** to make, have made, use, offer to sell, sell, import, and otherwise transfer
22 ** this software, where such license applies only to those patent claims, already
23 ** acquired or hereafter acquired, licensable by such copyright holder or contributor
24 ** that are necessarily infringed by:
25 **
26 ** (a) their Contribution(s) (the licensed copyrights of copyright holders and
27 ** non-copyrightable additions of contributors, in source or binary form) alone;
28 ** or
29 **
30 ** (b) combination of their Contribution(s) with the work of authorship to which
31 ** such Contribution(s) was added by such copyright holder or contributor, if,
32 ** at the time the Contribution is added, such addition causes such combination
33 ** to be necessarily infringed. The patent license shall not apply to any other
34 ** combinations which include the Contribution.
35 **
36 ** Except as expressly stated above, no rights or licenses from any copyright
37 ** holder or contributor is granted under this license, whether expressly, by
38 ** implication, estoppel or otherwise.
39 **
40 ** DISCLAIMER
41 **
42 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
43 ** AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
44 ** IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
45 ** ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
46 ** LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
47 ** DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
48 ** SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
49 ** CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
50 ** OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
51 ** USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
52 **
53 ****************************************************************************/
54 
55 #if !defined(__AMXC_HTABLE_H__)
56 #define __AMXC_HTABLE_H__
57 
58 #ifdef __cplusplus
59 extern "C"
60 {
61 #endif
62 
63 #include <amxc/amxc_common.h>
64 #include <amxc/amxc_array.h>
65 
87 #define amxc_htable_it_get_data(it, type, member) \
88  ((type*) (((char*) it) - offsetof(type, member)))
89 
102 #define amxc_htable_for_each(it, htable) \
103  for(amxc_htable_it_t* it = amxc_htable_get_first(htable), \
104  * it ## _next = amxc_htable_it_get_next(it); \
105  it; \
106  it = it ## _next, \
107  it ## _next = amxc_htable_it_get_next(it))
108 
119 #define amxc_htable_iterate(it, htable) \
120  for(amxc_htable_it_t* it = amxc_htable_get_first(htable); \
121  it; \
122  it = amxc_htable_it_get_next(it))
123 
129 #define AMXC_HTABLE_RANGE UINT32_MAX
130 
131 typedef struct _amxc_htable_it amxc_htable_it_t;
132 
140  char* key;
142 };
143 
158 typedef unsigned int (* amxc_htable_hash_func_t) (const char* key, const unsigned int len);
159 
168 typedef void (* amxc_htable_it_delete_t) (const char* key, amxc_htable_it_t* it);
169 
175 typedef struct _amxc_htable {
177  size_t items;
181 
203 int amxc_htable_new(amxc_htable_t** htable, const size_t reserve);
204 
225 
249 int amxc_htable_init(amxc_htable_t* const htable, const size_t reserve);
250 
265 
280 
296 unsigned int amxc_htable_key2index(const amxc_htable_t* const htable,
297  const char* const key);
298 
312  return htable != NULL ? (htable->items == 0) : true;
313 }
314 
334 size_t amxc_htable_size(const amxc_htable_t* const htable) {
335  return htable != NULL ? htable->items : 0;
336 }
337 
352  return htable != NULL ? htable->table.items : 0;
353 }
354 
375  const char* const key,
376  amxc_htable_it_t* const it);
377 
402  const char* const key);
403 
429 amxc_htable_it_t* amxc_htable_take(amxc_htable_t* const htable, const char* const key);
430 
451 
472 
499 
512 bool amxc_htable_contains(const amxc_htable_t* const htable, const char* const key) {
513  return amxc_htable_get(htable, key) ? true : false;
514 }
515 
536 int amxc_htable_move(amxc_htable_t* const dest, amxc_htable_t* const src);
537 
558 
575 
593 
611 
628 
645 
657 
672 const char* amxc_htable_it_get_key(const amxc_htable_it_t* const it) {
673  return it != NULL ? it->key : NULL;
674 }
675 
699  return it;
700 }
701 
702 #ifdef __cplusplus
703 }
704 #endif
705 
706 #endif // __AMXC_HTABLE_H__
Ambiorix array API header file.
#define AMXC_INLINE
Definition: amxc_common.h:64
amxc_htable_it_t * amxc_htable_it_get_next(const amxc_htable_it_t *const reference)
Gets the next iterator in the hash table.
int amxc_htable_it_init(amxc_htable_it_t *const it)
Initializes a hash table.iterator.
void amxc_htable_it_clean(amxc_htable_it_t *const it, amxc_htable_it_delete_t func)
Removes the iterator from the htable and frees allocated memory.
amxc_htable_it_t * amxc_htable_it_get_next_key(const amxc_htable_it_t *const reference)
Gets the next iterator in the hash table with the same key.
void amxc_htable_it_take(amxc_htable_it_t *const it)
Removes the iterator from the hash table.
amxc_htable_it_t * amxc_htable_it_get_previous(const amxc_htable_it_t *const reference)
Gets the previous iterator in the hash table.
AMXC_INLINE const char * amxc_htable_it_get_key(const amxc_htable_it_t *const it)
Gets the key from the iterator.
Definition: amxc_htable.h:672
amxc_htable_it_t * amxc_htable_it_get_previous_key(const amxc_htable_it_t *const reference)
Gets the previous iterator in the hash table with the same key.
int amxc_htable_init(amxc_htable_t *const htable, const size_t reserve)
Initializes a hash table.
Definition: amxc_htable.c:185
void amxc_htable_delete(amxc_htable_t **htable, amxc_htable_it_delete_t func)
Frees the previously allocated hash table.
Definition: amxc_htable.c:172
amxc_array_t * amxc_htable_get_sorted_keys(const amxc_htable_t *const htable)
Creates an array containing all keys of the hash table.
Definition: amxc_htable.c:339
amxc_htable_it_t * amxc_htable_get_first(const amxc_htable_t *const htable)
Gets the first item stored in the table.
Definition: amxc_htable.c:313
int amxc_htable_new(amxc_htable_t **htable, const size_t reserve)
Allocates a hash table.
Definition: amxc_htable.c:148
AMXC_INLINE size_t amxc_htable_size(const amxc_htable_t *const htable)
Calculates the size of the hash table.
Definition: amxc_htable.h:334
AMXC_INLINE bool amxc_htable_contains(const amxc_htable_t *const htable, const char *const key)
Verifies that a key is in the hash table.
Definition: amxc_htable.h:512
AMXC_INLINE bool amxc_htable_is_empty(const amxc_htable_t *const htable)
Checks that the hash table is empty.
Definition: amxc_htable.h:311
unsigned int(* amxc_htable_hash_func_t)(const char *key, const unsigned int len)
Definition of the hash function.
Definition: amxc_htable.h:158
int amxc_htable_move(amxc_htable_t *const dest, amxc_htable_t *const src)
Moves all items from one hash table to another hash table.
Definition: amxc_htable.c:363
unsigned int amxc_htable_key2index(const amxc_htable_t *const htable, const char *const key)
Converts a key into an index.
Definition: amxc_htable.c:225
struct _amxc_htable amxc_htable_t
The hash table structure.
amxc_htable_it_t * amxc_htable_take(amxc_htable_t *const htable, const char *const key)
Removes a hash table iterator from the hash table.
Definition: amxc_htable.c:304
void(* amxc_htable_it_delete_t)(const char *key, amxc_htable_it_t *it)
Definition of the hash table item delete function.
Definition: amxc_htable.h:168
amxc_htable_it_t * amxc_htable_get_last(const amxc_htable_t *const htable)
Gets the last item stored in the table.
Definition: amxc_htable.c:326
amxc_htable_it_t * amxc_htable_get(const amxc_htable_t *const htable, const char *const key)
Gets a hash table iterator from the hash table.
Definition: amxc_htable.c:278
AMXC_INLINE size_t amxc_htable_capacity(const amxc_htable_t *const htable)
Calculates the capacity of the hash table.
Definition: amxc_htable.h:351
AMXC_INLINE amxc_htable_it_t * amxc_htable_take_first(const amxc_htable_t *const htable)
Removes the first item stored in the table.
Definition: amxc_htable.h:696
int amxc_htable_insert(amxc_htable_t *const htable, const char *const key, amxc_htable_it_t *const it)
Inserts an item in the hash table.
Definition: amxc_htable.c:237
void amxc_htable_clean(amxc_htable_t *const htable, amxc_htable_it_delete_t func)
Removes all items from the hash table.
Definition: amxc_htable.c:200
void amxc_htable_set_hash_func(amxc_htable_t *const htable, amxc_htable_hash_func_t func)
Sets the hash function for the hash table.
Definition: amxc_htable.c:211
The array iterator structure.
Definition: amxc_array.h:174
The array structure.
Definition: amxc_array.h:162
size_t items
Definition: amxc_array.h:163
The hash table iterator structure.
Definition: amxc_htable.h:138
amxc_array_it_t * ait
Definition: amxc_htable.h:139
amxc_htable_it_t * next
Definition: amxc_htable.h:141
The hash table structure.
Definition: amxc_htable.h:175
amxc_array_t table
Definition: amxc_htable.h:176
size_t items
Definition: amxc_htable.h:177
amxc_htable_hash_func_t hfunc
Definition: amxc_htable.h:178
amxc_htable_it_delete_t it_del
Definition: amxc_htable.h:179
static amxc_htable_it_t it[2000]
static amxc_htable_t * htable