Advertisement
Guest User

Untitled

a guest
Dec 18th, 2018
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.77 KB | None | 0 0
  1. // Implements a dictionary's functionality
  2.  
  3. #include <ctype.h>
  4. #include <stdbool.h>
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include "dictionary.h"
  9.  
  10. int dictionary_size = 0;
  11.  
  12. typedef struct node
  13. {
  14.     char word[LENGTH + 1];
  15.     struct node* next;
  16. }
  17. node;
  18.  
  19. // set array to be the size of words in large dictionary
  20. node *hashtable[143091];
  21.  
  22. // hashing new_node->word will give index of a bucket in the hash table
  23. // https://stackoverflow.com/questions/7666509/hash-function-for-string
  24. // http://www.cse.yorku.ca/~oz/hash.html
  25.  
  26. unsigned int hash_word(const char* word)
  27. {
  28.     unsigned long hash = 5381;
  29.     for (const char* ptr = word; *ptr != '\0'; ptr++)
  30.     {
  31.         hash = ((hash << 5) + hash) + tolower(*ptr);
  32.     }
  33.     return (hash % 143091);
  34.  }
  35.  
  36. // Returns true if word is in dictionary else false
  37. bool check(const char *word)
  38. {
  39.  
  40.     // format word to wordf
  41.     int len = strlen(word);
  42.     char wordf[len + 1];
  43.     for (int i = 0; i < len; i++)
  44.     {
  45.         wordf[i] = tolower(word[i]);
  46.     }
  47.     wordf[len] = '\0';
  48.  
  49.     // get hash value for "bucket"
  50.     int hash = hash_word(wordf);
  51.  
  52.     // assign cursor node to the first node of the bucket
  53.     node* cursor = hashtable[hash];
  54.  
  55.     while (cursor != NULL)
  56.     {
  57.         if (strcmp(cursor->word, wordf) == 0)
  58.         {
  59.             return true;
  60.         }
  61.         else
  62.         {
  63.             cursor = cursor->next;
  64.         }
  65.     }
  66.     return false;
  67. }
  68.  
  69. // Loads dictionary into memory, returning true if successful else false
  70. bool load(const char *dictionary)
  71. {
  72.  
  73.     FILE* source = fopen(dictionary, "r");
  74.     if (source == NULL)
  75.     {
  76.         fprintf(stderr, "Could not open dictionary.\n");
  77.         return false;
  78.     }
  79.  
  80.     char word[LENGTH + 1];
  81.  
  82.     while(fscanf(source, "%s", word) != EOF)
  83.     {
  84.         node *new_node = malloc(sizeof(node));
  85.         if (new_node == NULL)
  86.         {
  87.             unload();
  88.             fprintf(stderr, "Out of memory.\n");
  89.             return false;
  90.         }
  91.         dictionary_size++;
  92.  
  93.         strcpy(new_node->word, word);
  94.         int hashed = hash_word(new_node->word);
  95.         node *head = hashtable[hashed];
  96.         new_node->next = head;
  97.         hashtable[hashed] = new_node;
  98.     }
  99.     fclose(source);
  100.     return true;
  101. }
  102.  
  103. // Returns number of words in dictionary if loaded else 0 if not yet loaded
  104. unsigned int size(void)
  105. {
  106.     return dictionary_size;
  107. }
  108.  
  109. // Unloads dictionary from memory, returning true if successful else false
  110. bool unload(void)
  111. {
  112.     for (int i = 0; i < 143091; i++)
  113.     {
  114.         node *cursor = hashtable[i];
  115.         while (cursor != NULL)
  116.         {
  117.             node *temp = cursor;
  118.             cursor = cursor->next;
  119.             free(temp);
  120.         }
  121.     }
  122.     return true;
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement