Advertisement
UME14

speller/pset5/cs50

Feb 12th, 2019
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.08 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 <strings.h>
  9.  
  10. #include "dictionary.h"
  11.  
  12. #define LENGTH_alpha 27
  13.  
  14. // node declaration
  15. typedef struct node
  16. {
  17. char word[45 + 1]; //highest length of a word: 45
  18. struct node * next;
  19. }
  20. node;
  21.  
  22. // remember a pointer location for later use
  23. FILE **ploc;
  24.  
  25. // hash function
  26. unsigned int hash_word(const char * word)
  27. {
  28. //http://stackoverflow.com/questions/2571683/djb2-hash-function.
  29. unsigned long hash = 5381;
  30. for (const char * ptr = word; *ptr != '\0'; ptr++)
  31. {
  32. hash = ((hash << 5) + hash) + tolower(*ptr);
  33. }
  34. //return hash % NUM_BUCKETS;
  35. return hash % LENGTH_alpha;
  36. //return hash;
  37. }
  38.  
  39. node *hashTable[LENGTH_alpha];
  40.  
  41.  
  42.  
  43. // Returns true if word is in dictionary else false
  44. bool check(const char *word)
  45. {
  46. node *cursor = hashTable[hash_word(word) % LENGTH_alpha];
  47.  
  48. // saving input in a word
  49. char strng[46];
  50. strcpy(strng, word); //copying input in a variable
  51.  
  52.  
  53. char string[46]; // for lowercase
  54.  
  55. // input to lowercase
  56. int index = 0;
  57. while (strng[index] != '\0')
  58. {
  59. if (isalpha(strng[index]))
  60. {
  61. string[index] = tolower(strng[index]);
  62. }
  63. else if (strng[index] == '\'')
  64. {
  65. string[index] = strng[index];
  66. }
  67. index += 1;
  68. }
  69. string[index] = '\0';
  70.  
  71. // traversing
  72. while (cursor != NULL)
  73. {
  74. if (strcasecmp(cursor->word, string) == 0)
  75. {
  76. return true;
  77. }
  78. else
  79. {
  80. cursor = cursor->next;
  81. }
  82. }
  83. // else (if not found through while-loop)
  84. return false;
  85. }
  86.  
  87.  
  88. // Loads dictionary into memory, returning true if successful else false
  89. bool load(const char *dictionary)
  90. {
  91. //node *hashTable[LENGTH_alpha];
  92. FILE *file = fopen(dictionary, "r");
  93. char c[45 + 1];
  94.  
  95. // send pointer location in 'ploc'
  96. ploc = &file;
  97.  
  98. // start iterating
  99. //for (int c = fgetc(file); c != EOF; c = fgetc(file))
  100. while (fscanf(file, "%s", c) != EOF)
  101. {
  102. // new node for every word found
  103. node *new_node = malloc(sizeof(node));
  104. if (new_node == NULL)
  105. {
  106. unload();
  107. return false;
  108. }
  109.  
  110. // push word into a node
  111. strcpy(new_node->word, c);
  112. new_node->next = NULL;
  113.  
  114. // which scope/bucket in hash table
  115. unsigned int count = hash_word(new_node->word) % LENGTH_alpha;
  116.  
  117. // insert node in hashtable
  118. if (&hashTable[count] == NULL)
  119. {
  120. hashTable[count] = new_node;
  121. }
  122. else
  123. {
  124. // inserting at head
  125. new_node->next = hashTable[count];
  126. hashTable[count] = new_node;
  127. }
  128. }
  129.  
  130. // close the file
  131. fclose(file);
  132.  
  133. // if succeed
  134. return true;
  135. }
  136.  
  137.  
  138. // Returns number of words in dictionary if loaded else 0 if not yet loaded
  139. unsigned int size(void)
  140. {
  141. // counters
  142. int index = 0;
  143. int count = 0;
  144.  
  145. while (index < LENGTH_alpha)
  146. {
  147. node *cursor = hashTable[index];
  148.  
  149. // for nodes in a scope
  150. while (cursor != NULL)
  151. {
  152. cursor = cursor->next;
  153. count += 1;
  154. }
  155. index += 1;
  156. }
  157.  
  158. // if succeed
  159. return count;
  160. }
  161.  
  162.  
  163. // Unloads dictionary from memory, returning true if successful else false
  164. bool unload(void)
  165. {
  166. // remember the command:
  167. // valgrind -v --leak-check=full austinpowers.txt
  168.  
  169. int count = 0;
  170.  
  171. // till the length of hash-table scopes
  172. while (count < LENGTH_alpha)
  173. {
  174. node *cursor = hashTable[count];
  175.  
  176. // for nodes in a scope
  177. while (cursor != NULL)
  178. {
  179. node *temp = cursor;
  180. cursor = cursor->next;
  181. free(temp);
  182. }
  183. count += 1;
  184. }
  185.  
  186. // after finishing the loop above
  187. return true;
  188.  
  189. free(ploc);
  190.  
  191. // TODO
  192. //return false;
  193. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement