2 * trie.c: implementation of trie.h.
11 #include <sys/types.h>
17 #define alignof(typ) ( offsetof(struct { char c; typ t; }, t) )
20 * Compare functions for pathnames. Returns the relative order of
21 * the names, like strcmp; also passes back the offset of the
22 * first differing character if desired.
24 static int trieccmp(unsigned char a
, unsigned char b
)
26 a
= (a
== '\0' ?
'\0' : a
== '/' ?
'\1' : a
+1);
27 b
= (b
== '\0' ?
'\0' : b
== '/' ?
'\1' : b
+1);
31 static int triencmp(const char *a
, size_t alen
,
32 const char *b
, size_t blen
, int *offset
)
35 while (off
< alen
&& off
< blen
&& a
[off
] == b
[off
])
39 if (off
== alen
|| off
== blen
) return (off
== blen
) - (off
== alen
);
40 return trieccmp(a
[off
], b
[off
]);
43 static int triecmp(const char *a
, const char *b
, int *offset
)
45 return triencmp(a
, strlen(a
), b
, strlen(b
), offset
);
48 /* ----------------------------------------------------------------------
49 * Trie node structures.
51 * The trie format stored in the file consists of three distinct
52 * node types, each with a distinguishing type field at the start.
54 * TRIE_LEAF is a leaf node; it contains an actual trie_file
55 * structure, and indicates that when you're searching down the
56 * trie with a string, you should now expect to encounter
59 * TRIE_SWITCH indicates that the set of strings in the trie
60 * include strings with more than one distinct character after the
61 * prefix leading up to this point. Hence, it stores multiple
62 * subnode pointers and a different single character for each one.
64 * TRIE_STRING indicates that at this point everything in the trie
65 * has the same next few characters; it stores a single mandatory
66 * string fragment and exactly one subnode pointer.
69 TRIE_LEAF
= 0x7fffe000,
78 struct trie_switchentry
{
85 struct trie_file file
;
91 * sw[0] to sw[len-1] give the subnode pointers and element
92 * counts. At &sw[len] is stored len single bytes which are
93 * the characters corresponding to each subnode.
96 struct trie_switchentry sw
[];
100 struct trie_common c
;
108 off_t root
, indexroot
;
113 /* Union only used for computing alignment */
115 struct trie_leaf leaf
;
116 struct { /* fake trie_switch with indeterminate array length filled in */
117 struct trie_common c
;
119 struct trie_switchentry sw
[1];
121 struct { /* fake trie_string with indeterminate array length filled in */
122 struct trie_common c
;
128 #define TRIE_MAGIC 0x75646761UL
129 #define TRIE_ALIGN alignof(union trie_node)
131 /* ----------------------------------------------------------------------
132 * Trie-building functions.
146 int lastlen
, lastsize
;
148 struct tbswitch
*switches
;
153 static void tb_seek(triebuild
*tb
, off_t off
)
156 if (lseek(tb
->fd
, off
, SEEK_SET
) < 0) {
157 fprintf(stderr
, "agedu: lseek: %s\n", strerror(errno
));
162 static void tb_write(triebuild
*tb
, const void *buf
, size_t len
)
166 int ret
= write(tb
->fd
, buf
, len
);
168 fprintf(stderr
, "agedu: write: %s\n", strerror(errno
));
172 buf
= (const void *)((const char *)buf
+ ret
);
176 static char trie_align_zeroes
[TRIE_ALIGN
];
178 static void tb_align(triebuild
*tb
)
180 int off
= (TRIE_ALIGN
- ((tb
)->offset
% TRIE_ALIGN
)) % TRIE_ALIGN
;
181 tb_write(tb
, trie_align_zeroes
, off
);
184 triebuild
*triebuild_new(int fd
)
186 triebuild
*tb
= snew(triebuild
);
187 struct trie_header th
;
191 tb
->lastlen
= tb
->lastsize
= 0;
197 th
.magic
= TRIE_MAGIC
;
198 th
.root
= th
.count
= 0;
203 tb_write(tb
, &th
, sizeof(th
));
208 static off_t
triebuild_unwind(triebuild
*tb
, int targetdepth
, int *outcount
)
213 if (tb
->lastoff
== 0) {
218 offset
= tb
->lastoff
;
220 depth
= tb
->lastlen
+ 1;
222 assert(depth
>= targetdepth
);
224 while (depth
> targetdepth
) {
226 while (depth
> targetdepth
&&
227 (depth
-1 > tb
->switchsize
|| tb
->switches
[depth
-1].len
== 0))
229 if (odepth
> depth
) {
231 * Write out a string node.
233 size_t nodesize
= sizeof(struct trie_string
) + odepth
- depth
;
234 struct trie_string
*st
= (struct trie_string
*)smalloc(nodesize
);
235 st
->c
.type
= TRIE_STRING
;
236 st
->stringlen
= odepth
- depth
;
237 st
->subnode
= offset
;
238 memcpy(st
->string
, tb
->lastpath
+ depth
, odepth
- depth
);
241 tb_write(tb
, st
, nodesize
);
245 assert(depth
>= targetdepth
);
246 if (depth
<= targetdepth
)
250 * Now we expect to be sitting just below a switch node.
251 * Add our final entry to it and write it out.
255 struct trie_switch
*sw
;
258 int swlen
= tb
->switches
[depth
].len
;
263 tb
->switches
[depth
].c
[swlen
] = tb
->lastpath
[depth
];
264 tb
->switches
[depth
].off
[swlen
] = offset
;
265 tb
->switches
[depth
].count
[swlen
] = count
;
268 nodesize
= sizeof(struct trie_switch
) +
269 swlen
* sizeof(struct trie_switchentry
) + swlen
;
270 sw
= (struct trie_switch
*)smalloc(nodesize
);
271 chars
= (char *)&sw
->sw
[swlen
];
273 sw
->c
.type
= TRIE_SWITCH
;
276 for (i
= 0; i
< swlen
; i
++) {
277 sw
->sw
[i
].subnode
= tb
->switches
[depth
].off
[i
];
278 sw
->sw
[i
].subcount
= tb
->switches
[depth
].count
[i
];
279 chars
[i
] = tb
->switches
[depth
].c
[i
];
281 count
+= tb
->switches
[depth
].count
[i
];
286 tb_write(tb
, sw
, nodesize
);
289 tb
->switches
[depth
].len
= 0; /* clear this node */
297 void triebuild_add(triebuild
*tb
, const char *pathname
,
298 const struct trie_file
*file
)
300 int pathlen
= strlen(pathname
);
303 if (tb
->maxpathlen
< pathlen
+1)
304 tb
->maxpathlen
= pathlen
+1;
311 * Find the first differing character between this pathname
312 * and the previous one.
314 int ret
= triecmp(tb
->lastpath
, pathname
, &depth
);
318 * Finalise all nodes above this depth.
320 offset
= triebuild_unwind(tb
, depth
+1, &count
);
323 * Add the final node we just acquired to the switch node
324 * at our chosen depth, creating it if it isn't already
327 if (tb
->switchsize
<= depth
) {
328 int oldsize
= tb
->switchsize
;
329 tb
->switchsize
= depth
* 3 / 2 + 64;
330 tb
->switches
= sresize(tb
->switches
, tb
->switchsize
,
332 while (oldsize
< tb
->switchsize
)
333 tb
->switches
[oldsize
++].len
= 0;
336 tb
->switches
[depth
].c
[tb
->switches
[depth
].len
] = tb
->lastpath
[depth
];
337 tb
->switches
[depth
].off
[tb
->switches
[depth
].len
] = offset
;
338 tb
->switches
[depth
].count
[tb
->switches
[depth
].len
] = count
;
339 tb
->switches
[depth
].len
++;
343 * Write out a leaf node for the new file, and remember its
347 struct trie_leaf leaf
;
349 leaf
.c
.type
= TRIE_LEAF
;
350 leaf
.file
= *file
; /* structure copy */
353 tb
->lastoff
= tb
->offset
;
354 tb_write(tb
, &leaf
, sizeof(leaf
));
358 * Store this pathname for comparison with the next one.
360 if (tb
->lastsize
< pathlen
+1) {
361 tb
->lastsize
= pathlen
* 3 / 2 + 64;
362 tb
->lastpath
= sresize(tb
->lastpath
, tb
->lastsize
, char);
364 strcpy(tb
->lastpath
, pathname
);
365 tb
->lastlen
= pathlen
;
368 int triebuild_finish(triebuild
*tb
)
370 struct trie_header th
;
372 th
.magic
= TRIE_MAGIC
;
373 th
.root
= triebuild_unwind(tb
, 0, &th
.count
);
375 th
.maxpathlen
= tb
->maxpathlen
;
378 tb_write(tb
, &th
, sizeof(th
));
383 void triebuild_free(triebuild
*tb
)
390 /* ----------------------------------------------------------------------
391 * Querying functions.
394 #define NODE(t, off, type) \
395 ((const struct type *)((const char *)(t) + (off)))
397 size_t trie_maxpathlen(const void *t
)
399 const struct trie_header
*hdr
= NODE(t
, 0, trie_header
);
400 return hdr
->maxpathlen
;
403 unsigned long trie_before(const void *t
, const char *pathname
)
405 const struct trie_header
*hdr
= NODE(t
, 0, trie_header
);
406 int ret
= 0, lastcount
= hdr
->count
;
407 int len
= 1 + strlen(pathname
), depth
= 0;
408 off_t off
= hdr
->root
;
411 const struct trie_common
*node
= NODE(t
, off
, trie_common
);
412 if (node
->type
== TRIE_LEAF
) {
414 ret
+= lastcount
; /* _shouldn't_ happen, but in principle */
416 } else if (node
->type
== TRIE_STRING
) {
417 const struct trie_string
*st
= NODE(t
, off
, trie_string
);
420 int cmp
= triencmp(st
->string
, st
->stringlen
,
421 pathname
+ depth
, len
-depth
, &offset
);
423 if (offset
< st
->stringlen
) {
429 depth
+= st
->stringlen
;
431 } else if (node
->type
== TRIE_SWITCH
) {
432 const struct trie_switch
*sw
= NODE(t
, off
, trie_switch
);
433 const char *chars
= (const char *)&sw
->sw
[sw
->len
];
436 for (i
= 0; i
< sw
->len
; i
++) {
438 int cmp
= trieccmp(pathname
[depth
], c
);
440 ret
+= sw
->sw
[i
].subcount
;
444 off
= sw
->sw
[i
].subnode
;
445 lastcount
= sw
->sw
[i
].subcount
;
456 void trie_getpath(const void *t
, unsigned long n
, char *buf
)
458 const struct trie_header
*hdr
= NODE(t
, 0, trie_header
);
460 off_t off
= hdr
->root
;
463 const struct trie_common
*node
= NODE(t
, off
, trie_common
);
464 if (node
->type
== TRIE_LEAF
) {
465 assert(depth
> 0 && buf
[depth
-1] == '\0');
467 } else if (node
->type
== TRIE_STRING
) {
468 const struct trie_string
*st
= NODE(t
, off
, trie_string
);
470 memcpy(buf
+ depth
, st
->string
, st
->stringlen
);
471 depth
+= st
->stringlen
;
473 } else if (node
->type
== TRIE_SWITCH
) {
474 const struct trie_switch
*sw
= NODE(t
, off
, trie_switch
);
475 const char *chars
= (const char *)&sw
->sw
[sw
->len
];
478 for (i
= 0; i
< sw
->len
; i
++) {
479 if (n
< sw
->sw
[i
].subcount
) {
480 buf
[depth
++] = chars
[i
];
481 off
= sw
->sw
[i
].subnode
;
484 n
-= sw
->sw
[i
].subcount
;
491 unsigned long trie_count(const void *t
)
493 const struct trie_header
*hdr
= NODE(t
, 0, trie_header
);
497 struct triewalk_switch
{
498 const struct trie_switch
*sw
;
499 int pos
, depth
, count
;
503 struct triewalk_switch
*switches
;
504 int nswitches
, switchsize
;
507 triewalk
*triewalk_new(const void *vt
)
509 triewalk
*tw
= snew(triewalk
);
511 tw
->t
= (const char *)vt
;
519 const struct trie_file
*triewalk_next(triewalk
*tw
, char *buf
)
524 if (tw
->nswitches
< 0) {
525 const struct trie_header
*hdr
= NODE(tw
->t
, 0, trie_header
);
532 const struct trie_switch
*sw
;
535 if (tw
->nswitches
== 0) {
536 assert(tw
->count
== NODE(tw
->t
, 0, trie_header
)->count
);
537 return NULL
; /* run out of trie */
540 swpos
= tw
->switches
[tw
->nswitches
-1].pos
;
541 sw
= tw
->switches
[tw
->nswitches
-1].sw
;
542 chars
= (const char *)&sw
->sw
[sw
->len
];
544 if (swpos
< sw
->len
) {
545 depth
= tw
->switches
[tw
->nswitches
-1].depth
;
546 off
= sw
->sw
[swpos
].subnode
;
548 buf
[depth
++] = chars
[swpos
];
549 assert(tw
->count
== tw
->switches
[tw
->nswitches
-1].count
);
550 tw
->switches
[tw
->nswitches
-1].count
+= sw
->sw
[swpos
].subcount
;
551 tw
->switches
[tw
->nswitches
-1].pos
++;
560 const struct trie_common
*node
= NODE(tw
->t
, off
, trie_common
);
561 if (node
->type
== TRIE_LEAF
) {
562 const struct trie_leaf
*lf
= NODE(tw
->t
, off
, trie_leaf
);
564 assert(depth
> 0 && buf
[depth
-1] == '\0');
567 } else if (node
->type
== TRIE_STRING
) {
568 const struct trie_string
*st
= NODE(tw
->t
, off
, trie_string
);
571 memcpy(buf
+ depth
, st
->string
, st
->stringlen
);
572 depth
+= st
->stringlen
;
574 } else if (node
->type
== TRIE_SWITCH
) {
575 const struct trie_switch
*sw
= NODE(tw
->t
, off
, trie_switch
);
576 const char *chars
= (const char *)&sw
->sw
[sw
->len
];
578 if (tw
->nswitches
>= tw
->switchsize
) {
579 tw
->switchsize
= tw
->nswitches
* 3 / 2 + 32;
580 tw
->switches
= sresize(tw
->switches
, tw
->switchsize
,
581 struct triewalk_switch
);
584 tw
->switches
[tw
->nswitches
].sw
= sw
;
585 tw
->switches
[tw
->nswitches
].pos
= 1;
586 tw
->switches
[tw
->nswitches
].depth
= depth
;
587 tw
->switches
[tw
->nswitches
].count
= tw
->count
+ sw
->sw
[0].subcount
;
588 off
= sw
->sw
[0].subnode
;
590 buf
[depth
++] = chars
[0];
595 void triewalk_free(triewalk
*tw
)
601 void trie_set_index_offset(void *t
, off_t ptr
)
603 ((struct trie_header
*)t
)->indexroot
= ptr
;
605 off_t
trie_get_index_offset(const void *t
)
607 return ((const struct trie_header
*)t
)->indexroot
;