Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- b/****************************************************************************
- * 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;
- }
- /**
- * 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;
- }
- // iterate over array
- for (int i = 0; i < strlen(map); i++) // here is were im stuck on logic
- // the dictionary at this point is in *map every word has a \n seperating
- // it.
- // not sure if i should change the \n to \0 and put it in another array
- // or what ? i know that it needs to be put into the hashtable ?
- // i believe the idea is to not use scanf to read and not malloc for the
- // new-node->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