/* Deallocate buckets and the hash map.*/ void deleteMap(hashMap *ht) { assert(ht!= 0); /* Free all memory used by the buckets */ _freeMap(ht); /* free the hashMap struct */ free(ht); }
/* Resizes the hash table to be the size newTableSize */ void _setTableSize(struct hashMap * ht, int newTableSize){ int i = 0; struct hashMap * newHM, * oldHM; struct hashLink * currLink; /* Create new hash table of size newTableSize */ newHM = createMap(newTableSize); oldHM = ht; /* Iterate through all hashLinks in old hashMap and copy them into new hashMap */ while(i < ht->tableSize){ currLink = ht->table[i]; while(currLink != 0){ insertMap(newHM, currLink->key, currLink->value); currLink = currLink->next; } i++; } /* Replace hashMap pointer with new hashMap and free old hashMap */ _freeMap(oldHM); ht->table = newHM->table; ht->tableSize = newHM->tableSize; ht->count = newHM->count; }
/* Resizes the hash table to be the size newTableSize */ void _setTableSize(struct hashMap * ht, int newTableSize, comparator keyCompare, hashFuncPtr hashFunc) { /*write this*/ int idx, temp_count = ht->count; hashLink **newTable = malloc(sizeof(hashLink *) * newTableSize); hashLink *tempOld; hashLink *tempNew; for (int i = 0; i < ht->tableSize; i++) { tempOld = ht->table[i]; while (tempOld != 0) { idx = (*hashFunc)(tempOld->key) % newTableSize; tempNew = malloc(sizeof(hashLink *)); tempNew->value = tempOld->value; tempNew->key = tempOld->key; tempNew->next = newTable[idx]; newTable[idx] = tempNew; tempOld = tempOld->next; } } _freeMap(ht); ht->tableSize = newTableSize; ht->count = temp_count; ht->table = newTable; printf("Resized\n"); }
/* freeMap: deallocate buckets and the hash map param1: ht - the hash map pre: - ht is not null post: memory used by the hash buckets and the hash table has been freed */ void deleteMap(struct hashMap *ht) { assert(ht != NULL); _freeMap(ht);/* free all memory used by the buckets */ free(ht);/* free the hash map */ }