| 1 |
/* |
| 2 |
* index.c: create and collate index data structures |
| 3 |
*/ |
| 4 |
|
| 5 |
#include <stdio.h> |
| 6 |
#include <stdlib.h> |
| 7 |
#include "halibut.h" |
| 8 |
|
| 9 |
static int compare_tags(void *av, void *bv); |
| 10 |
static int compare_entries(void *av, void *bv); |
| 11 |
|
| 12 |
indexdata *make_index(void) |
| 13 |
{ |
| 14 |
indexdata *ret = mknew(indexdata); |
| 15 |
ret->tags = newtree234(compare_tags); |
| 16 |
ret->entries = newtree234(compare_entries); |
| 17 |
return ret; |
| 18 |
} |
| 19 |
|
| 20 |
static indextag *make_indextag(void) |
| 21 |
{ |
| 22 |
indextag *ret = mknew(indextag); |
| 23 |
ret->name = NULL; |
| 24 |
ret->implicit_text = NULL; |
| 25 |
ret->explicit_texts = NULL; |
| 26 |
ret->nexplicit = ret->explicit_size = ret->nrefs = 0; |
| 27 |
ret->refs = NULL; |
| 28 |
return ret; |
| 29 |
} |
| 30 |
|
| 31 |
static int compare_tags(void *av, void *bv) |
| 32 |
{ |
| 33 |
indextag *a = (indextag *) av, *b = (indextag *) bv; |
| 34 |
return ustricmp(a->name, b->name); |
| 35 |
} |
| 36 |
|
| 37 |
static int compare_to_find_tag(void *av, void *bv) |
| 38 |
{ |
| 39 |
wchar_t *a = (wchar_t *) av; |
| 40 |
indextag *b = (indextag *) bv; |
| 41 |
return ustricmp(a, b->name); |
| 42 |
} |
| 43 |
|
| 44 |
static int compare_entries(void *av, void *bv) |
| 45 |
{ |
| 46 |
indexentry *a = (indexentry *) av, *b = (indexentry *) bv; |
| 47 |
return compare_wordlists(a->text, b->text); |
| 48 |
} |
| 49 |
|
| 50 |
/* |
| 51 |
* Back-end utility: find the indextag with a given name. |
| 52 |
*/ |
| 53 |
indextag *index_findtag(indexdata * idx, wchar_t * name) |
| 54 |
{ |
| 55 |
return find234(idx->tags, name, compare_to_find_tag); |
| 56 |
} |
| 57 |
|
| 58 |
/* |
| 59 |
* Add a \IM. `tags' points to a zero-terminated chain of |
| 60 |
* zero-terminated strings ("first\0second\0thirdandlast\0\0"). |
| 61 |
* `text' points to a word list. |
| 62 |
* |
| 63 |
* Guarantee on calling sequence: all implicit merges are given |
| 64 |
* before the explicit ones. |
| 65 |
*/ |
| 66 |
void |
| 67 |
index_merge(indexdata * idx, int is_explicit, wchar_t * tags, word * text) |
| 68 |
{ |
| 69 |
indextag *t, *existing; |
| 70 |
|
| 71 |
/* |
| 72 |
* FIXME: want to warn on overlapping source sets. |
| 73 |
*/ |
| 74 |
for (; *tags; tags = uadv(tags)) |
| 75 |
{ |
| 76 |
t = make_indextag(); |
| 77 |
t->name = tags; |
| 78 |
existing = add234(idx->tags, t); |
| 79 |
if (existing == t) |
| 80 |
{ |
| 81 |
/* |
| 82 |
* Duplicate this so we can free it independently. |
| 83 |
*/ |
| 84 |
t->name = ustrdup(tags); |
| 85 |
|
| 86 |
/* |
| 87 |
* Every tag has an implicit \IM. So if this tag |
| 88 |
* doesn't exist and we're explicit, then we should |
| 89 |
* warn (and drop it, since it won't be referenced). |
| 90 |
*/ |
| 91 |
if (is_explicit) |
| 92 |
{ |
| 93 |
error(err_nosuchidxtag, tags); |
| 94 |
continue; |
| 95 |
} |
| 96 |
|
| 97 |
/* |
| 98 |
* Otherwise, this is a new tag with an implicit \IM. |
| 99 |
*/ |
| 100 |
t->implicit_text = text; |
| 101 |
} else |
| 102 |
{ |
| 103 |
sfree(t); |
| 104 |
t = existing; |
| 105 |
if (!is_explicit) |
| 106 |
{ |
| 107 |
/* |
| 108 |
* An implicit \IM for a tag that's had an implicit |
| 109 |
* \IM before. FIXME: we should check the text |
| 110 |
* against the existing text and warn on |
| 111 |
* differences. And check the tag for case match |
| 112 |
* against the existing tag, likewise. |
| 113 |
*/ |
| 114 |
} else |
| 115 |
{ |
| 116 |
/* |
| 117 |
* An explicit \IM added to a valid tag. In |
| 118 |
* particular, this removes the implicit \IM if |
| 119 |
* present. |
| 120 |
*/ |
| 121 |
if (t->implicit_text) |
| 122 |
{ |
| 123 |
free_word_list(t->implicit_text); |
| 124 |
t->implicit_text = NULL; |
| 125 |
} |
| 126 |
if (t->nexplicit >= t->explicit_size) |
| 127 |
{ |
| 128 |
t->explicit_size = t->nexplicit + 8; |
| 129 |
t->explicit_texts = resize(t->explicit_texts, t->explicit_size); |
| 130 |
} |
| 131 |
t->explicit_texts[t->nexplicit++] = text; |
| 132 |
} |
| 133 |
} |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
/* |
| 138 |
* Build the final-form index. We now have every tag, with every |
| 139 |
* \IM, set up in a 2-3 tree indexed by tag. We now want to collate |
| 140 |
* the RHSes of the \IMs, and sort by final form, and decorate the |
| 141 |
* entries in the original 2-3 tree with pointers to the RHS |
| 142 |
* entries. |
| 143 |
*/ |
| 144 |
void build_index(indexdata * i) |
| 145 |
{ |
| 146 |
indextag *t; |
| 147 |
word **ta; |
| 148 |
int ti; |
| 149 |
int j; |
| 150 |
|
| 151 |
for (ti = 0; (t = (indextag *) index234(i->tags, ti)) != NULL; ti++) |
| 152 |
{ |
| 153 |
if (t->implicit_text) |
| 154 |
{ |
| 155 |
t->nrefs = 1; |
| 156 |
ta = &t->implicit_text; |
| 157 |
} else |
| 158 |
{ |
| 159 |
t->nrefs = t->nexplicit; |
| 160 |
ta = t->explicit_texts; |
| 161 |
} |
| 162 |
if (t->nrefs) |
| 163 |
{ |
| 164 |
t->refs = mknewa(indexentry *, t->nrefs); |
| 165 |
for (j = 0; j < t->nrefs; j++) |
| 166 |
{ |
| 167 |
indexentry *ent = mknew(indexentry); |
| 168 |
ent->text = *ta++; |
| 169 |
t->refs[j] = add234(i->entries, ent); |
| 170 |
if (t->refs[j] != ent) /* duplicate */ |
| 171 |
sfree(ent); |
| 172 |
} |
| 173 |
} |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
void cleanup_index(indexdata * i) |
| 178 |
{ |
| 179 |
indextag *t; |
| 180 |
indexentry *ent; |
| 181 |
int ti; |
| 182 |
|
| 183 |
for (ti = 0; (t = (indextag *) index234(i->tags, ti)) != NULL; ti++) |
| 184 |
{ |
| 185 |
sfree(t->name); |
| 186 |
free_word_list(t->implicit_text); |
| 187 |
sfree(t->explicit_texts); |
| 188 |
sfree(t->refs); |
| 189 |
sfree(t); |
| 190 |
} |
| 191 |
freetree234(i->tags); |
| 192 |
for (ti = 0; (ent = (indexentry *) index234(i->entries, ti)) != NULL; |
| 193 |
ti++) |
| 194 |
{ |
| 195 |
sfree(ent); |
| 196 |
} |
| 197 |
freetree234(i->entries); |
| 198 |
sfree(i); |
| 199 |
} |
| 200 |
|
| 201 |
static void dbg_prtwordlist(int level, word * w); |
| 202 |
static void dbg_prtmerge(int is_explicit, wchar_t * tag, word * text); |
| 203 |
|
| 204 |
void index_debug(indexdata * i) |
| 205 |
{ |
| 206 |
indextag *t; |
| 207 |
indexentry *y; |
| 208 |
int ti; |
| 209 |
int j; |
| 210 |
|
| 211 |
printf("\nINDEX TAGS\n==========\n\n"); |
| 212 |
for (ti = 0; (t = (indextag *) index234(i->tags, ti)) != NULL; ti++) |
| 213 |
{ |
| 214 |
printf("\n"); |
| 215 |
if (t->implicit_text) |
| 216 |
dbg_prtmerge(0, t->name, t->implicit_text); |
| 217 |
for (j = 0; j < t->nexplicit; j++) |
| 218 |
dbg_prtmerge(1, t->name, t->explicit_texts[j]); |
| 219 |
} |
| 220 |
|
| 221 |
printf("\nINDEX ENTRIES\n=============\n\n"); |
| 222 |
for (ti = 0; (y = (indexentry *) index234(i->entries, ti)) != NULL; ti++) |
| 223 |
{ |
| 224 |
printf("\n"); |
| 225 |
printf("{\n"); |
| 226 |
dbg_prtwordlist(1, y->text); |
| 227 |
printf("}\n"); |
| 228 |
} |
| 229 |
} |
| 230 |
|
| 231 |
static void dbg_prtmerge(int is_explicit, wchar_t * tag, word * text) |
| 232 |
{ |
| 233 |
printf("\\IM: %splicit: \"", is_explicit ? "ex" : "im"); |
| 234 |
for (; *tag; tag++) |
| 235 |
putchar(*tag); |
| 236 |
printf("\" {\n"); |
| 237 |
dbg_prtwordlist(1, text); |
| 238 |
printf("}\n"); |
| 239 |
} |
| 240 |
|
| 241 |
static void dbg_prtwordlist(int level, word * w) |
| 242 |
{ |
| 243 |
for (; w; w = w->next) |
| 244 |
{ |
| 245 |
wchar_t *wp; |
| 246 |
printf("%*sword %d ", level * 4, "", w->type); |
| 247 |
if (w->text) |
| 248 |
{ |
| 249 |
printf("\""); |
| 250 |
for (wp = w->text; *wp; wp++) |
| 251 |
putchar(*wp); |
| 252 |
printf("\""); |
| 253 |
} else |
| 254 |
printf("(no text)"); |
| 255 |
if (w->alt) |
| 256 |
{ |
| 257 |
printf(" alt = {\n"); |
| 258 |
dbg_prtwordlist(level + 1, w->alt); |
| 259 |
printf("%*s}", level * 4, ""); |
| 260 |
} |
| 261 |
printf("\n"); |
| 262 |
} |
| 263 |
} |