Repository navigation
Expand file tree
/
Copy pathdictionary.c
More file actions
480 lines (387 loc) · 10.1 KB
/
Copy pathdictionary.c
File metadata and controls
480 lines (387 loc) · 10.1 KB
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
/*
* dictionary.c
* This file is part of Gniggle
*
* Copyright (C) 2006 - Rob Kendrick <rjek@rjek.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, and/or sell copies
* of the Software, and to permit persons to whom the Software is furnished to
* do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
#include <stdbool.h>
#include <string.h>
#include <stdio.h>
#include <stdint.h>
#include <malloc.h>
#include <ctype.h>
#include <zlib.h>
#include "dictionary.h"
struct gniggle_dictionary_hash_e {
char *word;
struct gniggle_dictionary_hash_e *next;
};
struct gniggle_dictionary {
unsigned int nwords; /* number of words loaded */
unsigned int gx; /* grid width */
unsigned int gy; /* grid height */
unsigned int hashsize; /* number of hash buckets */
struct gniggle_dictionary_hash_e **hash; /* hash table */
};
struct gniggle_dictionary_iter {
struct gniggle_dictionary *dict;/* dictionary we're iterating */
unsigned int bucket; /* current bucket */
struct gniggle_dictionary_hash_e *entry; /* last hash entry */
};
char *gniggle_dictionary_trim_qu(const char *word)
{
size_t i, len = strlen(word);
char *r = calloc(len + 1, 1);
char *p = r;
for (i = 0; i < strlen(word); i++) {
*p++ = word[i];
if (word[i] == 'q')
i++;
}
return r;
}
char *gniggle_dictionary_restore_qu(const char *word)
{
size_t qs = 0, i;
char *r;
char *b;
for (i = 0; i < strlen(word); i++)
if (word[i] == 'q')
qs++;
if (qs == 0)
return strdup(word);
r = calloc(strlen(word) + qs + 1, 1);
b = r;
for (i = 0; i < strlen(word); i++) {
*b = word[i];
if (*b == 'q') {
*(b + 1) = 'u';
b++;
}
b++;
}
return r;
}
bool gniggle_dictionary_word_qualifies(const char *word,
const int maxlen)
{
char *qu;
int i, len = strlen(word);
/* Word is at least three characters long */
if (len < 3)
return false;
/* Word is entirely lower-case */
for (i = 0; i < len; i++)
if (isupper(word[i]))
return false;
/* If word contains a Q, it is followed by a U */
for (i = 0; i < len; i++)
if (word[i] == 'q' && word[i + 1] != 'u')
return false;
/* Word is made up only of letters (no punctuation) */
for (i = 0; i < len; i++)
if (isalpha(word[i]) == 0)
return false;
/* Convert qu to just q when considering other tests */
qu = gniggle_dictionary_trim_qu(word);
len = strlen(qu);
free(qu);
/* Word is not longer than the maximum length (which is the total
* number of letters on the board, if by some marvel they can all be
* be used to spell a word
*/
if (len > maxlen)
return false;
return true;
}
unsigned int gniggle_dictionary_fnv(const char *word)
{
unsigned int z = 0x01000193;
if (word == NULL)
return 0;
while (*word) {
z *= 0x01000193;
z ^= *word++;
}
return z;
}
struct gniggle_dictionary *gniggle_dictionary_new(const unsigned int x,
const unsigned int y,
const unsigned int hashsize)
{
struct gniggle_dictionary *r = calloc(sizeof(struct gniggle_dictionary),
1);
r->gx = x;
r->gy = y;
r->hashsize = (hashsize == 0) ? 7919 : hashsize;
r->hash = calloc(sizeof(struct gniggle_dictionary_hash_e **),
r->hashsize);
return r;
}
void gniggle_dictionary_delete(struct gniggle_dictionary *dict)
{
unsigned int i;
for (i = 0; i < dict->hashsize; i++) {
struct gniggle_dictionary_hash_e *e = dict->hash[i];
while (e != NULL) {
struct gniggle_dictionary_hash_e *n = e;
free(e->word);
e = e->next;
free(n);
}
}
free(dict->hash);
free(dict);
}
void gniggle_dictionary_add(struct gniggle_dictionary *dict,
const char *word)
{
char *nqu;
if (gniggle_dictionary_word_qualifies(word, dict->gx * dict->gy)
== false)
return;
nqu = gniggle_dictionary_trim_qu(word);
if (gniggle_dictionary_lookup(dict, nqu) == false) {
struct gniggle_dictionary_hash_e *e = calloc(
sizeof(struct gniggle_dictionary_hash_e), 1);
unsigned int hash = gniggle_dictionary_fnv(nqu);
unsigned int bucket = hash % (dict->hashsize);
e->word = nqu;
e->next = dict->hash[bucket];
dict->hash[bucket] = e;
dict->nwords++;
} else {
free(nqu);
}
}
bool gniggle_dictionary_lookup(struct gniggle_dictionary *dict,
const char *word)
{
unsigned int hash = gniggle_dictionary_fnv(word);
unsigned int bucket = hash % (dict->hashsize);
struct gniggle_dictionary_hash_e *e = dict->hash[bucket];
while (e != NULL) {
if (strcmp(word, e->word) == 0)
return true;
e = e->next;
}
return false;
}
unsigned int gniggle_dictionary_size(struct gniggle_dictionary *dict)
{
return dict->nwords;
}
struct gniggle_dictionary_iter *gniggle_dictionary_iterator(
struct gniggle_dictionary *dict)
{
struct gniggle_dictionary_iter *r = calloc(
sizeof(struct gniggle_dictionary_iter), 1);
r->dict = dict;
r->bucket = 0;
r->entry = dict->hash[0];
return r;
}
const char *gniggle_dictionary_next(struct gniggle_dictionary_iter *iter)
{
const char *r;
while (iter->entry == NULL) {
iter->bucket += 1;
if (iter->bucket == iter->dict->hashsize)
return NULL;
iter->entry = iter->dict->hash[iter->bucket];
}
r = iter->entry->word;
iter->entry = iter->entry->next;
return r;
}
void gniggle_dictionary_iterator_delete(struct gniggle_dictionary_iter *iter)
{
free(iter);
}
int gniggle_dictionary_dump(struct gniggle_dictionary *dict,
const char *filename)
{
gzFile fh = gzopen(filename, "wb");
# define WRITE(d, s) gzwrite(fh, (d), (s))
size_t l;
uint32_t magic = 0x12345678;
unsigned char fl;
if (fh == NULL)
return -1;
gzsetparams(fh, Z_BEST_COMPRESSION, Z_DEFAULT_STRATEGY);
WRITE("GNIGDICT", 8); /* identifier */
WRITE(&magic, sizeof(magic)); /* endian detection word */
fl = sizeof(unsigned char);
WRITE(&fl, sizeof(fl)); /* size of unsigned char */
fl = sizeof(unsigned short);
WRITE(&fl, sizeof(fl)); /* size of short */
WRITE(&dict->nwords, sizeof(dict->nwords));
WRITE(&dict->hashsize, sizeof(dict->hashsize));
for (l = 0; l < dict->hashsize; l++) {
unsigned short chainsize = 0;
struct gniggle_dictionary_hash_e *e = dict->hash[l];
while (e != NULL) {
chainsize++;
e = e->next;
}
WRITE(&chainsize, sizeof(chainsize));
if (chainsize != 0) {
unsigned char sl;
e = dict->hash[l];
while (e != NULL) {
sl = strlen(e->word);
WRITE(&sl, sizeof(sl));
WRITE(e->word, sl);
e = e->next;
}
}
}
gzclose(fh);
#undef WRITE
return 0;
}
struct gniggle_dictionary *gniggle_dictionary_undump(const unsigned x,
const unsigned y, const char *filename)
{
gzFile fh = gzopen(filename, "rb");
# define READ(p, s) gzread(fh, (p), (s))
uint32_t magic;
unsigned char fl;
char head[8];
size_t l;
struct gniggle_dictionary *d;
if (fh == NULL)
return NULL;
READ(head, 8);
if (strncmp(head, "GNIGDICT", 8) != 0) {
gzclose(fh);
return NULL;
}
READ(&magic, sizeof(uint32_t));
if (magic != 0x12345678) {
gzclose(fh);
return NULL;
}
READ(&fl, sizeof(fl));
if (fl != sizeof(unsigned char)) {
gzclose(fh);
return NULL;
}
READ(&fl, sizeof(fl));
if (fl != sizeof(unsigned short)) {
gzclose(fh);
return NULL;
}
d = calloc(sizeof(struct gniggle_dictionary), 1);
READ(&d->nwords, sizeof(d->nwords));
READ(&d->hashsize, sizeof(d->hashsize));
d->gx = x;
d->gy = y;
d->hash = calloc(sizeof(struct gniggle_dictionary_hash_e **),
d->hashsize);
for (l = 0; l < d->hashsize; l++) {
unsigned short chainsize;
int w;
READ(&chainsize, sizeof(chainsize));
for (w = 0; w < chainsize; w++) {
struct gniggle_dictionary_hash_e *e;
unsigned char wl;
e = calloc(sizeof(
struct gniggle_dictionary_hash_e), 1);
READ(&wl, sizeof(wl));
e->word = calloc(wl + 1, 1);
READ(e->word, wl);
e->next = d->hash[l];
d->hash[l] = e;
}
}
gzclose(fh);
#undef READ
return d;
}
struct gniggle_dictionary *gniggle_dictionary_new_from_file(
const unsigned int x,
const unsigned int y,
const unsigned int hashsize,
const char *filename)
{
FILE *fh = fopen(filename, "rb");
unsigned char head[2];
if (fh == NULL)
return NULL;
if (fread(head, 2, 1, fh) < 1) {
fclose(fh);
return NULL;
}
fclose(fh);
if (head[0] == 0x1f && head[1] == 0x8b) { /* gzip header? */
return gniggle_dictionary_undump(x, y, filename);
} else {
struct gniggle_dictionary *d;
d = gniggle_dictionary_new(x, y, hashsize);
gniggle_dictionary_load_file(d, filename);
return d;
}
return NULL;
}
int gniggle_dictionary_load_file(struct gniggle_dictionary *dict,
const char *filename)
{
FILE *fh = fopen(filename, "r");
char word[BUFSIZ];
int count = 0;
if (fh == NULL)
return -1;
while (!feof(fh)) {
if (fscanf(fh, "%s", word) < 1)
break;
gniggle_dictionary_add(dict, word);
count++;
}
fclose(fh);
return count;
}
#ifdef TEST_RIG
#include <stdio.h>
int main(int argc, char *argv[])
{
struct gniggle_dictionary *dict = gniggle_dictionary_new(10, 10, 0);
struct gniggle_dictionary_iter *iter;
unsigned char word[BUFSIZ];
const char *iterword;
FILE *d = fopen("./word.list", "r");
while (!feof(d)) {
fscanf(d, "%s", word);
gniggle_dictionary_add(dict, word);
}
fclose(d);
iter = gniggle_dictionary_iterator(dict);
do {
iterword = gniggle_dictionary_next(iter);
if (iterword != NULL) {
printf("%s\n", iterword);
}
} while (iterword != NULL);
gniggle_dictionary_iterator_delete(iter);
gniggle_dictionary_delete(dict);
return 0;
}
#endif