Advertisement
Guest User

my dictonary.c

a guest
Apr 6th, 2017
254
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.13 KB | None | 0 0
  1. /**
  2. * Implements a dictionary's functionality.
  3. */
  4.  
  5. #include <stdbool.h>
  6. #include <stdio.h>
  7. #include <cs50.h>
  8. #include <string.h>
  9. #include <strings.h>
  10.  
  11. /* from Paul Hsieh http://www.azillionmonkeys.com/qed/hash.html */
  12. #include <stdint.h> /* Replace with <stdint.h> if appropriate */
  13. #undef get16bits
  14. #if (defined(__GNUC__) && defined(__i386__)) || defined(__WATCOMC__) \
  15. || defined(_MSC_VER) || defined (__BORLANDC__) || defined (__TURBOC__)
  16. #define get16bits(d) (*((const uint16_t *) (d)))
  17. #endif
  18.  
  19. #if !defined (get16bits)
  20. #define get16bits(d) ((((uint32_t)(((const uint8_t *)(d))[1])) << 8)\
  21. +(uint32_t)(((const uint8_t *)(d))[0]) )
  22. #endif
  23.  
  24. #include "dictionary.h"
  25.  
  26. // a node to contain a word and reference other nodes
  27. typedef struct _node
  28. {
  29. char word[LENGTH + 1];
  30. struct _node *next;
  31. }
  32. node;
  33.  
  34. /* from Paul Hsieh http://www.azillionmonkeys.com/qed/hash.html */
  35. uint32_t SuperFastHash (const char * data, int len) {
  36. uint32_t hash = len, tmp;
  37. int rem;
  38.  
  39. if (len <= 0 || data == NULL) return 0;
  40.  
  41. rem = len & 3;
  42. len >>= 2;
  43.  
  44. /* Main loop */
  45. for (;len > 0; len--) {
  46. hash += get16bits (data);
  47. tmp = (get16bits (data+2) << 11) ^ hash;
  48. hash = (hash << 16) ^ tmp;
  49. data += 2*sizeof (uint16_t);
  50. hash += hash >> 11;
  51. }
  52.  
  53. /* Handle end cases */
  54. switch (rem) {
  55. case 3: hash += get16bits (data);
  56. hash ^= hash << 16;
  57. hash ^= ((signed char)data[sizeof (uint16_t)]) << 18;
  58. hash += hash >> 11;
  59. break;
  60. case 2: hash += get16bits (data);
  61. hash ^= hash << 11;
  62. hash += hash >> 17;
  63. break;
  64. case 1: hash += (signed char)*data;
  65. hash ^= hash << 10;
  66. hash += hash >> 1;
  67. }
  68.  
  69. /* Force "avalanching" of final 127 bits */
  70. hash ^= hash << 3;
  71. hash += hash >> 5;
  72. hash ^= hash << 4;
  73. hash += hash >> 17;
  74. hash ^= hash << 25;
  75. hash += hash >> 6;
  76.  
  77. return hash;
  78. }
  79. // node *head;
  80.  
  81. // array of nodes i.e. a hashtable
  82. node *htable[10000];
  83.  
  84. int num_words = 0;
  85.  
  86. /**
  87. * Returns true if word is in dictionary else false.
  88. */
  89. bool check(const char *word)
  90. {
  91. unsigned long hash = SuperFastHash(word, strlen(word));
  92. int i = hash % 10000; // 10000 is size of the hashtable
  93.  
  94. node *trav = htable[i];
  95.  
  96. if (trav->next != NULL)
  97. {
  98. trav = trav->next; // move trav to first word containing node
  99. while (trav != NULL)
  100. {
  101. if (strcasecmp(trav->word, word) == 0)
  102. {
  103. return true;
  104. }
  105. trav = trav->next; // move one node down
  106. }
  107. }
  108. return false;
  109. }
  110.  
  111. /**
  112. * Loads dictionary into memory. Returns true if successful else false.
  113. */
  114. bool load(const char *dictionary)
  115. {
  116.  
  117. /*
  118. head = malloc(sizeof(node));
  119. if (head == NULL)
  120. {
  121. free(head);
  122. }
  123. head->next = NULL;
  124. // cursor = head;
  125. */
  126.  
  127. // open the dictionary
  128. FILE *dict = fopen(dictionary, "r");
  129.  
  130. // to hold words from the dictionary
  131. char word[LENGTH + 1];
  132.  
  133. // read a word from the dictionary into the variable 'word'
  134. if (ftell(dict) != EOF)
  135. {
  136. while (fscanf(dict, "%s", word) != EOF)
  137. {
  138. num_words += 1;
  139.  
  140. node *nnode = malloc(sizeof(node));
  141. nnode->next = NULL;
  142. if (nnode == NULL)
  143. {
  144. free(nnode);
  145. return false;
  146. }
  147. else
  148. {
  149. strcpy(nnode->word, word);
  150. }
  151.  
  152. // hashing stuff
  153. unsigned long hash = SuperFastHash(word, strlen(word));
  154. int i = hash % 10000;
  155.  
  156. if (htable[i] == NULL)
  157. {
  158. htable[i] = malloc(sizeof(node));
  159. if (htable[i] == NULL)
  160. {
  161. free(htable[i]);
  162. }
  163. htable[i]->next = nnode;
  164. }
  165. else
  166. {
  167. nnode->next = htable[i]->next;
  168. htable[i]->next = nnode;
  169. }
  170.  
  171. /*
  172. if(head->next == NULL)
  173. {
  174. head->next = nnode;
  175. }
  176. else
  177. {
  178. nnode->next = head->next;
  179. head->next = nnode;
  180. }
  181. */
  182.  
  183. }
  184. fclose(dict);
  185. return true;
  186. }
  187. return false;
  188. }
  189.  
  190. /**
  191. * Returns number of words in dictionary if loaded else 0 if not yet loaded.
  192. */
  193. unsigned int size(void)
  194. {
  195. // TODO
  196. return num_words;
  197. }
  198.  
  199. /**
  200. * Unloads dictionary from memory. Returns true if successful else false.
  201. */
  202. bool unload(void)
  203. {
  204. for (int i = 0; i <= 9999; i++)
  205. {
  206. while (htable[i]->next != NULL)
  207. {
  208. node *temp = htable[i];
  209. htable[i] = htable[i]->next;
  210. free(temp);
  211. }
  212. free(htable[i]);
  213. }
  214. return true;
  215. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement