strigram.c

filter: Statementstmt 97.5% 239 / 245 Branchbran 82.3% 145 / 176 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 91.2% 384 / 421 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/sg_abi_impl.h ↑ 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 1200114 static uint8_t lc_byte(uint8_t c) {
43 1200114 ▶ return (c >= 'A' && c <= 'Z') ? (uint8_t)(c + 32) : c;
Branch: 2 branches
BranchTrueFalseTotal
return (c >= 'A' && c <= 'Z') ? (uint8_t)(c + 32) : c; 941066 259048 1200114
return (c >= 'A' && c <= 'Z') ? (uint8_t)(c + 32) : c; 7 941059 941066
44 }
45  
46 /* Build a lowercased, space-padded copy: " text " */
47 10070 static uint8_t *build_padded(const char *text, uint32_t len) {
48 uint8_t *buf;
49 uint32_t j;
50 10070 buf = (uint8_t *)malloc(len + 2);
51 10070 ▶ if (!buf) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!buf) return NULL; 0 10070 10070
52 10070 buf[0] = ' ';
53 1210184 ▶ 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]); 1200114 10070 1210184
54 10070 buf[len + 1] = ' ';
55 10070 return buf;
56 }
57  
58 /* Extract trigrams from text: plen=len+2, ntg=len (one per byte). */
59 10072 static uint32_t *extract_trigrams(const char *text, uint32_t len, uint32_t *out_count) {
60 10072 uint32_t ntg = len; /* plen-2 == len */
61 uint8_t *buf;
62 uint32_t *tgs;
63 uint32_t i;
64  
65 10072 ▶ if (ntg == 0) { *out_count = 0; return NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (ntg == 0) { *out_count = 0; return NULL; } 2 10070 10072
66  
67 10070 buf = build_padded(text, len);
68 10070 ▶ if (!buf) { *out_count = 0; return NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (!buf) { *out_count = 0; return NULL; } 0 10070 10070
69  
70 10070 tgs = (uint32_t *)malloc(ntg * sizeof(uint32_t));
71 10070 ▶ if (!tgs) { free(buf); *out_count = 0; return NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (!tgs) { free(buf); *out_count = 0; return NULL; } 0 10070 10070
72  
73 1210184 ▶ for (i = 0; i < ntg; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < ntg; i++) 1200114 10070 1210184
74 1200114 tgs[i] = ((uint32_t)buf[i] << 16) | ((uint32_t)buf[i+1] << 8) | (uint32_t)buf[i+2];
75  
76 10070 free(buf);
77 10070 *out_count = ntg;
78 10070 return tgs;
79 }
80  
81 17898355 static int cmp_u32(const void *a, const void *b) {
82 17898355 uint32_t x = *(const uint32_t *)a;
83 17898355 uint32_t y = *(const uint32_t *)b;
84 17898355 return (x > y) - (x < y);
85 }
86  
87 10070 static uint32_t sort_unique(uint32_t *arr, uint32_t n) {
88 uint32_t w, i;
89 10070 ▶ if (n == 0) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (n == 0) return 0; 0 10070 10070
90 10070 qsort(arr, (size_t)n, sizeof(uint32_t), cmp_u32);
91 10070 w = 1;
92 1200114 ▶ for (i = 1; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i < n; i++) { 1190044 10070 1200114
93 1190044 ▶ if (arr[i] != arr[i - 1]) arr[w++] = arr[i];
Branch: 1 branch
BranchTrueFalseTotal
if (arr[i] != arr[i - 1]) arr[w++] = arr[i]; 190017 1000027 1190044
94 }
95 10070 return w;
96 }
97  
98 /* --- Posting list management --- */
99  
100 184 static uint32_t posting_find(const strigram_t *idx, uint32_t trigram) {
101 184 uint32_t lo = 0, hi = idx->posting_count;
102 867 ▶ while (lo < hi) {
Branch: 1 branch
BranchTrueFalseTotal
while (lo < hi) { 834 33 867
103 834 uint32_t mid = lo + (hi - lo) / 2;
104 834 ▶ if (idx->postings[mid].trigram < trigram) lo = mid + 1;
Branch: 1 branch
BranchTrueFalseTotal
if (idx->postings[mid].trigram < trigram) lo = mid + 1; 304 530 834
105 530 ▶ else if (idx->postings[mid].trigram > trigram) hi = mid;
Branch: 1 branch
BranchTrueFalseTotal
else if (idx->postings[mid].trigram > trigram) hi = mid; 379 151 530
106 151 else return mid;
107 }
108 33 return UINT32_MAX;
109 }
110  
111 199889 static uint32_t posting_find_insert(strigram_t *idx, uint32_t trigram) {
112 199889 uint32_t lo = 0, hi = idx->posting_count;
113 1850250 ▶ while (lo < hi) {
Branch: 1 branch
BranchTrueFalseTotal
while (lo < hi) { 1848125 2125 1850250
114 1848125 uint32_t mid = lo + (hi - lo) / 2;
115 1848125 ▶ if (idx->postings[mid].trigram < trigram) lo = mid + 1;
Branch: 1 branch
BranchTrueFalseTotal
if (idx->postings[mid].trigram < trigram) lo = mid + 1; 1147326 700799 1848125
116 700799 ▶ else if (idx->postings[mid].trigram > trigram) hi = mid;
Branch: 1 branch
BranchTrueFalseTotal
else if (idx->postings[mid].trigram > trigram) hi = mid; 503035 197764 700799
117 197764 else return mid;
118 }
119 2125 ▶ if (idx->posting_count >= idx->posting_cap) {
Branch: 1 branch
BranchTrueFalseTotal
if (idx->posting_count >= idx->posting_cap) { 31 2094 2125
120 31 ▶ 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 28 31
121 31 posting_t *np = (posting_t *)realloc(idx->postings,
122 new_cap * sizeof(posting_t));
123 31 ▶ if (!np) return UINT32_MAX;
Branch: 1 branch
BranchTrueFalseTotal
if (!np) return UINT32_MAX; 0 31 31
124 31 idx->postings = np;
125 31 idx->posting_cap = new_cap;
126 }
127 2125 ▶ if (lo < idx->posting_count) {
Branch: 1 branch
BranchTrueFalseTotal
if (lo < idx->posting_count) { 1630 495 2125
128 1630 memmove(idx->postings + lo + 1,
129 1630 idx->postings + lo,
130 1630 (idx->posting_count - lo) * sizeof(posting_t));
131 }
132 2125 idx->postings[lo].trigram = trigram;
133 2125 idx->postings[lo].doc_ids = NULL;
134 2125 idx->postings[lo].count = 0;
135 2125 idx->postings[lo].cap = 0;
136 2125 idx->posting_count++;
137 2125 return lo;
138 }
139  
140 199889 static int posting_append_doc(posting_t *p, uint32_t doc_id) {
141 199889 ▶ if (p->count >= p->cap) {
Branch: 1 branch
BranchTrueFalseTotal
if (p->count >= p->cap) { 6244 193645 199889
142 6244 ▶ 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 2125 6244
143 6244 uint32_t *np = (uint32_t *)realloc(p->doc_ids,
144 new_cap * sizeof(uint32_t));
145 6244 ▶ if (!np) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!np) return 0; 0 6244 6244
146 6244 p->doc_ids = np;
147 6244 p->cap = new_cap;
148 }
149 199889 p->doc_ids[p->count++] = doc_id;
150 199889 return 1;
151 }
152  
153 /* --- Lifecycle --- */
154  
155 30 strigram_t *strigram_new(void) {
156 30 return (strigram_t *)calloc(1, sizeof(strigram_t));
157 }
158  
159 31 void strigram_clear(strigram_t *idx) {
160 uint32_t i;
161 31 ▶ if (!idx) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!idx) return; 0 31 31
162 10075 ▶ for (i = 0; i < idx->doc_count; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < idx->doc_count; i++) 10044 31 10075
163 10044 free(idx->docs[i].text);
164 31 idx->doc_count = 0;
165 31 idx->live_count = 0;
166 31 idx->next_id = 0;
167 2156 ▶ for (i = 0; i < idx->posting_count; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < idx->posting_count; i++) 2125 31 2156
168 2125 free(idx->postings[i].doc_ids);
169 31 idx->posting_count = 0;
170 /* Reset epoch so all cached match slots appear stale */
171 31 idx->search_epoch = 0;
172 }
173  
174 30 void strigram_free(strigram_t *idx) {
175 30 ▶ if (!idx) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!idx) return; 0 30 30
176 30 strigram_clear(idx);
177 30 free(idx->docs);
178 30 free(idx->postings);
179 30 free(idx->match_epoch);
180 30 free(idx->match_count_buf);
181 30 free(idx);
182 }
183  
184 /* --- Indexing --- */
185  
186 10046 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 10046 ▶ if (idx->doc_count >= idx->doc_cap) {
Branch: 1 branch
BranchTrueFalseTotal
if (idx->doc_count >= idx->doc_cap) { 37 10009 10046
192 37 ▶ 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 29 37
193 37 doc_entry_t *np = (doc_entry_t *)realloc(idx->docs,
194 new_cap * sizeof(doc_entry_t));
195 37 ▶ if (!np) return UINT32_MAX;
Branch: 1 branch
BranchTrueFalseTotal
if (!np) return UINT32_MAX; 0 37 37
196 37 idx->docs = np;
197 37 idx->doc_cap = new_cap;
198 }
199  
200 10046 doc_id = idx->next_id++;
201 10046 doc = &idx->docs[idx->doc_count++];
202 10046 doc->doc_id = doc_id;
203 10046 doc->text = (char *)malloc(len + 1);
204 10046 ▶ 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 10046 10046
205 10046 memcpy(doc->text, text, len);
206 10046 doc->text[len] = '\0';
207 10046 doc->text_len = len;
208 10046 doc->deleted = 0;
209  
210 10046 tgs = extract_trigrams(text, len, &ntg);
211 10046 utg = 0;
212 10046 ▶ if (tgs) {
Branch: 1 branch
BranchTrueFalseTotal
if (tgs) { 10045 1 10046
213 10045 utg = sort_unique(tgs, ntg);
214 209934 ▶ for (i = 0; i < utg; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < utg; i++) { 199889 10045 209934
215 199889 uint32_t pi = posting_find_insert(idx, tgs[i]);
216 199889 ▶ if (pi != UINT32_MAX)
Branch: 1 branch
BranchTrueFalseTotal
if (pi != UINT32_MAX) 199889 0 199889
217 199889 posting_append_doc(&idx->postings[pi], doc_id);
218 }
219 10045 free(tgs);
220 }
221 10046 doc->tcount = utg;
222 10046 idx->live_count++;
223 10046 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 10 void strigram_optimize(strigram_t *idx) {
238 uint32_t i, pi, w;
239 uint8_t *is_del;
240  
241 10 ▶ if (!idx || idx->next_id == 0) return;
Branch: 2 branches
BranchTrueFalseTotal
if (!idx || idx->next_id == 0) return; 10 0 10
if (!idx || idx->next_id == 0) return; 1 9 10
242  
243 9 is_del = (uint8_t *)calloc((size_t)idx->next_id, 1);
244 9 ▶ if (!is_del) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!is_del) return; 0 9 9
245  
246 25 ▶ for (i = 0; i < idx->doc_count; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < idx->doc_count; i++) { 16 9 25
247 16 ▶ if (idx->docs[i].deleted)
Branch: 1 branch
BranchTrueFalseTotal
if (idx->docs[i].deleted) 2 14 16
248 2 is_del[idx->docs[i].doc_id] = 1;
249 }
250 473 ▶ for (pi = 0; pi < idx->posting_count; pi++) {
Branch: 1 branch
BranchTrueFalseTotal
for (pi = 0; pi < idx->posting_count; pi++) { 464 9 473
251 464 posting_t *p = &idx->postings[pi];
252 464 w = 0;
253 950 ▶ for (i = 0; i < p->count; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < p->count; i++) { 486 464 950
254 486 ▶ if (!is_del[p->doc_ids[i]])
Branch: 1 branch
BranchTrueFalseTotal
if (!is_del[p->doc_ids[i]]) 457 29 486
255 457 p->doc_ids[w++] = p->doc_ids[i];
256 }
257 464 p->count = w;
258 }
259 9 free(is_del);
260  
261 9 w = 0;
262 25 ▶ for (i = 0; i < idx->doc_count; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < idx->doc_count; i++) { 16 9 25
263 16 ▶ if (!idx->docs[i].deleted) {
Branch: 1 branch
BranchTrueFalseTotal
if (!idx->docs[i].deleted) { 14 2 16
264 14 ▶ if (w != i) idx->docs[w] = idx->docs[i];
Branch: 1 branch
BranchTrueFalseTotal
if (w != i) idx->docs[w] = idx->docs[i]; 1 13 14
265 14 w++;
266 } else {
267 2 free(idx->docs[i].text);
268 }
269 }
270 9 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 25 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 35 s = i; l = 2*i+1; r = 2*i+2;
285 35 ▶ 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; 17 18 35
if (l < n && h[l].score < h[s].score) s = l; 8 9 17
286 35 ▶ 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; 7 28 35
if (r < n && h[r].score < h[s].score) s = r; 3 4 7
287 35 ▶ if (s == i) break;
Branch: 1 branch
BranchTrueFalseTotal
if (s == i) break; 25 10 35
288 10 tmp = h[i]; h[i] = h[s]; h[s] = tmp;
289 10 i = s;
290 }
291 25 }
292  
293 /* --- Search --- */
294  
295 26 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 26 *result_count = 0;
307 26 ▶ if (!idx || limit == 0) return NULL;
Branch: 2 branches
BranchTrueFalseTotal
if (!idx || limit == 0) return NULL; 26 0 26
if (!idx || limit == 0) return NULL; 0 26 26
308  
309 26 tgs = extract_trigrams(query, qlen, &ntg);
310 26 qtcount = 0;
311 26 ▶ if (tgs) {
Branch: 1 branch
BranchTrueFalseTotal
if (tgs) { 25 1 26
312 25 qtcount = sort_unique(tgs, ntg);
313 }
314 26 ▶ if (qtcount == 0 || idx->live_count == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (qtcount == 0 || idx->live_count == 0) { 25 1 26
if (qtcount == 0 || idx->live_count == 0) { 2 23 25
315 3 free(tgs);
316 3 return NULL;
317 }
318  
319 /* Grow epoch arrays to cover next_id without per-search zeroing */
320 23 ▶ if (idx->next_id > idx->match_cap) {
Branch: 1 branch
BranchTrueFalseTotal
if (idx->next_id > idx->match_cap) { 18 5 23
321 18 uint32_t new_cap = idx->next_id + (idx->next_id >> 1) + 16;
322 18 uint32_t *ne = (uint32_t *)realloc(idx->match_epoch,
323 new_cap * sizeof(uint32_t));
324 18 uint32_t *nc = (uint32_t *)realloc(idx->match_count_buf,
325 new_cap * sizeof(uint32_t));
326 18 ▶ if (!ne || !nc) {
Branch: 2 branches
BranchTrueFalseTotal
if (!ne || !nc) { 18 0 18
if (!ne || !nc) { 0 18 18
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 18 memset(ne + idx->match_cap, 0,
334 18 (new_cap - idx->match_cap) * sizeof(uint32_t));
335 18 idx->match_epoch = ne;
336 18 idx->match_count_buf = nc;
337 18 idx->match_cap = new_cap;
338 }
339  
340 /* Advance epoch; on wrap, zero all slots and restart from 1 */
341 23 ▶ if (++idx->search_epoch == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (++idx->search_epoch == 0) { 0 23 23
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 207 ▶ for (i = 0; i < qtcount; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < qtcount; i++) { 184 23 207
348 184 uint32_t pi = posting_find(idx, tgs[i]);
349 184 ▶ if (pi != UINT32_MAX) {
Branch: 1 branch
BranchTrueFalseTotal
if (pi != UINT32_MAX) { 151 33 184
350 151 posting_t *p = &idx->postings[pi];
351 uint32_t j;
352 431 ▶ for (j = 0; j < p->count; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < p->count; j++) { 280 151 431
353 280 uint32_t did = p->doc_ids[j];
354 280 ▶ if (idx->match_epoch[did] != idx->search_epoch) {
Branch: 1 branch
BranchTrueFalseTotal
if (idx->match_epoch[did] != idx->search_epoch) { 39 241 280
355 39 idx->match_epoch[did] = idx->search_epoch;
356 39 idx->match_count_buf[did] = 0;
357 }
358 280 idx->match_count_buf[did]++;
359 }
360 }
361 }
362 23 free(tgs);
363  
364 /* Top-K selection with min-heap: O(n log limit) vs O(n log n) */
365 23 scored = (scored_t *)malloc(limit * sizeof(scored_t));
366 23 ▶ if (!scored) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!scored) return NULL; 0 23 23
367 23 heap_n = 0;
368  
369 76 ▶ for (i = 0; i < idx->doc_count; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < idx->doc_count; i++) { 53 23 76
370 53 doc_entry_t *d = &idx->docs[i];
371 uint32_t mc;
372 float score;
373 53 ▶ if (d->deleted) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (d->deleted) continue; 0 53 53
374 53 ▶ 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; 14 39 53
375 39 mc = idx->match_count_buf[d->doc_id];
376 39 ▶ if (mc == 0) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (mc == 0) continue; 0 39 39
377 78 score = (float)(2 * mc) /
378 39 ▶ (float)(qtcount + (d->tcount ? d->tcount : 1));
Branch: 1 branch
BranchTrueFalseTotal
(float)(qtcount + (d->tcount ? d->tcount : 1)); 39 0 39
379 39 ▶ if (heap_n < limit) {
Branch: 1 branch
BranchTrueFalseTotal
if (heap_n < limit) { 33 6 39
380 33 scored[heap_n].doc_idx = i;
381 33 scored[heap_n].score = score;
382 33 heap_n++;
383 33 ▶ if (heap_n == limit) {
Branch: 1 branch
BranchTrueFalseTotal
if (heap_n == limit) { 3 30 33
384 /* Build min-heap in-place */
385 int k;
386 5 ▶ 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 3 5
387 2 heap_sift_down(scored, heap_n, (uint32_t)k);
388 }
389 6 ▶ } else if (score > scored[0].score) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (score > scored[0].score) { 3 3 6
390 3 scored[0].doc_idx = i;
391 3 scored[0].score = score;
392 3 heap_sift_down(scored, heap_n, 0);
393 }
394 }
395  
396 23 ▶ if (heap_n == 0) { free(scored); return NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (heap_n == 0) { free(scored); return NULL; } 3 20 23
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 20 ▶ if (heap_n < limit) {
Branch: 1 branch
BranchTrueFalseTotal
if (heap_n < limit) { 17 3 20
402 int k;
403 24 ▶ for (k = (int)(heap_n >> 1) - 1; k >= 0; k--)
Branch: 1 branch
BranchTrueFalseTotal
for (k = (int)(heap_n >> 1) - 1; k >= 0; k--) 7 17 24
404 7 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 20 nresults = heap_n;
410 {
411 20 uint32_t rem = nresults;
412 33 ▶ while (rem > 1) {
Branch: 1 branch
BranchTrueFalseTotal
while (rem > 1) { 13 20 33
413 13 scored_t tmp = scored[0]; scored[0] = scored[rem-1]; scored[rem-1] = tmp;
414 13 rem--;
415 13 heap_sift_down(scored, rem, 0);
416 }
417 }
418  
419 20 results = (strigram_result_t *)malloc(nresults * sizeof(strigram_result_t));
420 20 ▶ if (!results) { free(scored); return NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (!results) { free(scored); return NULL; } 0 20 20
421  
422 53 ▶ for (i = 0; i < nresults; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nresults; i++) { 33 20 53
423 33 doc_entry_t *d = &idx->docs[scored[i].doc_idx];
424 33 results[i].doc_id = d->doc_id;
425 33 results[i].score = scored[i].score;
426 33 results[i].text = d->text;
427 33 results[i].text_len = d->text_len;
428 }
429 20 free(scored);
430  
431 20 *result_count = nresults;
432 20 return results;
433 }
434  
435 20 void strigram_results_free(strigram_result_t *results) {
436 20 free(results);
437 20 }
438  
439 /* --- Stats --- */
440  
441 10 uint32_t strigram_doc_count(const strigram_t *idx) {
442 10 ▶ return idx ? idx->live_count : 0;
Branch: 1 branch
BranchTrueFalseTotal
return idx ? idx->live_count : 0; 10 0 10
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 }
« include/sg_abi_impl.h ↑ Summary blib/lib/Search/Trigram.pm »