Advertisement
saltycracker

hash.c

Apr 14th, 2020
542
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.78 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. typedef struct{
  5.     int size;
  6.     int count;
  7.     char *names;
  8. } HashTable;
  9.  
  10. typedef enum{
  11.     false, true
  12. } Boolean;
  13.  
  14. void rehashTable(HashTable *table);
  15.  
  16. char *copyArray(char *array, int size){
  17.     char *newArray = (char*)malloc(size * sizeof(char));
  18.     if (newArray == NULL){
  19.         printf("Error allocating memory to copy array.");
  20.         return NULL;
  21.     }
  22.     for (int i = 0; i < size; i++){
  23.         newArray[i] = array[i];
  24.     }
  25.     return newArray;
  26. }
  27.  
  28. int hashFunction(char name, int size){
  29.     int ascii = name;
  30.     return ascii % size;
  31. }
  32.  
  33. void add(HashTable *table, char name){
  34.     int key = hashFunction(name, table->size);
  35.     if (table->names[key] == 0){
  36.         table->names[key] = name;
  37.     }else{
  38.         while (table->names[key] != 0){
  39.             key++;
  40.             if (key >= table->size){
  41.                 key = 0;
  42.             }
  43.         }
  44.         table->names[key] = name;
  45.     }
  46.     table->count++;
  47.  
  48.     if ((double) table->count / (double) table->size >= 0.7){
  49.         printf("Rehashing!");
  50.         rehashTable(table);
  51.         printf("%d--->", table->size);
  52.     }
  53. }
  54.  
  55. void rehashTable(HashTable *table){
  56.     char *tempArray = copyArray(table->names, table->size);
  57.     free(table->names);
  58.  
  59.     int oldSize = table->size;
  60.     int updatedSize = table->size * 2;
  61.     char *newNamesArray = (char*)calloc(updatedSize, sizeof(char));
  62.     if (newNamesArray == NULL){
  63.         printf("Error allocating memory to the new hash table.");
  64.     }else{
  65.         table->names = newNamesArray;
  66.         table->size = updatedSize;
  67.         for (int i = 0; i < oldSize; i++){
  68.             if (tempArray[i] != 0){
  69.                 add(table, tempArray[i]);
  70.             }
  71.         }
  72.     }
  73. }
  74.  
  75. HashTable initializeTable(int size){
  76.  
  77.     HashTable table;
  78.  
  79.     char *nameArray = (char*)calloc(size, sizeof(char));
  80.     if (nameArray == NULL){
  81.         printf("Error allocating memory to array of names.");
  82.         exit(1);
  83.     }
  84.  
  85.     table.names = nameArray;
  86.     table.size = size;
  87.     table.count = 0;
  88.  
  89.     return table;
  90. }
  91.  
  92. Boolean search(HashTable *table, char name){
  93.     int key = hashFunction(name, table->size);
  94.     if (table->names[key] == 0){
  95.         return false;
  96.     }else if (table->names[key] == name){
  97.         return true;
  98.     }else{
  99.         while (table->names[key] != 0){
  100.             key++;
  101.             if (key >= table->size){
  102.                 key = 0;
  103.             }
  104.             if (table->names[key] == name){
  105.                 return true;
  106.             }
  107.         }
  108.         return false;
  109.     }
  110. }
  111.  
  112.  
  113. int main(){
  114.     const char test[] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'K'};
  115.     const char test2[] = {'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U'};
  116.     const int tableSize = 10;
  117.     HashTable myHashTable = initializeTable(tableSize);
  118.     add(&myHashTable, 'A');
  119.     add(&myHashTable, 'B');
  120.     add(&myHashTable, 'C');
  121.     add(&myHashTable, 'D');
  122.     add(&myHashTable, 'E');
  123.     add(&myHashTable, 'F');
  124.     add(&myHashTable, 'G');
  125.     add(&myHashTable, 'K');
  126.     add(&myHashTable, 'H');
  127.     add(&myHashTable, 'M');
  128.     add(&myHashTable, 'N');
  129.     add(&myHashTable, 'O');
  130.     add(&myHashTable, 'P');
  131.     fputs("\n", stdout);
  132.     for (size_t i = 0; i < 9; i++) {
  133.       fprintf(stdout, "Testing: %c\n", test[i]);
  134.       if (search(&myHashTable, test[i]) == 1){
  135.           printf("Name is in hash table.\n");
  136.       }else {
  137.         fprintf(stdout, "Nothing found here!\n");
  138.       }
  139.     }
  140.     fputs("\n", stdout);
  141.     for (size_t i = 0; i < 9; i++) {
  142.       fprintf(stdout, "Testing: %c\n", test2[i]);
  143.       if (search(&myHashTable, test2[i]) == 1){
  144.           printf("Name is in hash table.\n");
  145.       }else {
  146.         fprintf(stdout, "Nothing found here!\n");
  147.       }
  148.     }
  149.     return 0;
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement