Grassroots Infrastructure
The Grassroots Infrastructure is a suite of computing tools to help users and developers use scientific data infrastructure that can easily be interconnected.
hash_table.h
Go to the documentation of this file.
1 /*
2 ** Copyright 2014-2016 The Earlham Institute
3 **
4 ** Licensed under the Apache License, Version 2.0 (the "License");
5 ** you may not use this file except in compliance with the License.
6 ** You may obtain a copy of the License at
7 **
8 ** http://www.apache.org/licenses/LICENSE-2.0
9 **
10 ** Unless required by applicable law or agreed to in writing, software
11 ** distributed under the License is distributed on an "AS IS" BASIS,
12 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 ** See the License for the specific language governing permissions and
14 ** limitations under the License.
15 */
16 
21 #ifndef HASH_TABLE_H
22 #define HASH_TABLE_H
23 
24 #include <stdio.h>
25 
26 #include "typedefs.h"
27 #include "memory_allocations.h"
29 #include "streams.h"
30 
31 #ifdef __cplusplus
32  extern "C" {
33 #endif
34 
35 
42 typedef struct HashBucket
43 {
45  uint32 hb_hashed_key;
46 
48  const void *hb_key_p;
49 
51  const void *hb_value_p;
52 
55 
58 } HashBucket;
59 
60 
67 GRASSROOTS_UTIL_API void FreeHashBucket (HashBucket * const bucket_p);
68 
69 
80 GRASSROOTS_UTIL_API HashBucket *CreateHashBuckets (const uint32 num_buckets, const MEM_FLAG key_mem_flag, const MEM_FLAG value_mem_flag);
81 
82 
92 GRASSROOTS_UTIL_API HashBucket *CreateDeepCopyHashBuckets (const uint32 num_buckets);
93 
94 
105 
106 
116 GRASSROOTS_UTIL_API HashBucket *CreateShadowUseHashBuckets (const uint32 num_buckets);
117 
118 
130 
131 
132 
139 typedef struct HashTable
140 {
142  uint32 ht_size;
143 
145  uint32 ht_capacity;
146 
148  uint8 ht_load;
149 
152 
155 
157  uint32 (*ht_hash_fn) (const void *);
158 
160  HashBucket *(*ht_create_buckets_fn) (const uint32 num_buckets);
161 
163  void (*ht_free_bucket_fn) (HashBucket * const bucket_p);
164 
166  bool (*ht_fill_bucket_fn) (HashBucket * const bucket_p, const void * const key_p, const void * const value_p);
167 
169  bool (*ht_compare_keys_fn) (const void * const bucket_key_p, const void * const key_p);
170 
172  void (*ht_print_bucket_fn) (const HashBucket * const bucket_p, OutputStream * const stream_p);
173 
175  bool (*ht_save_bucket_fn) (const HashBucket * const bucket_p, FILE *out_f);
176 
177 } HashTable;
178 
179 
203 GRASSROOTS_UTIL_API HashTable *AllocateHashTable (const uint32 initial_size,
204  const uint8 load_percentage,
205  uint32 (*hash_fn) (const void * const key_p),
206  HashBucket *(*create_buckets_fn) (const uint32 num_buckets),
207  void (*free_bucket_fn) (HashBucket * const bucket_p),
208  bool (*fill_bucket_fn) (HashBucket * const bucket_p, const void * const key_p, const void * const value_p),
209  bool (*compare_keys_fn) (const void * const bucket_key_p, const void * const key_p),
210  void (*print_bucket_fn) (const HashBucket * const bucket_p, OutputStream * const stream_p),
211  bool (*save_bucket_fn) (const HashBucket * const bucket_p, FILE *out_f));
212 
213 
220 GRASSROOTS_UTIL_API void FreeHashTable (HashTable * const hash_table_p);
221 
222 
229 GRASSROOTS_UTIL_API void ClearHashTable (HashTable * const hash_table_p);
230 
231 
239 GRASSROOTS_UTIL_API bool IsValidHashBucket (const HashBucket * const bucket_p);
240 
241 
254 GRASSROOTS_UTIL_API bool PutInHashTable (HashTable * const hash_table_p, const void * const key_p, const void * const value_p);
255 
256 
267 GRASSROOTS_UTIL_API const void *GetFromHashTable (const HashTable * const hash_table_p, const void * const key_p);
268 
276 GRASSROOTS_UTIL_API void **GetKeysIndexFromHashTable (const HashTable * const hash_table_p);
277 
284 GRASSROOTS_UTIL_API void FreeKeysIndex (void **index_pp);
285 
294 GRASSROOTS_UTIL_API void RemoveFromHashTable (HashTable * const hash_table_p, const void * const key_p);
295 
296 
304 GRASSROOTS_UTIL_API void PrintHashTable (const HashTable * const hash_table_p, OutputStream * const stream_p);
305 
306 
324 GRASSROOTS_UTIL_API HashTable *CopyHashTable (const HashTable * const src_table_p, const bool deep_copy_if_necessary);
325 
326 
339 GRASSROOTS_UTIL_API bool SaveHashTable (const HashTable * const table_p, const char * const filename_s, int (*compare_fn) (const void *v0_p, const void *v1_p));
340 
341 
352 GRASSROOTS_UTIL_API bool LoadHashTable (HashTable * const table_p, char *current_path_s, const char * const filename_s);
353 
354 
362 GRASSROOTS_UTIL_API uint32 GetHashTableSize (const HashTable * const table_p);
363 
364 #ifdef __cplusplus
365 }
366 #endif
367 
368 #endif /* #ifndef HASH_TABLE_H */
HashTable::RemoveFromHashTable
void RemoveFromHashTable(HashTable *const hash_table_p, const void *const key_p)
For a given key, empty the corresponding bucket in a HashTable.
HashTable::SaveHashTable
bool SaveHashTable(const HashTable *const table_p, const char *const filename_s, int(*compare_fn)(const void *v0_p, const void *v1_p))
Save a HashTable to a file.
HashBucket::CreateDeepCopyHashBuckets
HashBucket * CreateDeepCopyHashBuckets(const uint32 num_buckets)
Create an array of HashBuckets where each one will make a deep copy of the key and value when they ar...
HashBucket::hb_owns_key
MEM_FLAG hb_owns_key
Can the HashBucket free its key or is the memory owned by another process?
Definition: hash_table.h:54
HashTable::ht_hash_fn
uint32(* ht_hash_fn)(const void *)
Function for calculating the hashed value of a HashBucket key.
Definition: hash_table.h:157
HashTable::ht_load_limit
uint32 ht_load_limit
The ht_capacity * ht_load.
Definition: hash_table.h:151
HashTable::ht_save_bucket_fn
bool(* ht_save_bucket_fn)(const HashBucket *const bucket_p, FILE *out_f)
Function for saving the HashBuckets in this HashTable to a file.
Definition: hash_table.h:175
HashBucket::hb_hashed_key
uint32 hb_hashed_key
The hashed value of this HashBucket key.
Definition: hash_table.h:45
MEM_FLAG
MEM_FLAG
An enum specifying the particular status of a piece of dynamically allocated memory for a particular ...
Definition: memory_allocations.h:38
grassroots_util_library.h
streams.h
HashTable::ht_fill_bucket_fn
bool(* ht_fill_bucket_fn)(HashBucket *const bucket_p, const void *const key_p, const void *const value_p)
Function for filling a HashBucket.
Definition: hash_table.h:166
HashTable::AllocateHashTable
HashTable * AllocateHashTable(const uint32 initial_size, const uint8 load_percentage, uint32(*hash_fn)(const void *const key_p), HashBucket *(*create_buckets_fn)(const uint32 num_buckets), void(*free_bucket_fn)(HashBucket *const bucket_p), bool(*fill_bucket_fn)(HashBucket *const bucket_p, const void *const key_p, const void *const value_p), bool(*compare_keys_fn)(const void *const bucket_key_p, const void *const key_p), void(*print_bucket_fn)(const HashBucket *const bucket_p, OutputStream *const stream_p), bool(*save_bucket_fn)(const HashBucket *const bucket_p, FILE *out_f))
Allocate a new HashTable.
HashBucket::CreateShadowUseHashBuckets
HashBucket * CreateShadowUseHashBuckets(const uint32 num_buckets)
Create an array of HashBuckets where each one will make shadow use the key and value when they are pu...
HashBucket::hb_owns_value
MEM_FLAG hb_owns_value
Can the HashBucket free its value or is the memory owned by another process?
Definition: hash_table.h:57
HashTable::ClearHashTable
void ClearHashTable(HashTable *const hash_table_p)
Clear a HashTable.
typedefs.h
HashBucket::hb_key_p
const void * hb_key_p
The key.
Definition: hash_table.h:48
HashTable::ht_load
uint8 ht_load
How full to let the hash table get before rehashing, between 0 and 100.
Definition: hash_table.h:148
HashTable::ht_capacity
uint32 ht_capacity
The actual number of HashBucket slots in the HashTable.
Definition: hash_table.h:145
HashBucket::CreateDeepCopyKeysShallowCopyValueHashBuckets
HashBucket * CreateDeepCopyKeysShallowCopyValueHashBuckets(const uint32 num_buckets)
Create an array of HashBuckets where each one will make a deep copy of the key and a shallow copy of ...
HashTable::CopyHashTable
HashTable * CopyHashTable(const HashTable *const src_table_p, const bool deep_copy_if_necessary)
Copy a HashTable.
HashTable::PrintHashTable
void PrintHashTable(const HashTable *const hash_table_p, OutputStream *const stream_p)
Print out a HashTable to a given stream.
HashBucket
A datatype for holding a key-value pair along with the hashed value of the key.
Definition: hash_table.h:42
HashTable::GetFromHashTable
const void * GetFromHashTable(const HashTable *const hash_table_p, const void *const key_p)
For a given key, get the value from a HashTable.
HashTable::ht_free_bucket_fn
void(* ht_free_bucket_fn)(HashBucket *const bucket_p)
Function for feeing a HashBucket.
Definition: hash_table.h:163
HashTable::PutInHashTable
bool PutInHashTable(HashTable *const hash_table_p, const void *const key_p, const void *const value_p)
Put a key-value pair in the HashTable.
HashTable::ht_size
uint32 ht_size
The number of valid HashBuckets in the HashTable.
Definition: hash_table.h:142
HashBucket::CreateHashBuckets
HashBucket * CreateHashBuckets(const uint32 num_buckets, const MEM_FLAG key_mem_flag, const MEM_FLAG value_mem_flag)
Function for creating the HashBuckets.
HashBucket::CreateShallowCopyHashBuckets
HashBucket * CreateShallowCopyHashBuckets(const uint32 num_buckets)
Create an array of HashBuckets where each one will make a shallow copy of the key and value when they...
HashTable::ht_buckets_p
HashBucket * ht_buckets_p
The HashBuckets.
Definition: hash_table.h:154
HashTable::FreeKeysIndex
void FreeKeysIndex(void **index_pp)
Free an index array previously returned by a call to GetKeysIndexFromHashTable.
HashTable::IsValidHashBucket
bool IsValidHashBucket(const HashBucket *const bucket_p)
Does the HashBucket contain a valid key-value pair.
HashTable::GetHashTableSize
uint32 GetHashTableSize(const HashTable *const table_p)
Get the number of entries in a HashTable.
memory_allocations.h
HashBucket::FreeHashBucket
void FreeHashBucket(HashBucket *const bucket_p)
Function for freeing a HashBucket.
HashTable::LoadHashTable
bool LoadHashTable(HashTable *const table_p, char *current_path_s, const char *const filename_s)
Load the contents of a previously-saved HashTable file into a HashTable.
HashTable::FreeHashTable
void FreeHashTable(HashTable *const hash_table_p)
Free a HashTable.
HashBucket::hb_value_p
const void * hb_value_p
The value.
Definition: hash_table.h:51
OutputStream
An datatype to abstract out the process of writing log and error messages to the appropriate processe...
Definition: streams.h:106
GRASSROOTS_UTIL_API
#define GRASSROOTS_UTIL_API
Definition: grassroots_util_library.h:47
HashTable::ht_print_bucket_fn
void(* ht_print_bucket_fn)(const HashBucket *const bucket_p, OutputStream *const stream_p)
Function for printing out the HashBuckets in this HashTable.
Definition: hash_table.h:172
HashTable
A container using HashBuckets to allow for fast lookup of key-value pairs.
Definition: hash_table.h:139
HashTable::GetKeysIndexFromHashTable
void ** GetKeysIndexFromHashTable(const HashTable *const hash_table_p)
Get a new array with pointers to each of the keys in a given HashTable.
HashTable::ht_compare_keys_fn
bool(* ht_compare_keys_fn)(const void *const bucket_key_p, const void *const key_p)
Function for comparing the keys of two HashBuckets.
Definition: hash_table.h:169