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 <stdbool.h>
- #include <stdint.h> // stdint is required for the uint32_t and other type declarations for murmurhash
- #include <string.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include "dictionary.h"
- #include <ctype.h>
- // Initial values
- int num_words; // define the count of words in the dictionary
- int index = 0; // set an index for counting characters in 'words'
- // define a structure for linear probing in hash function; will have the hash index, and the maximum distance to probe.
- typedef struct trie
- {
- bool isWord;
- struct trie* children[27];
- } trie;
- trie* head; // declare a global variable for the head of the trie
- // Prototype some functions
- trie* insert(char c, trie* ptr); // insert a character into the trie at an appropriate location and return a pointer to where the value was inserted
- bool endword(trie* wordPtr); // end the world by setting the boolean value to true
- bool delete(trie* node); // hopefully a recursive function to delete nodes
- trie* buildTrie(void); // create the head of the Trie
- trie* trieCheck(char c, trie* ptr); // checks to see if a character is in the list given a set location in the trie
- /**
- * Returns true if word is in dictionary else false.
- */
- bool check(const char* word)
- {
- trie* checkPtr = head;
- int len = strlen(word);
- for (int k = 0; k < len; k++)
- {
- char c2 = tolower(word[k]);
- if (checkPtr != 0)
- {
- checkPtr = trieCheck(c2, checkPtr);
- }
- else
- return false;
- }
- if (checkPtr->isWord == 1)
- {
- return true;
- }
- else
- return false;
- }
- /**
- * Loads dictionary into memory. Returns true if successful else false.
- */
- bool load(const char* dictionary)
- {
- /**
- * We'll load each word character by character into a trie. Once a word is ended we'll set the boolean to true
- **/
- head = buildTrie(); // buildTrie returns a pointer to head
- FILE* fp = fopen(dictionary, "r");
- if (fp == NULL)
- {
- printf("Could not open %s.\n", dictionary);
- unload();
- return 1;
- }
- // to insert the words into the trie, we will start at the head, and then add letter by letter
- // inside of word, we'll use the function insert to allocate memory for new letters, returning pointers to the new memory or the memory that is there
- // this way we keep progress and don't have to start at the beginning of the word - nor do we have to keep the word in a buffer, we can go char by char
- // when the word is done, we set the pointer to the head of the trie, and restart the process;
- trie* wordPtr = head;
- for (int c = fgetc(fp); c != EOF; c = fgetc(fp))
- {
- if (isalpha(c) || (c == '\'' && index > 0))
- {
- wordPtr = insert(c, wordPtr);
- index++;
- }
- else if (index > 0)
- {
- // update counter
- num_words++;
- endword(wordPtr);
- wordPtr = head;
- index = 0;
- }
- }
- fclose(fp);
- return false;
- }
- /**
- * Returns number of words in dictionary if loaded else 0 if not yet loaded.
- */
- unsigned int size(void)
- {
- // since num_words is a good proxy for whether we loaded a dictionary (unless it's size zero, which is the same thing)
- // we just return the number of words loaded.
- return num_words;
- }
- /**
- * Unloads dictionary from memory. Returns true if successful else false.
- */
- bool unload(void)
- {
- if (delete(head) == true)
- {
- return 0;
- }
- else
- return -1;
- }
- trie* insert(char c, trie* ptr)
- {
- if (c == '\'' && ptr->children[26] == 0)
- {
- ptr->children[26] = calloc(1, sizeof(trie));
- return ptr->children[26];
- }
- else if (c == '\'' && ptr->children[26] != 0)
- {
- return ptr->children[26];
- }
- if (ptr->children[c -'a'] == 0)
- {
- ptr->children[c - 'a'] = calloc(1, sizeof(trie));
- return ptr->children[c - 'a'];
- }
- else
- {
- // check to see if our logic is tight - in this instance there should already be a place there.
- // assert(ptr->children[c - 'a']);
- return ptr->children[c - 'a'];
- }
- }
- trie* trieCheck(char c, trie* checkPtr)
- {
- if (c == '\'' && checkPtr->children[26] == 0)
- {
- return 0;
- }
- else if (c == '\'' && checkPtr->children[26] != 0)
- {
- return checkPtr->children[26];
- }
- if (checkPtr->children[c -'a'] == 0)
- {
- return 0;
- }
- else
- {
- // check to see if our logic is tight - in this instance there should already be a place there.
- // assert(ptr->children[c - 'a']);
- return checkPtr->children[c - 'a'];
- }
- }
- bool endword(trie* wordPtr)
- {
- wordPtr->isWord = 1;
- return 0;
- }
- trie* buildTrie(void)
- {
- return (trie*)calloc(1, sizeof(trie));
- }
- bool delete(trie* deleteptr)
- {
- for (int i = 0; i < 27; i++)
- {
- if(deleteptr->children[i] != NULL)
- {
- deleteptr = deleteptr->children[i];
- delete(deleteptr);
- }
- }
- free(deleteptr);
- return 0;
- }
- /*
- bool wrd_insert(uint32_t key, char word[])
- {
- if (dctnry[key] == NULL)
- {
- strcpy(dctnry[key], word);
- }
- else
- {
- collisions[num_col].index = key;
- collisions[num_col].probe += 1;
- num_col++;
- key += 1;
- wrd_insert(key, word);
- }
- }
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement