Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /****************************************************************************
- * dictionary.c
- *
- * Computer Science 50
- * Problem Set 5
- *
- * Implements a dictionary's functionality.
- ***************************************************************************/
- #include <stdio.h>
- #include <stdbool.h>
- #include <stdlib.h>
- #include "dictionary.h"
- #include <ctype.h>
- #include <string.h>
- #include <sys/mman.h>
- #include <sys/stat.h>
- #include <unistd.h>
- #include <fcntl.h>
- #define SIZE 250000
- typedef struct node
- {
- char word[LENGTH + 1];
- struct node* next;
- }
- node;
- // Make a hash table
- node* hashtable[SIZE];
- char temp_word[LENGTH + 1];
- unsigned int word_ctr = 0;
- // this hash function is from delipity on reddit
- int hash_d(char* word)
- {
- unsigned int hash = 0;
- for (int i = 0, n = strlen(word); i < n; i++)
- {
- hash = (hash << 2) ^ word[i];
- }
- return hash % SIZE;
- }
- /**
- * Returns true if word is in dictionary else false.
- */
- bool check(const char* word)
- {
- // TODO
- char new_word[LENGTH + 1];
- for (int i = 0, n = strlen(word); i <= n; i++)
- {
- new_word[i] = word[i];
- new_word[i] = tolower(new_word[i]);
- }
- // determine the correct hash position
- int y = hash_d(new_word);
- // point the cursor to the first item in the list we identified
- node* cursor = hashtable[y];
- // search the linked list
- while (cursor != NULL)
- {
- if (strcmp(cursor->word,new_word) == 0)
- {
- return true;
- }
- else
- {
- cursor = cursor->next;
- }
- }
- return false;
- }
- /**
- * checks length of '\n' terminated string
- *
- */
- int strlen1(char *s)
- {
- int len = 0;
- while(s[len] != '\n' || s[len] != '\0')
- {
- len++;
- }
- return len;
- }
- /**
- * Loads dictionary into memory. Returns true if successful else false.
- */
- bool load(const char* dictionary)
- {
- // TODO
- // do some prep work
- struct stat buf;
- if (stat(dictionary,&buf))
- return false;
- // map file into memory
- int fd = open(dictionary, O_RDONLY);
- if (fd < 0)
- return false;
- char *map = mmap(0, buf.st_size, PROT_READ, MAP_SHARED, fd, 0);
- if (map == MAP_FAILED)
- {
- close(fd);
- return false;
- }
- // for as many characters as there are scan for a newline
- for (int i = 0; i < buf.st_size ; i++)
- {
- // replace that newline with null terminater
- if (map[i] == '\n')
- {
- }
- printf("%c\n",map[i]);
- }
- // now I have a word: insert it into the hash table->word?
- // do some cleanup work
- close(fd);
- return true;
- }
- /**
- * Returns number of words in dictionary if loaded else 0 if not yet loaded.
- */
- unsigned int size(void)
- {
- // TODO
- return word_ctr;
- }
- /**
- * Unloads dictionary from memory. Returns true if successful else false.
- */
- bool unload(void)
- {
- // TODO
- for (int i = 0; i < SIZE ; i++)
- {
- node* cursor = hashtable[i];
- while (cursor != NULL)
- {
- node* temp = cursor;
- cursor = cursor->next;
- free(temp);
- }
- }
- return true;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement