Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Implements a dictionary's functionality
- #include <ctype.h>
- #include <stdbool.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <strings.h>
- #include "dictionary.h"
- #define LENGTH_alpha 27
- // node declaration
- typedef struct node
- {
- char word[45 + 1]; //highest length of a word: 45
- struct node * next;
- }
- node;
- // remember a pointer location for later use
- FILE **ploc;
- // hash function
- unsigned int hash_word(const char * word)
- {
- //http://stackoverflow.com/questions/2571683/djb2-hash-function.
- unsigned long hash = 5381;
- for (const char * ptr = word; *ptr != '\0'; ptr++)
- {
- hash = ((hash << 5) + hash) + tolower(*ptr);
- }
- //return hash % NUM_BUCKETS;
- return hash % LENGTH_alpha;
- //return hash;
- }
- node *hashTable[LENGTH_alpha];
- // Returns true if word is in dictionary else false
- bool check(const char *word)
- {
- node *cursor = hashTable[hash_word(word) % LENGTH_alpha];
- // saving input in a word
- char strng[46];
- strcpy(strng, word); //copying input in a variable
- char string[46]; // for lowercase
- // input to lowercase
- int index = 0;
- while (strng[index] != '\0')
- {
- if (isalpha(strng[index]))
- {
- string[index] = tolower(strng[index]);
- }
- else if (strng[index] == '\'')
- {
- string[index] = strng[index];
- }
- index += 1;
- }
- string[index] = '\0';
- // traversing
- while (cursor != NULL)
- {
- if (strcasecmp(cursor->word, string) == 0)
- {
- return true;
- }
- else
- {
- cursor = cursor->next;
- }
- }
- // else (if not found through while-loop)
- return false;
- }
- // Loads dictionary into memory, returning true if successful else false
- bool load(const char *dictionary)
- {
- //node *hashTable[LENGTH_alpha];
- FILE *file = fopen(dictionary, "r");
- char c[45 + 1];
- // send pointer location in 'ploc'
- ploc = &file;
- // start iterating
- //for (int c = fgetc(file); c != EOF; c = fgetc(file))
- while (fscanf(file, "%s", c) != EOF)
- {
- // new node for every word found
- node *new_node = malloc(sizeof(node));
- if (new_node == NULL)
- {
- unload();
- return false;
- }
- // push word into a node
- strcpy(new_node->word, c);
- new_node->next = NULL;
- // which scope/bucket in hash table
- unsigned int count = hash_word(new_node->word) % LENGTH_alpha;
- // insert node in hashtable
- if (&hashTable[count] == NULL)
- {
- hashTable[count] = new_node;
- }
- else
- {
- // inserting at head
- new_node->next = hashTable[count];
- hashTable[count] = new_node;
- }
- }
- // close the file
- fclose(file);
- // if succeed
- return true;
- }
- // Returns number of words in dictionary if loaded else 0 if not yet loaded
- unsigned int size(void)
- {
- // counters
- int index = 0;
- int count = 0;
- while (index < LENGTH_alpha)
- {
- node *cursor = hashTable[index];
- // for nodes in a scope
- while (cursor != NULL)
- {
- cursor = cursor->next;
- count += 1;
- }
- index += 1;
- }
- // if succeed
- return count;
- }
- // Unloads dictionary from memory, returning true if successful else false
- bool unload(void)
- {
- // remember the command:
- // valgrind -v --leak-check=full austinpowers.txt
- int count = 0;
- // till the length of hash-table scopes
- while (count < LENGTH_alpha)
- {
- node *cursor = hashTable[count];
- // for nodes in a scope
- while (cursor != NULL)
- {
- node *temp = cursor;
- cursor = cursor->next;
- free(temp);
- }
- count += 1;
- }
- // after finishing the loop above
- return true;
- free(ploc);
- // TODO
- //return false;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement