Advertisement
Guest User

Untitled

a guest
Feb 21st, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.67 KB | None | 0 0
  1. void resizeTable(HashMap* map, int capacity)
  2. {
  3. printf("resize triggered\n");
  4. //HashMap* doubled = hashMapNew(capacity);
  5. HashLink** doubled = malloc(sizeof(HashLink*) * capacity);
  6. HashLink** temp = map->table;
  7. map->table = doubled;
  8.  
  9. if(map->size > 0) { //No need to iterate if there are no elems
  10. map->size = 0;
  11. printf("rehashing triggered\n");
  12. for (int i = 0; i < map->capacity; i++) { //Iterate through old maps members
  13. struct HashLink *iterator = temp[i];
  14. if (iterator != NULL) { //Current bucket has elems
  15. while(iterator != 0){
  16. //printf("K/V Pair inside of rehash is: (%s -> %d)\n",iterator->key, iterator->value);
  17. hashMapPut(map, iterator->key, iterator->value); //Put elems into new hashMap
  18. iterator = iterator->next;
  19. }
  20. }
  21. }
  22. }
  23. printf("rehashing complete\n");
  24. //Free the temp table
  25. for (int i = 0; i < map->capacity; i++) {
  26. struct HashLink *hashLinkIterator = temp[i];
  27. if(hashLinkIterator != NULL){
  28. while(hashLinkIterator != NULL){
  29. struct HashLink *toFree = hashLinkIterator;
  30. hashLinkIterator = hashLinkIterator->next;
  31. hashLinkDelete(toFree);
  32. }
  33. }else{
  34. hashLinkDelete(hashLinkIterator);
  35. }
  36.  
  37. }
  38. free(temp);
  39. //Free complete
  40. map->capacity = capacity;
  41. //struct HashMap *temp = map;
  42. //map = doubled;
  43. //hashMapDelete(temp);
  44. printf("resizing complete\n");
  45.  
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement