-
Notifications
You must be signed in to change notification settings - Fork 1
/
dictionary.c
122 lines (106 loc) · 2.67 KB
/
dictionary.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
// Implements a dictionary's functionality
#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <strings.h>
#include <ctype.h>
#include "dictionary.h"
int word_count = 0;
// Represents a node in a hash table
typedef struct node
{
char word[LENGTH + 1];
struct node *next;
} node;
// Create new node function
node *create_new_node(char word[LENGTH + 1])
{
node *result = malloc(sizeof(node));
strcpy(result->word, word);
result->next = NULL;
return result;
}
// Number of buckets in hash table
const unsigned int N = 89696662;
// Hash table
node *table[N];
// Returns true if word is in dictionary else false
bool check(const char *word)
{
int hash_lookup = hash(word); // Hash the word to compare
node *crawler; // Set node to search list
crawler = table[hash_lookup]; // Set list to hash
while (crawler != NULL)
{
if (strcasecmp(crawler->word, word) == 0)
{
return true;
}
crawler = crawler->next;
}
return false;
}
// Hashes word to a number
unsigned int hash(const char *word)
{
int hash, i; // Jenkins "One at a Time" hash
for (hash = i = 0; i < strlen(word); ++i)
{
hash += tolower(word[i]);
hash += (hash << 10);
hash ^= (hash >> 6);
}
hash += (hash << 3);
hash ^= (hash >> 11);
hash += (hash << 15);
return (hash % N);
}
// Loads dictionary into memory, returning true if successful else false
bool load(const char *dictionary)
{
FILE *dict = fopen(dictionary, "r"); // Open file
if (dict == NULL)
{
return false;
}
char word[LENGTH + 1];
node *tmp;
while (fscanf(dict, "%s", word) != EOF) //Condition to read whole of dict
{
tmp = create_new_node(word);
int result = hash(word);
tmp->next = table[result];
table[result] = tmp;
word_count++;
}
fclose(dict);
return true;
}
// Returns number of words in dictionary if loaded else 0 if not yet loaded
unsigned int size(void)
{
return word_count;
}
// Unloads dictionary from memory, returning true if successful else false
bool unload(void)
{
node *temp; // Temp and crawler to seek and delete
node *crawler;
for (int i = 0; i < N; i++) // Run for length of table
{
if (table[i] != NULL) // If there's no node, ignore
{
crawler = table[i]; // Go to each node and free
while (crawler != NULL)
{
temp = crawler->next;
free(crawler);
crawler = temp;
}
temp = crawler;
free(temp);
}
}
return true;
}