h_search
+64 −17 · 4 files changed
-
mylib/include/myhash.h+1 −0 -
mylib/src/myhash.c+58 −13 -
mylib/test/Tupfile+1 −3 -
mylib/test/t_hash.c+4 −1
@@ -20,3 +20,4 @@ typedef struct { extern HashNode *h_insert (HashTable *hTable, char *key, char *value); extern HashTable *h_create ( unsigned int size ); +extern HashNode *h_search (HashTable *hTable, char *key); @@ -1,8 +1,8 @@ #include <stdio.h> #include <stdlib.h> #include <string.h> -#include "myhash.h" -#include "mylib.h" +#include "../include/myhash.h" +#include "../include/mylib.h" #define DEFAULT_HASH_SIZE 97 @@ -18,10 +18,10 @@ void h_free (HashNode *hash) { // Hash a key and return integer result. static size_t hash(char *key, int hashSize) { - size_t k = 0; + unsigned int k = 0; while(*key != '\0') - k = (k << 2) + *key++; - return k % hashSize; + k = (k << 2) + (unsigned int) *key++; + return k % (unsigned int) hashSize; } // initialises a new hash table @@ -70,11 +70,11 @@ HashTable *h_create ( unsigned int size ) { HashNode *h_insert (HashTable *hTable, char *key, char *value) { HashNode *newNode; - int resultHash = hash(key,hTable->h_size); + size_t resultHash = hash(key,hTable->h_size); if ( (newNode = (HashNode *) malloc(sizeof(HashNode) ) ) == NULL || - (newNode->h_key = strdup(key) ) == NULL || - (newNode->h_value = strdup(value) ) == NULL ) { + (newNode->h_key = strdup(key) ) == NULL || + (newNode->h_value = strdup(value) ) == NULL ) { perror("malloc () failed for HashNode"); exit(1); @@ -91,11 +91,56 @@ HashNode *h_insert (HashTable *hTable, char *key, char *value) { // declare extern in myhash.h // takes Hastable and key // ret - HastNodes pointer -HashNodes *h_search (HashTable *hTable, char *key) { -1. call hash function with key and table size - -2. search down link list in the array element using strcmp to find node that matches key -3. return node that found or null if end of list + */ +HashNode *h_search (HashTable *hTable, char *key) { + //1. call hash function with key and table size + size_t resultHash = hash(key,hTable->h_size); + // 1a. arguments for strcmp + char *keyStrAtIndex = hTable->h_items[resultHash]->h_key; + // 1b. nextvalue local variable + HashNode *nextValue = hTable->h_items[resultHash]->h_next; + // 2. set return value to a friendly variable + HashNode *returnVar = hTable->h_items[resultHash]; + + fprintf(stderr, "'resultHash' is %zu for 'key' %s\n", resultHash, key); +//2. search down link list in the array element using strcmp to find node that matches key + fprintf(stderr, "The hashtable table is at memory address: %p\n",hTable); + fprintf(stderr, "at index %zu in hastable we have the memory address %p that goes to our node \n", resultHash, hTable->h_items[resultHash] ); + /* +(lldb) print hTable->h_items[5] +(HashNode *) $11 = 0x00005555555592e0 +(lldb) print *hTable->h_items[5] +(HashNode) $12 = { + h_next = 0x0000000000000000 + h_key = 0x0000555555559300 "400" + h_value = 0x0000555555559320 "101" } */ + fprintf(stderr, "De-referencing the pointer at %p we have our nosde strcutre that holds:\n\ + h_next: Address: %p\n\ + h_key: Address: %p, Value: %s\n\ + h_value: Address: %p, Value: %s\n", hTable->h_items[resultHash], + hTable->h_items[resultHash]->h_next, + hTable->h_items[resultHash]->h_next, + hTable->h_items[resultHash]->h_key, + hTable->h_items[resultHash]->h_key, + hTable->h_items[resultHash]->h_value, + hTable->h_items[resultHash]->h_value + ); + if ( (nextValue == NULL) && (strcmp(keyStrAtIndex,key) == 0) ) + fprintf(stderr, "They are the same\n"); + else { + // looking for the key inside the next element of the linked list + char* keyAtNextValue = hTable->h_items[resultHash]->h_next->h_key; + while( nextValue != NULL) { + fprintf(stderr, "Found a collision, traversing Linked list at address %p, key is: %s\n", + nextValue, nextValue->h_key); + nextValue = nextValue->h_next; + } + } +// fprintf(stderr,"%s", ) +//3. return node that found or null if end of list + return returnVar; +} + //static HashTable *h_delete() @@ -5,6 +5,4 @@ CXFLAGS += -lm CXFLAGS += ../lib/mylib.a : *.c |> !cc |> ../obj/t_hash.o -: ../obj/t_?.o | ../lib/mylib.a |> clang %f -lc -lm ../lib/mylib.a -o t_hash |> %B - - +: ../obj/t_hash.o | ../lib/mylib.a |> clang %f $(CXFLAGS) -o t_hash |> %B @@ -1,4 +1,4 @@ -#include "myhash.h" +#include "../include/myhash.h" // edit library implement search func. // test h_search - call on same keys and verify that is same result as the initial call to insert @@ -8,7 +8,10 @@ int main(){ HashTable *tableResult = h_create(10); // HashNode *nodeResult = h_insert(tableResult, "400", "101"); +// HashNode *nodeResult = h_insert(tableResult, "400", "101"); + HashNode *nolt = h_insert(tableResult, "400", "101"); + HashNode *searchResult = h_search(tableResult,"400"); #if 0 int i = 0;