-
Notifications
You must be signed in to change notification settings - Fork 0
/
libdictionary.c
182 lines (151 loc) · 5.37 KB
/
libdictionary.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "libdictionary.h"
#define max(a, b) \
({ \
__typeof__(a) _a = (a); \
__typeof__(b) _b = (b); \
_a > _b ? _a : _b; \
})
int dictionary_getSize(struct Dictionary **dictionary) {
int count = 1;
for (int i = 0; i < (*dictionary)->length; i++) {
count *= max(1, (*dictionary)->entries[i]->length);
}
return count;
}
void dictionaryIterator_increment(
struct DictionaryIterator **dictionaryIterator) {
for (int last = (*dictionaryIterator)->dictionary->length - 1; last >= 0;
last--) {
(*dictionaryIterator)->entries[last] += 1;
if ((*dictionaryIterator)->entries[last] <
(*dictionaryIterator)->dictionary->entries[last]->length) {
return;
}
(*dictionaryIterator)->entries[last] = 0;
}
(*dictionaryIterator)->hasReachedLast = 1;
}
int dictionaryIterator_nextEntry(struct DictionaryIterator **dictionaryIterator,
char **nextElement) {
int requiredStringLength = 1;
for (int i = 0; i < (*dictionaryIterator)->dictionary->length; i++) {
if ((*dictionaryIterator)->dictionary->entries[i]->length == 0) {
continue;
}
requiredStringLength +=
strlen((*dictionaryIterator)
->dictionary->entries[i]
->entries[(*dictionaryIterator)->entries[i]]);
}
if (*nextElement != NULL) {
free(*nextElement);
}
*nextElement = malloc(requiredStringLength * sizeof(char));
(*nextElement)[0] = 0;
for (int i = 0; i < (*dictionaryIterator)->dictionary->length; i++) {
if ((*dictionaryIterator)->dictionary->entries[i]->length == 0) {
continue;
}
strcat(*nextElement, (*dictionaryIterator)
->dictionary->entries[i]
->entries[(*dictionaryIterator)->entries[i]]);
}
int returnValue = (*dictionaryIterator)->hasReachedLast;
dictionaryIterator_increment(dictionaryIterator);
return !returnValue;
}
void dictionary_fromFile(char *filename, struct Dictionary **dictionary) {
char *line = NULL;
size_t len = 0, read;
FILE *toBeLoadedFrom = fopen(filename, "r");
if (toBeLoadedFrom == NULL) return;
while ((read = getline(&line, &len, toBeLoadedFrom)) != -1) {
if (line[read - 1] == '\n') {
line[read - 1] = '\0';
}
if (strstr(line, "[NEXT]")) {
dictionary_newSegment(dictionary);
} else if (strstr(line, "[END]")) {
break;
} else {
dictionary_addWord(dictionary, line);
}
}
fclose(toBeLoadedFrom);
if (line) free(line);
}
void dictionaryIterator_allocate(struct DictionaryIterator **dictionaryIterator,
struct Dictionary **dictionary) {
*dictionaryIterator = malloc(sizeof(struct DictionaryIterator));
(*dictionaryIterator)->dictionary = *dictionary;
(*dictionaryIterator)->hasReachedLast = 0;
(*dictionaryIterator)->entries = calloc((*dictionary)->length, sizeof(int));
}
void dictionary_allocate(struct Dictionary **dictionary) {
*dictionary = malloc(sizeof(struct Dictionary));
(*dictionary)->length = 0;
}
void dictionary_allocateEntry(struct DictionaryEntry **dictionaryEntry) {
*dictionaryEntry = malloc(sizeof(struct DictionaryEntry));
(*dictionaryEntry)->length = 0;
}
char **dictionaryEntry_append(struct DictionaryEntry **dictionaryEntry) {
if ((*dictionaryEntry)->length == 0) {
(*dictionaryEntry)->entries = malloc(sizeof(char *));
} else {
(*dictionaryEntry)->entries =
realloc((*dictionaryEntry)->entries,
((*dictionaryEntry)->length + 1) * sizeof(char *));
}
(*dictionaryEntry)->length += 1;
return &((*dictionaryEntry)->entries[(*dictionaryEntry)->length - 1]);
}
struct DictionaryEntry **dictionary_append(struct Dictionary **dictionary) {
if ((*dictionary)->length == 0) {
(*dictionary)->entries = malloc(sizeof(struct DictionaryEntry *));
} else {
(*dictionary)->entries =
realloc((*dictionary)->entries,
((*dictionary)->length + 1) * sizeof(struct DictionaryEntry *));
}
(*dictionary)->length += 1;
return &((*dictionary)->entries[(*dictionary)->length - 1]);
}
void dictionary_newSegment(struct Dictionary **dictionary) {
struct DictionaryEntry **dictionaryEntry = dictionary_append(dictionary);
dictionary_allocateEntry(dictionaryEntry);
}
void dictionary_addWord(struct Dictionary **dictionary, char *word) {
if ((*dictionary)->length == 0) {
dictionary_newSegment(dictionary);
}
struct DictionaryEntry *dictionaryEntry =
(*dictionary)->entries[(*dictionary)->length - 1];
char **wordPosition = dictionaryEntry_append(&dictionaryEntry);
*wordPosition = strdup(word);
}
void dictionaryEntry_free(struct DictionaryEntry **dictionaryEntry) {
if ((*dictionaryEntry)->length > 0) {
for (int i = 0; i < (*dictionaryEntry)->length; i++) {
free((*dictionaryEntry)->entries[i]);
}
free((*dictionaryEntry)->entries);
}
free(*dictionaryEntry);
}
void dictionary_free(struct Dictionary **dictionary) {
if ((*dictionary)->length > 0) {
for (int i = 0; i < (*dictionary)->length; i++) {
dictionaryEntry_free(&((*dictionary)->entries[i]));
}
free((*dictionary)->entries);
}
free(*dictionary);
}
void dictionaryIterator_free(struct DictionaryIterator **dictionaryIterator) {
free((*dictionaryIterator)->entries);
free(*dictionaryIterator);
}