Guest User

Speller

a guest
May 21st, 2016
198
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2. * dictionary.c
  3. *
  4. * Computer Science 50
  5. * Problem Set 5
  6. *
  7. * Implements a dictionary's functionality.
  8. */
  9. #include <stdio.h>
  10. #include <stdbool.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include <ctype.h>
  14. #include "dictionary.h"
  15. #define HASHTABLE_SIZE 65536
  16. /**
  17. * Returns true if word is in dictionary else false.
  18. */
  19.  
  20. int hashit(char* needs_hashing);
  21. node* root[HASHTABLE_SIZE];
  22.  
  23. bool check(const char* word)
  24. {
  25. printf("checking: ");
  26. int n = strlen(word);
  27. printf("%i", n);
  28. char wordcopy[n+1];
  29. //alernatively: char* wordcopy = malloc(sizeof(word)); , but I don't want to deal with the memory management there
  30. for (int i =0;i<=n;i++)
  31. {
  32. wordcopy[i] = tolower(word[i]);
  33. printf ("%c", wordcopy[i]);
  34. }
  35. printf(" ");
  36. int hv = hashit(wordcopy);
  37. node* cursor = root[hv];
  38. while (cursor != NULL)
  39. {
  40. if
  41. (strcmp(cursor->node, wordcopy) == 0)
  42. {
  43. return true;
  44. break;
  45. }
  46. else
  47. {
  48. cursor = cursor->next;
  49. }
  50. }
  51. return false;
  52. }
  53.  
  54. /**
  55. * Loads dictionary into memory. Returns true if successful else false.
  56. */
  57.  
  58.  
  59. int count = 0;
  60.  
  61. bool load(const char* dictionary)
  62. {
  63. printf("\nLoading...\n");
  64. FILE* ptr = fopen(dictionary, "r");
  65.  
  66. if (ptr == NULL) {
  67. printf("no.");
  68. return false;
  69. }
  70. node* nunode = malloc(sizeof(node));
  71. // char tmp[LENGTH + 1];
  72. while ( (fscanf(ptr, "%s", nunode->node) != EOF))
  73. {
  74.  
  75. int hv = hashit(nunode->node);
  76. nunode->next = root[hv];
  77. if (root[hv] == NULL)
  78. {
  79. nunode->next = NULL;
  80. root[hv] = nunode;
  81. }
  82. else
  83. {
  84. nunode->next = root[hv];
  85. root[hv] = nunode;
  86. }
  87. count++;
  88. printf("\n%s\n", nunode->node);
  89. }
  90. if (count > 0) {
  91. printf ("k done.\n");
  92. fclose(ptr);
  93. return true;
  94. }
  95. return false;
  96. }
  97.  
  98. /**
  99. * Returns number of words in dictionary if loaded else 0 if not yet loaded.
  100. */
  101. unsigned int size(void)
  102. {
  103. // TODO
  104. return count;
  105. }
  106.  
  107. /**
  108. * Unloads dictionary from memory. Returns true if successful else false.
  109. */
  110. bool unload(void)
  111. {
  112. for (int i = 0; i < HASHTABLE_SIZE; i++)
  113. {
  114.  
  115. node* cursor = root[i];
  116. while (cursor != NULL)
  117. {
  118. node* temp = cursor;
  119. cursor = cursor->next;
  120. free(temp);
  121. }
  122. }
  123. return false;
  124. }
  125.  
  126.  
  127.  
  128. int hashit(char* needs_hashing)
  129. {
  130. unsigned int hash = 0;
  131. for (int i=0, n=strlen(needs_hashing); i<n; i++)
  132. hash = (hash << 2) ^ needs_hashing[i];
  133. return hash % HASHTABLE_SIZE;
  134. }
RAW Paste Data