Advertisement
tkamiten

speller load

May 1st, 2016
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.36 KB | None | 0 0
  1. /**
  2. * Loads dictionary into memory. Returns true if successful else false.
  3. */
  4. bool load(const char* dictionary)
  5. {
  6.  
  7.  
  8. // initialize hashtable
  9. for(int i = 0; i < 19683; i++)
  10. hashtable[i] = NULL;
  11.  
  12. // file pointer to dictionary
  13. FILE *fptr;
  14.  
  15. // node pointer
  16. node *n;
  17.  
  18. fptr = fopen("./dictionaries/large","r");
  19.  
  20. for(int i = 0; i < 143091; i++)
  21. {
  22. node* new_node = malloc(sizeof(node));
  23. new_node->next = NULL;
  24.  
  25. fscanf(fptr, "%s", new_node->word);
  26.  
  27. double num = 0;
  28.  
  29. for(int i = 0; i < 4; i++)
  30. {
  31. if(new_node->word[i] == '\0')
  32. break;
  33. else
  34. num += pow (27.0, (4.0 - i - 1))*(1 + new_node->word[i] - 'a');
  35. }
  36.  
  37. int index = (int)num % 19683;
  38.  
  39. if (hashtable[index] == NULL)
  40. {
  41. hashtable[index] = new_node;
  42. dic_num++;
  43. }
  44. else
  45. {
  46. // find end of linked list
  47. for(n = hashtable[index]; n != NULL; n = n-> next)
  48. {
  49. if(n -> next == NULL)
  50. {
  51. n->next = new_node;
  52. dic_num++;
  53. break;
  54. }
  55. }
  56. }
  57. }
  58. return true;
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement