← cproj

for resize

0de8dca376d9bff904e8455b29f03c1f95ce0075 · ovidiucs · Aug 2, 2019 06:39

+35 −10 · 2 files changed

  • mylib/src/myhash.c +31 −7
  • mylib/test/t_hash.c +4 −3
diff --git a/mylib/src/myhash.c b/mylib/src/myhash.c
index 151c9b0..f772f1e 100644
--- a/mylib/src/myhash.c
+++ b/mylib/src/myhash.c
@@ -12,6 +12,7 @@
 
 #define DEFAULT_HASH_SIZE 97
 #define PATH_MAX 255
+#define ARRAY_MAX 0.7
 #define BUFF 2048
 //  Free allocated memory for entire hash table
 #if 0
@@ -26,17 +27,19 @@ static size_t hash(char *key, unsigned int hashSize) {
     return k % (unsigned int) hashSize;
 }
 
+// Global variables static for resizing
+static int usedArrayElements = 0;
+
 // initialises a new hash table
 // size - how many HashNodes we want to store
 // default at 97 HashNodes
 // initialise the array of items with calloc
 
+
 HashTable *h_create ( unsigned int size ) {
 
 	// Create hash pointer (for malloc)
 	HashTable *hash;
-	// Hold the prime number
-	unsigned int primeSize = 0;
 
 	if ( (hash = (HashTable *) malloc(sizeof(HashTable) ) ) == NULL ) {
 		perror("malloc () failed");
@@ -47,6 +50,9 @@ HashTable *h_create ( unsigned int size ) {
 	if (size == 0)
 		hash->h_size = DEFAULT_HASH_SIZE;
 	else {
+		// Hold the prime number
+		unsigned int primeSize = 0;
+	
 		primeSize = prime(size);
 		if (primeSize == 0) {
 			fprintf(stderr, "Can't get prime number for %d size\n", size );
@@ -54,10 +60,10 @@ HashTable *h_create ( unsigned int size ) {
 		}
 		hash->h_size = primeSize;
 	}
-
+	 
 	// calloc - allocate the array and init to null
-
-	hash->h_items = (HashNode**) calloc((size_t) size ,sizeof(HashNode*));
+	
+	hash->h_items = (HashNode**) calloc((size_t) hash->h_size ,sizeof(HashNode*));
 	if (hash->h_items == NULL) {
 		perror("calloc () failed");
 		exit(1);
@@ -66,11 +72,15 @@ HashTable *h_create ( unsigned int size ) {
 	// return hash
 	return hash;
 }
+
+extern size_t cmpCount;
+
 // Find a key in a given linked list
 // called by h_insert and h_search and h_delete
 static HashNode *h_findKey(HashNode *hNode, char *key) {
 // increment a global variable to find out how many times strcmp has been called
 	while (hNode != NULL) {
+	cmpCount++;
 		if(strcmp(hNode->h_key,key) == 0) {
 			break;
 		}
@@ -91,14 +101,28 @@ HashNode *h_insert (HashTable *hTable, char *key, char *value) {
 	newNode = hTable->h_items[resultHash];
 	// overwrites pointer if two key are same. Collisions are overwritten
 	// todo: allow for hash collisions by pushing to the next value
-
+	// new node is null, increase counter of used array elements
+	if (newNode == NULL)
+		usedArrayElements++;
+	float quota = usedArrayElements/hTable->h_size;
+	if (quota > ARRAY_MAX)
+		//h_resize(hTable);
+		// calloc to allocate a new array with a new size
+		// seprate functions - called from - resize and h_insert
+		// keep existing hash table and change its array memeber
+		// - newNode if condition - separate function
+		// - and also the newNode = h_findKey
+		// for each element in the current array / rehash to find new key in new array/
+		// ---///--- create new function for resizing.
+		int (*x)(char *y, int *z) = &h_finKey;
+		(*x)(			
 	// cannot use realloc (save old pointer if this is the case)
 	//HashNode *oldNodePtr = newNode;
 	newNode = h_findKey(newNode, key);
 
 
 	// Executes only if the newNode is NULL-not visited before
-    if ( newNode == NULL ) {
+        if ( newNode == NULL ) {
 		if ( (newNode = (HashNode *) malloc(sizeof(HashNode) ) ) == NULL ||
 				(newNode->h_key = strdup(key) ) == NULL ||
 				(newNode->h_value = strdup(value) ) == NULL ) {
diff --git a/mylib/test/t_hash.c b/mylib/test/t_hash.c
index 3f7c78a..ce27a91 100644
--- a/mylib/test/t_hash.c
+++ b/mylib/test/t_hash.c
@@ -114,7 +114,7 @@ static char *readFile(char *fn) {
 
 		// Allocate string that can hold all of the data
 		// includes + 1 for null character + 1 to codfy end of file for Tuple
-		buf = (char *) malloc(s.st_size + 2);
+		buf = (char *) malloc( (unsigned) s.st_size + 2);
 
 		// read
 		readSize = fread(buf, sizeof(char), s.st_size, stream);
@@ -141,9 +141,10 @@ static char *readFile(char *fn) {
 
 // insert multpile keys
 //
+size_t cmpCount = 0;
 
 int main(int argc, char** argv){
-	HashTable *tableResult = h_create(10);
+	HashTable *tableResult = h_create(10000);
 	assert(tableResult != NULL);
 
 	// initialise filename to NULL
@@ -176,6 +177,6 @@ int main(int argc, char** argv){
 			}
 	}
 	dumpTable(tableResult,0);
-
+	fprintf(stderr,"Number of strcmps is: %lu\n", cmpCount);
 	return 0;
 }