lib/KinoSearch1.xs

filter: Statementstmt 84.4% 632 / 748 Branchbran 62.0% 186 / 300 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 78.0% 818 / 1048 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« lib/KinoSearch1/Index/MultiTermDocs.pm ↑ Summary lib/KinoSearch1/Util/BitVector.pm »
1 /***********************************************
2  
3 !!!! DO NOT EDIT THIS FILE !!!!
4  
5 This file was auto-generated by Build.PL from
6 lib/KinoSearch1/Util/SortExternal.pm
7  
8 ***********************************************/
9  
10 #include "KinoSearch1UtilVerifyArgs.h"
11 #include "KinoSearch1UtilStringHelper.h"
12 #include "KinoSearch1UtilSortExternal.h"
13 #include "KinoSearch1UtilPriorityQueue.h"
14 #include "KinoSearch1UtilMemManager.h"
15 #include "KinoSearch1UtilMathUtils.h"
16 #include "KinoSearch1UtilIntMap.h"
17 #include "KinoSearch1UtilCClass.h"
18 #include "KinoSearch1UtilCarp.h"
19 #include "KinoSearch1UtilByteBuf.h"
20 #include "KinoSearch1UtilBitVector.h"
21 #include "KinoSearch1StoreOutStream.h"
22 #include "KinoSearch1StoreInStream.h"
23 #include "KinoSearch1SearchTermScorer.h"
24 #include "KinoSearch1SearchSimilarity.h"
25 #include "KinoSearch1SearchScorer.h"
26 #include "KinoSearch1SearchPhraseScorer.h"
27 #include "KinoSearch1SearchHitQueue.h"
28 #include "KinoSearch1SearchHitCollector.h"
29 #include "KinoSearch1SearchBooleanScorer.h"
30 #include "KinoSearch1IndexTermInfosWriter.h"
31 #include "KinoSearch1IndexTermInfo.h"
32 #include "KinoSearch1IndexTermDocs.h"
33 #include "KinoSearch1IndexTermBuffer.h"
34 #include "KinoSearch1IndexTerm.h"
35 #include "KinoSearch1IndexSegWriter.h"
36 #include "KinoSearch1IndexSegTermEnum.h"
37 #include "KinoSearch1IndexSegTermDocs.h"
38 #include "KinoSearch1IndexPostingsWriter.h"
39 #include "KinoSearch1IndexMultiTermDocs.h"
40 #include "KinoSearch1IndexDelDocs.h"
41 #include "KinoSearch1DocumentField.h"
42 #include "KinoSearch1AnalysisTokenBatch.h"
43 #include "KinoSearch1AnalysisToken.h"
44 #include "KinoSearch1AnalysisStopalizer.h"
45  
46 #include "limits.h"
47  
48 #include "EXTERN.h"
49 #include "perl.h"
50 #include "XSUB.h"
51  
52 #define NEED_newRV_noinc_GLOBAL
53 #include "ppport.h"
54  
55 MODULE = KinoSearch1 PACKAGE = KinoSearch1
56  
57 PROTOTYPES: disable
58  
59 BOOT:
60 51 items = 0;
61  
62 =for comment
63 Return 1 if memory debugging is enabled. See KinoSearch1::Util::MemManager.
64  
65 =cut
66  
67 IV
68 memory_debugging_enabled()
69 CODE:
70 1 ▶ RETVAL = KINO_MEM_LEAK_DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = KINO_MEM_LEAK_DEBUG; 0 1 1
71 OUTPUT:
72 RETVAL
73  
74  
75 MODULE = KinoSearch1 PACKAGE = KinoSearch1::Analysis::Stopalizer
76  
77 SV*
78 analyze(self_hash, batch_sv)
79 HV *self_hash;
80 SV *batch_sv;
81 PREINIT:
82 TokenBatch *batch;
83 CODE:
84 222 ▶ Kino1_extract_struct( batch_sv, batch, TokenBatch*,
Branch: 1 branch
BranchTrueFalseTotal
Kino1_extract_struct( batch_sv, batch, TokenBatch*, 222 0 222
85 "KinoSearch1::Analysis::TokenBatch");
86 222 Kino1_Stopalizer_analyze(self_hash, batch);
87 222 SvREFCNT_inc(batch_sv);
88 222 RETVAL = batch_sv;
89 OUTPUT: RETVAL
90
91  
92 MODULE = KinoSearch1 PACKAGE = KinoSearch1::Analysis::TokenBatch
93  
94 void
95 new(either_sv)
96 SV *either_sv;
97 PREINIT:
98 const char *class;
99 TokenBatch *batch;
100 PPCODE:
101 /* determine the class */
102 31729 class = sv_isobject(either_sv)
103 0 ? sv_reftype(either_sv, 0)
104 63458 ▶ : SvPV_nolen(either_sv);
Branch: 1 branch
BranchTrueFalseTotal
: SvPV_nolen(either_sv); 0 31729 31729
105 /* build object */
106 31729 batch = Kino1_TokenBatch_new();
107 31729 ST(0) = sv_newmortal();
108 31729 sv_setref_pv(ST(0), class, (void*)batch);
109 31729 XSRETURN(1);
110  
111 void
112 append(batch, text_sv, start_offset, end_offset, ...)
113 TokenBatch *batch;
114 SV *text_sv;
115 I32 start_offset;
116 I32 end_offset;
117 PREINIT:
118 char *text;
119 STRLEN len;
120 15800 I32 pos_inc = 1;
121 Token *token;
122 PPCODE:
123 15800 text = SvPV(text_sv, len);
124 15800 ▶ if (items == 5)
Branch: 1 branch
BranchTrueFalseTotal
if (items == 5) 0 15800 15800
125 0 pos_inc = SvIV( ST(4) );
126 15800 ▶ else if (items > 5)
Branch: 1 branch
BranchTrueFalseTotal
else if (items > 5) 0 15800 15800
127 0 Kino1_confess("Too many arguments: %d", items);
128  
129 15800 token = Kino1_Token_new(text, len, start_offset, end_offset, pos_inc);
130 15800 Kino1_TokenBatch_append(batch, token);
131  
132 =for comment
133  
134 Add many tokens to the batch, by supplying the string to be tokenized, and
135 arrays of token starts and token ends (specified in bytes).
136  
137 =cut
138  
139 void
140 add_many_tokens(batch, string_sv, starts_av, ends_av)
141 TokenBatch *batch;
142 SV *string_sv;
143 AV *starts_av;
144 AV *ends_av;
145 PREINIT:
146 char *string_start;
147 STRLEN len, start_offset, end_offset;
148 I32 i, max;
149 SV **start_sv_ptr;
150 SV **end_sv_ptr;
151 Token *token;
152 PPCODE:
153 {
154 15778 string_start = SvPV(string_sv, len);
155  
156 15778 max = av_len(starts_av);
157 57415 ▶ for (i = 0; i <= max; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i <= max; i++) { 41637 15778 57415
158 /* retrieve start */
159 41637 start_sv_ptr = av_fetch(starts_av, i, 0);
160 41637 ▶ if (start_sv_ptr == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (start_sv_ptr == NULL) 0 41637 41637
161 0 Kino1_confess("Failed to retrieve @starts array element");
162 41637 start_offset = SvIV(*start_sv_ptr);
163  
164 /* retrieve end */
165 41637 end_sv_ptr = av_fetch(ends_av, i, 0);
166 41637 ▶ if (end_sv_ptr == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (end_sv_ptr == NULL) 0 41637 41637
167 0 Kino1_confess("Failed to retrieve @ends array element");
168 41637 end_offset = SvIV(*end_sv_ptr);
169  
170 /* sanity check the offsets to make sure they're inside the string */
171 41637 ▶ if (start_offset > len)
Branch: 1 branch
BranchTrueFalseTotal
if (start_offset > len) 0 41637 41637
172 0 Kino1_confess("start_offset > len (%d > %"UVuf")",
173 start_offset, (UV)len);
174 41637 ▶ if (end_offset > len)
Branch: 1 branch
BranchTrueFalseTotal
if (end_offset > len) 0 41637 41637
175 0 Kino1_confess("end_offset > len (%d > %"UVuf")",
176 end_offset, (UV)len);
177  
178 /* calculate the start of the substring and add the token */
179 41637 token = Kino1_Token_new(
180 (string_start + start_offset),
181 (end_offset - start_offset),
182 start_offset,
183 end_offset,
184 1
185 );
186 41637 Kino1_TokenBatch_append(batch, token);
187 }
188 }
189  
190 =begin comment
191  
192 Add the postings to the segment. Postings are serialized and dumped into a
193 SortExternal sort pool. The actual writing takes place later.
194  
195 The serialization algo is designed so that postings emerge from the sort
196 pool in the order ideal for writing an index after a simple lexical sort.
197 The concatenated components are:
198  
199 field number
200 term text
201 null byte
202 document number
203 positions (C array of U32)
204 term length
205  
206 =end comment
207 =cut
208  
209 void
210 build_posting_list(batch, doc_num, field_num)
211 TokenBatch *batch;
212 U32 doc_num;
213 U16 field_num;
214 PPCODE:
215 15376 Kino1_TokenBatch_build_plist(batch, doc_num, field_num);
216  
217 void
218 set_all_texts(batch, texts_av)
219 TokenBatch *batch;
220 AV *texts_av;
221 PREINIT:
222 Token *token;
223 I32 i, max;
224 SV **sv_ptr;
225 char *text;
226 STRLEN len;
227 PPCODE:
228 {
229 11075 token = batch->first;
230 11075 max = av_len(texts_av);
231 38069 ▶ for (i = 0; i <= max; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i <= max; i++) { 26994 11075 38069
232 26994 ▶ if (token == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (token == NULL) { 0 26994 26994
233 0 Kino1_confess("Batch size %d doesn't match array size %d",
234 batch->size, (max + 1));
235 }
236 26994 sv_ptr = av_fetch(texts_av, i, 0);
237 26994 ▶ if (sv_ptr == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (sv_ptr == NULL) { 0 26994 26994
238 0 Kino1_confess("Encountered a null SV* pointer");
239 }
240 26994 text = SvPV(*sv_ptr, len);
241 26994 Kino1_Safefree(token->text);
242 26994 token->text = Kino1_savepvn(text, len);
243 26994 token->len = len;
244 26994 token = token->next;
245 }
246 }
247  
248 void
249 get_all_texts(batch)
250 TokenBatch *batch;
251 PREINIT:
252 Token *token;
253 AV *out_av;
254 PPCODE:
255 {
256 11075 out_av = newAV();
257 11075 token = batch->first;
258 38069 ▶ while (token != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
while (token != NULL) { 26994 11075 38069
259 26994 SV *text = newSVpvn(token->text, token->len);
260 26994 av_push(out_av, text);
261 26994 token = token->next;
262 }
263 11075 ▶ XPUSHs(sv_2mortal( newRV_noinc((SV*)out_av) ));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal( newRV_noinc((SV*)out_av) )); 0 11075 11075
264 11075 XSRETURN(1);
265 }
266  
267  
268 SV*
269 _set_or_get(batch, ...)
270 TokenBatch *batch;
271 ALIAS:
272 set_text = 1
273 get_text = 2
274 set_start_offset = 3
275 get_start_offset = 4
276 set_end_offset = 5
277 get_end_offset = 6
278 set_pos_inc = 7
279 get_pos_inc = 8
280 set_size = 9
281 get_size = 10
282 set_postings = 11
283 get_postings = 12
284 set_tv_string = 13
285 get_tv_string = 14
286 CODE:
287 {
288 /* fail if looking for info on a single token but there isn't one */
289 84554 ▶ if ((ix < 7) && (batch->current == NULL))
Branch: 2 branches
BranchTrueFalseTotal
if ((ix < 7) && (batch->current == NULL)) 38426 46128 84554
if ((ix < 7) && (batch->current == NULL)) 0 38426 38426
290 0 Kino1_confess("TokenBatch doesn't currently hold a valid token");
291  
292 84554 ▶ KINO_START_SET_OR_GET_SWITCH
Branch: 2 branches
BranchTrueFalseTotal
KINO_START_SET_OR_GET_SWITCH 11020 73534 84554
KINO_START_SET_OR_GET_SWITCH 0 11020 11020
293  
294 11020 case 1: {
295 11020 Token *current = batch->current;
296 char *text;
297 11020 Kino1_Safefree(current->text);
298 11020 text = SvPV( ST(1), current->len );
299 11020 current->text = Kino1_savepvn( text, current->len );
300 }
301 /* fall through */
302 38410 case 2: RETVAL = newSVpvn(batch->current->text, batch->current->len);
303 38410 break;
304  
305 0 case 3: batch->current->start_offset = SvIV( ST(1) );
306 /* fall through */
307 8 case 4: RETVAL = newSViv(batch->current->start_offset);
308 8 break;
309  
310 0 case 5: batch->current->end_offset = SvIV( ST(1) );
311 /* fall through */
312 8 case 6: RETVAL = newSViv(batch->current->end_offset);
313 8 break;
314  
315 0 case 7: batch->current->pos_inc = SvIV( ST(1) );
316 /* fall through */
317 0 case 8: RETVAL = newSViv(batch->current->pos_inc);
318 0 break;
319  
320 0 case 9: Kino1_confess("Can't set size on a TokenBatch object");
321 /* fall through */
322 15376 case 10: RETVAL = newSVuv(batch->size);
323 15376 break;
324
325 0 case 11: Kino1_confess("can't set_postings");
326 /* fall through */
327 15376 case 12: RETVAL = newRV_inc( (SV*)batch->postings );
328 15376 break;
329  
330 0 case 13: Kino1_confess("can't set_tv_string");
331 /* fall through */
332 15376 case 14: RETVAL = newSVsv(batch->tv_string);
333 15376 break;
334
335 0 KINO_END_SET_OR_GET_SWITCH
336 }
337 OUTPUT: RETVAL
338  
339 void
340 reset(batch)
341 TokenBatch *batch;
342 PPCODE:
343 22155 Kino1_TokenBatch_reset(batch);
344  
345 I32
346 next(batch)
347 TokenBatch *batch;
348 CODE:
349 54821 RETVAL = Kino1_TokenBatch_next(batch);
350 OUTPUT: RETVAL
351  
352 void
353 DESTROY(batch)
354 TokenBatch *batch;
355 PPCODE:
356 31729 Kino1_TokenBatch_destroy(batch);
357  
358  
359  
360 MODULE = KinoSearch1 PACKAGE = KinoSearch1::Document::Field
361  
362 =for comment
363  
364 Return ref to a hash where the keys are term texts and the values are encoded
365 positional data.
366  
367 =cut
368  
369 void
370 _extract_tv_cache(tv_string_sv)
371 SV *tv_string_sv;
372 PREINIT:
373 HV *tv_cache_hv;
374 PPCODE:
375 1 tv_cache_hv = Kino1_Field_extract_tv_cache(tv_string_sv);
376 1 ▶ XPUSHs( sv_2mortal( newRV_noinc( (SV*)tv_cache_hv ) ) );
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs( sv_2mortal( newRV_noinc( (SV*)tv_cache_hv ) ) ); 0 1 1
377 1 XSRETURN(1);
378  
379 =for comment
380  
381 Decompress positional data.
382  
383 =cut
384  
385 void
386 _unpack_posdata(posdata_sv)
387 SV *posdata_sv;
388 PREINIT:
389 AV *positions_av, *starts_av, *ends_av;
390 PPCODE:
391 4 positions_av = newAV();
392 4 starts_av = newAV();
393 4 ends_av = newAV();
394 4 Kino1_Field_unpack_posdata(posdata_sv, positions_av, starts_av, ends_av);
395 4 ▶ XPUSHs(sv_2mortal( newRV_noinc((SV*)positions_av) ));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal( newRV_noinc((SV*)positions_av) )); 0 4 4
396 4 ▶ XPUSHs(sv_2mortal( newRV_noinc((SV*)starts_av) ));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal( newRV_noinc((SV*)starts_av) )); 0 4 4
397 4 ▶ XPUSHs(sv_2mortal( newRV_noinc((SV*)ends_av) ));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal( newRV_noinc((SV*)ends_av) )); 0 4 4
398 4 XSRETURN(3);
399  
400  
401  
402 MODULE = KinoSearch1 PACKAGE = KinoSearch1::Index::DelDocs
403  
404 SV*
405 _generate_doc_map(deldocs, max, offset);
406 BitVector *deldocs;
407 I32 max;
408 I32 offset;
409 PREINIT:
410 SV *map_sv;
411 CODE:
412 18 map_sv = Kino1_DelDocs_generate_doc_map(deldocs, max, offset);
413 18 RETVAL = newRV_noinc(map_sv);
414 OUTPUT: RETVAL
415  
416 I32
417 _delete_by_term_docs(deldocs, term_docs)
418 BitVector *deldocs;
419 TermDocs *term_docs;
420 CODE:
421 4 RETVAL = Kino1_DelDocs_delete_by_term_docs(deldocs, term_docs);
422 OUTPUT: RETVAL
423  
424  
425 MODULE = KinoSearch1 PACKAGE = KinoSearch1::Index::MultiTermDocs
426  
427 void
428 _init_child(term_docs, sub_term_docs_avref, starts_av)
429 TermDocs *term_docs;
430 SV *sub_term_docs_avref;
431 AV *starts_av;
432 PPCODE:
433 3 Kino1_MultiTermDocs_init_child(term_docs, sub_term_docs_avref, starts_av);
434  
435  
436 =for comment
437 Helper for seek().
438  
439 =cut
440  
441 void
442 _reset_pointer(term_docs)
443 TermDocs *term_docs;
444 PREINIT:
445 MultiTermDocsChild *child;
446 PPCODE:
447 4 child = (MultiTermDocsChild*)term_docs->child;
448 4 child->base = 0;
449 4 child->pointer = 0;
450 4 child->current = NULL;
451
452  
453 SV*
454 _set_or_get(term_docs, ...)
455 TermDocs *term_docs;
456 ALIAS:
457 _set_sub_term_docs = 1
458 _get_sub_term_docs = 2
459 CODE:
460 {
461 5 MultiTermDocsChild *child = (MultiTermDocsChild*)term_docs->child;
462  
463 5 ▶ KINO_START_SET_OR_GET_SWITCH
Branch: 2 branches
BranchTrueFalseTotal
KINO_START_SET_OR_GET_SWITCH 0 5 5
KINO_START_SET_OR_GET_SWITCH 0 0 0
464
465 0 case 1: Kino1_confess("Can't set sub_term_docs");
466 /* fall through */
467 5 case 2: RETVAL = newSVsv( child->sub_term_docs_avref );
468 5 break;
469  
470 0 KINO_END_SET_OR_GET_SWITCH
471 }
472 OUTPUT: RETVAL
473  
474  
475 MODULE = KinoSearch1 PACKAGE = KinoSearch1::Index::PostingsWriter
476  
477 void
478 _write_postings (sort_pool, tinfos_writer, frq_out, prx_out)
479 SortExternal *sort_pool;
480 TermInfosWriter *tinfos_writer;
481 OutStream *frq_out;
482 OutStream *prx_out;
483 PPCODE:
484 62 Kino1_PostWriter_write_postings(sort_pool, tinfos_writer, frq_out,
485 prx_out);
486  
487 void
488 _add_segment(sort_pool, term_enum, term_docs, doc_map_ref)
489 SortExternal *sort_pool;
490 SegTermEnum *term_enum;
491 TermDocs *term_docs;
492 SV *doc_map_ref;
493 PPCODE:
494 16 Kino1_PostWriter_add_segment(sort_pool, term_enum, term_docs,
495 doc_map_ref);
496  
497  
498 MODULE = KinoSearch1 PACKAGE = KinoSearch1::Index::SegTermDocs
499  
500 void
501 _init_child(term_docs)
502 TermDocs *term_docs;
503 PPCODE:
504 642 Kino1_SegTermDocs_init_child(term_docs);
505  
506 SV*
507 _set_or_get(term_docs, ...)
508 TermDocs *term_docs;
509 ALIAS:
510 _set_count = 1
511 _get_count = 2
512 _set_freq_stream = 3
513 _get_freq_stream = 4
514 _set_prox_stream = 5
515 _get_prox_stream = 6
516 _set_skip_stream = 7
517 _get_skip_stream = 8
518 _set_deldocs = 9
519 _get_deldocs = 10
520 _set_reader = 11
521 _get_reader = 12
522 set_read_positions = 13
523 get_read_positions = 14
524 _set_skip_interval = 15
525 _get_skip_interval = 16
526 CODE:
527 {
528 4634 SegTermDocsChild *child = (SegTermDocsChild*)term_docs->child;
529  
530 4634 ▶ KINO_START_SET_OR_GET_SWITCH
Branch: 2 branches
BranchTrueFalseTotal
KINO_START_SET_OR_GET_SWITCH 4001 633 4634
KINO_START_SET_OR_GET_SWITCH 0 4001 4001
531  
532 0 case 1: child->count = SvUV(ST(1));
533 /* fall through */
534 0 case 2: RETVAL = newSVuv(child->count);
535 0 break;
536  
537 642 case 3: SvREFCNT_dec(child->freq_stream_sv);
538 642 child->freq_stream_sv = newSVsv( ST(1) );
539 642 ▶ Kino1_extract_struct( child->freq_stream_sv, child->freq_stream,
Branch: 1 branch
BranchTrueFalseTotal
Kino1_extract_struct( child->freq_stream_sv, child->freq_stream, 642 0 642
540 InStream*, "KinoSearch1::Store::InStream");
541 /* fall through */
542 642 case 4: RETVAL = newSVsv(child->freq_stream_sv);
543 642 break;
544  
545 642 case 5: SvREFCNT_dec(child->prox_stream_sv);
546 642 child->prox_stream_sv = newSVsv( ST(1) );
547 642 ▶ Kino1_extract_struct( child->prox_stream_sv, child->prox_stream,
Branch: 1 branch
BranchTrueFalseTotal
Kino1_extract_struct( child->prox_stream_sv, child->prox_stream, 642 0 642
548 InStream*, "KinoSearch1::Store::InStream");
549 /* fall through */
550 642 case 6: RETVAL = newSVsv(child->prox_stream_sv);
551 642 break;
552  
553 642 case 7: SvREFCNT_dec(child->skip_stream_sv);
554 642 child->skip_stream_sv = newSVsv( ST(1) );
555 642 ▶ Kino1_extract_struct( child->skip_stream_sv, child->skip_stream,
Branch: 1 branch
BranchTrueFalseTotal
Kino1_extract_struct( child->skip_stream_sv, child->skip_stream, 642 0 642
556 InStream*, "KinoSearch1::Store::InStream");
557 /* fall through */
558 642 case 8: RETVAL = newSVsv(child->skip_stream_sv);
559 642 break;
560  
561 642 case 9: SvREFCNT_dec(child->deldocs_sv);
562 642 child->deldocs_sv = newSVsv( ST(1) );
563 642 ▶ Kino1_extract_struct( child->deldocs_sv, child->deldocs,
Branch: 1 branch
BranchTrueFalseTotal
Kino1_extract_struct( child->deldocs_sv, child->deldocs, 642 0 642
564 BitVector*, "KinoSearch1::Index::DelDocs" );
565 /* fall through */
566 643 case 10: RETVAL = newSVsv(child->deldocs_sv);
567 643 break;
568  
569 642 case 11: SvREFCNT_dec(child->reader_sv);
570 642 ▶ if (!sv_derived_from( ST(1), "KinoSearch1::Index::IndexReader") )
Branch: 1 branch
BranchTrueFalseTotal
if (!sv_derived_from( ST(1), "KinoSearch1::Index::IndexReader") ) 0 642 642
571 0 Kino1_confess("not a KinoSearch1::Index::IndexReader");
572 642 child->reader_sv = newSVsv( ST(1) );
573 /* fall through */
574 1274 case 12: RETVAL = newSVsv(child->reader_sv);
575 1274 break;
576  
577 149 case 13: child->read_positions = SvTRUE( ST(1) ) ? 1 : 0;
578 /* fall through */
579 149 case 14: RETVAL = newSViv(child->read_positions);
580 149 break;
581  
582 642 case 15: child->skip_interval = SvUV(ST(1));
583 /* fall through */
584 642 case 16: RETVAL = newSVuv(child->skip_interval);
585 642 break;
586  
587 0 KINO_END_SET_OR_GET_SWITCH
588 }
589 OUTPUT: RETVAL
590  
591  
592 MODULE = KinoSearch1 PACKAGE = KinoSearch1::Index::SegTermEnum
593  
594  
595 SegTermEnum*
596 _new_helper(instream_sv, is_index, finfos_sv, term_buffer_sv)
597 SV *instream_sv;
598 I32 is_index;
599 SV *finfos_sv
600 SV *term_buffer_sv;
601 CODE:
602 208 RETVAL = Kino1_SegTermEnum_new_helper(instream_sv, is_index, finfos_sv,
603 term_buffer_sv);
604 OUTPUT: RETVAL
605  
606  
607 =for comment
608  
609 fill_cache() loads the entire Enum into memory. This should only be called
610 for index Enums -- never for primary Enums.
611  
612 =cut
613  
614 void
615 fill_cache(obj)
616 SegTermEnum *obj;
617 PPCODE:
618 95 Kino1_SegTermEnum_fill_cache(obj);
619  
620  
621 =begin comment
622  
623 scan_to() iterates through the Enum until the Enum's state is ge the target.
624 This is called on the main Enum, after seek() has gotten it close. You don't
625 want to scan through the entire main Enum, just through a small part.
626  
627 Scanning through an Enum is an involved process, due to the heavy data
628 compression. See the Java Lucene File Format definition for details.
629  
630 =end comment
631 =cut
632  
633 void
634 scan_to(obj, target_termstring_sv)
635 SegTermEnum *obj;
636 SV *target_termstring_sv;
637 PREINIT:
638 char *ptr;
639 STRLEN len;
640 PPCODE:
641 1980 ptr = SvPV(target_termstring_sv, len);
642 1980 ▶ if (len < 2)
Branch: 1 branch
BranchTrueFalseTotal
if (len < 2) 0 1980 1980
643 0 Kino1_confess("length of termstring < 2: %"UVuf, (UV)len);
644 1980 Kino1_SegTermEnum_scan_to(obj, ptr, len);
645  
646  
647 =for comment
648  
649 Reset the Enum to the top, so that after next() is called, the Enum is located
650 at the first term in the segment.
651  
652 =cut
653  
654 void
655 reset(obj)
656 SegTermEnum *obj;
657 PPCODE:
658 16 Kino1_SegTermEnum_reset(obj);
659  
660  
661 =for comment
662  
663 next() advances the state of the Enum one term. If the current position of
664 the Enum is valid, it returns 1; when the Enum is exhausted, it returns 0.
665  
666 =cut
667  
668 IV
669 next(obj)
670 SegTermEnum *obj;
671 CODE:
672 11 ▶ RETVAL = Kino1_SegTermEnum_next(obj);
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = Kino1_SegTermEnum_next(obj); 9 2 11
673 OUTPUT: RETVAL
674  
675  
676 =for comment
677  
678 For an Enum which has been loaded into memory, scan to the target as quickly
679 as possible.
680  
681 =cut
682  
683 I32
684 scan_cache(obj, target_termstring_sv)
685 SegTermEnum *obj;
686 SV *target_termstring_sv;
687 PREINIT:
688 char *ptr;
689 STRLEN len;
690 CODE:
691 1980 ptr = SvPV(target_termstring_sv, len);
692 1980 ▶ if (len < 2)
Branch: 1 branch
BranchTrueFalseTotal
if (len < 2) 0 1980 1980
693 0 Kino1_confess("length of termstring < 2: %"UVuf, (UV)len);
694 1980 RETVAL = Kino1_SegTermEnum_scan_cache(obj, ptr, len);
695 OUTPUT: RETVAL
696  
697  
698 =for comment
699  
700 Setters and getters for members in the SegTermEnum struct. Not all of these
701 are useful.
702  
703 =cut
704  
705 SV*
706 _set_or_get(obj, ...)
707 SegTermEnum *obj;
708 ALIAS:
709 _set_instream = 1
710 _get_instream = 2
711 _set_finfos = 3
712 _get_finfos = 4
713 _set_size = 5
714 get_size = 6
715 _set_termstring = 7
716 get_termstring = 8
717 _set_term_info = 9
718 get_term_info = 10
719 _set_index_interval = 11
720 get_index_interval = 12
721 _set_skip_interval = 13
722 get_skip_interval = 14
723 _set_position = 15
724 _get_position = 16
725 _set_is_index = 17
726 is_index = 18
727 CODE:
728 {
729 18535 ▶ KINO_START_SET_OR_GET_SWITCH
Branch: 2 branches
BranchTrueFalseTotal
KINO_START_SET_OR_GET_SWITCH 5994 12541 18535
KINO_START_SET_OR_GET_SWITCH 0 5994 5994
730  
731 0 case 0: croak("can't call _get_or_set on it's own");
732 break; /* probably unreachable */
733  
734 0 case 1: SvREFCNT_dec(obj->instream_sv);
735 0 obj->instream_sv = newSVsv( ST(1) );
736 /* fall through */
737 2094 case 2: RETVAL = newSVsv(obj->instream_sv);
738 2094 break;
739  
740 0 case 3: SvREFCNT_dec(obj->finfos);
741 0 obj->finfos = newSVsv( ST(1) );
742 /* fall through */
743 17 case 4: RETVAL = newSVsv(obj->finfos);
744 17 break;
745  
746 0 case 5: obj->enum_size = (I32)SvIV( ST(1) );
747 /* fall through */
748 0 case 6: RETVAL = newSViv(obj->enum_size);
749 0 break;
750  
751 1998 ▶ case 7: if ( SvOK( ST(1) ) ) {
Branch: 1 branch
BranchTrueFalseTotal
case 7: if ( SvOK( ST(1) ) ) { 1982 16 1998
752 1982 STRLEN len = SvCUR( ST(1) );
753 1982 ▶ if (len < KINO_FIELD_NUM_LEN)
Branch: 1 branch
BranchTrueFalseTotal
if (len < KINO_FIELD_NUM_LEN) 0 1982 1982
754 0 Kino1_confess("Internal error: termstring too short");
755 1982 Kino1_TermBuf_set_termstring(obj->term_buf,
756 1982 SvPVX(ST(1)), len);
757 }
758 else {
759 16 Kino1_TermBuf_reset(obj->term_buf);
760 }
761 /* fall through */
762 11972 case 8: RETVAL = (obj->term_buf->termstring == NULL)
763 ? &PL_sv_undef
764 5986 ▶ : newSVpv( obj->term_buf->termstring->ptr,
Branch: 1 branch
BranchTrueFalseTotal
: newSVpv( obj->term_buf->termstring->ptr, 5914 72 5986
765 obj->term_buf->termstring->size );
766 5986 break;
767  
768 1998 case 9: {
769 TermInfo* new_tinfo;
770 1998 ▶ Kino1_extract_struct( ST(1), new_tinfo, TermInfo*,
Branch: 1 branch
BranchTrueFalseTotal
Kino1_extract_struct( ST(1), new_tinfo, TermInfo*, 1998 0 1998
771 "KinoSearch1::Index::TermInfo");
772 1998 Kino1_TInfo_destroy(obj->tinfo);
773 1998 obj->tinfo = Kino1_TInfo_dupe(new_tinfo);
774 }
775 /* fall through */
776 5783 case 10: {
777 TermInfo* new_tinfo;
778 5783 RETVAL = newSV(0);
779 5783 new_tinfo = Kino1_TInfo_dupe(obj->tinfo);
780 5783 sv_setref_pv(RETVAL, "KinoSearch1::Index::TermInfo",
781 (void*)new_tinfo);
782 }
783 5783 break;
784  
785 0 case 11: obj->index_interval = SvIV( ST(1) );
786 /* fall through */
787 1980 case 12: RETVAL = newSViv(obj->index_interval);
788 1980 break;
789  
790 0 case 13: obj->skip_interval = SvIV( ST(1) );
791 /* fall through */
792 642 case 14: RETVAL = newSViv(obj->skip_interval);
793 642 break;
794  
795 1998 case 15: obj->position = SvIV( ST(1) );
796 /* fall through */
797 2016 case 16: RETVAL = newSViv(obj->position);
798 2016 break;
799  
800 0 case 17: Kino1_confess("can't set is_index");
801 /* fall through */
802 17 case 18: RETVAL = newSViv(obj->is_index);
803 17 break;
804
805 0 KINO_END_SET_OR_GET_SWITCH
806 }
807 OUTPUT: RETVAL
808  
809  
810 void
811 DESTROY(obj)
812 SegTermEnum* obj;
813 PPCODE:
814 208 Kino1_SegTermEnum_destroy(obj);
815  
816  
817 MODULE = KinoSearch1 PACKAGE = KinoSearch1::Index::SegWriter
818  
819 void
820 _write_remapped_norms(outstream, doc_map_ref, norms_ref)
821 OutStream *outstream;
822 SV *doc_map_ref;
823 SV *norms_ref;
824 PPCODE:
825 18 Kino1_SegWriter_write_remapped_norms(outstream, doc_map_ref, norms_ref);
826  
827  
828 MODULE = KinoSearch1 PACKAGE = KinoSearch1::Index::TermBuffer
829  
830 void
831 _new(class, finfos_size)
832 char *class;
833 I32 finfos_size;
834 PREINIT:
835 TermBuffer *term_buf;
836 PPCODE:
837 208 term_buf = Kino1_TermBuf_new(finfos_size);
838 208 ST(0) = sv_newmortal();
839 208 sv_setref_pv(ST(0), class, (void*)term_buf);
840 208 XSRETURN(1);
841
842  
843 void
844 DESTROY(term_buf)
845 TermBuffer *term_buf;
846 PPCODE:
847 208 Kino1_TermBuf_destroy(term_buf);
848  
849  
850 MODULE = KinoSearch1 PACKAGE = KinoSearch1::Index::TermDocs
851  
852 void
853 new(either_sv)
854 SV *either_sv;
855 PREINIT:
856 const char *class;
857 TermDocs *term_docs;
858 PPCODE:
859 /* determine the class */
860 645 class = sv_isobject(either_sv)
861 0 ? sv_reftype(either_sv, 0)
862 1290 ▶ : SvPV_nolen(either_sv);
Branch: 1 branch
BranchTrueFalseTotal
: SvPV_nolen(either_sv); 0 645 645
863  
864 /* build object */
865 645 term_docs = Kino1_TermDocs_new();
866 645 ST(0) = sv_newmortal();
867 645 sv_setref_pv(ST(0), class, (void*)term_docs);
868 645 XSRETURN(1);
869  
870 void
871 seek_tinfo(term_docs, maybe_tinfo_sv)
872 TermDocs *term_docs;
873 SV *maybe_tinfo_sv;
874 PREINIT:
875 660 TermInfo *tinfo = NULL;
876 PPCODE:
877 /* if maybe_tinfo_sv is undef, tinfo is NULL */
878 660 ▶ if (SvOK(maybe_tinfo_sv)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(maybe_tinfo_sv)) { 564 96 660
879 564 ▶ Kino1_extract_struct(maybe_tinfo_sv, tinfo,
Branch: 1 branch
BranchTrueFalseTotal
Kino1_extract_struct(maybe_tinfo_sv, tinfo, 564 0 564
880 TermInfo*, "KinoSearch1::Index::TermInfo");
881 }
882 660 term_docs->seek_tinfo(term_docs, tinfo);
883  
884  
885 =begin comment
886  
887 while ($term_docs->next) {
888 # ...
889 }
890  
891 Advance the TermDocs object to the next document. Returns false when the
892 iterator is exhausted, true otherwise.
893  
894 =end comment
895 =cut
896  
897 bool
898 next(term_docs)
899 TermDocs *term_docs;
900 CODE:
901 12 RETVAL = term_docs->next(term_docs);
902 OUTPUT: RETVAL
903  
904 U32
905 bulk_read(term_docs, doc_nums_sv, freqs_sv, num_wanted)
906 TermDocs *term_docs
907 SV *doc_nums_sv;
908 SV *freqs_sv;
909 U32 num_wanted;
910 CODE:
911 8 RETVAL = term_docs->bulk_read(term_docs, doc_nums_sv, freqs_sv,
912 num_wanted);
913 OUTPUT: RETVAL
914  
915 =begin comment
916  
917 To do.
918  
919 =end comment
920 =cut
921  
922 bool
923 skip_to(term_docs, target)
924 TermDocs *term_docs;
925 U32 target;
926 CODE:
927 0 RETVAL = term_docs->skip_to(term_docs, target);
928 OUTPUT: RETVAL
929  
930 SV*
931 _parent_set_or_get(term_docs, ...)
932 TermDocs *term_docs;
933 ALIAS:
934 set_doc = 1
935 get_doc = 2
936 set_freq = 3
937 get_freq = 4
938 set_positions = 5
939 get_positions = 6
940 set_doc_freq = 7
941 get_doc_freq = 8
942 PREINIT:
943 U32 num;
944 CODE:
945 {
946 632 ▶ KINO_START_SET_OR_GET_SWITCH
Branch: 2 branches
BranchTrueFalseTotal
KINO_START_SET_OR_GET_SWITCH 0 632 632
KINO_START_SET_OR_GET_SWITCH 0 0 0
947  
948 0 case 1: Kino1_confess("Can't set_doc");
949 /* fall through */
950 3 case 2: num = term_docs->get_doc(term_docs);
951 3 RETVAL = num == KINO_TERM_DOCS_SENTINEL
952 ? &PL_sv_undef
953 3 ▶ : newSVuv(num);
Branch: 1 branch
BranchTrueFalseTotal
: newSVuv(num); 3 0 3
954 3 break;
955  
956 0 case 3: Kino1_confess("Can't set_freq");
957 /* fall through */
958 0 case 4: num = term_docs->get_freq(term_docs);
959 0 RETVAL = num == KINO_TERM_DOCS_SENTINEL
960 ? &PL_sv_undef
961 0 ▶ : newSVuv(num);
Branch: 1 branch
BranchTrueFalseTotal
: newSVuv(num); 0 0 0
962 0 break;
963  
964 0 case 5: Kino1_confess("Can't set_positions");
965 /* fall through */
966 6 case 6: RETVAL = newSVsv(term_docs->get_positions(term_docs));
967 6 break;
968  
969 0 case 7: term_docs->set_doc_freq(term_docs, (U32)SvUV(ST(1)) );
970 /* fall through */
971 623 case 8: num = term_docs->get_doc_freq(term_docs);
972 623 RETVAL = num == KINO_TERM_DOCS_SENTINEL
973 ? &PL_sv_undef
974 623 ▶ : newSVuv(num);
Branch: 1 branch
BranchTrueFalseTotal
: newSVuv(num); 623 0 623
975 623 break;
976  
977 0 KINO_END_SET_OR_GET_SWITCH
978 }
979 OUTPUT: RETVAL
980  
981 void
982 DESTROY(term_docs)
983 TermDocs *term_docs;
984 PPCODE:
985 645 term_docs->destroy(term_docs);
986  
987  
988  
989 MODULE = KinoSearch1 PACKAGE = KinoSearch1::Index::TermInfo
990  
991 TermInfo*
992 new( class_sv, doc_freq, frq_fileptr, prx_fileptr, skip_offset, index_fileptr )
993 SV *class_sv;
994 I32 doc_freq;
995 double frq_fileptr;
996 double prx_fileptr;
997 I32 skip_offset;
998 double index_fileptr;
999 PREINIT:
1000 TermInfo *tinfo;
1001 CODE:
1002 1 class_sv = NULL; /* suppress "unused variable" warning */
1003 1 Kino1_New(0, tinfo, 1, TermInfo);
1004 1 tinfo->doc_freq = doc_freq;
1005 1 tinfo->frq_fileptr = frq_fileptr;
1006 1 tinfo->prx_fileptr = prx_fileptr;
1007 1 tinfo->skip_offset = skip_offset;
1008 1 tinfo->index_fileptr = index_fileptr;
1009 1 RETVAL = tinfo;
1010 OUTPUT: RETVAL
1011  
1012  
1013 =begin comment
1014  
1015 Duplicate a TermInfo object.
1016  
1017 =end comment
1018 =cut
1019  
1020 TermInfo*
1021 clone(tinfo)
1022 TermInfo *tinfo;
1023 CODE:
1024 1 RETVAL = Kino1_TInfo_dupe(tinfo);
1025 OUTPUT: RETVAL
1026  
1027 =for comment
1028 Zero out the TermInfo object.
1029  
1030 =cut
1031  
1032 void
1033 reset(tinfo)
1034 TermInfo *tinfo;
1035 PPCODE:
1036 0 Kino1_TInfo_reset(tinfo);
1037  
1038  
1039 =begin comment
1040  
1041 Setters and getters.
1042  
1043 =end comment
1044 =cut
1045  
1046 SV*
1047 _set_or_get(tinfo, ...)
1048 TermInfo *tinfo;
1049 ALIAS:
1050 set_doc_freq = 1
1051 get_doc_freq = 2
1052 set_frq_fileptr = 3
1053 get_frq_fileptr = 4
1054 set_prx_fileptr = 5
1055 get_prx_fileptr = 6
1056 set_skip_offset = 7
1057 get_skip_offset = 8
1058 set_index_fileptr = 9
1059 get_index_fileptr = 10
1060 CODE:
1061 {
1062 3223 ▶ KINO_START_SET_OR_GET_SWITCH
Branch: 2 branches
BranchTrueFalseTotal
KINO_START_SET_OR_GET_SWITCH 5 3218 3223
KINO_START_SET_OR_GET_SWITCH 0 5 5
1063  
1064 1 case 1: tinfo->doc_freq = SvIV(ST(1));
1065 /* fall through */
1066 1227 case 2: RETVAL = newSViv(tinfo->doc_freq);
1067 1227 break;
1068  
1069 1 case 3: tinfo->frq_fileptr = SvNV(ST(1));
1070 /* fall through */
1071 4 case 4: RETVAL = newSVnv(tinfo->frq_fileptr);
1072 4 break;
1073  
1074 1 case 5: tinfo->prx_fileptr = SvNV(ST(1));
1075 /* fall through */
1076 4 case 6: RETVAL = newSVnv(tinfo->prx_fileptr);
1077 4 break;
1078  
1079 1 case 7: tinfo->skip_offset = SvIV(ST(1));
1080 /* fall through */
1081 4 case 8: RETVAL = newSViv(tinfo->skip_offset);
1082 4 break;
1083  
1084 1 case 9: tinfo->index_fileptr = SvNV(ST(1));
1085 /* fall through */
1086 1984 case 10: RETVAL = newSVnv(tinfo->index_fileptr);
1087 1984 break;
1088
1089 0 KINO_END_SET_OR_GET_SWITCH
1090 }
1091 OUTPUT: RETVAL
1092  
1093 void
1094 DESTROY(tinfo)
1095 TermInfo* tinfo;
1096 CODE:
1097 5785 Kino1_Safefree(tinfo);
1098  
1099  
1100 MODULE = KinoSearch1 PACKAGE = KinoSearch1::Index::TermInfosWriter
1101  
1102 TermInfosWriter*
1103 _new(outstream_sv, is_index, index_interval, skip_interval)
1104 SV *outstream_sv;
1105 I32 is_index;
1106 I32 index_interval;
1107 I32 skip_interval;
1108 CODE:
1109 124 RETVAL = Kino1_TInfosWriter_new(outstream_sv, is_index, index_interval,
1110 skip_interval);
1111 OUTPUT: RETVAL
1112  
1113 =for comment
1114  
1115 Add a Term (encoded as a termstring) and its associated TermInfo.
1116  
1117 =cut
1118  
1119 void
1120 add(obj, termstring_sv, tinfo)
1121 TermInfosWriter *obj;
1122 SV *termstring_sv;
1123 TermInfo *tinfo;
1124 PREINIT:
1125 ByteBuf bb;
1126 STRLEN len;
1127 PPCODE:
1128 0 bb.ptr = SvPV(termstring_sv, len);
1129 0 bb.size = len;
1130 0 Kino1_TInfosWriter_add(obj, &bb, tinfo);
1131  
1132 =for comment
1133  
1134 Export the FORMAT constant to Perl.
1135  
1136 =cut
1137  
1138 IV
1139 FORMAT()
1140 CODE:
1141 0 ▶ RETVAL = KINO_TINFOS_FORMAT;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = KINO_TINFOS_FORMAT; 0 0 0
1142 OUTPUT: RETVAL
1143  
1144  
1145 SV*
1146 _set_or_get(obj, ...)
1147 TermInfosWriter *obj;
1148 ALIAS:
1149 _set_other = 1
1150 _get_other = 2
1151 _get_outstream = 4
1152 _get_is_index = 6
1153 _get_size = 8
1154 CODE:
1155 {
1156 558 ▶ KINO_START_SET_OR_GET_SWITCH
Branch: 2 branches
BranchTrueFalseTotal
KINO_START_SET_OR_GET_SWITCH 124 434 558
KINO_START_SET_OR_GET_SWITCH 0 124 124
1157  
1158 124 case 1: SvREFCNT_dec(obj->other_sv);
1159 124 obj->other_sv = newSVsv( ST(1) );
1160 124 ▶ Kino1_extract_struct(obj->other_sv, obj->other, TermInfosWriter*,
Branch: 1 branch
BranchTrueFalseTotal
Kino1_extract_struct(obj->other_sv, obj->other, TermInfosWriter*, 124 0 124
1161 "KinoSearch1::Index::TermInfosWriter");
1162 /* fall through */
1163 186 case 2: RETVAL = newSVsv(obj->other_sv);
1164 186 break;
1165  
1166 124 case 4: RETVAL = newSVsv(obj->fh_sv);
1167 124 break;
1168  
1169 124 case 6: RETVAL = newSViv(obj->is_index);
1170 124 break;
1171  
1172 124 case 8: RETVAL = newSViv(obj->size);
1173 124 break;
1174  
1175 0 KINO_END_SET_OR_GET_SWITCH
1176 }
1177 OUTPUT: RETVAL
1178  
1179  
1180 void
1181 DESTROY(obj)
1182 TermInfosWriter *obj;
1183 PPCODE:
1184 124 Kino1_TInfosWriter_destroy(obj);
1185  
1186  
1187 MODULE = KinoSearch1 PACKAGE = KinoSearch1::Search::BooleanScorer
1188  
1189 void
1190 _init_child(scorer)
1191 Scorer *scorer;
1192 PPCODE:
1193 248 Kino1_BoolScorer_init_child(scorer);
1194  
1195 =for comment
1196 Add a scorer for a sub-query of the BooleanQuery.
1197  
1198 =cut
1199  
1200 void
1201 add_subscorer(scorer, subscorer_sv, occur)
1202 Scorer *scorer;
1203 SV *subscorer_sv;
1204 char *occur;
1205 PREINIT:
1206 BoolScorerChild* child;
1207 Scorer *subscorer;
1208 SV *subscorer_sv_copy;
1209 PPCODE:
1210 463 child = (BoolScorerChild*)scorer->child;
1211 463 ▶ Kino1_extract_struct(subscorer_sv, subscorer,
Branch: 1 branch
BranchTrueFalseTotal
Kino1_extract_struct(subscorer_sv, subscorer, 463 0 463
1212 Scorer*, "KinoSearch1::Search::Scorer");
1213 463 subscorer_sv_copy = newSVsv(subscorer_sv);
1214 463 av_push(child->subscorers_av, subscorer_sv_copy);
1215 463 Kino1_BoolScorer_add_subscorer(scorer, subscorer, occur);
1216  
1217 SV*
1218 _boolean_scorer_set_or_get(scorer, ...)
1219 Scorer* scorer;
1220 ALIAS:
1221 _get_subscorer_storage = 2
1222 CODE:
1223 {
1224 0 BoolScorerChild* child = (BoolScorerChild*)scorer->child;
1225  
1226 0 ▶ KINO_START_SET_OR_GET_SWITCH
Branch: 3 branches
BranchTrueFalseTotal
KINO_START_SET_OR_GET_SWITCH 0 0 0
KINO_START_SET_OR_GET_SWITCH 0 0 0
KINO_START_SET_OR_GET_SWITCH 0 0 0
1227  
1228 0 case 2: RETVAL = newRV((SV*)child->subscorers_av);
1229 0 break;
1230  
1231 0 KINO_END_SET_OR_GET_SWITCH
1232 }
1233 OUTPUT: RETVAL
1234  
1235 void
1236 DESTROY(scorer)
1237 Scorer *scorer;
1238 PPCODE:
1239 248 Kino1_BoolScorer_destroy(scorer);
1240  
1241  
1242 MODULE = KinoSearch1 PACKAGE = KinoSearch1::Search::HitCollector
1243  
1244 void
1245 new(either_sv)
1246 SV *either_sv;
1247 PREINIT:
1248 const char *class;
1249 HitCollector *hc;
1250 PPCODE:
1251 293 hc = Kino1_HC_new();
1252 293 class = sv_isobject(either_sv)
1253 0 ? sv_reftype(either_sv, 0)
1254 586 ▶ : SvPV_nolen(either_sv);
Branch: 1 branch
BranchTrueFalseTotal
: SvPV_nolen(either_sv); 0 293 293
1255 293 ST(0) = sv_newmortal();
1256 293 sv_setref_pv(ST(0), class, (void*)hc);
1257 293 XSRETURN(1);
1258  
1259 =begin comment
1260  
1261 $hit_collector->collect( $doc_num, $score );
1262  
1263 Process a doc_num/score combination. In production, this method should not be
1264 called from Perl, as collecting hits is an extremely data-intensive operation.
1265  
1266 =end comment
1267 =cut
1268  
1269 void
1270 collect(hc, doc_num, score)
1271 HitCollector *hc;
1272 U32 doc_num;
1273 float score;
1274 PPCODE:
1275 13 hc->collect(hc, doc_num, score);
1276  
1277 SV*
1278 _set_or_get(hc, ...)
1279 HitCollector *hc;
1280 ALIAS:
1281 _set_storage = 1
1282 get_storage = 2
1283 _set_i = 3
1284 get_i = 4
1285 _set_f = 5
1286 _get_f = 6
1287 _set_filter_bits = 7
1288 _get_filter_bits = 8
1289 CODE:
1290 {
1291 871 ▶ KINO_START_SET_OR_GET_SWITCH
Branch: 2 branches
BranchTrueFalseTotal
KINO_START_SET_OR_GET_SWITCH 302 569 871
KINO_START_SET_OR_GET_SWITCH 0 302 302
1292
1293 293 case 1: SvREFCNT_dec(hc->storage_ref);
1294 293 hc->storage_ref = newSVsv( ST(1) );
1295 293 ▶ Kino1_extract_anon_struct(hc->storage_ref, hc->storage);
Branch: 1 branch
BranchTrueFalseTotal
Kino1_extract_anon_struct(hc->storage_ref, hc->storage); 293 0 293
1296 /* fall through */
1297 581 case 2: RETVAL = newSVsv(hc->storage_ref);
1298 581 break;
1299  
1300 0 case 3: hc->i = SvUV( ST(1) );
1301 /* fall through */
1302 281 case 4: RETVAL = newSVuv(hc->i);
1303 281 break;
1304  
1305 8 case 5: hc->f = SvNV( ST(1) );
1306 /* fall through */
1307 8 case 6: RETVAL = newSVnv(hc->f);
1308 8 break;
1309
1310 1 case 7: SvREFCNT_dec(hc->filter_bits_ref);
1311 1 hc->filter_bits_ref = newSVsv( ST(1) );
1312 1 ▶ Kino1_extract_struct( hc->filter_bits_ref, hc->filter_bits,
Branch: 1 branch
BranchTrueFalseTotal
Kino1_extract_struct( hc->filter_bits_ref, hc->filter_bits, 1 0 1
1313 BitVector*, "KinoSearch1::Util::BitVector" );
1314 /* fall through */
1315 1 case 8: RETVAL = newSVsv(hc->filter_bits_ref);
1316 1 break;
1317  
1318 0 KINO_END_SET_OR_GET_SWITCH
1319 }
1320 OUTPUT: RETVAL
1321  
1322 void
1323 DESTROY(hc)
1324 HitCollector *hc;
1325 PPCODE:
1326 293 Kino1_HC_destroy(hc);
1327  
1328  
1329 MODULE = KinoSearch1 PACKAGE = KinoSearch1::Search::HitQueueCollector
1330  
1331 void
1332 _define_collect(hc)
1333 HitCollector *hc;
1334 PPCODE:
1335 282 hc->collect = Kino1_HC_collect_HitQueue;
1336  
1337 MODULE = KinoSearch1 PACKAGE = KinoSearch1::Search::BitCollector
1338  
1339 void
1340 _define_collect(hc)
1341 HitCollector *hc;
1342 PPCODE:
1343 2 hc->collect = Kino1_HC_collect_BitVec;
1344  
1345 MODULE = KinoSearch1 PACKAGE = KinoSearch1::Search::FilteredCollector
1346  
1347 void
1348 _define_collect(hc);
1349 HitCollector *hc;
1350 PPCODE:
1351 1 hc->collect = Kino1_HC_collect_filtered;
1352  
1353 MODULE = KinoSearch1 PACKAGE = KinoSearch1::Search::OffsetCollector
1354  
1355 void
1356 _define_collect(hc);
1357 HitCollector *hc;
1358 PPCODE:
1359 8 hc->collect = Kino1_HC_collect_offset;
1360  
1361  
1362  
1363  
1364 MODULE = KinoSearch1 PACKAGE = KinoSearch1::Search::HitQueue
1365  
1366 void
1367 define_less_than(hitq)
1368 PriorityQueue *hitq;
1369 PPCODE:
1370 283 hitq->less_than = &Kino1_HitQ_less_than;
1371  
1372  
1373 MODULE = KinoSearch1 PACKAGE = KinoSearch1::Search::PhraseScorer
1374  
1375 void
1376 _init_child(scorer)
1377 Scorer *scorer;
1378 PPCODE:
1379 60 Kino1_PhraseScorer_init_child(scorer);
1380  
1381 void
1382 _init_elements(scorer, term_docs_av, phrase_offsets_av)
1383 Scorer *scorer;
1384 AV *term_docs_av;
1385 AV *phrase_offsets_av;
1386 PREINIT:
1387 PhraseScorerChild *child;
1388 I32 i;
1389 SV **sv_ptr;
1390 IV tmp;
1391 PPCODE:
1392 {
1393 60 child = (PhraseScorerChild*)scorer->child;
1394  
1395 60 SvREFCNT_inc(term_docs_av);
1396 60 SvREFCNT_dec(child->term_docs_av);
1397 60 child->term_docs_av = term_docs_av;
1398  
1399 60 child->num_elements = av_len(term_docs_av) + 1;
1400 60 Kino1_New(0, child->term_docs, child->num_elements, TermDocs*);
1401 60 Kino1_New(0, child->phrase_offsets, child->num_elements, U32);
1402
1403 /* create an array of TermDocs* */
1404 188 ▶ for(i = 0; i < child->num_elements; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for(i = 0; i < child->num_elements; i++) { 128 60 188
1405 128 sv_ptr = av_fetch(term_docs_av, i, 0);
1406 128 tmp = SvIV((SV*)SvRV( *sv_ptr ));
1407 128 child->term_docs[i] = INT2PTR(TermDocs*, tmp);
1408 128 sv_ptr = av_fetch(phrase_offsets_av, i, 0);
1409 128 child->phrase_offsets[i] = SvIV( *sv_ptr );
1410 }
1411 }
1412  
1413 SV*
1414 _phrase_scorer_set_or_get(scorer, ...)
1415 Scorer *scorer;
1416 ALIAS:
1417 _set_slop = 1
1418 _get_slop = 2
1419 _set_weight_value = 3
1420 _get_weight_value = 4
1421 _set_norms = 5
1422 _get_norms = 6
1423 CODE:
1424 {
1425 180 PhraseScorerChild *child = (PhraseScorerChild*)scorer->child;
1426  
1427 180 ▶ KINO_START_SET_OR_GET_SWITCH
Branch: 2 branches
BranchTrueFalseTotal
KINO_START_SET_OR_GET_SWITCH 180 0 180
KINO_START_SET_OR_GET_SWITCH 0 180 180
1428  
1429 60 case 1: child->slop = SvIV( ST(1) );
1430 /* fall through */
1431 60 case 2: RETVAL = newSViv(child->slop);
1432 60 break;
1433  
1434 60 case 3: child->weight_value = SvNV( ST(1) );
1435 /* fall through */
1436 60 case 4: RETVAL = newSVnv(child->weight_value);
1437 60 break;
1438  
1439 60 case 5: SvREFCNT_dec(child->norms_sv);
1440 60 child->norms_sv = newSVsv( ST(1) );
1441 {
1442 SV* bytes_deref_sv;
1443 60 bytes_deref_sv = SvRV(child->norms_sv);
1444 60 ▶ if (SvPOK(bytes_deref_sv)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvPOK(bytes_deref_sv)) { 60 0 60
1445 60 child->norms = (unsigned char*)SvPVX(bytes_deref_sv);
1446 }
1447 else {
1448 0 child->norms = NULL;
1449 }
1450 }
1451 /* fall through */
1452 60 case 6: RETVAL = newSVsv(child->norms_sv);
1453 60 break;
1454  
1455 0 KINO_END_SET_OR_GET_SWITCH
1456 }
1457 OUTPUT: RETVAL
1458  
1459 void
1460 DESTROY(scorer)
1461 Scorer *scorer;
1462 PPCODE:
1463 60 Kino1_PhraseScorer_destroy(scorer);
1464  
1465  
1466 MODULE = KinoSearch1 PACKAGE = KinoSearch1::Search::Scorer
1467  
1468 void
1469 _construct_parent(class)
1470 char *class;
1471 PREINIT:
1472 Scorer *scorer;
1473 PPCODE:
1474 724 scorer = Kino1_Scorer_new();
1475 724 ST(0) = sv_newmortal();
1476 724 sv_setref_pv(ST(0), class, (void*)scorer);
1477 724 XSRETURN(1);
1478  
1479 SV*
1480 _scorer_set_or_get(scorer, ...)
1481 Scorer *scorer;
1482 ALIAS:
1483 set_similarity = 1
1484 get_similarity = 2
1485 CODE:
1486 {
1487 724 ▶ KINO_START_SET_OR_GET_SWITCH
Branch: 2 branches
BranchTrueFalseTotal
KINO_START_SET_OR_GET_SWITCH 724 0 724
KINO_START_SET_OR_GET_SWITCH 0 724 724
1488  
1489 724 case 1: SvREFCNT_dec(scorer->similarity_sv);
1490 724 scorer->similarity_sv = newSVsv( ST(1) );
1491 724 ▶ Kino1_extract_struct( scorer->similarity_sv, scorer->sim,
Branch: 1 branch
BranchTrueFalseTotal
Kino1_extract_struct( scorer->similarity_sv, scorer->sim, 724 0 724
1492 Similarity*, "KinoSearch1::Search::Similarity" );
1493 /* fall through */
1494 724 case 2: RETVAL = newSVsv(scorer->similarity_sv);
1495 724 break;
1496  
1497 0 KINO_END_SET_OR_GET_SWITCH
1498 }
1499 OUTPUT: RETVAL
1500  
1501  
1502 =begin comment
1503  
1504 my $score = $scorer->score;
1505  
1506 Calculate and return a score for the scorer's current document.
1507  
1508 =end comment
1509 =cut
1510  
1511 float
1512 score(scorer)
1513 Scorer* scorer;
1514 CODE:
1515 0 RETVAL = scorer->score(scorer);
1516 OUTPUT: RETVAL
1517  
1518  
1519 =begin comment
1520  
1521 my $valid_state = $scorer->next;
1522  
1523 Move the internal state of the scorer to the next document. Return false when
1524 there are no more documents to score.
1525  
1526 =end comment
1527 =cut
1528  
1529 bool
1530 next(scorer)
1531 Scorer* scorer;
1532 CODE:
1533 0 RETVAL = scorer->next(scorer);
1534 OUTPUT: RETVAL
1535  
1536  
1537 =begin comment
1538  
1539 $scorer->score_batch(
1540 hit_collector => $collector,
1541 start => $start,
1542 end => $end,
1543 );
1544  
1545 Execute the scoring number crunching, accumulating results via the
1546 $hit_collector.
1547  
1548 TODO: Doesn't actually pay any attention to start/end at present.
1549  
1550 =end comment
1551 =cut
1552  
1553 void
1554 score_batch(scorer, ...)
1555 Scorer *scorer;
1556 PREINIT:
1557 HV *args_hash;
1558 U32 start, end;
1559 HitCollector *hc;
1560 PPCODE:
1561 /* process hash-style params */
1562 199 ▶ Kino1_Verify_build_args_hash(args_hash,
Branch: 1 branch
BranchTrueFalseTotal
Kino1_Verify_build_args_hash(args_hash, 0 199 199
1563 "KinoSearch1::Search::Scorer::score_batch_args", 1);
1564 199 ▶ Kino1_extract_struct_from_hv(args_hash, hc, "hit_collector", 13,
Branch: 2 branches
BranchTrueFalseTotal
Kino1_extract_struct_from_hv(args_hash, hc, "hit_collector", 13, 0 199 199
Kino1_extract_struct_from_hv(args_hash, hc, "hit_collector", 13, 199 0 199
1565 HitCollector*, "KinoSearch1::Search::HitCollector");
1566 199 start = (U32)SvUV( Kino1_Verify_extract_arg(args_hash, "start", 5) );
1567 199 end = (U32)SvUV( Kino1_Verify_extract_arg(args_hash, "end", 3) );
1568  
1569 /* execute scoring loop */
1570 758 ▶ while (scorer->next(scorer)) {
Branch: 1 branch
BranchTrueFalseTotal
while (scorer->next(scorer)) { 559 199 758
1571 559 hc->collect( hc, scorer->doc(scorer), scorer->score(scorer) );
1572 }
1573  
1574  
1575 =begin comment
1576  
1577 Not implemented yet.
1578  
1579 =end comment
1580 =cut
1581  
1582 bool
1583 skip_to(scorer, target_doc_num)
1584 Scorer* scorer;
1585 U32 target_doc_num;
1586 CODE:
1587 0 RETVAL = scorer->skip_to(scorer, target_doc_num);
1588 OUTPUT: RETVAL
1589  
1590  
1591 void
1592 DESTROY(scorer)
1593 Scorer *scorer;
1594 PPCODE:
1595 0 Kino1_Scorer_destroy(scorer);
1596  
1597
1598  
1599 MODULE = KinoSearch1 PACKAGE = KinoSearch1::Search::Similarity
1600  
1601 =begin comment
1602  
1603 KLUDGE!!
1604  
1605 Rather than attempt to serialize a Similarity, we just create a new one.
1606  
1607 =end comment
1608 =cut
1609  
1610 void
1611 STORABLE_thaw(blank_obj, cloning, serialized)
1612 SV *blank_obj;
1613 SV *cloning;
1614 SV *serialized;
1615 PPCODE:
1616 {
1617 0 Similarity *sim = Kino1_Sim_new();
1618 0 SV *deep_obj = SvRV(blank_obj);
1619 0 sv_setiv(deep_obj, PTR2IV(sim));
1620 }
1621  
1622 void
1623 new(either_sv)
1624 SV *either_sv;
1625 PREINIT:
1626 const char *class;
1627 Similarity *sim;
1628 PPCODE:
1629 /* determine the class */
1630 107 class = sv_isobject(either_sv)
1631 0 ? sv_reftype(either_sv, 0)
1632 214 ▶ : SvPV_nolen(either_sv);
Branch: 1 branch
BranchTrueFalseTotal
: SvPV_nolen(either_sv); 0 107 107
1633  
1634 /* build object */
1635 107 sim = Kino1_Sim_new();
1636 107 ST(0) = sv_newmortal();
1637 107 sv_setref_pv(ST(0), class, (void*)sim);
1638 107 XSRETURN(1);
1639  
1640 =for comment
1641  
1642 Provide a normalization factor for a field based on the square-root of the
1643 number of terms in it.
1644  
1645 =cut
1646  
1647 float
1648 lengthnorm(sim, num_terms)
1649 Similarity *sim;
1650 U32 num_terms;
1651 CODE:
1652 15380 num_terms = num_terms < 100 ? 100 : num_terms;
1653 15380 ▶ RETVAL = (float)1 / sqrt(num_terms);
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = (float)1 / sqrt(num_terms); 5 15375 15380
1654 OUTPUT: RETVAL
1655  
1656 =for comment
1657  
1658 Return a score factor based on the frequency of a term in a given document.
1659 The default implementation is sqrt(freq). Other implementations typically
1660 produce ascending scores with ascending freqs, since the more times a doc
1661 matches, the more relevant it is likely to be.
1662  
1663 =cut
1664  
1665 float
1666 tf(sim, freq)
1667 Similarity *sim;
1668 U32 freq;
1669 CODE:
1670 0 RETVAL = sim->tf(sim, freq);
1671 OUTPUT: RETVAL
1672  
1673  
1674 =for comment
1675  
1676 _float_to_byte and _byte_to_float encode and decode between 32-bit IEEE
1677 floating point numbers and a 5-bit exponent, 3-bit mantissa float. The range
1678 covered by the single-byte encoding is 7x10^9 to 2x10^-9. The accuracy is
1679 about one significant decimal digit.
1680  
1681 =cut
1682  
1683 SV*
1684 _float_to_byte(sim, f)
1685 Similarity *sim;
1686 float f;
1687 PREINIT:
1688 char b;
1689 CODE:
1690 15638 b = Kino1_Sim_float2byte(sim, f);
1691 15638 RETVAL = newSVpv(&b, 1);
1692 OUTPUT: RETVAL
1693  
1694 float
1695 _byte_to_float(sim, b)
1696 Similarity *sim;
1697 char b;
1698 CODE:
1699 261 RETVAL = Kino1_Sim_byte2float(sim, b);
1700 OUTPUT: RETVAL
1701  
1702  
1703 =for comment
1704  
1705 The norm_decoder caches the 256 possible byte => float pairs, obviating the
1706 need to call decode_norm over and over for a scoring implementation that
1707 knows how to use it.
1708  
1709 =cut
1710  
1711 SV*
1712 get_norm_decoder(sim)
1713 Similarity *sim;
1714 CODE:
1715 1 RETVAL = newSVpv( (char*)sim->norm_decoder, (256 * sizeof(float)) );
1716 OUTPUT: RETVAL
1717  
1718 float
1719 coord(sim, overlap, max_overlap)
1720 Similarity *sim;
1721 U32 overlap;
1722 U32 max_overlap;
1723 CODE:
1724 0 RETVAL = sim->coord(sim, overlap, max_overlap);
1725 OUTPUT: RETVAL
1726  
1727 void
1728 _use_title_tf(sim)
1729 Similarity *sim;
1730 PPCODE:
1731 1 sim->tf = Kino1_Sim_title_tf;
1732  
1733 void
1734 DESTROY(sim)
1735 Similarity *sim;
1736 PPCODE:
1737 107 Kino1_Sim_destroy(sim);
1738  
1739
1740  
1741 MODULE = KinoSearch1 PACKAGE = KinoSearch1::Search::TermScorer
1742  
1743 void
1744 _init_child(scorer)
1745 Scorer *scorer;
1746 PPCODE:
1747 416 Kino1_TermScorer_init_child(scorer);
1748  
1749 =for comment
1750  
1751 Build up a cache of scores for common (i.e. low) freqs, so they don't have to
1752 be continually recalculated.
1753  
1754 =cut
1755  
1756 void
1757 _fill_score_cache(scorer)
1758 Scorer* scorer;
1759 PPCODE:
1760 416 Kino1_TermScorer_fill_score_cache(scorer);
1761  
1762 void
1763 score_batch(scorer, ...)
1764 Scorer *scorer;
1765 PREINIT:
1766 HV *args_hash;
1767 U32 start, end;
1768 HitCollector *hc;
1769 PPCODE:
1770 /* process hash-style params */
1771 52 ▶ Kino1_Verify_build_args_hash(args_hash,
Branch: 1 branch
BranchTrueFalseTotal
Kino1_Verify_build_args_hash(args_hash, 0 52 52
1772 "KinoSearch1::Search::TermScorer::score_batch_args", 1);
1773 52 ▶ Kino1_extract_struct_from_hv(args_hash, hc, "hit_collector", 13,
Branch: 2 branches
BranchTrueFalseTotal
Kino1_extract_struct_from_hv(args_hash, hc, "hit_collector", 13, 0 52 52
Kino1_extract_struct_from_hv(args_hash, hc, "hit_collector", 13, 52 0 52
1774 HitCollector*, "KinoSearch1::Search::HitCollector");
1775 52 start = (U32)SvUV( Kino1_Verify_extract_arg(args_hash, "start", 5) );
1776 52 end = (U32)SvUV( Kino1_Verify_extract_arg(args_hash, "end", 3) );
1777  
1778 52 Kino1_TermScorer_score_batch(scorer, start, end, hc);
1779  
1780 SV*
1781 _term_scorer_set_or_get(scorer, ...)
1782 Scorer *scorer;
1783 ALIAS:
1784 _set_term_docs = 1
1785 _get_term_docs = 2
1786 _set_weight = 3
1787 _get_weight = 4
1788 _set_weight_value = 5
1789 _get_weight_value = 6
1790 _set_norms = 7
1791 _get_norms = 8
1792 CODE:
1793 {
1794 1664 TermScorerChild *child = (TermScorerChild*)scorer->child;
1795
1796 1664 ▶ KINO_START_SET_OR_GET_SWITCH
Branch: 2 branches
BranchTrueFalseTotal
KINO_START_SET_OR_GET_SWITCH 1664 0 1664
KINO_START_SET_OR_GET_SWITCH 0 1664 1664
1797  
1798 416 case 1: SvREFCNT_dec(child->term_docs_sv);
1799 416 child->term_docs_sv = newSVsv( ST(1) );
1800 416 ▶ Kino1_extract_struct( child->term_docs_sv, child->term_docs,
Branch: 1 branch
BranchTrueFalseTotal
Kino1_extract_struct( child->term_docs_sv, child->term_docs, 416 0 416
1801 TermDocs*, "KinoSearch1::Index::TermDocs");
1802 /* fall through */
1803 416 case 2: RETVAL = newSVsv(child->term_docs_sv);
1804 416 break;
1805  
1806 416 case 3: SvREFCNT_dec(child->weight_sv);
1807 416 ▶ if (!sv_derived_from( ST(1), "KinoSearch1::Search::Weight"))
Branch: 1 branch
BranchTrueFalseTotal
if (!sv_derived_from( ST(1), "KinoSearch1::Search::Weight")) 0 416 416
1808 0 Kino1_confess("not a KinoSearch1::Search::Weight");
1809 416 child->weight_sv = newSVsv( ST(1) );
1810 /* fall through */
1811 416 case 4: RETVAL = newSVsv(child->weight_sv);
1812 416 break;
1813  
1814 416 case 5: child->weight_value = SvNV( ST(1) );
1815 /* fall through */
1816 416 case 6: RETVAL = newSVnv(child->weight_value);
1817 416 break;
1818  
1819 416 case 7: SvREFCNT_dec(child->norms_sv);
1820 416 child->norms_sv = newSVsv( ST(1) );
1821 {
1822 SV* bytes_deref_sv;
1823 416 bytes_deref_sv = SvRV(child->norms_sv);
1824 416 ▶ if (SvPOK(bytes_deref_sv)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvPOK(bytes_deref_sv)) { 416 0 416
1825 416 child->norms = (unsigned char*)SvPVX(bytes_deref_sv);
1826 }
1827 else {
1828 0 child->norms = NULL;
1829 }
1830 }
1831 /* fall through */
1832 416 case 8: RETVAL = newSVsv(child->norms_sv);
1833 416 break;
1834  
1835 0 KINO_END_SET_OR_GET_SWITCH
1836 }
1837 OUTPUT: RETVAL
1838  
1839 void
1840 DESTROY(scorer)
1841 Scorer *scorer;
1842 PPCODE:
1843 416 Kino1_TermScorer_destroy(scorer);
1844  
1845  
1846 MODULE = KinoSearch1 PACKAGE = KinoSearch1::Store::InStream
1847  
1848 =begin comment
1849  
1850 my $instream = KinoSearch1::Store::Instream->new(
1851 $filehandle, $offset, $length
1852 );
1853  
1854 Constructor. Takes 1-3 arguments, and unlike most classes in the KinoSearch1
1855 suite, the arguments to the constructor are not labeled parameters.
1856  
1857 The second argument, an offset, defaults to 0 if not supplied. Non-zero
1858 offsets get factored in when calling seek and tell.
1859  
1860 The last argument, a length, is the length of the "file" in bytes. Supplying
1861 an explicit value is only essential for InStreams which are assigned to read a
1862 portion of a compound file -- otherwise, the length gets auto-calculated
1863 correctly.
1864  
1865 =end comment
1866 =cut
1867  
1868 InStream*
1869 new(class, fh_sv, ...)
1870 char *class;
1871 SV *fh_sv;
1872 PREINIT:
1873 3638 double offset = 0;
1874 3638 double len = -1;
1875 CODE:
1876 3638 ▶ if (items > 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (items > 2) { 3638 0 3638
1877 SV* offset_sv;
1878 3638 offset_sv = ST(2);
1879 3638 ▶ if (SvOK(offset_sv))
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(offset_sv)) 2804 834 3638
1880 2804 offset = SvNV(offset_sv);
1881 }
1882 3638 ▶ if (items > 3) {
Branch: 1 branch
BranchTrueFalseTotal
if (items > 3) { 3638 0 3638
1883 SV *len_sv;
1884 3638 len_sv = ST(3);
1885 3638 ▶ if (SvOK(len_sv))
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(len_sv)) 2804 834 3638
1886 2804 len = SvNV(len_sv);
1887 }
1888 3638 RETVAL = Kino1_InStream_new(class, fh_sv, offset, len);
1889 OUTPUT: RETVAL
1890  
1891  
1892 =for comment
1893 Seek to target plus the object's start offset.
1894  
1895 =cut
1896  
1897 void
1898 seek(instream, target)
1899 InStream *instream;
1900 double target;
1901 PPCODE:
1902 24326 instream->seek(instream, target);
1903  
1904 =for comment
1905 Return the filehandle's position minus the offset.
1906  
1907 =cut
1908  
1909 double
1910 tell(instream)
1911 InStream *instream;
1912 CODE:
1913 1961 RETVAL = instream->tell(instream);
1914 OUTPUT: RETVAL
1915  
1916 =for comment
1917 Return the length of the "file" in bytes, factoring in the offset.
1918  
1919 =cut
1920  
1921 double
1922 length(instream)
1923 InStream *instream;
1924 CODE:
1925 2138 ▶ RETVAL = instream->len;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = instream->len; 1995 143 2138
1926 OUTPUT: RETVAL
1927  
1928 =begin comment
1929  
1930 @items = $instream->lu_read( TEMPLATE );
1931  
1932 Read the items specified by TEMPLATE from the InStream.
1933  
1934 =end comment
1935 =cut
1936  
1937 SV*
1938 _set_or_get(instream, ...)
1939 InStream *instream;
1940 ALIAS:
1941 set_len = 1
1942 get_len = 2
1943 set_offset = 3
1944 get_offset = 4
1945 set_fh = 5
1946 get_fh = 6
1947 CODE:
1948 {
1949 5139 ▶ KINO_START_SET_OR_GET_SWITCH
Branch: 2 branches
BranchTrueFalseTotal
KINO_START_SET_OR_GET_SWITCH 0 5139 5139
KINO_START_SET_OR_GET_SWITCH 0 0 0
1950  
1951 0 case 1: instream->len = SvNV( ST(1) );
1952 /* fall through */
1953 0 case 2: RETVAL = newSVnv(instream->len);
1954 0 break;
1955
1956 0 case 3: instream->offset = SvNV( ST(1) );
1957 /* fall through */
1958 1943 case 4: RETVAL = newSVnv(instream->offset);
1959 1943 break;
1960
1961 0 case 5: Kino1_confess("Can't set_fh");
1962 /* fall through */
1963 3196 case 6: RETVAL = newSVsv(instream->fh_sv);
1964 3196 break;
1965  
1966 0 KINO_END_SET_OR_GET_SWITCH
1967 }
1968 OUTPUT: RETVAL
1969  
1970  
1971 void
1972 lu_read (instream, template_sv)
1973 InStream *instream;
1974 SV *template_sv
1975 PREINIT:
1976 STRLEN tpt_len; /* bytelength of template */
1977 char *template; /* ptr to a spot in the template */
1978 char *tpt_end; /* ptr to the end of the template */
1979 int repeat_count; /* number of times to repeat sym */
1980 char sym; /* the current symbol in the template */
1981 char countsym; /* used when calculating repeat counts */
1982 IV aIV;
1983 SV *aSV;
1984 char aChar;
1985 char* string;
1986 STRLEN len;
1987 PPCODE:
1988 {
1989 /* prepare template string pointers */
1990 31421 template = SvPV(template_sv, tpt_len);
1991 31421 tpt_end = SvEND(template_sv);
1992  
1993 31421 repeat_count = 0;
1994 while (1) {
1995 106770 ▶ if (repeat_count == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (repeat_count == 0) { 96067 10703 106770
1996 /* fast-forward past space characters */
1997 96102 ▶ while (*template == ' ' && template < tpt_end) {
Branch: 2 branches
BranchTrueFalseTotal
while (*template == ' ' && template < tpt_end) { 35 96067 96102
while (*template == ' ' && template < tpt_end) { 35 0 35
1998 35 template++;
1999 }
2000  
2001 /* break out of the loop if we've exhausted the template */
2002 96067 ▶ if (template == tpt_end) {
Branch: 1 branch
BranchTrueFalseTotal
if (template == tpt_end) { 31421 64646 96067
2003 31421 break;
2004 }
2005
2006 /* derive the current symbol and a possible digit repeat sym */
2007 64646 sym = *template++;
2008 64646 countsym = *template;
2009  
2010 64646 ▶ if (template == tpt_end) {
Branch: 1 branch
BranchTrueFalseTotal
if (template == tpt_end) { 31291 33355 64646
2011 /* sym is last char in template, so process once */
2012 31291 repeat_count = 1;
2013 }
2014 33355 ▶ else if (countsym >= '0' && countsym <= '9') {
Branch: 2 branches
BranchTrueFalseTotal
else if (countsym >= '0' && countsym <= '9') { 33353 2 33355
else if (countsym >= '0' && countsym <= '9') { 155 33198 33353
2015 /* calculate numerical repeat count */
2016 155 repeat_count = countsym - KINO_NUM_CHAR_OFFSET;
2017 155 countsym = *(++template);
2018 155 while ( template <= tpt_end
2019 284 ▶ && countsym >= '0'
Branch: 1 branch
BranchTrueFalseTotal
&& countsym >= '0' 131 153 284
2020 415 ▶ && countsym <= '9'
Branch: 2 branches
BranchTrueFalseTotal
&& countsym <= '9' 284 0 284
&& countsym <= '9' 129 2 131
2021 ) {
2022 129 repeat_count = (repeat_count * 10)
2023 129 + (countsym - KINO_NUM_CHAR_OFFSET);
2024 129 countsym = *(++template);
2025 }
2026 }
2027 else { /* no numeric repeat count, so process sym only once */
2028 33200 repeat_count = 1;
2029 }
2030 }
2031  
2032 /* thwart potential infinite loop */
2033 75349 ▶ if (repeat_count < 1)
Branch: 1 branch
BranchTrueFalseTotal
if (repeat_count < 1) 0 75349 75349
2034 0 Kino1_confess( "invalid repeat_count: %d", repeat_count);
2035
2036 75349 switch(sym) {
2037  
2038 10617 case 'a': /* arbitrary binary data */
2039 10617 len = repeat_count;
2040 10617 repeat_count = 1;
2041 10617 aSV = newSV(len + 1);
2042 10617 SvCUR_set(aSV, len);
2043 10617 SvPOK_on(aSV);
2044 10617 string = SvPVX(aSV);
2045 10617 instream->read_bytes(instream, string, len);
2046 10617 break;
2047  
2048 1444 case 'b': /* signed byte */
2049 case 'B': /* unsigned byte */
2050 1444 aChar = instream->read_byte(instream);
2051 1444 ▶ if (sym == 'b')
Branch: 1 branch
BranchTrueFalseTotal
if (sym == 'b') 917 527 1444
2052 917 aIV = (signed char)aChar;
2053 else
2054 527 aIV = (unsigned char)aChar;
2055 1444 aSV = newSViv(aIV);
2056 1444 break;
2057  
2058 2513 case 'i': /* signed 32-bit integer */
2059 2513 aSV = newSViv( (I32)instream->read_int(instream) );
2060 2513 break;
2061
2062 2126 case 'I': /* unsigned 32-bit integer */
2063 2126 aSV = newSVuv( instream->read_int(instream) );
2064 2126 break;
2065  
2066 13290 case 'Q': /* unsigned "64-bit integer" */
2067 13290 aSV = newSVnv( instream->read_long(instream) );
2068 13290 break;
2069  
2070 21972 case 'T': /* string */
2071 21972 len = instream->read_vint(instream);
2072 21972 aSV = newSV(len + 1);
2073 21972 SvCUR_set(aSV, len);
2074 21972 SvPOK_on(aSV);
2075 21972 string = SvPVX(aSV);
2076 21972 instream->read_chars(instream, string, 0, len);
2077 21972 break;
2078  
2079 20763 case 'V': /* VInt */
2080 20763 aSV = newSVuv( instream->read_vint(instream) );
2081 20763 break;
2082  
2083 2624 case 'W': /* VLong */
2084 2624 aSV = newSVnv( instream->read_vlong(instream) );
2085 2624 break;
2086  
2087 0 default:
2088 0 aSV = NULL; /* suppress unused var compiler warning */
2089 0 Kino1_confess("Invalid type in template: '%c'", sym);
2090 }
2091  
2092 /* Put a scalar on the stack, use up one symbol or repeater */
2093 75349 ▶ XPUSHs( sv_2mortal(aSV) );
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs( sv_2mortal(aSV) ); 0 75349 75349
2094 75349 repeat_count -= 1;
2095 }
2096 }
2097  
2098 void
2099 DESTROY(instream)
2100 InStream *instream;
2101 PPCODE:
2102 3638 Kino1_InStream_destroy(instream);
2103  
2104  
2105 MODULE = KinoSearch1 PACKAGE = KinoSearch1::Store::OutStream
2106  
2107 =for comment
2108 Constructor - takes one arg: a filehandle.
2109  
2110 =cut
2111  
2112 OutStream*
2113 new(class, fh_sv)
2114 char *class;
2115 SV *fh_sv;
2116 CODE:
2117 1021 RETVAL = Kino1_OutStream_new(class, fh_sv);
2118 OUTPUT: RETVAL
2119  
2120 void
2121 seek(outstream, target)
2122 OutStream *outstream;
2123 double target;
2124 PPCODE:
2125 185 outstream->seek(outstream, target);
2126  
2127 double
2128 tell(outstream)
2129 OutStream *outstream;
2130 CODE:
2131 24708 RETVAL = outstream->tell(outstream);
2132 OUTPUT: RETVAL
2133  
2134 double
2135 length(outstream)
2136 OutStream *outstream;
2137 CODE:
2138 0 RETVAL = Kino1_OutStream_length(outstream);
2139 OUTPUT: RETVAL
2140  
2141 void
2142 flush(outstream);
2143 OutStream *outstream;
2144 PPCODE:
2145 1020 Kino1_OutStream_flush(outstream);
2146  
2147 =for comment
2148 Write the entire contents of an instream to an outstream.
2149  
2150 =cut
2151  
2152 void
2153 absorb(outstream, instream)
2154 OutStream *outstream;
2155 InStream *instream;
2156 PPCODE:
2157 554 Kino1_OutStream_absorb(outstream, instream);
2158  
2159 SV*
2160 _set_or_get(outstream, ...)
2161 OutStream *outstream;
2162 ALIAS:
2163 set_fh = 1
2164 get_fh = 2
2165 CODE:
2166 {
2167 1020 ▶ KINO_START_SET_OR_GET_SWITCH
Branch: 2 branches
BranchTrueFalseTotal
KINO_START_SET_OR_GET_SWITCH 0 1020 1020
KINO_START_SET_OR_GET_SWITCH 0 0 0
2168  
2169 0 case 1: Kino1_confess("Can't set_fh");
2170 /* fall through */
2171 1020 case 2: RETVAL = newSVsv(outstream->fh_sv);
2172 1020 break;
2173
2174 0 KINO_END_SET_OR_GET_SWITCH
2175 }
2176 OUTPUT: RETVAL
2177  
2178  
2179 =begin comment
2180  
2181 $outstream->lu_write( TEMPLATE, LIST );
2182  
2183 Write the items in LIST to the OutStream using the serialization schemes
2184 specified by TEMPLATE.
2185  
2186 =end comment
2187 =cut
2188  
2189 void
2190 lu_write (outstream, template_sv, ...)
2191 OutStream *outstream;
2192 SV *template_sv;
2193 PREINIT:
2194 STRLEN tpt_len; /* bytelength of template */
2195 char *template; /* ptr to a spot in the template */
2196 char *tpt_end; /* ptr to the end of the template */
2197 int repeat_count; /* number of times to repeat sym */
2198 int item_count; /* current place in @_ */
2199 char sym; /* the current symbol in the template */
2200 char countsym; /* used when calculating repeat counts */
2201 I32 aI32;
2202 U32 aU32;
2203 double aDouble;
2204 SV *aSV;
2205 char *string;
2206 STRLEN string_len;
2207 PPCODE:
2208 {
2209 /* require an object, a template, and at least 1 item */
2210 74439 ▶ if (items < 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (items < 2) { 0 74439 74439
2211 0 Kino1_confess("lu_write error: too few arguments");
2212 }
2213  
2214 /* prepare the template and get pointers */
2215 74439 template = SvPV(template_sv, tpt_len);
2216 74439 tpt_end = template + tpt_len;
2217  
2218 /* reject an empty template */
2219 74439 ▶ if (tpt_len == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (tpt_len == 0) { 0 74439 74439
2220 0 Kino1_confess("lu_write error: TEMPLATE cannot be empty string");
2221 }
2222
2223 /* init counters */
2224 74439 repeat_count = 0;
2225 74439 item_count = 2;
2226  
2227 while (1) {
2228 /* only process template if we're not in the midst of a repeat */
2229 254455 ▶ if (repeat_count == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (repeat_count == 0) { 243752 10703 254455
2230 /* fast-forward past space characters */
2231 243787 ▶ while (*template == ' ' && template < tpt_end) {
Branch: 2 branches
BranchTrueFalseTotal
while (*template == ' ' && template < tpt_end) { 35 243752 243787
while (*template == ' ' && template < tpt_end) { 35 0 35
2232 35 template++;
2233 }
2234  
2235 /* if we're done, return or throw error */
2236 243752 ▶ if (template == tpt_end || item_count == items) {
Branch: 2 branches
BranchTrueFalseTotal
if (template == tpt_end || item_count == items) { 169314 74438 243752
if (template == tpt_end || item_count == items) { 0 169314 169314
2237 74438 ▶ if (item_count != items) {
Branch: 1 branch
BranchTrueFalseTotal
if (item_count != items) { 0 74438 74438
2238 0 Kino1_confess(
2239 "lu_write error: Too many ITEMS, not enough TEMPLATE");
2240 }
2241 74438 ▶ else if (template != tpt_end) {
Branch: 1 branch
BranchTrueFalseTotal
else if (template != tpt_end) { 0 74438 74438
2242 0 Kino1_confess(
2243 "lu_write error: Too much TEMPLATE, not enough ITEMS");
2244 }
2245 else { /* success! */
2246 74438 break;
2247 }
2248 }
2249  
2250 /* derive the current symbol and a possible digit repeat sym */
2251 169314 sym = *template++;
2252 169314 countsym = *template;
2253  
2254 169314 ▶ if (template == tpt_end) { /* sym is last char in template */
Branch: 1 branch
BranchTrueFalseTotal
if (template == tpt_end) { /* sym is last char in template */ 74411 94903 169314
2255 74411 repeat_count = 1;
2256 }
2257 94903 ▶ else if (countsym >= '0' && countsym <= '9') {
Branch: 2 branches
BranchTrueFalseTotal
else if (countsym >= '0' && countsym <= '9') { 94901 2 94903
else if (countsym >= '0' && countsym <= '9') { 53 94848 94901
2258 /* calculate numerical repeat count */
2259 53 repeat_count = countsym - KINO_NUM_CHAR_OFFSET;
2260 53 countsym = *(++template);
2261 53 while ( template <= tpt_end
2262 108 ▶ && countsym >= '0'
Branch: 1 branch
BranchTrueFalseTotal
&& countsym >= '0' 57 51 108
2263 165 ▶ && countsym <= '9'
Branch: 2 branches
BranchTrueFalseTotal
&& countsym <= '9' 108 0 108
&& countsym <= '9' 55 2 57
2264 ) {
2265 55 repeat_count = (repeat_count * 10)
2266 55 + (countsym - KINO_NUM_CHAR_OFFSET);
2267 55 countsym = *(++template);
2268 }
2269 }
2270 else { /* no numeric repeat count, so process sym only once */
2271 94850 repeat_count = 1;
2272 }
2273 }
2274  
2275  
2276 180017 switch(sym) {
2277  
2278 41076 case 'a': /* arbitrary binary data */
2279 41076 aSV = ST(item_count);
2280 41076 ▶ if (!SvOK(aSV)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(aSV)) { 0 41076 41076
2281 0 Kino1_confess("Internal error: undef at lu_write 'a'");
2282 }
2283 41076 string = SvPV(aSV, string_len);
2284 41076 ▶ if (repeat_count != string_len) {
Branch: 1 branch
BranchTrueFalseTotal
if (repeat_count != string_len) { 0 41076 41076
2285 0 Kino1_confess(
2286 "lu_write error: repeat_count != string_len: %d %d",
2287 repeat_count, string_len);
2288 }
2289 41076 Kino1_OutStream_write_bytes(outstream, string, string_len);
2290 /* trigger next sym */
2291 41076 repeat_count = 1;
2292 41076 break;
2293  
2294 1444 case 'b': /* signed byte */
2295 case 'B': /* unsigned byte */
2296 1444 aI32 = SvIV( ST(item_count) );
2297 1444 Kino1_OutStream_write_byte(outstream, (char)(aI32 & 0xff));
2298 1444 break;
2299  
2300 2540 case 'i': /* signed 32-bit integer */
2301 2540 aI32 = SvIV( ST(item_count) );
2302 2540 Kino1_OutStream_write_int(outstream, (U32)aI32);
2303 2540 break;
2304
2305  
2306 2126 case 'I': /* unsigned 32-bit integer */
2307 2126 aU32 = SvUV( ST(item_count) );
2308 2126 Kino1_OutStream_write_int(outstream, aU32);
2309 2126 break;
2310
2311 27622 case 'Q': /* unsigned "64-bit" integer */
2312 27622 aDouble = SvNV( ST(item_count) );
2313 27622 Kino1_OutStream_write_long(outstream, aDouble);
2314 27622 break;
2315
2316 49991 case 'V': /* VInt */
2317 49991 aU32 = SvUV( ST(item_count) );
2318 49991 Kino1_OutStream_write_vint(outstream, aU32);
2319 49991 break;
2320  
2321 2624 case 'W': /* VLong */
2322 2624 aDouble = SvNV( ST(item_count) );
2323 2624 Kino1_OutStream_write_vlong(outstream, aDouble);
2324 2624 break;
2325  
2326 52593 case 'T': /* string */
2327 52593 aSV = ST(item_count);
2328 52593 string = SvPV(aSV, string_len);
2329 52593 Kino1_OutStream_write_string(outstream, string, string_len);
2330 52593 break;
2331  
2332 1 default:
2333 1 Kino1_confess("Illegal character in template: %c", sym);
2334 }
2335  
2336 /* use up one repeat_count and one item from the stack */
2337 180016 repeat_count--;
2338 180016 item_count++;
2339 }
2340 }
2341  
2342 void
2343 DESTROY(outstream)
2344 OutStream *outstream;
2345 PPCODE:
2346 1021 Kino1_OutStream_destroy(outstream);
2347  
2348  
2349 MODULE = KinoSearch1 PACKAGE = KinoSearch1::Util::BitVector
2350  
2351 void
2352 new(either_sv, ...)
2353 SV *either_sv;
2354 PREINIT:
2355 const char *class;
2356 HV *args_hash;
2357 U32 capacity;
2358 BitVector *bit_vec;
2359 PPCODE:
2360 /* determine the class */
2361 128 class = sv_isobject(either_sv)
2362 0 ? sv_reftype(either_sv, 0)
2363 256 ▶ : SvPV_nolen(either_sv);
Branch: 1 branch
BranchTrueFalseTotal
: SvPV_nolen(either_sv); 0 128 128
2364  
2365 /* process hash-style params */
2366 128 ▶ Kino1_Verify_build_args_hash(args_hash,
Branch: 1 branch
BranchTrueFalseTotal
Kino1_Verify_build_args_hash(args_hash, 0 128 128
2367 "KinoSearch1::Util::BitVector::instance_vars", 1);
2368 128 capacity = (U32)SvUV( Kino1_Verify_extract_arg(args_hash, "capacity", 8) );
2369  
2370 /* build object */
2371 128 bit_vec = Kino1_BitVec_new(capacity);
2372 128 ST(0) = sv_newmortal();
2373 128 sv_setref_pv(ST(0), class, (void*)bit_vec);
2374 128 XSRETURN(1);
2375  
2376  
2377 =for comment
2378 Return true if the bit indcated by $num has been set, false if it hasn't
2379 (regardless of whether $num lies within the bounds of the object's capacity).
2380  
2381 =cut
2382  
2383 bool
2384 get(bit_vec, num)
2385 BitVector *bit_vec;
2386 U32 num;
2387 CODE:
2388 56 RETVAL = Kino1_BitVec_get(bit_vec, num);
2389 OUTPUT: RETVAL
2390  
2391 =for comment
2392 Set the bit at $num to 1.
2393  
2394 =cut
2395  
2396 void
2397 set(bit_vec, ...)
2398 BitVector *bit_vec;
2399 PREINIT:
2400 U32 i, num;
2401 PPCODE:
2402 222 ▶ for (i = 1; i < items; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i < items; i++) { 123 99 222
2403 123 num = (U32)( SvUV( ST(i) ) );
2404 123 Kino1_BitVec_set(bit_vec, num);
2405 }
2406  
2407 =for comment
2408 Clear the bit at $num (i.e. set it to 0).
2409  
2410 =cut
2411  
2412 void
2413 clear(bit_vec, num)
2414 BitVector *bit_vec;
2415 U32 num;
2416 PPCODE:
2417 1 Kino1_BitVec_clear(bit_vec, num);
2418  
2419 =for comment
2420 Set all the bits bounded by $first and $last, inclusive, to 1.
2421  
2422 =cut
2423  
2424 void
2425 bulk_set(bit_vec, first, last)
2426 BitVector *bit_vec;
2427 U32 first;
2428 U32 last;
2429 PPCODE:
2430 1 Kino1_BitVec_bulk_set(bit_vec, first, last);
2431
2432 =for comment
2433 Clear all the bits bounded by $first and $last, inclusive.
2434  
2435 =cut
2436  
2437 void
2438 bulk_clear(bit_vec, first, last)
2439 BitVector *bit_vec;
2440 U32 first;
2441 U32 last;
2442 PPCODE:
2443 1 Kino1_BitVec_bulk_clear(bit_vec, first, last);
2444  
2445 =for comment
2446 Given $num, return either $num (if it is set), the next set bit above it, or
2447 if no such bit exists, undef (from Perl) or a sentinel (0xFFFFFFFF) from C.
2448  
2449 =cut
2450
2451 SV*
2452 next_set_bit(bit_vec, num)
2453 BitVector *bit_vec;
2454 U32 num;
2455 CODE:
2456 312 num = Kino1_BitVec_next_set_bit(bit_vec, num);
2457 312 ▶ RETVAL = num == KINO_BITVEC_SENTINEL ? &PL_sv_undef : newSVuv(num);
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = num == KINO_BITVEC_SENTINEL ? &PL_sv_undef : newSVuv(num); 271 41 312
2458 OUTPUT: RETVAL
2459  
2460 =for comment
2461 Given $num, return $num (if it is clear), or the next clear bit above it.
2462 The highest number that next_clear_bit can return is the object's capacity.
2463  
2464 =cut
2465  
2466 SV*
2467 next_clear_bit(bit_vec, num)
2468 BitVector *bit_vec;
2469 U32 num;
2470 CODE:
2471 2 num = Kino1_BitVec_next_clear_bit(bit_vec, num);
2472 2 ▶ RETVAL = num == KINO_BITVEC_SENTINEL ? &PL_sv_undef : newSVuv(num);
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = num == KINO_BITVEC_SENTINEL ? &PL_sv_undef : newSVuv(num); 2 0 2
2473 OUTPUT: RETVAL
2474  
2475 =for comment
2476 Modify the BitVector so that only bits which remain set are those which 1)
2477 were already set in this BitVector, and 2) were also set in the other
2478 BitVector.
2479  
2480 =cut
2481  
2482 void
2483 logical_and(bit_vec, other)
2484 BitVector *bit_vec;
2485 BitVector *other;
2486 PPCODE:
2487 3 Kino1_BitVec_logical_and(bit_vec, other);
2488  
2489  
2490 =for comment
2491 Return a count of the number of set bits in the BitVector.
2492  
2493 =cut
2494  
2495 U32
2496 count(bit_vec)
2497 BitVector *bit_vec;
2498 CODE:
2499 64 RETVAL = Kino1_BitVec_count(bit_vec);
2500 OUTPUT: RETVAL
2501  
2502  
2503 =for comment
2504 Return an arrayref of the with each element the number of a set bit.
2505  
2506 =cut
2507  
2508 void
2509 to_arrayref(bit_vec)
2510 BitVector *bit_vec;
2511 PREINIT:
2512 AV *out_av;
2513 PPCODE:
2514 6 out_av = Kino1_BitVec_to_array(bit_vec);
2515 6 ▶ XPUSHs( sv_2mortal(newRV_noinc( (SV*)out_av )) );
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs( sv_2mortal(newRV_noinc( (SV*)out_av )) ); 0 6 6
2516 6 XSRETURN(1);
2517
2518  
2519 =for comment
2520 Setters and getters. A quirk: set_bits automatically adjusts capacity
2521 upwards to the appropriate multiple of 8 if necessary.
2522  
2523 =cut
2524  
2525 SV*
2526 _set_or_get(bit_vec, ...)
2527 BitVector *bit_vec;
2528 ALIAS:
2529 set_capacity = 1
2530 get_capacity = 2
2531 set_bits = 3
2532 get_bits = 4
2533 PREINIT:
2534 STRLEN len;
2535 U32 new_capacity;
2536 char *new_bits;
2537 CODE:
2538 {
2539 14 ▶ KINO_START_SET_OR_GET_SWITCH
Branch: 2 branches
BranchTrueFalseTotal
KINO_START_SET_OR_GET_SWITCH 6 8 14
KINO_START_SET_OR_GET_SWITCH 0 6 6
2540  
2541 3 case 1: new_capacity = SvUV(ST(1));
2542 3 ▶ if (new_capacity < bit_vec->capacity) {
Branch: 1 branch
BranchTrueFalseTotal
if (new_capacity < bit_vec->capacity) { 0 3 3
2543 0 Kino1_BitVec_shrink(bit_vec, new_capacity);
2544 }
2545 3 ▶ else if (new_capacity > bit_vec->capacity) {
Branch: 1 branch
BranchTrueFalseTotal
else if (new_capacity > bit_vec->capacity) { 2 1 3
2546 2 Kino1_BitVec_grow(bit_vec, new_capacity);
2547 }
2548 /* fall through */
2549 7 case 2: RETVAL = newSVuv(bit_vec->capacity);
2550 7 break;
2551  
2552 3 case 3: Kino1_Safefree(bit_vec->bits);
2553 3 new_bits = SvPV(ST(1), len);
2554 3 bit_vec->bits = (unsigned char*)Kino1_savepvn(new_bits, len);
2555 3 bit_vec->capacity = len << 3;
2556 /* fall through */
2557 7 case 4: len = ceil(bit_vec->capacity / 8.0);
2558 7 RETVAL = newSVpv((char*)bit_vec->bits, len);
2559 7 break;
2560  
2561 0 KINO_END_SET_OR_GET_SWITCH
2562 }
2563 OUTPUT: RETVAL
2564  
2565  
2566 void
2567 DESTROY(bit_vec)
2568 BitVector *bit_vec;
2569 PPCODE:
2570 128 Kino1_BitVec_destroy(bit_vec);
2571  
2572  
2573  
2574 MODULE = KinoSearch1 PACKAGE = KinoSearch1::Util::IntMap
2575  
2576 =for comment
2577  
2578 Return either the remapped number, or undef if orig has been removed.
2579  
2580 =cut
2581  
2582 SV*
2583 get(int_map_ref, orig);
2584 SV *int_map_ref;
2585 I32 orig;
2586 PREINIT:
2587 I32 result;
2588 CODE:
2589 20279 result = Kino1_IntMap_get(int_map_ref, orig);
2590 20279 RETVAL = result == -1
2591 ? &PL_sv_undef
2592 20279 ▶ : newSViv(result);
Branch: 1 branch
BranchTrueFalseTotal
: newSViv(result); 20277 2 20279
2593 OUTPUT: RETVAL
2594  
2595  
2596 MODULE = KinoSearch1 PACKAGE = KinoSearch1::Util::MathUtils
2597  
2598 double
2599 ceil(in)
2600 double in;
2601 CODE:
2602 3 ▶ RETVAL = ceil(in);
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = ceil(in); 1 2 3
2603 OUTPUT: RETVAL
2604  
2605  
2606 MODULE = KinoSearch1 PACKAGE = KinoSearch1::Util::PriorityQueue
2607  
2608 void
2609 new(either_sv, ...)
2610 SV *either_sv;
2611 PREINIT:
2612 const char *class;
2613 HV *args_hash;
2614 U32 max_size;
2615 PriorityQueue *pq;
2616 PPCODE:
2617 /* determine the class */
2618 285 class = sv_isobject(either_sv)
2619 0 ? sv_reftype(either_sv, 0)
2620 570 ▶ : SvPV_nolen(either_sv);
Branch: 1 branch
BranchTrueFalseTotal
: SvPV_nolen(either_sv); 0 285 285
2621
2622 /* process hash-style params */
2623 285 ▶ Kino1_Verify_build_args_hash(args_hash,
Branch: 1 branch
BranchTrueFalseTotal
Kino1_Verify_build_args_hash(args_hash, 0 285 285
2624 "KinoSearch1::Util::PriorityQueue::instance_vars", 1);
2625 285 max_size = (U32)SvUV( Kino1_Verify_extract_arg(args_hash, "max_size", 8) );
2626  
2627 /* build object */
2628 285 pq = Kino1_PriQ_new(max_size);
2629 285 ST(0) = sv_newmortal();
2630 285 sv_setref_pv(ST(0), class, (void*)pq);
2631 285 XSRETURN(1);
2632  
2633 =for comment
2634  
2635 Add an element to the Queue if either...
2636 a) the queue isn't full, or
2637 b) the element belongs in the queue and should displace another
2638  
2639 =cut
2640  
2641 void
2642 insert(pq, element)
2643 PriorityQueue *pq;
2644 SV *element;
2645 PPCODE:
2646 45 Kino1_PriQ_insert(pq, element);
2647  
2648  
2649 =for comment
2650  
2651 Pop the *least* item off of the priority queue.
2652  
2653 =cut
2654  
2655 SV*
2656 pop(pq)
2657 PriorityQueue *pq;
2658 CODE:
2659 13 RETVAL = Kino1_PriQ_pop(pq);
2660 13 ▶ if (RETVAL == Nullsv) {
Branch: 1 branch
BranchTrueFalseTotal
if (RETVAL == Nullsv) { 2 11 13
2661 2 RETVAL = &PL_sv_undef;
2662 }
2663 else {
2664 11 RETVAL = newSVsv(RETVAL);
2665 }
2666 OUTPUT: RETVAL
2667  
2668  
2669 =for comment
2670  
2671 Return the least item in the queue, but don't remove it.
2672  
2673 =cut
2674  
2675 SV*
2676 peek(pq)
2677 PriorityQueue *pq;
2678 CODE:
2679 1 RETVAL = Kino1_PriQ_peek(pq);
2680 1 ▶ if (RETVAL == Nullsv) {
Branch: 1 branch
BranchTrueFalseTotal
if (RETVAL == Nullsv) { 0 1 1
2681 0 RETVAL = &PL_sv_undef;
2682 }
2683 else {
2684 1 RETVAL = newSVsv(RETVAL);
2685 }
2686 OUTPUT: RETVAL
2687  
2688  
2689  
2690 =for comment
2691  
2692 Empty the queue into an array, with the highest priority item at index 0.
2693  
2694 =cut
2695  
2696 void
2697 pop_all(pq)
2698 PriorityQueue *pq;
2699 PREINIT:
2700 AV* out_av;
2701 PPCODE:
2702 284 out_av = Kino1_PriQ_pop_all(pq);
2703 284 ▶ XPUSHs( sv_2mortal(newRV_noinc( (SV*)out_av )) );
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs( sv_2mortal(newRV_noinc( (SV*)out_av )) ); 0 284 284
2704  
2705  
2706 SV*
2707 _set_or_get(pq, ...)
2708 PriorityQueue *pq;
2709 ALIAS:
2710 get_size = 2
2711 get_max_size = 4
2712 CODE:
2713 {
2714 5 ▶ KINO_START_SET_OR_GET_SWITCH
Branch: 2 branches
BranchTrueFalseTotal
KINO_START_SET_OR_GET_SWITCH 0 5 5
KINO_START_SET_OR_GET_SWITCH 0 0 0
2715  
2716 1 case 2: RETVAL = newSVuv(pq->size);
2717 1 break;
2718  
2719 4 case 4: RETVAL = newSVuv(pq->max_size);
2720 4 break;
2721  
2722 0 KINO_END_SET_OR_GET_SWITCH
2723 }
2724 OUTPUT: RETVAL
2725  
2726  
2727 void
2728 DESTROY(pq)
2729 PriorityQueue *pq;
2730 PPCODE:
2731 285 Kino1_PriQ_destroy(pq);
2732  
2733  
2734 MODULE = KinoSearch1 PACKAGE = KinoSearch1::Util::SortExternal
2735  
2736 void
2737 _new(class, outstream_sv, invindex_sv, seg_name_sv, mem_threshold)
2738 char *class;
2739 SV *outstream_sv;
2740 SV *invindex_sv;
2741 SV *seg_name_sv;
2742 I32 mem_threshold;
2743 PREINIT:
2744 SortExternal *sortex;
2745 PPCODE:
2746 69 sortex = Kino1_SortEx_new(outstream_sv, invindex_sv, seg_name_sv,
2747 mem_threshold);
2748 69 ST(0) = sv_newmortal();
2749 69 sv_setref_pv( ST(0), class, (void*)sortex );
2750 69 XSRETURN(1);
2751  
2752 =for comment
2753  
2754 Add one or more items to the sort pool.
2755  
2756 =cut
2757  
2758 void
2759 feed(sortex, ...)
2760 SortExternal *sortex;
2761 PREINIT:
2762 I32 i;
2763 PPCODE:
2764 62849 ▶ for (i = 1; i < items; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i < items; i++) { 47352 15497 62849
2765 47352 SV const * item_sv = ST(i);
2766 47352 ▶ if (!SvPOK(item_sv))
Branch: 1 branch
BranchTrueFalseTotal
if (!SvPOK(item_sv)) 0 47352 47352
2767 0 continue;
2768 47352 sortex->feed(sortex, SvPVX(item_sv), SvCUR(item_sv));
2769 }
2770  
2771 =for comment
2772  
2773 Fetch the next sorted item from the sort pool. sort_all must be called first.
2774  
2775 =cut
2776  
2777 SV*
2778 fetch(sortex)
2779 SortExternal *sortex;
2780 PREINIT:
2781 ByteBuf *bb;
2782 CODE:
2783 11128 bb = sortex->fetch(sortex);
2784 11128 ▶ if (bb == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (bb == NULL) { 7 11121 11128
2785 7 RETVAL = newSV(0);
2786 }
2787 else {
2788 11121 RETVAL = newSVpvn(bb->ptr, bb->size);
2789 11121 Kino1_BB_destroy(bb);
2790 }
2791 OUTPUT: RETVAL
2792  
2793 =for comment
2794  
2795 Sort all items currently in memory.
2796  
2797 =cut
2798  
2799 void
2800 _sort_cache(sortex)
2801 SortExternal *sortex;
2802 PPCODE:
2803 66 Kino1_SortEx_sort_cache(sortex);
2804  
2805 =for comment
2806  
2807 Sort everything in memory and write the sorted elements to disk, creating a
2808 SortExRun C object.
2809  
2810 =cut
2811  
2812 void
2813 _sort_run(sortex);
2814 SortExternal *sortex;
2815 PPCODE:
2816 3 Kino1_SortEx_sort_run(sortex);
2817  
2818 =for comment
2819  
2820 Turn on fetching.
2821  
2822 =cut
2823  
2824 void
2825 _enable_fetch(sortex)
2826 SortExternal *sortex;
2827 PPCODE:
2828 69 Kino1_SortEx_enable_fetch(sortex);
2829
2830 SV*
2831 _set_or_get(sortex, ...)
2832 SortExternal *sortex;
2833 ALIAS:
2834 _set_outstream = 1
2835 _get_outstream = 2
2836 _set_instream = 3
2837 _get_instream = 4
2838 _set_num_runs = 5
2839 _get_num_runs = 6
2840 _set_invindex = 7
2841 _get_invindex = 8
2842 _set_seg_name = 9
2843 _get_seg_name = 10
2844 CODE:
2845 {
2846 407 ▶ KINO_START_SET_OR_GET_SWITCH
Branch: 2 branches
BranchTrueFalseTotal
KINO_START_SET_OR_GET_SWITCH 69 338 407
KINO_START_SET_OR_GET_SWITCH 0 69 69
2847  
2848 0 case 1: SvREFCNT_dec(sortex->outstream_sv);
2849 0 sortex->outstream_sv = newSVsv( ST(1) );
2850 0 ▶ Kino1_extract_struct(sortex->outstream_sv, sortex->outstream,
Branch: 1 branch
BranchTrueFalseTotal
Kino1_extract_struct(sortex->outstream_sv, sortex->outstream, 0 0 0
2851 OutStream*, "KinoSearch1::Store::OutStream");
2852 /* fall through */
2853 69 case 2: RETVAL = newSVsv(sortex->outstream_sv);
2854 69 break;
2855
2856 69 case 3: SvREFCNT_dec(sortex->instream_sv);
2857 69 sortex->instream_sv = newSVsv( ST(1) );
2858 69 ▶ Kino1_extract_struct(sortex->instream_sv, sortex->instream,
Branch: 1 branch
BranchTrueFalseTotal
Kino1_extract_struct(sortex->instream_sv, sortex->instream, 69 0 69
2859 InStream*, "KinoSearch1::Store::InStream");
2860 /* fall through */
2861 131 case 4: RETVAL = newSVsv(sortex->instream_sv);
2862 131 break;
2863  
2864 0 case 5: Kino1_confess("can't set num_runs");
2865 /* fall through */
2866 69 case 6: RETVAL = newSViv(sortex->num_runs);
2867 69 break;
2868  
2869 0 case 7: Kino1_confess("can't set_invindex");
2870 /* fall through */
2871 69 case 8: RETVAL = newSVsv(sortex->invindex_sv);
2872 69 break;
2873
2874 0 case 9: Kino1_confess("can't set_seg_name");
2875 /* fall through */
2876 69 case 10: RETVAL = newSVsv(sortex->seg_name_sv);
2877 69 break;
2878  
2879 0 KINO_END_SET_OR_GET_SWITCH
2880 }
2881 OUTPUT: RETVAL
2882  
2883 void
2884 DESTROY(sortex)
2885 SortExternal *sortex;
2886 PPCODE:
2887 69 Kino1_SortEx_destroy(sortex);
2888  
« lib/KinoSearch1/Index/MultiTermDocs.pm ↑ Summary lib/KinoSearch1/Util/BitVector.pm »