b044df4018bc7a093f815b76ee434897a93ecb64
2 * trie.c: implementation of trie.h.
11 #include <sys/types.h>
18 #define alignof(typ) ( offsetof(struct { char c; typ t; }, t) )
21 * Compare functions for pathnames. Returns the relative order of
22 * the names, like strcmp; also passes back the offset of the
23 * first differing character if desired.
25 static int trieccmp(unsigned char a
, unsigned char b
)
27 a
= (a
== '\0' ?
'\0' : a
== pathsep ?
'\1' : a
+1);
28 b
= (b
== '\0' ?
'\0' : b
== pathsep ?
'\1' : b
+1);
29 return (int)a
- (int)b
;
32 static int triencmp(const char *a
, size_t alen
,
33 const char *b
, size_t blen
, int *offset
)
36 while (off
< alen
&& off
< blen
&& a
[off
] == b
[off
])
40 if (off
== alen
|| off
== blen
) return (off
== blen
) - (off
== alen
);
41 return trieccmp(a
[off
], b
[off
]);
44 static int triecmp(const char *a
, const char *b
, int *offset
)
46 return triencmp(a
, strlen(a
), b
, strlen(b
), offset
);
49 /* ----------------------------------------------------------------------
50 * Trie node structures.
52 * The trie format stored in the file consists of three distinct
53 * node types, each with a distinguishing type field at the start.
55 * TRIE_LEAF is a leaf node; it contains an actual trie_file
56 * structure, and indicates that when you're searching down the
57 * trie with a string, you should now expect to encounter
60 * TRIE_SWITCH indicates that the set of strings in the trie
61 * include strings with more than one distinct character after the
62 * prefix leading up to this point. Hence, it stores multiple
63 * subnode pointers and a different single character for each one.
65 * TRIE_STRING indicates that at this point everything in the trie
66 * has the same next few characters; it stores a single mandatory
67 * string fragment and exactly one subnode pointer.
70 TRIE_LEAF
= 0x7fffe000,
79 struct trie_switchentry
{
86 struct trie_file file
;
92 * sw[0] to sw[len-1] give the subnode pointers and element
93 * counts. At &sw[len] is stored len single bytes which are
94 * the characters corresponding to each subnode.
97 struct trie_switchentry sw
[];
101 struct trie_common c
;
109 off_t root
, indexroot
;
115 /* Union only used for computing alignment */
117 struct trie_leaf leaf
;
118 struct { /* fake trie_switch with indeterminate array length filled in */
119 struct trie_common c
;
121 struct trie_switchentry sw
[1];
123 struct { /* fake trie_string with indeterminate array length filled in */
124 struct trie_common c
;
130 #define TRIE_MAGIC 0x75646761UL
131 #define TRIE_ALIGN alignof(union trie_node)
133 /* ----------------------------------------------------------------------
134 * Trie-building functions.
148 int lastlen
, lastsize
;
150 struct tbswitch
*switches
;
155 static void tb_seek(triebuild
*tb
, off_t off
)
158 if (lseek(tb
->fd
, off
, SEEK_SET
) < 0) {
159 fprintf(stderr
, "agedu: lseek: %s\n", strerror(errno
));
164 static void tb_write(triebuild
*tb
, const void *buf
, size_t len
)
168 int ret
= write(tb
->fd
, buf
, len
);
170 fprintf(stderr
, "agedu: write: %s\n", strerror(errno
));
174 buf
= (const void *)((const char *)buf
+ ret
);
178 static char trie_align_zeroes
[TRIE_ALIGN
];
180 static void tb_align(triebuild
*tb
)
182 int off
= (TRIE_ALIGN
- ((tb
)->offset
% TRIE_ALIGN
)) % TRIE_ALIGN
;
183 tb_write(tb
, trie_align_zeroes
, off
);
186 triebuild
*triebuild_new(int fd
)
188 triebuild
*tb
= snew(triebuild
);
189 struct trie_header th
;
193 tb
->lastlen
= tb
->lastsize
= 0;
199 th
.magic
= TRIE_MAGIC
;
200 th
.root
= th
.count
= 0;
203 th
.pathsep
= (unsigned char)pathsep
;
206 tb_write(tb
, &th
, sizeof(th
));
211 static off_t
triebuild_unwind(triebuild
*tb
, int targetdepth
, int *outcount
)
216 if (tb
->lastoff
== 0) {
221 offset
= tb
->lastoff
;
223 depth
= tb
->lastlen
+ 1;
225 assert(depth
>= targetdepth
);
227 while (depth
> targetdepth
) {
229 while (depth
> targetdepth
&&
230 (depth
-1 > tb
->switchsize
|| tb
->switches
[depth
-1].len
== 0))
232 if (odepth
> depth
) {
234 * Write out a string node.
236 size_t nodesize
= sizeof(struct trie_string
) + odepth
- depth
;
237 struct trie_string
*st
= (struct trie_string
*)smalloc(nodesize
);
238 st
->c
.type
= TRIE_STRING
;
239 st
->stringlen
= odepth
- depth
;
240 st
->subnode
= offset
;
241 memcpy(st
->string
, tb
->lastpath
+ depth
, odepth
- depth
);
244 tb_write(tb
, st
, nodesize
);
248 assert(depth
>= targetdepth
);
249 if (depth
<= targetdepth
)
253 * Now we expect to be sitting just below a switch node.
254 * Add our final entry to it and write it out.
258 struct trie_switch
*sw
;
261 int swlen
= tb
->switches
[depth
].len
;
266 tb
->switches
[depth
].c
[swlen
] = tb
->lastpath
[depth
];
267 tb
->switches
[depth
].off
[swlen
] = offset
;
268 tb
->switches
[depth
].count
[swlen
] = count
;
271 nodesize
= sizeof(struct trie_switch
) +
272 swlen
* sizeof(struct trie_switchentry
) + swlen
;
273 sw
= (struct trie_switch
*)smalloc(nodesize
);
274 chars
= (char *)&sw
->sw
[swlen
];
276 sw
->c
.type
= TRIE_SWITCH
;
279 for (i
= 0; i
< swlen
; i
++) {
280 sw
->sw
[i
].subnode
= tb
->switches
[depth
].off
[i
];
281 sw
->sw
[i
].subcount
= tb
->switches
[depth
].count
[i
];
282 chars
[i
] = tb
->switches
[depth
].c
[i
];
284 count
+= tb
->switches
[depth
].count
[i
];
289 tb_write(tb
, sw
, nodesize
);
292 tb
->switches
[depth
].len
= 0; /* clear this node */
300 void triebuild_add(triebuild
*tb
, const char *pathname
,
301 const struct trie_file
*file
)
303 int pathlen
= strlen(pathname
);
306 if (tb
->maxpathlen
< pathlen
+1)
307 tb
->maxpathlen
= pathlen
+1;
314 * Find the first differing character between this pathname
315 * and the previous one.
317 int ret
= triecmp(tb
->lastpath
, pathname
, &depth
);
321 * Finalise all nodes above this depth.
323 offset
= triebuild_unwind(tb
, depth
+1, &count
);
326 * Add the final node we just acquired to the switch node
327 * at our chosen depth, creating it if it isn't already
330 if (tb
->switchsize
<= depth
) {
331 int oldsize
= tb
->switchsize
;
332 tb
->switchsize
= depth
* 3 / 2 + 64;
333 tb
->switches
= sresize(tb
->switches
, tb
->switchsize
,
335 while (oldsize
< tb
->switchsize
)
336 tb
->switches
[oldsize
++].len
= 0;
339 tb
->switches
[depth
].c
[tb
->switches
[depth
].len
] = tb
->lastpath
[depth
];
340 tb
->switches
[depth
].off
[tb
->switches
[depth
].len
] = offset
;
341 tb
->switches
[depth
].count
[tb
->switches
[depth
].len
] = count
;
342 tb
->switches
[depth
].len
++;
346 * Write out a leaf node for the new file, and remember its
350 struct trie_leaf leaf
;
352 leaf
.c
.type
= TRIE_LEAF
;
353 leaf
.file
= *file
; /* structure copy */
356 tb
->lastoff
= tb
->offset
;
357 tb_write(tb
, &leaf
, sizeof(leaf
));
361 * Store this pathname for comparison with the next one.
363 if (tb
->lastsize
< pathlen
+1) {
364 tb
->lastsize
= pathlen
* 3 / 2 + 64;
365 tb
->lastpath
= sresize(tb
->lastpath
, tb
->lastsize
, char);
367 strcpy(tb
->lastpath
, pathname
);
368 tb
->lastlen
= pathlen
;
371 int triebuild_finish(triebuild
*tb
)
373 struct trie_header th
;
375 th
.magic
= TRIE_MAGIC
;
376 th
.root
= triebuild_unwind(tb
, 0, &th
.count
);
378 th
.maxpathlen
= tb
->maxpathlen
;
379 th
.pathsep
= (unsigned char)pathsep
;
382 tb_write(tb
, &th
, sizeof(th
));
387 void triebuild_free(triebuild
*tb
)
394 /* ----------------------------------------------------------------------
395 * Querying functions.
398 #define NODE(t, off, type) \
399 ((const struct type *)((const char *)(t) + (off)))
401 size_t trie_maxpathlen(const void *t
)
403 const struct trie_header
*hdr
= NODE(t
, 0, trie_header
);
404 return hdr
->maxpathlen
;
407 unsigned long trie_before(const void *t
, const char *pathname
)
409 const struct trie_header
*hdr
= NODE(t
, 0, trie_header
);
410 int ret
= 0, lastcount
= hdr
->count
;
411 int len
= 1 + strlen(pathname
), depth
= 0;
412 off_t off
= hdr
->root
;
415 const struct trie_common
*node
= NODE(t
, off
, trie_common
);
416 if (node
->type
== TRIE_LEAF
) {
418 ret
+= lastcount
; /* _shouldn't_ happen, but in principle */
420 } else if (node
->type
== TRIE_STRING
) {
421 const struct trie_string
*st
= NODE(t
, off
, trie_string
);
424 int cmp
= triencmp(st
->string
, st
->stringlen
,
425 pathname
+ depth
, len
-depth
, &offset
);
427 if (offset
< st
->stringlen
) {
433 depth
+= st
->stringlen
;
435 } else if (node
->type
== TRIE_SWITCH
) {
436 const struct trie_switch
*sw
= NODE(t
, off
, trie_switch
);
437 const char *chars
= (const char *)&sw
->sw
[sw
->len
];
440 for (i
= 0; i
< sw
->len
; i
++) {
442 int cmp
= trieccmp(pathname
[depth
], c
);
444 ret
+= sw
->sw
[i
].subcount
;
448 off
= sw
->sw
[i
].subnode
;
449 lastcount
= sw
->sw
[i
].subcount
;
460 void trie_getpath(const void *t
, unsigned long n
, char *buf
)
462 const struct trie_header
*hdr
= NODE(t
, 0, trie_header
);
464 off_t off
= hdr
->root
;
467 const struct trie_common
*node
= NODE(t
, off
, trie_common
);
468 if (node
->type
== TRIE_LEAF
) {
469 assert(depth
> 0 && buf
[depth
-1] == '\0');
471 } else if (node
->type
== TRIE_STRING
) {
472 const struct trie_string
*st
= NODE(t
, off
, trie_string
);
474 memcpy(buf
+ depth
, st
->string
, st
->stringlen
);
475 depth
+= st
->stringlen
;
477 } else if (node
->type
== TRIE_SWITCH
) {
478 const struct trie_switch
*sw
= NODE(t
, off
, trie_switch
);
479 const char *chars
= (const char *)&sw
->sw
[sw
->len
];
482 for (i
= 0; i
< sw
->len
; i
++) {
483 if (n
< sw
->sw
[i
].subcount
) {
484 buf
[depth
++] = chars
[i
];
485 off
= sw
->sw
[i
].subnode
;
488 n
-= sw
->sw
[i
].subcount
;
495 unsigned long trie_count(const void *t
)
497 const struct trie_header
*hdr
= NODE(t
, 0, trie_header
);
501 char trie_pathsep(const void *t
)
503 const struct trie_header
*hdr
= NODE(t
, 0, trie_header
);
504 return (char)hdr
->pathsep
;
507 struct triewalk_switch
{
508 const struct trie_switch
*sw
;
509 int pos
, depth
, count
;
513 struct triewalk_switch
*switches
;
514 int nswitches
, switchsize
;
517 triewalk
*triewalk_new(const void *vt
)
519 triewalk
*tw
= snew(triewalk
);
521 tw
->t
= (const char *)vt
;
529 const struct trie_file
*triewalk_next(triewalk
*tw
, char *buf
)
534 if (tw
->nswitches
< 0) {
535 const struct trie_header
*hdr
= NODE(tw
->t
, 0, trie_header
);
542 const struct trie_switch
*sw
;
545 if (tw
->nswitches
== 0) {
546 assert(tw
->count
== NODE(tw
->t
, 0, trie_header
)->count
);
547 return NULL
; /* run out of trie */
550 swpos
= tw
->switches
[tw
->nswitches
-1].pos
;
551 sw
= tw
->switches
[tw
->nswitches
-1].sw
;
552 chars
= (const char *)&sw
->sw
[sw
->len
];
554 if (swpos
< sw
->len
) {
555 depth
= tw
->switches
[tw
->nswitches
-1].depth
;
556 off
= sw
->sw
[swpos
].subnode
;
558 buf
[depth
++] = chars
[swpos
];
559 assert(tw
->count
== tw
->switches
[tw
->nswitches
-1].count
);
560 tw
->switches
[tw
->nswitches
-1].count
+= sw
->sw
[swpos
].subcount
;
561 tw
->switches
[tw
->nswitches
-1].pos
++;
570 const struct trie_common
*node
= NODE(tw
->t
, off
, trie_common
);
571 if (node
->type
== TRIE_LEAF
) {
572 const struct trie_leaf
*lf
= NODE(tw
->t
, off
, trie_leaf
);
574 assert(depth
> 0 && buf
[depth
-1] == '\0');
577 } else if (node
->type
== TRIE_STRING
) {
578 const struct trie_string
*st
= NODE(tw
->t
, off
, trie_string
);
581 memcpy(buf
+ depth
, st
->string
, st
->stringlen
);
582 depth
+= st
->stringlen
;
584 } else if (node
->type
== TRIE_SWITCH
) {
585 const struct trie_switch
*sw
= NODE(tw
->t
, off
, trie_switch
);
586 const char *chars
= (const char *)&sw
->sw
[sw
->len
];
588 if (tw
->nswitches
>= tw
->switchsize
) {
589 tw
->switchsize
= tw
->nswitches
* 3 / 2 + 32;
590 tw
->switches
= sresize(tw
->switches
, tw
->switchsize
,
591 struct triewalk_switch
);
594 tw
->switches
[tw
->nswitches
].sw
= sw
;
595 tw
->switches
[tw
->nswitches
].pos
= 1;
596 tw
->switches
[tw
->nswitches
].depth
= depth
;
597 tw
->switches
[tw
->nswitches
].count
= tw
->count
+ sw
->sw
[0].subcount
;
598 off
= sw
->sw
[0].subnode
;
600 buf
[depth
++] = chars
[0];
605 void triewalk_free(triewalk
*tw
)
611 void trie_set_index_offset(void *t
, off_t ptr
)
613 ((struct trie_header
*)t
)->indexroot
= ptr
;
615 off_t
trie_get_index_offset(const void *t
)
617 return ((const struct trie_header
*)t
)->indexroot
;
620 void make_successor(char *pathbuf
)
622 int len
= strlen(pathbuf
);
623 if (len
> 0 && pathbuf
[len
-1] == pathsep
)
625 pathbuf
[len
] = '\001';
626 pathbuf
[len
+1] = '\0';