strigram.c

filter: Statementstmt 97.5% 239 / 245 Branchbran 81.8% 144 / 176 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 90.9% 383 / 421 SCAR 0
CC
0
Cov
0%
CRAP 0
« Trigram.xs ↑ Summary blib/lib/Search/Trigram.pm »
1 #include "strigram.h"
2 #include <stdlib.h>
3 #include <string.h>
4  
5 /* --- Internal types --- */
6  
7 typedef struct {
8 uint32_t trigram;
9 uint32_t *doc_ids;
10 uint32_t count;
11 uint32_t cap;
12 } posting_t;
13  
14 typedef struct {
15 uint32_t doc_id;
16 char *text;
17 uint32_t text_len;
18 uint32_t tcount; /* unique trigrams in this doc */
19 int deleted;
20 } doc_entry_t;
21  
22 struct strigram_s {
23 posting_t *postings;
24 uint32_t posting_count;
25 uint32_t posting_cap;
26  
27 doc_entry_t *docs;
28 uint32_t doc_count;
29 uint32_t doc_cap;
30 uint32_t next_id;
31 uint32_t live_count;
32  
33 /* Epoch-based match accumulator: avoids per-search calloc */
34 uint32_t search_epoch;
35 uint32_t *match_epoch;
36 uint32_t *match_count_buf;
37 uint32_t match_cap;
38 };
39  
40 /* --- Trigram extraction --- */
41  
42 1199878 static uint8_t lc_byte(uint8_t c) {
43 1199878 ▶ return (c >= 'A' && c <= 'Z') ? (uint8_t)(c + 32) : c;
Branch: 2 branches
BranchTrueFalseTotal
return (c >= 'A' && c <= 'Z') ? (uint8_t)(c + 32) : c; 940856 259022 1199878
return (c >= 'A' && c <= 'Z') ? (uint8_t)(c + 32) : c; 7 940849 940856
44 }
45  
46 /* Build a lowercased, space-padded copy: " text " */
47 10058 static uint8_t *build_padded(const char *text, uint32_t len) {
48 uint8_t *buf;
49 uint32_t j;
50 10058 buf = (uint8_t *)malloc(len + 2);
51 10058 ▶ if (!buf) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!buf) return NULL; 0 10058 10058
52 10058 buf[0] = ' ';
53 1209936 ▶ for (j = 0; j < len; j++) buf[j + 1] = lc_byte((uint8_t)text[j]);
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < len; j++) buf[j + 1] = lc_byte((uint8_t)text[j]); 1199878 10058 1209936
54 10058 buf[len + 1] = ' ';
55 10058 return buf;
56 }
57  
58 /* Extract trigrams from text: plen=len+2, ntg=len (one per byte). */
59 10060 static uint32_t *extract_trigrams(const char *text, uint32_t len, uint32_t *out_count) {
60 10060 uint32_t ntg = len; /* plen-2 == len */
61 uint8_t *buf;
62 uint32_t *tgs;
63 uint32_t i;
64  
65 10060 ▶ if (ntg == 0) { *out_count = 0; return NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (ntg == 0) { *out_count = 0; return NULL; } 2 10058 10060
66  
67 10058 buf = build_padded(text, len);
68 10058 ▶ if (!buf) { *out_count = 0; return NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (!buf) { *out_count = 0; return NULL; } 0 10058 10058
69  
70 10058 tgs = (uint32_t *)malloc(ntg * sizeof(uint32_t));
71 10058 ▶ if (!tgs) { free(buf); *out_count = 0; return NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (!tgs) { free(buf); *out_count = 0; return NULL; } 0 10058 10058
72  
73 1209936 ▶ for (i = 0; i < ntg; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < ntg; i++) 1199878 10058 1209936
74 1199878 tgs[i] = ((uint32_t)buf[i] << 16) | ((uint32_t)buf[i+1] << 8) | (uint32_t)buf[i+2];
75  
76 10058 free(buf);
77 10058 *out_count = ntg;
78 10058 return tgs;
79 }
80  
81 17897526 static int cmp_u32(const void *a, const void *b) {
82 17897526 uint32_t x = *(const uint32_t *)a;
83 17897526 uint32_t y = *(const uint32_t *)b;
84 17897526 return (x > y) - (x < y);
85 }
86  
87 10058 static uint32_t sort_unique(uint32_t *arr, uint32_t n) {
88 uint32_t w, i;
89 10058 ▶ if (n == 0) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (n == 0) return 0; 0 10058 10058
90 10058 qsort(arr, (size_t)n, sizeof(uint32_t), cmp_u32);
91 10058 w = 1;
92 1199878 ▶ for (i = 1; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i < n; i++) { 1189820 10058 1199878
93 1189820 ▶ if (arr[i] != arr[i - 1]) arr[w++] = arr[i];
Branch: 1 branch
BranchTrueFalseTotal
if (arr[i] != arr[i - 1]) arr[w++] = arr[i]; 189798 1000022 1189820
94 }
95 10058 return w;
96 }
97  
98 /* --- Posting list management --- */
99  
100 140 static uint32_t posting_find(const strigram_t *idx, uint32_t trigram) {
101 140 uint32_t lo = 0, hi = idx->posting_count;
102 577 ▶ while (lo < hi) {
Branch: 1 branch
BranchTrueFalseTotal
while (lo < hi) { 554 23 577
103 554 uint32_t mid = lo + (hi - lo) / 2;
104 554 ▶ if (idx->postings[mid].trigram < trigram) lo = mid + 1;
Branch: 1 branch
BranchTrueFalseTotal
if (idx->postings[mid].trigram < trigram) lo = mid + 1; 187 367 554
105 367 ▶ else if (idx->postings[mid].trigram > trigram) hi = mid;
Branch: 1 branch
BranchTrueFalseTotal
else if (idx->postings[mid].trigram > trigram) hi = mid; 250 117 367
106 117 else return mid;
107 }
108 23 return UINT32_MAX;
109 }
110  
111 199702 static uint32_t posting_find_insert(strigram_t *idx, uint32_t trigram) {
112 199702 uint32_t lo = 0, hi = idx->posting_count;
113 1849106 ▶ while (lo < hi) {
Branch: 1 branch
BranchTrueFalseTotal
while (lo < hi) { 1847149 1957 1849106
114 1847149 uint32_t mid = lo + (hi - lo) / 2;
115 1847149 ▶ if (idx->postings[mid].trigram < trigram) lo = mid + 1;
Branch: 1 branch
BranchTrueFalseTotal
if (idx->postings[mid].trigram < trigram) lo = mid + 1; 1146735 700414 1847149
116 700414 ▶ else if (idx->postings[mid].trigram > trigram) hi = mid;
Branch: 1 branch
BranchTrueFalseTotal
else if (idx->postings[mid].trigram > trigram) hi = mid; 502669 197745 700414
117 197745 else return mid;
118 }
119 1957 ▶ if (idx->posting_count >= idx->posting_cap) {
Branch: 1 branch
BranchTrueFalseTotal
if (idx->posting_count >= idx->posting_cap) { 29 1928 1957
120 29 ▶ uint32_t new_cap = idx->posting_cap ? idx->posting_cap * 2 : 256;
Branch: 1 branch
BranchTrueFalseTotal
uint32_t new_cap = idx->posting_cap ? idx->posting_cap * 2 : 256; 3 26 29
121 29 posting_t *np = (posting_t *)realloc(idx->postings,
122 new_cap * sizeof(posting_t));
123 29 ▶ if (!np) return UINT32_MAX;
Branch: 1 branch
BranchTrueFalseTotal
if (!np) return UINT32_MAX; 0 29 29
124 29 idx->postings = np;
125 29 idx->posting_cap = new_cap;
126 }
127 1957 ▶ if (lo < idx->posting_count) {
Branch: 1 branch
BranchTrueFalseTotal
if (lo < idx->posting_count) { 1521 436 1957
128 1521 memmove(idx->postings + lo + 1,
129 1521 idx->postings + lo,
130 1521 (idx->posting_count - lo) * sizeof(posting_t));
131 }
132 1957 idx->postings[lo].trigram = trigram;
133 1957 idx->postings[lo].doc_ids = NULL;
134 1957 idx->postings[lo].count = 0;
135 1957 idx->postings[lo].cap = 0;
136 1957 idx->posting_count++;
137 1957 return lo;
138 }
139  
140 199702 static int posting_append_doc(posting_t *p, uint32_t doc_id) {
141 199702 ▶ if (p->count >= p->cap) {
Branch: 1 branch
BranchTrueFalseTotal
if (p->count >= p->cap) { 6076 193626 199702
142 6076 ▶ uint32_t new_cap = p->cap ? p->cap * 2 : 4;
Branch: 1 branch
BranchTrueFalseTotal
uint32_t new_cap = p->cap ? p->cap * 2 : 4; 4119 1957 6076
143 6076 uint32_t *np = (uint32_t *)realloc(p->doc_ids,
144 new_cap * sizeof(uint32_t));
145 6076 ▶ if (!np) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!np) return 0; 0 6076 6076
146 6076 p->doc_ids = np;
147 6076 p->cap = new_cap;
148 }
149 199702 p->doc_ids[p->count++] = doc_id;
150 199702 return 1;
151 }
152  
153 /* --- Lifecycle --- */
154  
155 28 strigram_t *strigram_new(void) {
156 28 return (strigram_t *)calloc(1, sizeof(strigram_t));
157 }
158  
159 29 void strigram_clear(strigram_t *idx) {
160 uint32_t i;
161 29 ▶ if (!idx) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!idx) return; 0 29 29
162 10067 ▶ for (i = 0; i < idx->doc_count; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < idx->doc_count; i++) 10038 29 10067
163 10038 free(idx->docs[i].text);
164 29 idx->doc_count = 0;
165 29 idx->live_count = 0;
166 29 idx->next_id = 0;
167 1986 ▶ for (i = 0; i < idx->posting_count; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < idx->posting_count; i++) 1957 29 1986
168 1957 free(idx->postings[i].doc_ids);
169 29 idx->posting_count = 0;
170 /* Reset epoch so all cached match slots appear stale */
171 29 idx->search_epoch = 0;
172 }
173  
174 28 void strigram_free(strigram_t *idx) {
175 28 ▶ if (!idx) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!idx) return; 0 28 28
176 28 strigram_clear(idx);
177 28 free(idx->docs);
178 28 free(idx->postings);
179 28 free(idx->match_epoch);
180 28 free(idx->match_count_buf);
181 28 free(idx);
182 }
183  
184 /* --- Indexing --- */
185  
186 10040 uint32_t strigram_add(strigram_t *idx, const char *text, uint32_t len) {
187 uint32_t doc_id, ntg, utg, i;
188 doc_entry_t *doc;
189 uint32_t *tgs;
190  
191 10040 ▶ if (idx->doc_count >= idx->doc_cap) {
Branch: 1 branch
BranchTrueFalseTotal
if (idx->doc_count >= idx->doc_cap) { 35 10005 10040
192 35 ▶ uint32_t new_cap = idx->doc_cap ? idx->doc_cap * 2 : 64;
Branch: 1 branch
BranchTrueFalseTotal
uint32_t new_cap = idx->doc_cap ? idx->doc_cap * 2 : 64; 8 27 35
193 35 doc_entry_t *np = (doc_entry_t *)realloc(idx->docs,
194 new_cap * sizeof(doc_entry_t));
195 35 ▶ if (!np) return UINT32_MAX;
Branch: 1 branch
BranchTrueFalseTotal
if (!np) return UINT32_MAX; 0 35 35
196 35 idx->docs = np;
197 35 idx->doc_cap = new_cap;
198 }
199  
200 10040 doc_id = idx->next_id++;
201 10040 doc = &idx->docs[idx->doc_count++];
202 10040 doc->doc_id = doc_id;
203 10040 doc->text = (char *)malloc(len + 1);
204 10040 ▶ if (!doc->text) { idx->doc_count--; idx->next_id--; return UINT32_MAX; }
Branch: 1 branch
BranchTrueFalseTotal
if (!doc->text) { idx->doc_count--; idx->next_id--; return UINT32_MAX; } 0 10040 10040
205 10040 memcpy(doc->text, text, len);
206 10040 doc->text[len] = '\0';
207 10040 doc->text_len = len;
208 10040 doc->deleted = 0;
209  
210 10040 tgs = extract_trigrams(text, len, &ntg);
211 10040 utg = 0;
212 10040 ▶ if (tgs) {
Branch: 1 branch
BranchTrueFalseTotal
if (tgs) { 10039 1 10040
213 10039 utg = sort_unique(tgs, ntg);
214 209741 ▶ for (i = 0; i < utg; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < utg; i++) { 199702 10039 209741
215 199702 uint32_t pi = posting_find_insert(idx, tgs[i]);
216 199702 ▶ if (pi != UINT32_MAX)
Branch: 1 branch
BranchTrueFalseTotal
if (pi != UINT32_MAX) 199702 0 199702
217 199702 posting_append_doc(&idx->postings[pi], doc_id);
218 }
219 10039 free(tgs);
220 }
221 10040 doc->tcount = utg;
222 10040 idx->live_count++;
223 10040 return doc_id;
224 }
225  
226 5 void strigram_remove(strigram_t *idx, uint32_t doc_id) {
227 uint32_t i;
228 6 ▶ for (i = 0; i < idx->doc_count; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < idx->doc_count; i++) { 5 1 6
229 5 ▶ if (idx->docs[i].doc_id == doc_id && !idx->docs[i].deleted) {
Branch: 2 branches
BranchTrueFalseTotal
if (idx->docs[i].doc_id == doc_id && !idx->docs[i].deleted) { 4 1 5
if (idx->docs[i].doc_id == doc_id && !idx->docs[i].deleted) { 4 0 4
230 4 idx->docs[i].deleted = 1;
231 4 ▶ if (idx->live_count > 0) idx->live_count--;
Branch: 1 branch
BranchTrueFalseTotal
if (idx->live_count > 0) idx->live_count--; 4 0 4
232 4 return;
233 }
234 }
235 }
236  
237 4 void strigram_optimize(strigram_t *idx) {
238 uint32_t i, pi, w;
239 uint8_t *is_del;
240  
241 4 ▶ if (!idx || idx->next_id == 0) return;
Branch: 2 branches
BranchTrueFalseTotal
if (!idx || idx->next_id == 0) return; 4 0 4
if (!idx || idx->next_id == 0) return; 1 3 4
242  
243 3 is_del = (uint8_t *)calloc((size_t)idx->next_id, 1);
244 3 ▶ if (!is_del) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!is_del) return; 0 3 3
245  
246 6 ▶ for (i = 0; i < idx->doc_count; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < idx->doc_count; i++) { 3 3 6
247 3 ▶ if (idx->docs[i].deleted)
Branch: 1 branch
BranchTrueFalseTotal
if (idx->docs[i].deleted) 2 1 3
248 2 is_del[idx->docs[i].doc_id] = 1;
249 }
250 59 ▶ for (pi = 0; pi < idx->posting_count; pi++) {
Branch: 1 branch
BranchTrueFalseTotal
for (pi = 0; pi < idx->posting_count; pi++) { 56 3 59
251 56 posting_t *p = &idx->postings[pi];
252 56 w = 0;
253 108 ▶ for (i = 0; i < p->count; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < p->count; i++) { 52 56 108
254 52 ▶ if (!is_del[p->doc_ids[i]])
Branch: 1 branch
BranchTrueFalseTotal
if (!is_del[p->doc_ids[i]]) 23 29 52
255 23 p->doc_ids[w++] = p->doc_ids[i];
256 }
257 56 p->count = w;
258 }
259 3 free(is_del);
260  
261 3 w = 0;
262 6 ▶ for (i = 0; i < idx->doc_count; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < idx->doc_count; i++) { 3 3 6
263 3 ▶ if (!idx->docs[i].deleted) {
Branch: 1 branch
BranchTrueFalseTotal
if (!idx->docs[i].deleted) { 1 2 3
264 1 ▶ if (w != i) idx->docs[w] = idx->docs[i];
Branch: 1 branch
BranchTrueFalseTotal
if (w != i) idx->docs[w] = idx->docs[i]; 1 0 1
265 1 w++;
266 } else {
267 2 free(idx->docs[i].text);
268 }
269 }
270 3 idx->doc_count = w;
271 }
272  
273 /* --- Min-heap for top-K selection --- */
274  
275 typedef struct {
276 uint32_t doc_idx;
277 float score;
278 } scored_t;
279  
280 16 static void heap_sift_down(scored_t *h, uint32_t n, uint32_t i) {
281 uint32_t s, l, r;
282 scored_t tmp;
283 for (;;) {
284 26 s = i; l = 2*i+1; r = 2*i+2;
285 26 ▶ if (l < n && h[l].score < h[s].score) s = l;
Branch: 2 branches
BranchTrueFalseTotal
if (l < n && h[l].score < h[s].score) s = l; 12 14 26
if (l < n && h[l].score < h[s].score) s = l; 8 4 12
286 26 ▶ if (r < n && h[r].score < h[s].score) s = r;
Branch: 2 branches
BranchTrueFalseTotal
if (r < n && h[r].score < h[s].score) s = r; 5 21 26
if (r < n && h[r].score < h[s].score) s = r; 3 2 5
287 26 ▶ if (s == i) break;
Branch: 1 branch
BranchTrueFalseTotal
if (s == i) break; 16 10 26
288 10 tmp = h[i]; h[i] = h[s]; h[s] = tmp;
289 10 i = s;
290 }
291 16 }
292  
293 /* --- Search --- */
294  
295 20 strigram_result_t *strigram_search(strigram_t *idx,
296 const char *query, uint32_t qlen,
297 uint32_t limit,
298 uint32_t *result_count)
299 {
300 uint32_t ntg, qtcount, i;
301 uint32_t *tgs;
302 scored_t *scored;
303 strigram_result_t *results;
304 uint32_t heap_n, nresults;
305  
306 20 *result_count = 0;
307 20 ▶ if (!idx || limit == 0) return NULL;
Branch: 2 branches
BranchTrueFalseTotal
if (!idx || limit == 0) return NULL; 20 0 20
if (!idx || limit == 0) return NULL; 0 20 20
308  
309 20 tgs = extract_trigrams(query, qlen, &ntg);
310 20 qtcount = 0;
311 20 ▶ if (tgs) {
Branch: 1 branch
BranchTrueFalseTotal
if (tgs) { 19 1 20
312 19 qtcount = sort_unique(tgs, ntg);
313 }
314 20 ▶ if (qtcount == 0 || idx->live_count == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (qtcount == 0 || idx->live_count == 0) { 19 1 20
if (qtcount == 0 || idx->live_count == 0) { 2 17 19
315 3 free(tgs);
316 3 return NULL;
317 }
318  
319 /* Grow epoch arrays to cover next_id without per-search zeroing */
320 17 ▶ if (idx->next_id > idx->match_cap) {
Branch: 1 branch
BranchTrueFalseTotal
if (idx->next_id > idx->match_cap) { 16 1 17
321 16 uint32_t new_cap = idx->next_id + (idx->next_id >> 1) + 16;
322 16 uint32_t *ne = (uint32_t *)realloc(idx->match_epoch,
323 new_cap * sizeof(uint32_t));
324 16 uint32_t *nc = (uint32_t *)realloc(idx->match_count_buf,
325 new_cap * sizeof(uint32_t));
326 16 ▶ if (!ne || !nc) {
Branch: 2 branches
BranchTrueFalseTotal
if (!ne || !nc) { 16 0 16
if (!ne || !nc) { 0 16 16
327 0 ▶ if (ne) idx->match_epoch = ne;
Branch: 1 branch
BranchTrueFalseTotal
if (ne) idx->match_epoch = ne; 0 0 0
328 0 ▶ if (nc) idx->match_count_buf = nc;
Branch: 1 branch
BranchTrueFalseTotal
if (nc) idx->match_count_buf = nc; 0 0 0
329 0 free(tgs);
330 0 return NULL;
331 }
332 /* Zero new epoch slots so they appear stale */
333 16 memset(ne + idx->match_cap, 0,
334 16 (new_cap - idx->match_cap) * sizeof(uint32_t));
335 16 idx->match_epoch = ne;
336 16 idx->match_count_buf = nc;
337 16 idx->match_cap = new_cap;
338 }
339  
340 /* Advance epoch; on wrap, zero all slots and restart from 1 */
341 17 ▶ if (++idx->search_epoch == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (++idx->search_epoch == 0) { 0 17 17
342 0 memset(idx->match_epoch, 0, idx->match_cap * sizeof(uint32_t));
343 0 idx->search_epoch = 1;
344 }
345  
346 /* Accumulate match counts — lazy-init per slot on first touch */
347 157 ▶ for (i = 0; i < qtcount; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < qtcount; i++) { 140 17 157
348 140 uint32_t pi = posting_find(idx, tgs[i]);
349 140 ▶ if (pi != UINT32_MAX) {
Branch: 1 branch
BranchTrueFalseTotal
if (pi != UINT32_MAX) { 117 23 140
350 117 posting_t *p = &idx->postings[pi];
351 uint32_t j;
352 328 ▶ for (j = 0; j < p->count; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < p->count; j++) { 211 117 328
353 211 uint32_t did = p->doc_ids[j];
354 211 ▶ if (idx->match_epoch[did] != idx->search_epoch) {
Branch: 1 branch
BranchTrueFalseTotal
if (idx->match_epoch[did] != idx->search_epoch) { 27 184 211
355 27 idx->match_epoch[did] = idx->search_epoch;
356 27 idx->match_count_buf[did] = 0;
357 }
358 211 idx->match_count_buf[did]++;
359 }
360 }
361 }
362 17 free(tgs);
363  
364 /* Top-K selection with min-heap: O(n log limit) vs O(n log n) */
365 17 scored = (scored_t *)malloc(limit * sizeof(scored_t));
366 17 ▶ if (!scored) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!scored) return NULL; 0 17 17
367 17 heap_n = 0;
368  
369 48 ▶ for (i = 0; i < idx->doc_count; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < idx->doc_count; i++) { 31 17 48
370 31 doc_entry_t *d = &idx->docs[i];
371 uint32_t mc;
372 float score;
373 31 ▶ if (d->deleted) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (d->deleted) continue; 0 31 31
374 31 ▶ if (idx->match_epoch[d->doc_id] != idx->search_epoch) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (idx->match_epoch[d->doc_id] != idx->search_epoch) continue; 4 27 31
375 27 mc = idx->match_count_buf[d->doc_id];
376 27 ▶ if (mc == 0) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (mc == 0) continue; 0 27 27
377 54 score = (float)(2 * mc) /
378 27 ▶ (float)(qtcount + (d->tcount ? d->tcount : 1));
Branch: 1 branch
BranchTrueFalseTotal
(float)(qtcount + (d->tcount ? d->tcount : 1)); 27 0 27
379 27 ▶ if (heap_n < limit) {
Branch: 1 branch
BranchTrueFalseTotal
if (heap_n < limit) { 23 4 27
380 23 scored[heap_n].doc_idx = i;
381 23 scored[heap_n].score = score;
382 23 heap_n++;
383 23 ▶ if (heap_n == limit) {
Branch: 1 branch
BranchTrueFalseTotal
if (heap_n == limit) { 2 21 23
384 /* Build min-heap in-place */
385 int k;
386 4 ▶ for (k = (int)(heap_n >> 1) - 1; k >= 0; k--)
Branch: 1 branch
BranchTrueFalseTotal
for (k = (int)(heap_n >> 1) - 1; k >= 0; k--) 2 2 4
387 2 heap_sift_down(scored, heap_n, (uint32_t)k);
388 }
389 4 ▶ } else if (score > scored[0].score) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (score > scored[0].score) { 2 2 4
390 2 scored[0].doc_idx = i;
391 2 scored[0].score = score;
392 2 heap_sift_down(scored, heap_n, 0);
393 }
394 }
395  
396 17 ▶ if (heap_n == 0) { free(scored); return NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (heap_n == 0) { free(scored); return NULL; } 2 15 17
397  
398 /* When fewer results than limit, the inline heapify inside the loop
399 * never fired (it only triggers at heap_n == limit). Build the heap
400 * now so the heap-sort below produces a correctly ordered output. */
401 15 ▶ if (heap_n < limit) {
Branch: 1 branch
BranchTrueFalseTotal
if (heap_n < limit) { 13 2 15
402 int k;
403 17 ▶ for (k = (int)(heap_n >> 1) - 1; k >= 0; k--)
Branch: 1 branch
BranchTrueFalseTotal
for (k = (int)(heap_n >> 1) - 1; k >= 0; k--) 4 13 17
404 4 heap_sift_down(scored, heap_n, (uint32_t)k);
405 }
406  
407 /* Min-heap sort: each iteration extracts the minimum to the end,
408 * leaving scored[0..nresults-1] in descending order (max at [0]). */
409 15 nresults = heap_n;
410 {
411 15 uint32_t rem = nresults;
412 23 ▶ while (rem > 1) {
Branch: 1 branch
BranchTrueFalseTotal
while (rem > 1) { 8 15 23
413 8 scored_t tmp = scored[0]; scored[0] = scored[rem-1]; scored[rem-1] = tmp;
414 8 rem--;
415 8 heap_sift_down(scored, rem, 0);
416 }
417 }
418  
419 15 results = (strigram_result_t *)malloc(nresults * sizeof(strigram_result_t));
420 15 ▶ if (!results) { free(scored); return NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (!results) { free(scored); return NULL; } 0 15 15
421  
422 38 ▶ for (i = 0; i < nresults; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nresults; i++) { 23 15 38
423 23 doc_entry_t *d = &idx->docs[scored[i].doc_idx];
424 23 results[i].doc_id = d->doc_id;
425 23 results[i].score = scored[i].score;
426 23 results[i].text = d->text;
427 23 results[i].text_len = d->text_len;
428 }
429 15 free(scored);
430  
431 15 *result_count = nresults;
432 15 return results;
433 }
434  
435 15 void strigram_results_free(strigram_result_t *results) {
436 15 free(results);
437 15 }
438  
439 /* --- Stats --- */
440  
441 8 uint32_t strigram_doc_count(const strigram_t *idx) {
442 8 ▶ return idx ? idx->live_count : 0;
Branch: 1 branch
BranchTrueFalseTotal
return idx ? idx->live_count : 0; 8 0 8
443 }
444  
445 5 uint32_t strigram_trigram_count(const strigram_t *idx) {
446 5 ▶ return idx ? idx->posting_count : 0;
Branch: 1 branch
BranchTrueFalseTotal
return idx ? idx->posting_count : 0; 5 0 5
447 }
« Trigram.xs ↑ Summary blib/lib/Search/Trigram.pm »