Tinyhash
This is a library containing multiple C implementations of hashmap.
Loading...
Searching...
No Matches
tinyhash.c
Go to the documentation of this file.
1#include "tinyhash.h"
2
5
11static th_funcs_t th_funcs[] = {
13 {
15 .get = th_sc_table_get,
16 .put = th_sc_table_put,
17 ._delete = th_sc_table_delete,
18 ._free = th_sc_table_free,
19 .begin_iterator = th_sc_iterator_begin,
20 .len = th_sc_table_len,
21 },
22
24 {
25 .create = th_oa_table_create,
26 .get = th_oa_table_get,
27 .put = th_oa_table_put,
28 ._delete = th_oa_table_delete,
29 ._free = th_oa_table_free,
30 .begin_iterator = th_oa_iterator_begin,
31 .len = th_oa_table_len,
32 },
33};
34
35static size_t th_funcs_length = sizeof(th_funcs) / sizeof(th_funcs[0]);
36
38
40 if (method < 0 || method >= th_funcs_length) {
41 return th_create_default();
42 }
43
44 th_funcs_t funcs = th_funcs[method];
45
46 return (th_t){
47 .method = method,
48 .funcs = funcs,
49 .table = funcs.create(),
50 };
51}
52
53th_any_t th_get(th_t *th, th_any_t data, size_t data_size) {
54 return th->funcs.get(th->table, data, data_size);
55}
56
57bool th_put(th_t *th, th_any_t data, size_t data_size, th_any_t value) {
58 return th->funcs.put(th->table, data, data_size, value);
59}
60
61bool th_delete(th_t *th, th_any_t data, size_t data_size) {
62 return th->funcs._delete(th->table, data, data_size);
63}
64
65void th_clear(th_t *th) { th->funcs._free(th->table); }
66
67void th_free(th_t *th) {
68 th->funcs._free(th->table);
69
70 free(th->table);
71}
72
74 return th->funcs.begin_iterator(th->table, true);
75}
76
78 return th->funcs.begin_iterator(th->table, false);
79}
80
81int th_len(th_t *th) { return th->funcs.len(th->table); }
bool th_oa_table_delete(th_generic_table_t generic_table, th_any_t data, size_t data_size)
Delete a key value pair in an open addressing table. Return true on success.
Definition table.c:197
bool th_oa_table_put(th_generic_table_t generic_table, th_any_t data, size_t data_size, th_any_t value)
Insert a value within an open addressing table. Return true on success.
Definition table.c:189
th_any_t th_oa_table_get(th_generic_table_t generic_table, th_any_t data, size_t data_size)
Get a value from an open addressing table. Return NULL if it does exist.
Definition table.c:138
void th_oa_table_free(th_generic_table_t generic_table)
Free an open addressing table.
Definition table.c:219
th_iterator_t * th_oa_iterator_begin(th_generic_table_t generic_table, bool is_begin)
Return a new iterator.
Definition table.c:272
int th_oa_table_len(th_generic_table_t generic_table)
Returns the table length.
Definition table.c:289
th_generic_table_t th_oa_table_create()
Return an allocated open addressing table struct.
Definition table.c:38
th_iterator_t * th_sc_iterator_begin(th_generic_table_t generic_table, bool is_begin)
Return a new iterator.
Definition table.c:287
void th_sc_table_free(th_generic_table_t generic_table)
Free an separate chaining table.
Definition table.c:222
int th_sc_table_len(th_generic_table_t generic_table)
Returns the table length.
Definition table.c:304
th_any_t th_sc_table_get(th_generic_table_t generic_table, th_any_t data, size_t data_size)
Get a value from an separate chaining table. Return NULL if it does exist.
Definition table.c:133
bool th_sc_table_put(th_generic_table_t generic_table, th_any_t data, size_t data_size, th_any_t value)
Insert a value within an separate chaining table. Return true on success.
Definition table.c:183
th_generic_table_t th_sc_table_create()
Return an allocated separate chaining table.
Definition table.c:36
bool th_sc_table_delete(th_generic_table_t generic_table, th_any_t data, size_t data_size)
Delete a key value pair in an separate chaining table. Return true on success.
Definition table.c:191
Centralizing every function associated with an unique implementation method.
Definition tinyhash.h:57
th_begin_iterator_func_t begin_iterator
Definition tinyhash.h:63
th_free_func_t _free
Definition tinyhash.h:62
th_get_func_t get
Definition tinyhash.h:59
th_len_func_t len
Definition tinyhash.h:64
th_create_func_t create
Definition tinyhash.h:58
th_delete_func_t _delete
Definition tinyhash.h:61
th_put_func_t put
Definition tinyhash.h:60
Represents an iterator that allow to iterate over a generic table.
Definition iterator.h:11
Represent a hashmap controller.
Definition tinyhash.h:71
th_funcs_t funcs
Definition tinyhash.h:73
th_generic_table_t table
Definition tinyhash.h:74
static size_t th_funcs_length
Definition tinyhash.c:35
th_t th_create(th_method_t method)
Allocate then initialize a hashmap controller. based on the given method.
Definition tinyhash.c:39
static th_funcs_t th_funcs[]
Static array containing functions that accomplish hashmap operation associated with an implementation...
Definition tinyhash.c:11
th_iterator_t * th_empty_iterator(th_t *th)
Return an empty iterator.
Definition tinyhash.c:77
th_any_t th_get(th_t *th, th_any_t data, size_t data_size)
Returns a value from a hashmap. Return NULL if it doest not exist.
Definition tinyhash.c:53
bool th_delete(th_t *th, th_any_t data, size_t data_size)
Delete a key value pair from a hashmap. Return true on success.
Definition tinyhash.c:61
void th_clear(th_t *th)
Clear a hashmap.
Definition tinyhash.c:65
th_t th_create_default()
Allocate then initialize a hashmap controller with its default values.
Definition tinyhash.c:37
int th_len(th_t *th)
Get the hashmap length (total amount of key value pairs).
Definition tinyhash.c:81
void th_free(th_t *th)
Free a hashmap.
Definition tinyhash.c:67
bool th_put(th_t *th, th_any_t data, size_t data_size, th_any_t value)
Insert element within the hashmap. Return true on success.
Definition tinyhash.c:57
th_iterator_t * th_begin_iterator(th_t *th)
Return a pointer on an iterator with the first element.
Definition tinyhash.c:73
th_method_t
Implementation methods.
Definition tinyhash.h:14
@ TH_SEPARATE_CHAINING
Definition tinyhash.h:15
@ TH_OPEN_ADRESSING
Definition tinyhash.h:16
void * th_any_t
Represent any type of data.
Definition types.h:8