Shadow Network Framework  0.0.2-alpha
C Server Library for Shadow Network Framework
Loading...
Searching...
No Matches
About hashtables

◄ Previous: Tutorials

"In computing, a hash table is a data structure often used to implement the map (a.k.a. dictionary or associative array) abstract data type. A hash table uses a hash function to compute an index, also called a hash code, into an array of buckets or slots, from which the desired value can be found. During lookup, the key is hashed and the resulting hash indicates where the corresponding value is stored."(Source)

About my implementation of hashtables

After trial and error i have made my own implementation of hastables, I use one of the functions defined in lookup3.h , it should work as a normal hashtable that takes a char * as a Key however, for collisions, it will store it in a linked list array according to the collision, and as of 0.0.2-alpha there is no redifition or upsizing in case of a saturation. will be added in future releases.

How to:

First of all to start using the Hashtable header you must include

#include <SNF/hashtable.h>
This file Defines the HashTable functions and macros The HashTable uses lookup3.c 's hashing function...

Initializing the hashtable

To initialize the hastable we require one argument,

  • hashtable Items count
Remarks
If there is a collision or saturation, it would not cause a buffer overflow or any problem, it would just create a linked list and store it at the collision index, which would allow it to store more than the Max Item Count, just note that the performance will be impacted once that happens.
Note
The hashtable Items Count will most likely be changed. as it will be changed to a power of 2
eg: If you set a items count to 1000 the actual value would be 1024

And then call snf_hashtable_inis and returns a SNF_ht * pointer which is your hashtable

Example : We would want to set a hashtable storing 50 Items

Remarks
For all the following examples in this page, we will use SNF_ht *ht as our hashtable
Warning
for the sake for simplicity, no error handling was included in the example
#include <SNF/hashtable.h>
SNF_ht *ht;
int main()
{
...
}
SNF_ht * snf_hashtable_inis(int MaxItems)
Initializes the HashTime and allocates the needed amount.
Defines the structure of the hashTable.
Definition hashtable.h:39

Inserting into the hashtable

For inserting we need 3 things

  • The initialized hashtable to store on.
  • A Key for the Item you want to store which must be a char *
  • The Content of the item you want to store, must be a pointer to something

and then call snf_hashtable_insert

Eg I want to store a String containing "Lorem ipsum dolor sit amet, consectetur adipiscing elit." with a key lorem ipsum

Warning
for the sake for simplicity, no error handling was included in the example
int main()
{
...
ht, // Hash Table's pointer
"lorem ipsum", // Key
"Lorem ipsum dolor sit amet, consectetur adipiscing elit." //Content
);
..
}
int snf_hashtable_insert(SNF_ht *HashTable, const char *Key, void *Content)
Inserts a new Item into a HashTable.

Searching and Reading hashtable Items

For looking up we only require 2things

  • The initialized hashtable to look/search on
  • The key for the item we want to search for

then we'll call snf_hashtable_lookup which returns a SNF_ht_item * which could be NULL if there was no item found, else it will return the item's point

Important
Never free anything that you got from snf_hashtable_lookup , See Destroying/Removing a hashtable item

and then for accessing the Content of the item you use the struct member SNF_ht_item::Content and for knowing the item's Key you use the struct member SNF_ht_item::Key

Eg Let's say we want to search for the item we inserted in inserting into the hashtable

Warning
for the sake for simplicity, no error handling was included in the example
int main()
{
..
SNF_ht_item *item = snf_hashtable_lookup(ht, "lorem ipsum");
if(item != NULL)
{
printf("Key: %s\n", item->Key);
printf("Content: %s \n", (char *)item->Content);
// Output:
// Key: lorem ipsum
// Content: Lorem ipsum dolor sit amet, consectetur adipiscing elit.
}
....
}
SNF_ht_item * snf_hashtable_lookup(SNF_ht *HashTable, const char *key)
Fetches (looks up) an Item from a HashTable.
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

Deleting/Removing a hashtable item

Same as Searching a hashtable item we require the same thing

  • The initialized hashtable to delete from
  • The key for the item we want to delete

After that we call snf_hashtable_delete which returns a SNF_ht_item * that is removed from the hashtable if it is foudn or else NULL will be returned, however you neeed to free it after finishing your use of it.

Warning
You must free it properly once you finish using the returned iteem or else you'd have a memory leak

Eg We want to destroy the item we insereted earlier

Warning
for the sake for simplicity, no error handling was included in the example
int main()
{
....
SNF_ht_item * item = snf_hashtable_delete(ht, "lorem ipsum");
if(item != NULL)
{
/// If it enters this code block then it mean it had been deleted form the hasshtable structure
/// If the Content & Key are referenced somewhere else then you don't have to free them and skip to 3-
/// 1- Free the Key
free(item->Key);
/// 2- Free the Content
free(item->Content);
/// 3- Free the Item
free(item);
}
..
}
SNF_ht_item * snf_hashtable_delete(SNF_ht *HashTable, const char *key)
Removed an Item from a HashTable.