X-Git-Url: https://git.distorted.org.uk/~mdw/sgt/agedu/blobdiff_plain/522edd92f2bbef89ccd1dd0a3e874516fb47e2ef..1d3a7ff684c36162124901504e62a4c6d48ead2f:/index.c diff --git a/index.c b/index.c index db3d2b3..f4b9801 100644 --- a/index.c +++ b/index.c @@ -9,9 +9,6 @@ #define alignof(typ) ( offsetof(struct { char c; typ t; }, t) ) -#define min(x,y) ((x)<(y) ? (x):(y)) -#define max(x,y) ((x)>(y) ? (x):(y)) - #define PADDING(x, mod) ( ((mod) - ((x) % (mod))) % (mod) ) struct avlnode { @@ -52,7 +49,7 @@ static int index_maxdepth(int nodecount) off_t index_initial_size(off_t currentsize, int nodecount) { currentsize += PADDING(currentsize, alignof(off_t)); - currentsize += nodecount + sizeof(off_t); + currentsize += nodecount * sizeof(off_t); currentsize += PADDING(currentsize, alignof(struct avlnode)); return currentsize; @@ -72,11 +69,11 @@ struct indexbuild { }; #define ELEMENT(t,offset) \ - ((offset) ? (struct trie_file *)((char *)(t) + (offset)) : NULL) + ((struct trie_file *) ((offset) ? ((char *)(t) + (offset)) : NULL)) #define NODE(t,offset) \ - ((offset) ? (struct avlnode *)((char *)(t) + (offset)) : NULL) + ((struct avlnode *) ((offset) ? ((char *)(t) + (offset)) : NULL)) #define OFFSET(t,node) \ - ((node) ? (off_t)((const char *)node - (const char *)t) : 0) + ((node) ? (off_t)((const char *)node - (const char *)t) : (off_t)0) #define MAXDEPTH(node) ((node) ? (node)->maxdepth : 0) indexbuild *indexbuild_new(void *t, off_t startoff, int nodecount, @@ -269,7 +266,9 @@ void indexbuild_rebase(indexbuild *ib, void *t) ib->t = t; ib->nodes = (struct avlnode *)((unsigned char *)ib->nodes + diff); ib->roots = (off_t *)((unsigned char *)ib->roots + diff); - ib->currroot = (struct avlnode *)((unsigned char *)ib->currroot + diff); + if (ib->currroot) + ib->currroot = (struct avlnode *) + ((unsigned char *)ib->currroot + diff); ib->firstmutable = (struct avlnode *)((unsigned char *)ib->firstmutable + diff); } @@ -284,6 +283,19 @@ void indexbuild_free(indexbuild *ib) sfree(ib); } +int index_has_root(const void *t, int n) +{ + const off_t *roots; + + roots = (const off_t *)((const char *)t + trie_get_index_offset(t)); + + if (n == 0) + return 1; + if (n < 0 || n >= trie_count(t) || !roots[n-1]) + return 0; + return 1; +} + unsigned long long index_query(const void *t, int n, unsigned long long at) { const off_t *roots;