Skip to content

Instantly share code, notes, and snippets.

@Itsdenty
Last active April 20, 2018 06:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Itsdenty/4d25cac2239244c3656bf9c99ae995f5 to your computer and use it in GitHub Desktop.
Save Itsdenty/4d25cac2239244c3656bf9c99ae995f5 to your computer and use it in GitHub Desktop.
An implementation of the harsh table in c
#include <stdio.h>
#include <string.h>
/* entry_s is the structure declaration for storing a single key and value node*/
struct entry_s {
char not_empty;
char key[7];
char val[16];
};
/* creates a declaration keyword for the entry_s custom structure */
typedef struct entry_s entry_t;
/* creates the structure for the hashtable */
struct ht_s {
int size;
struct entry_s *tab;
};
/* creates a declaration keyword for the hashtable structure */
typedef struct ht_s ht_t;
/* Create a new ht. */
ht_t *ht_create(int size) {
ht_t *ht = NULL;
int i;
if (size < 1) return NULL;
/* Allocate the tab itself. */
if ((ht = malloc(sizeof(ht_t))) == NULL ) //this checking if the size of the allocated memory to ht_t is null
return NULL;
/* Allocate pointers to the head nodes. */
if ((ht->tab = malloc(sizeof( entry_t) * size)) == NULL) //this will allocate 3 pointers each for each entry_t based on the given defined
return NULL;
for (i = 0; i < size; i++)
memset(&ht->tab[i], 0, sizeof(entry_t)); //this will put 0 as place holders in each entry_t pointers needed in each entry
ht->size = size; //this will assign the define size to the size pointer of the ht_s structure
return ht; //this returns the newly created hash table
}
/* Hash a string for a particular hash tab. */
int ht_hash( ht_t *ht, char *key ) {
unsigned int hv;
int i = 0;
/* Convert our string to an integer */
while (i < strlen(key)) {
hv += key[i];
i++;
}
return hv % ht->size; //return the index integer for the given key
}
/* Insert a key-val pair into a hash tab. */
void ht_set(ht_t *ht, char *key, char *val) {
int ind = ht_hash(ht, key), i = ind;
for (; i < ht -> size; i++) //confirming there is a free memory location for the new data
if (!ht->tab[i].not_empty) { //confirm if the key position is not used
ht->tab[i].not_empty = 1; //change the not_empty memory location to 1 to flag the table index as used
strcpy(ht->tab[i].key, key); //move the key to the key memory location on the index
strcpy(ht->tab[i].val, val); //move the value to the value memory location on the index
return; //break out of the loop
}
for (i = 0; i < ind; i++) //loop through the indexes inside the hashtable
if (!ht->tab[i].not_empty) { //confirm if a harsh table index is free
ht->tab[i].not_empty = 1; //flag this harshtable as filled
strcpy(ht->tab[i].key, key); //move the key to the memory location on the chosen index
strcpy(ht->tab[i].val, val); //move the value to the memory location of the chosen index
return; //break out of the loop
}
}
/* Retrieve a key-val pair from a hash tab. */
char *ht_get(ht_t *ht, char *key) {
int ind = ht_hash(ht, key), i = ind;
for (; i < ht->size; i++) //confirm if the index is not bigger than the table size
if ((ht->tab[i].not_empty) && !strcmp(ht->tab[i].key, key)) //compare the given key with current index key
return ht->tab[i].val; //return the value of the index
for (i = 0; i < ind; i++) //loop through all the item in the harsh table
if ((ht->tab[i].not_empty) && !strcmp(ht->tab[i].key, key)) //compare the current index key with the given key
return ht->tab[i].val; //return the value of the key;
return "not found"; //return not found if no match is found
}
int main(void) {
ht_t *ht = ht_create(4);
ht_set(ht, "key1", "inky");
ht_set(ht, "key2", "pinky");
ht_set(ht, "key3", "blinky");
ht_set(ht, "kez2", "floyd");
printf( "%s\n", ht_get( ht, "key1" ) );
printf( "%s\n", ht_get( ht, "key2" ) );
printf( "%s\n", ht_get( ht, "key3" ) );
printf( "%s\n", ht_get( ht, "kez2" ) );
printf( "%s\n", ht_get( ht, "key4" ) );
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment