Shadow Network Framework  0.0.1-alpha
C Server Library for Shadow Network Framework
Loading...
Searching...
No Matches
hashtable.h
Go to the documentation of this file.
1//////////////////////////////////////////////////////////////
2///
3/// \file
4/// This file Defines the HashTable functions and macros
5/// The HashTable uses locate3.c 's hashing functions
6///
7/// //////////////////////////////////////////////////////////
8#ifndef hashtable_h
9#define hashtable_h
10
11#include <stdlib.h>
12#include <stdint.h>
13#include <string.h>
14#include <pthread.h>
15#include <math.h>
16
17#include <SNF/locate3.h>
18
19/// @brief Shortened definiton of struct SNF_ht_item_t .
21/// @brief Shortened definiton of struct SNF_ht_t .
22typedef struct SNF_ht_t SNF_ht;
23
24/// @brief Defines the structure of each element of the HashTable
26{
27 /// @brief Key for indexing the HashTable Item
28 const char *Key;
29 /// @brief HashTable Item's Content
30 void *Content;
31
32 /// @brief Pointer of the HashTable Item in case of a collision
34};
35
36/// @brief Defines the structure of the hashTable
37/// @warning Do not try to change the \ref SNF_ht::Size of the HashTable after Initializing it.
39{
40 pthread_mutex_t mutex;
41 int Size;
43};
44
45/// @brief Initializes the HashTime and allocates the needed amount
46/// @param MaxItems The amount used to calculate the length (See note)
47/// @return The results:
48/// - **NULL** Upon failure
49/// - __SNF_ht*__ (The HashTable's pointer)
50/// @note **MaxItems** is used to count the smallest power of 2 that could fit **MaxItems**,
51/// the latter would be used as the actual size of the HashTable <br><br>
52/// ***Example :*** if MaxItems == 5 , then the smallest power of 2 is **3**, and the actual size of the has table is 8 (2 to the power of **3**) <br><br>
53/// * this is due to the HashTable where it is recommended the HashTable's lenghth is a power of two for best performance.
54extern SNF_ht *snf_hashtable_inis(int MaxItems);
55
56/// @brief Inserts a new Item into a HashTable.
57/// @param HashTable The Hash tbale to be inserted to.
58/// @param Key Item's key
59/// @param Content Item's Content
60/// @return The results:
61/// - **-1** Upon failure
62/// - **0** Upon Success
63extern int snf_hashtable_insert(SNF_ht *HashTable, const char *Key, void *Content);
64/// @brief Fetches (looks up) an Item from a HashTable
65/// @param HashTable The HashTable to search on.
66/// @param key The Item's Key
67/// @return The result of the search:
68/// - **NULL** if item was not found or given paramters are invalid
69/// - **SNF_ht_item** (HashTable Item's pointer)
70/// @warning Do not Free the resulting pointer! if you want the item and be able to free it
71/// afterwards, use \ref snf_hashtable_delete instead
72extern SNF_ht_item *snf_hashtable_lookup(SNF_ht *HashTable, const char *key);
73/// @brief Removed an Item from a HashTable
74/// @param HashTable The HashTable to search on.
75/// @param key The Item's Key
76/// @return The result of the deletion:
77/// - **NULL** if item was not found or given paramters are invalid
78/// - **SNF_ht_item** (Deleted HashTable Item's pointer)
79/// @note make sure to free the returned item or else you'd get a memory leak.
80extern SNF_ht_item *snf_hashtable_delete(SNF_ht *HashTable, const char *key);
81
82#endif
SNF_ht_item * snf_hashtable_delete(SNF_ht *HashTable, const char *key)
Removed an Item from a HashTable.
SNF_ht * snf_hashtable_inis(int MaxItems)
Initializes the HashTime and allocates the needed amount.
SNF_ht_item * snf_hashtable_lookup(SNF_ht *HashTable, const char *key)
Fetches (looks up) an Item from a HashTable.
int snf_hashtable_insert(SNF_ht *HashTable, const char *Key, void *Content)
Inserts a new Item into a HashTable.
All the code in lookup3.c is written by Bob Jenkins, May 2006. this header file is written by acordin...
Defines the structure of each element of the HashTable.
Definition hashtable.h:26
void * Content
HashTable Item's Content.
Definition hashtable.h:30
const char * Key
Key for indexing the HashTable Item.
Definition hashtable.h:28
SNF_ht_item * next
Pointer of the HashTable Item in case of a collision.
Definition hashtable.h:33
Defines the structure of the hashTable.
Definition hashtable.h:39
int Size
Definition hashtable.h:41
pthread_mutex_t mutex
Definition hashtable.h:40
SNF_ht_item ** Contents
Definition hashtable.h:42