hash.h
Go to the documentation of this file.
1 #pragma once
2 
15 
16 #include <stddef.h>
17 #include <inttypes.h>
18 #include <stdbool.h>
19 
20 #include <crater/container.h>
21 
24 typedef struct{
26  void *table_a;
28  void *table_b;
31  uint64_t *flags_a;
33  uint64_t *flags_b;
38  uint64_t cap;
40  uint64_t len_a;
42  uint64_t len_b;
44  uint64_t i;
48  uint64_t r;
50  uint64_t full;
52 
57 typedef struct{
61  uint64_t (*hash)(const cr8r_base_ft*, const void*);
68  int (*cmp)(const cr8r_base_ft*, const void*, const void*);
74  int (*add)(cr8r_base_ft*, void*, void*);
79  void (*del)(cr8r_base_ft*, void*);
85  double load_factor;
87 
88 
103  void *data, uint64_t size,
104  uint64_t (*hash)(const cr8r_base_ft*, const void*),
105  int (*cmp)(const cr8r_base_ft*, const void*, const void*),
106  int (*add)(cr8r_base_ft*, void*, void*),
107  void (*del)(cr8r_base_ft*, void*)
108 );
109 
110 
117 int cr8r_hash_init(cr8r_hashtbl_t*, const cr8r_hashtbl_ft*, uint64_t reserve);
118 
126 void *cr8r_hash_get(cr8r_hashtbl_t*, const cr8r_hashtbl_ft*, const void *key);
127 
143 void *cr8r_hash_insert(cr8r_hashtbl_t*, const cr8r_hashtbl_ft*, const void *key, int *status);
144 
154 void *cr8r_hash_append(cr8r_hashtbl_t*, cr8r_hashtbl_ft*, void *key, int *status);
155 
163 
171 
178 
184 
207 inline static void *cr8r_hash_next(cr8r_hashtbl_t *self, const cr8r_hashtbl_ft *ft, void *cur){
208  uint64_t a = 0, b = 0;
209  if(self->table_b){
210  if(cur){
211  b = (cur - self->table_b)/ft->base.size;
212  if(b++ >= self->len_b){// also goes to SEARCH_A if "b < 0" due to unsigned overflow wrapping
213  goto SEARCH_A;
214  }
215  }
216  for(; b < self->len_b; ++b){
217  if((self->flags_b[b >> 5] >> (b&0x1F))&0x100000000ULL){
218  return self->table_b + b*ft->base.size;
219  }
220  }
221  }else{
222  SEARCH_A:
223  if(cur){
224  a = (cur - self->table_a)/ft->base.size;
225  if(a++ >= self->len_a){
226  return NULL;
227  }
228  }
229  }
230  for(; a < self->len_a; ++a){
231  if((self->flags_a[a >> 5] >> (a&0x1F))&0x100000000ULL){
232  return self->table_a + a*ft->base.size;
233  }
234  }
235  return NULL;
236 }
237 
239 extern const uint64_t cr8r_hash_u64_prime;
240 
243 
246 
248 typedef struct{
250  const char* str;
252  uint64_t n;
254 
257 
void * cr8r_hash_append(cr8r_hashtbl_t *, cr8r_hashtbl_ft *, void *key, int *status)
Insert an element into the hash table or modify its value.
int cr8r_hash_remove(cr8r_hashtbl_t *, cr8r_hashtbl_ft *, const void *key)
Remove the element with a given key.
bool cr8r_hash_ft_init(cr8r_hashtbl_ft *, void *data, uint64_t size, uint64_t(*hash)(const cr8r_base_ft *, const void *), int(*cmp)(const cr8r_base_ft *, const void *, const void *), int(*add)(cr8r_base_ft *, void *, void *), void(*del)(cr8r_base_ft *, void *))
Convenience function to initialize a cr8r_hashtbl_ft.
void * cr8r_hash_insert(cr8r_hashtbl_t *, const cr8r_hashtbl_ft *, const void *key, int *status)
Insert an element into the hash table.
const uint64_t cr8r_hash_u64_prime
arbitrary large prime
cr8r_hashtbl_ft cr8r_htft_cstr_u64
function table for using hash table as map from c strings to uint64_t's
void cr8r_hash_destroy(cr8r_hashtbl_t *, cr8r_hashtbl_ft *)
Free the resources held by the hashtable.
void * cr8r_hash_get(cr8r_hashtbl_t *, const cr8r_hashtbl_ft *, const void *key)
Get a pointer to an entry in the hash table.
cr8r_hashtbl_ft cr8r_htft_u64_u64
function table for using hash table as map rom uint64_t's to uint64_t's
void cr8r_hash_clear(cr8r_hashtbl_t *, cr8r_hashtbl_ft *)
Remove all entries from the hash table.
int cr8r_hash_init(cr8r_hashtbl_t *, const cr8r_hashtbl_ft *, uint64_t reserve)
Initialize a hash table.
void cr8r_hash_delete(cr8r_hashtbl_t *, cr8r_hashtbl_ft *, void *ent)
Remove an element of the hash table by pointer.
cr8r_hashtbl_ft cr8r_htft_u64_void
function table for using hash table as set of uint64_t's
Base function table for all Crater containers.
Definition: container.h:22
uint64_t size
how large each element is, in bytes
Definition: container.h:28
Function table for hash table.
Definition: hash.h:57
double load_factor
Maximum allowable ratio of entries present to total capacity.
Definition: hash.h:85
cr8r_base_ft base
Base function table values (data and size)
Definition: hash.h:59
A hash table.
Definition: hash.h:24
void * table_a
Buffer for the main internal table.
Definition: hash.h:26
uint64_t * flags_b
Metadata about which entries are present/deleted/absent in the second table.
Definition: hash.h:33
uint64_t len_b
Length of buffer for second internal table (in elements not bytes).
Definition: hash.h:42
uint64_t i
Current index for incremental rehashing.
Definition: hash.h:44
void * table_b
Buffer for the second internal table if present.
Definition: hash.h:28
uint64_t full
Total number of elements currently in the table.
Definition: hash.h:50
uint64_t * flags_a
Metadata about which entries are present/deleted/absent in the main table.
Definition: hash.h:31
uint64_t len_a
Length of buffer for main internal table (in elements not bytes).
Definition: hash.h:40
uint64_t r
Amount of entries that are rehashed at once.
Definition: hash.h:48
uint64_t cap
Total number of elements that can be stored before expanding the table.
Definition: hash.h:38
entry type for cstr -> uint64_t mapping
Definition: hash.h:248
uint64_t n
value
Definition: hash.h:252
const char * str
key
Definition: hash.h:250