Advertisement
Jodyone

dictionary2.c

Oct 23rd, 2014
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.33 KB | None | 0 0
  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 "dictionary.h"
  13. #include <ctype.h>
  14. #include <string.h>
  15. #include <sys/mman.h>
  16. #include <sys/stat.h>
  17. #include <unistd.h>
  18. #include <fcntl.h>
  19.  
  20. #define SIZE 250000
  21.  
  22. typedef struct node
  23. {
  24.     char word[LENGTH + 1];  
  25.     struct node* next;
  26. }
  27. node;
  28.  
  29. // Make a hash table
  30. node* hashtable[SIZE];
  31. char temp_word[LENGTH + 1];
  32. unsigned int word_ctr = 0;
  33. // this hash function is from delipity on reddit
  34. int hash_d(char* word)
  35. {
  36.    
  37.     unsigned int hash = 0;
  38.     for (int i = 0, n = strlen(word); i < n; i++)
  39.     {
  40.         hash = (hash << 2) ^ word[i];
  41.     }
  42.      
  43.     return hash % SIZE;
  44. }
  45. /**
  46.  * Returns true if word is in dictionary else false.
  47.  */
  48. bool check(const char* word)
  49. {
  50.     // TODO
  51.     char new_word[LENGTH + 1];
  52.     for (int i = 0, n = strlen(word); i <= n; i++)
  53.     {
  54.         new_word[i] = word[i];
  55.         new_word[i] = tolower(new_word[i]);
  56.     }
  57.      
  58.     // determine the correct hash position
  59.     int y = hash_d(new_word);
  60.  
  61.     // point the cursor to the first item in the list we identified
  62.     node* cursor = hashtable[y];
  63.    
  64.     // search the linked list
  65.     while (cursor != NULL)
  66.     {
  67.         if (strcmp(cursor->word,new_word) == 0)
  68.         {  
  69.             return true;
  70.         }
  71.         else
  72.         {
  73.             cursor = cursor->next;
  74.         }
  75.     }
  76.     return false;
  77. }
  78. /**
  79.  * checks length of '\n' terminated string
  80.  *
  81.  */
  82. int strlen1(char *s)
  83. {
  84.     int len = 0;
  85.     while(s[len] != '\n' || s[len] != '\0')
  86.     {
  87.         len++;
  88.     }
  89.     return len;
  90. }
  91.  
  92. /**
  93.  * Loads dictionary into memory.  Returns true if successful else false.
  94.  */  
  95. bool load(const char* dictionary)
  96. {
  97.     // TODO
  98.     // do some prep work
  99.    
  100.        struct stat buf;
  101.     if (stat(dictionary,&buf))
  102.         return false;
  103.  
  104.     // map file into memory
  105.     int fd = open(dictionary, O_RDONLY);
  106.     if (fd < 0)
  107.         return false;
  108.    
  109.     char *map = mmap(0, buf.st_size, PROT_READ, MAP_SHARED, fd, 0);
  110.     if (map == MAP_FAILED)
  111.     {
  112.         close(fd);
  113.         return false;
  114.     }
  115.  
  116.     // for as many characters as there are scan for a newline
  117.     for (int i = 0; i < buf.st_size ; i++)
  118.     {
  119.         // replace that newline with null terminater
  120.        if (map[i] == '\n')
  121.        {
  122.    
  123.        }
  124.         printf("%c\n",map[i]);
  125.  
  126.  
  127.     }
  128.        
  129.    
  130.  
  131.     // now I have a word: insert it into the hash table->word?
  132.    
  133.      
  134.  
  135.      
  136.  
  137.    
  138.     // do some cleanup work
  139.     close(fd);
  140.     return true;
  141.  }
  142. /**
  143.  * Returns number of words in dictionary if loaded else 0 if not yet loaded.
  144.  */
  145. unsigned int size(void)
  146. {
  147.     // TODO
  148.     return word_ctr;
  149. }
  150.  
  151. /**
  152.  * Unloads dictionary from memory.  Returns true if successful else false.
  153.  */
  154. bool unload(void)
  155. {
  156.     // TODO
  157.  
  158.     for (int i = 0; i < SIZE ; i++)
  159.     {
  160.         node* cursor = hashtable[i];
  161.         while (cursor != NULL)
  162.         {
  163.             node* temp = cursor;
  164.             cursor = cursor->next;
  165.             free(temp);
  166.         }
  167.     }
  168.     return true;
  169. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement