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