-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathd2_2.c
319 lines (282 loc) · 10.1 KB
/
d2_2.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
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
/*======================================================================
* Implementation of d2 dictionary
*======================================================================*/
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include "mem.h"
#include "d2.h"
#include "d2file.h" /* definition of file layout */
#define DEFAULT_STUB "Words:Words"
/*--- Definitions of structures half-declared in d2.h */
struct _Dictl {
int len;
Dict dict; /* Optional parent */
int nhead; /* How many 2-char heads */
D2fsect *hindex; /* Array of head data */
char *tails; /* char[][len-2] */
};
struct _Dict {
char *filestub;
Dictl dictl[MAXLEN+1]; /* Children managed */
int used[MAXLEN+1];
};
struct _Dscan {
Dictl dictl;
int len2;
char *word;
int hcount;
int tindex;
int tend;
D2fsect *hptr;
};
/*----------------------------------------------------------------------
* report an error and die
*----------------------------------------------------------------------*/
#define faile(str) (perror(str), exit(1))
static void fail(const char *fmt, ...)
{
va_list ap;
va_start(ap,fmt);
vfprintf(stderr, fmt, ap);
va_end(ap);
fputc('\n', stderr);
exit(1);
}
/*======================================================================
* class Dictl
* Controls part of a Dict dealing with words of a specific length.
* No per-object data, so just use the reference counts in Dict.
* Reads the data out of the file, which has the format:
* char[2] = "d2" magic number
* short len length of words
* int nhead number of head pairs
* ( char head[2] first two letters of word(s)
* nhead * ( 2 bytes padding
* ( int tend cumulative number of tails
* char tails[ntail][len-2] tails of words
*======================================================================*/
/*----------------------------------------------------------------------
* Destroy a Dictl. Called from Dict_close.
*----------------------------------------------------------------------*/
static void Dictl_delete(Dictl dl)
{
free(dl->hindex);
free(dl->tails);
free(dl);
}
/*----------------------------------------------------------------------
* Access a length dictionary from a file
*----------------------------------------------------------------------*/
Dictl Dictl_fopen(const char *filename)
{
Dictl dl;
FILE *f;
int ntail;
D2fhdr hdr;
dl = NEW(struct _Dictl);
dl->dict = NULL; /* No parent (yet) */
f = fopen(filename, "rb"); /* open it for reading */
if (f==NULL) fail("%s: Unable to open file\n", filename);
if (fread(&hdr, sizeof(D2fhdr), 1, f) < 1)
faile(filename);
if (strncmp(hdr.id, "d2", 2) != 0)
fail("%s: bad magic number", filename);
if ((unsigned)hdr.nhead > 26*26)
fail("%s: bad head count %d", filename, hdr.nhead);
dl->len = hdr.len;
dl->nhead = hdr.nhead;
dl->hindex = NEWARRAY(D2fsect, dl->nhead);
if (fread(dl->hindex, sizeof(D2fsect), dl->nhead, f) < dl->nhead)
faile(filename);
ntail = (dl->nhead == 0) ? 0 : dl->hindex[dl->nhead - 1].tend;
dl->tails = NEWARRAY(char, ntail*(hdr.len-2));
if (fread(dl->tails, sizeof(char), ntail*(hdr.len-2), f) < ntail*(hdr.len-2))
faile(filename);
fclose(f);
return dl;
}
/*----------------------------------------------------------------------
* Access a length part of a Dict
*----------------------------------------------------------------------*/
Dictl Dictl_open(Dict d, int len)
{
Dictl dl;
if (len < 2 || len > MAXLEN)
fail("Dictl_open: length %d is out of range [2:%d]", len, MAXLEN);
if ((dl = d->dictl[len]) == NULL) { /* not already cached */
char *fname = malloc(strlen(d->filestub)+2+1);
sprintf(fname, "%s%02d", d->filestub, len); /* make filename from stub */
dl = d->dictl[len] = Dictl_fopen(fname);
if (dl->len != len) fail("%s: bad word length %d", fname, dl->len);
dl->dict = d;
free(fname);
}
++ d->used[len];
return dl;
}
/*----------------------------------------------------------------------
* Relinquish use of a Dictl, but hang on to it internally in case
* it's needed again (saves re-reading the file).
*----------------------------------------------------------------------*/
void Dictl_close(Dictl dl)
{
if (dl->dict != NULL)
-- dl->dict->used[dl->len]; /* decrement usage count */
}
/*----------------------------------------------------------------------
* Return the number of items in a Dictl
*----------------------------------------------------------------------*/
int Dictl_length(Dictl dl)
{
return dl->hindex[dl->nhead - 1].tend;
}
/*----------------------------------------------------------------------
* Check if a word is in the given dictionary part
*----------------------------------------------------------------------*/
/*#define c2cmp(s,t) strncmp(s,t,2)*/
#ifdef _GNUC_
#define c2cmp(s,t) (((int)(s)[0]-(t)[0]) ?: ((int)(s)[1]-(int)(t)[1]))
#else
#define c2cmp(s,t) (((int)(s)[0]-(t)[0]) ? ((int)(s)[0]-(int)(t)[0]) : ((int)(s)[1]-(int)(t)[1]))
#endif
int Dictl_contains(Dictl dl, const char *word)
{
int i,j,k, cmp, len2;
for (i=0, j=dl->nhead; i < j; ) {
k = (i+j)/2; /* binary chop */
cmp = c2cmp(word, dl->hindex[k].head);
if (cmp < 0) j = k; /* try in lower half */
else if (cmp > 0) i = k+1; /* try in upper half */
else break; /* exact match */
}
if (i == j) return 0; /* head not found */
len2 = dl->len-2;
if (len2 <= 0) return 1; /* len=1,2: no tail */
for (i=(k==0)?0: dl->hindex[k-1].tend, j=dl->hindex[k].tend; i<j; ) {
k = (i+j)/2; /* binary chop */
cmp = strncmp(word+2, dl->tails+k*len2, len2);
if (cmp < 0) j = k; /* try in lower half */
else if (cmp > 0) i = k+1; /* try in upper half */
else break; /* exact match */
}
return i < j; /* true if tail found */
}
/*======================================================================
* class Dict
* Controls a set of dictionary files (one for each word length).
* The Dictl objects are cached so they can be re-used when needed.
* If memory is restricted, it may be better (a) to delete a Dictl
* when its `used' count has fallen to zero; or (b) delete unused
* Dictls selectively if we run out of space making a new one.
*======================================================================*/
/*--------------------------------------------------------------*
* If we've run out of free memory, de-allocate any Dictl
* entries that are unused at present
*--------------------------------------------------------------*/
static int Dict_freer(void *vdict, size_t size)
{
Dict d = vdict;
int i;
for (i=0; i<=MAXLEN; i++) {
if (d->dictl[i] != NULL && d->used[i] == 0) {
/* fprintf(stderr, "Dict_freer(%d): freeing dictl[%d]\n", size, i);*/
Dictl_delete(d->dictl[i]);
d->dictl[i] = NULL;
return 1;
}
}
return 0;
}
/*----------------------------------------------------------------------
* Create a dictionary from a given file stub (NULL => default)
*----------------------------------------------------------------------*/
Dict Dict_open(const char *stub)
{
int i;
Dict d = NEW(struct _Dict);
d->filestub = strdup(stub? stub: (const char *)DEFAULT_STUB); /* copy the file stub */
for (i=0; i<=MAXLEN; i++) {
d->dictl[i] = NULL; /* no Dictl objects yet */
d->used[i] = NULL; /* so not used */
}
Mem_add_freer(Dict_freer, d);
return d;
}
/*----------------------------------------------------------------------
* Finish using a Dict
*----------------------------------------------------------------------*/
void Dict_close(Dict d)
{
int i;
static void Dictl_delete(Dictl dl);
Mem_remove_freer(Dict_freer, d);
for (i=0; i<=MAXLEN; i++) { /* remove all Dictls */
if (d->dictl[i] != NULL) Dictl_delete(d->dictl[i]);
}
free(d->filestub);
free(d);
}
/*----------------------------------------------------------------------
* Test if a given word is in the dictionary
*----------------------------------------------------------------------*/
int Dict_isword(Dict d, const char *word, int len)
{
int result;
Dictl dl = Dictl_open(d, len); /* open section for this length */
result = Dictl_isword(dl, word); /* check the word */
Dictl_close(dl);
return result;
}
/*======================================================================
* class Dscan
* Allows a serial scan through a Dictl.
*======================================================================*/
Dscan Dscan_open(Dict d, int len)
{
Dscan ds = NEW(struct _Dscan);
ds->dictl = Dictl_open(d, len);
ds->len2 = len - 2; /* length of tails */
ds->word = NEWARRAY(char,len + 1); /* space for words returned */
ds->word[len] = '\0'; /* drop in a terminator */
ds->hcount = ds->dictl->nhead; /* number of heads untried */
ds->hptr = ds->dictl->hindex; /* point to start of `file' */
ds->tindex = 0;
ds->tend = 0; /* need to access next `block' */
return ds;
}
/*----------------------------------------------------------------------
* Return the next word in the scan.
*----------------------------------------------------------------------*/
const char *Dscan_read(Dscan ds)
{
if (ds->tindex == ds->tend) { /* at end of current head */
if (ds->hcount == 0) return NULL; /* no more to read */
ds->word[0] = ds->hptr->head[0]; /* fetch next head */
ds->word[1] = ds->hptr->head[1];
ds->tend = ds->hptr->tend; /* mark end of next block */
ds->hptr++;
ds->hcount--;
}
strncpy(ds->word+2, ds->dictl->tails+(ds->tindex++ * ds->len2),
ds->len2); /* copy the tail data */
return ds->word;
}
/*----------------------------------------------------------------------
* Hint to Dscan_read that we are not interested in words which
* begin with the same <skip> characters as the last word returned.
*----------------------------------------------------------------------*/
void Dscan_skip(Dscan ds, int skip)
{
if (skip < 2) ds->tindex = ds->tend; /* ignore rest of current `block' */
}
/*----------------------------------------------------------------------
* Terminate a scan
*----------------------------------------------------------------------*/
void Dscan_close(Dscan ds)
{
Dictl_close(ds->dictl);
free(ds->word);
free(ds);
}