forked from PolMine/RcppCWB
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcdaccess.c
More file actions
2565 lines (2118 loc) · 70.6 KB
/
Copy pathcdaccess.c
File metadata and controls
2565 lines (2118 loc) · 70.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* IMS Open Corpus Workbench (CWB)
* Copyright (C) 1993-2006 by IMS, University of Stuttgart
* Copyright (C) 2007- by the respective contributers (see file AUTHORS)
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2, or (at your option) any later
* version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details (in the file "COPYING", or available via
* WWW at http://www.gnu.org/copyleft/gpl.html).
*/
#include <stdarg.h>
#include <math.h>
#include <ctype.h>
#include <sys/types.h>
#include <errno.h>
#include "globals.h"
#include "endian2.h"
#include "attributes.h"
#include "special-chars.h"
#include "bitio.h"
#include "compression.h"
#include <stdint.h>
void Rprintf(const char *, ...);
/**
* If COMPRESS_DEBUG is set to a positive integer, cl_cpos2id() will
* print debugging messages. (2 prints more than 1!)
*/
#define COMPRESS_DEBUG 0
/**
* Error number for CL: is set after access to any of various corpus-data-access functions.
*/
int cl_errno = CDA_OK;
/**
* Macro to test for one of the two component state values that boil down to
* "the data is avaiable".
*/
#define compstate_data_available(st) ((st)==ComponentLoaded||(st)==ComponentUnloaded)
/**
* Checks an Attribute passed as a function argument for usability in that function.
*
* (a) arg must not be NULL.
* (b) arg type has to be the type specified in atyp.
*
* If these conditions are not fulfilled, the current function returns rval, and
* cl_errno is set.
*/
#define check_arg(arg,atyp,rval) \
if (arg == NULL) { \
cl_errno = CDA_ENULLATT; return rval; \
} \
else if (arg->type != atyp) { \
cl_errno = CDA_EATTTYPE; return rval; \
}
/**
* CL's string comparison function.
*
* Arguments must not be NULL (the pointers are dereferenced without checking).
* That's just as in standard C strcmp(), passing a null pointer to which
* results in "undefined behaviour".
*/
int
cl_strcmp(const char *s1, const char *s2)
{
/* we compare signed chars; this is declared & typecast explicitly for the sake of systems
* where char with no modifier is unsigned. */
register signed char *c1 = (signed char *)s1;
register signed char *c2 = (signed char *)s2;
for ( ; *c1 == *c2; c1++, c2++)
if (*c1 == '\0')
return 0;
return *c1 - *c2;
}
/**
* Gets a string describing the error identified by an error number.
*
* The string is a pointer to an internal constant string, i.e.,
* do not modify or free it!
*
* @param error_num Error number integer (a CDA_* constant as defined in cl.h)
*/
char *
cl_error_string(int error_num)
{
char *s;
switch (error_num) {
case CDA_OK:
s = "CL: No error";
break;
case CDA_ENULLATT:
s = "CL: NULL passed as attribute argument of function";
break;
case CDA_EATTTYPE:
s = "CL: function called with illegal attribute type";
break;
case CDA_EIDORNG:
s = "CL: id is out of range";
break;
case CDA_EPOSORNG:
s = "CL: position is out of range";
break;
case CDA_EIDXORNG:
s = "CL: index is out of range";
break;
case CDA_ENOSTRING:
s = "CL: no such string encoded";
break;
case CDA_EPATTERN:
s = "CL: illegal regular expression/illegal pattern";
break;
case CDA_ESTRUC:
s = "CL: no structure defined for this position";
break;
case CDA_EALIGN:
s = "CL: no alignment defined for this position";
break;
case CDA_EREMOTE:
s = "CL: error during access of remote data";
break;
case CDA_ENODATA:
s = "CL: can't load and/or create necessary data";
break;
case CDA_EARGS:
s = "CL: error in arguments of dynamic call or CL function";
break;
case CDA_ENOMEM:
s = "CL: not enough memory";
break;
case CDA_EOTHER:
s = "CL: unspecified error";
break;
case CDA_ENYI:
s = "CL: unimplemented feature/not yet implemented";
break;
case CDA_EBADREGEX:
s = "CL: bad regular expression";
break;
case CDA_EFSETINV:
s = "CL: invalid feature set (syntax error)";
break;
case CDA_EBUFFER:
s = "CL: internal buffer overflow";
break;
case CDA_EINTERNAL:
s = "CL: internal data inconsistency";
break;
case CDA_EACCESS:
s = "CL: insufficient access permissions";
break;
case CDA_EPOSIX:
s = strerror(errno);
break;
default:
s = "CL: ILLEGAL ERROR NUMBER";
break;
}
return s;
}
/**
* Prints an error message, together with a string identifying the current error number.
*/
void
cl_error(char *message)
{
if (message)
Rprintf("%s: %s\n", cl_error_string(cl_errno), message);
else
Rprintf("%s\n", cl_error_string(cl_errno));
}
/* ================================================== POSITIONAL ATTRIBUTES */
/* ==================== the mapping between strings and their ids */
/**
* Gets the string that corresponds to the specified item on the given P-attribute.
*
* @param attribute The Attribute to look the item up on
* @param id Identifier of an item on this attribute.
* @return The string (pointer to actual data within the
* attribute, DO NOT FREE!), or NULL if there is an error.
*/
char *
cl_id2str(Attribute *attribute, int id)
{
Component *lex;
Component *lexidx;
check_arg(attribute, ATT_POS, NULL);
lex = ensure_component(attribute, CompLexicon, 0);
lexidx = ensure_component(attribute, CompLexiconIdx, 0);
if (!lex || !lexidx) {
cl_errno = CDA_ENODATA;
return NULL;
}
else if (id < 0 || id >= lexidx->size) {
cl_errno = CDA_EIDORNG;
return NULL;
}
else {
cl_errno = CDA_OK;
return ((char *)lex->data.data + ntohl(lexidx->data.data[id]));
}
assert("Not reached" && 0);
return NULL;
}
/**
* Gets the ID code that corresponds to the specified string on the given P-attribute.
*
* @param attribute The (positional) Attribute to look the string up on
* @param id_string The string of an item on this attribute
* @return Either the integer ID of the item, or an error code (if less than 0).
* In the latter case, the error code will also be written to cl_errno.
*/
int
cl_str2id(Attribute *attribute, char *id_string)
{
int low, high, nr, mid, comp;
Component *lexidx;
Component *lexsrt;
Component *lex;
char *str2;
check_arg(attribute, ATT_POS, cl_errno);
lexidx = ensure_component(attribute, CompLexiconIdx, 0);
lexsrt = ensure_component(attribute, CompLexiconSrt, 0);
lex = ensure_component(attribute, CompLexicon, 0);
if (!(lexidx && lexsrt && lex))
return cl_errno = CDA_ENODATA;
low = 0;
high = lexidx->size;
/* simple binary search: any item-not-found condition returns the function */
for(nr = 0; ; nr++) {
if (nr >= 1000000) {
Rprintf("cl_str2id: too many comparisons with %s\n", id_string);
return cl_errno = CDA_EOTHER;
}
mid = low + (high - low)/2;
str2 = (char *)(lex->data.data) + ntohl(lexidx->data.data[ntohl(lexsrt->data.data[mid])]);
if (0 == (comp = cl_strcmp(id_string, str2)))
break; /* found it! */
if (mid == low)
return cl_errno = CDA_ENOSTRING;
if (comp > 0)
low = mid;
else
high = mid;
}
/* we broke the loop above having found the string (whose ID is addresses by "mid") */
cl_errno = CDA_OK;
return ntohl(lexsrt->data.data[mid]);
}
/**
* Calculates the length of the string that corresponds to the specified
* item on the given P-attribute.
*
* @param attribute The (positional) Attribute to look up the item on
* @param id Identifier of an item on this attribute.
* @return The length of the string, or a CDA_ error code
*/
int
cl_id2strlen(Attribute *attribute, int id)
{
Component *lexidx;
char *s;
check_arg(attribute, ATT_POS, cl_errno);
lexidx = ensure_component(attribute, CompLexiconIdx, 0);
if (lexidx == NULL)
return cl_errno = CDA_ENODATA;
if (id < 0 || id >= lexidx->size)
return cl_errno = CDA_EIDORNG;
if ((id + 1) == lexidx->size) {
/* last item */
s = cl_id2str(attribute, id);
if (s) {
cl_errno = CDA_OK;
return strlen(s);
}
else
return cl_all_ok() ? CDA_EOTHER : cl_errno;
}
else {
/* any other item */
cl_errno = CDA_OK;
return (ntohl(lexidx->data.data[id+1]) - ntohl(lexidx->data.data[id])) - 1;
}
assert("Not reached" && 0);
return 0;
}
/**
* Gets the ID code of the item at the specified position in the Attribute's sorted wordlist index.
*
* That is, given a sort-order position, the actual ID of the corresponding item is generated.
*
* @see get_sortidxpos_of_id
* @param attribute The (positional) Attribute whose index is to be searched.
* @param sort_index_position The offset in the index where the ID code is to be found.
* @return Either the integer ID, or an error code (if less than 0)
*/
int
cl_sort2id(Attribute *attribute, int sort_index_position)
{
Component *srtidx;
check_arg(attribute, ATT_POS, cl_errno);
if (!(srtidx = ensure_component(attribute, CompLexiconSrt, 0)))
return cl_errno = CDA_ENODATA;
if (!(sort_index_position >= 0 && sort_index_position < srtidx->size))
return cl_errno = CDA_EIDXORNG;
cl_errno = CDA_OK;
return ntohl(srtidx->data.data[sort_index_position]);
}
/**
* Gets the position in the Attribute's sorted wordlist index of the item
* with the specified ID code.
*
* This function is NOT YET IMPLEMENTED. (It also seems not to be used anywhere.)
*
* @see get_id_from_sortidx
* @param attribute The (positional) Attribute whose index is to be searched
* @param id Identifier of an item on this attribute.
* @return The offset of that item in the sorted wordlist index, or an error code.
*/
int
cl_id2sort(Attribute *attribute, int id)
{
Component *srtidx;
int offset;
/* not yet implemented!!! */
return cl_errno = CDA_ENYI;
check_arg(attribute, ATT_POS, cl_errno);
if (!(srtidx = ensure_component(attribute, CompLexiconSrt, 0)))
return cl_errno = CDA_ENODATA;
if (!(id >= 0 && id < srtidx->size))
return cl_errno = CDA_EIDORNG;
/* what follows is probably the worst possible way to do it. But it fills the function in. */
/* find sort index entry containing the id we were given. */
for (offset = 0 ; offset < srtidx->size ; offset++)
if (id == ntohl(srtidx->data.data[offset]))
break;
/* this ought not to be possible */
if (offset >= srtidx->size)
return cl_errno = CDA_EINTERNAL;
cl_errno = CDA_OK;
return offset;
}
/* ==================== information about the corpus */
/**
* Checks whether the item sequence of the given P-attribute should be
* accessed via its compressed data.
*
* See comments in body of function for what counts as "compressed".
*
* @return Boolean. True if the compressed data is available
* and should be used; false if the corpus hasn't been compressed
* or the full data has been loaded anyway.
* Or, negative CL error code if a non-p-attribute is passed.
*/
int
cl_sequence_compressed(Attribute *attribute)
{
ComponentState state;
check_arg(attribute, ATT_POS, cl_errno);
/* The item sequence is compressed iff all three components
* (CompHuffSeq, CompHuffCodes, CompHuffSync) are available
* OR when the code description block is already initialized. */
/* further, the CompCorpus component shouldn't be in memory. This is a trick,
* so that uncompressed access can be enforced if the CompCorpus is ensure'd. */
/* if the HCD has been set up, use compressed data */
if (attribute->pos.hc)
return 1;
/* if CompCorpus is in memory, we do not access the compressed sequence. */
if (ComponentLoaded == component_state(attribute, CompCorpus))
return 0;
/* If we don't have all the following 3 componenents either loaded or loadable,
* we do not access the compressed sequence.
*/
state = component_state(attribute, CompHuffSeq);
if (!compstate_data_available(state))
return 0;
state = component_state(attribute, CompHuffCodes);
if (!compstate_data_available(state))
return 0;
state = component_state(attribute, CompHuffSync);
if (!compstate_data_available(state))
return 0;
/* having run that gauntlet, we can return true. */
return 1;
}
/**
* Check whether the reverse-corpus index (inverted file) of the given P-attribute
* should be accessed via its compressed data.
*
* See comments in body of function for what counts as "compressed".
*
* @return Boolean. True if the compressed data is available
* and should be used; false if the index hasn't been compressed
* or the full data has been loaded anyway.
* Or, negative CL error code if a non-p-attribute is passed.
*/
int
cl_index_compressed(Attribute *attribute)
{
ComponentState state;
check_arg(attribute, ATT_POS, cl_errno);
/* The inverted file is compressed iff both components
* (CompCompRF, CompCompRFX) are available */
/* as per the equivalent sequence function:
* - when CompRevCorpus and CompRevCorpusIdx are already
* in memory, we do not use the compressed inverted file.
*/
if (
ComponentLoaded == component_state(attribute, CompRevCorpus)
&&
ComponentLoaded == component_state(attribute, CompRevCorpusIdx)
)
return 0;
state = component_state(attribute, CompCompRF);
if (!compstate_data_available(state))
return 0;
state = component_state(attribute, CompCompRFX);
if (!compstate_data_available(state))
return 0;
return 1;
}
/**
* Gets the maximum position on this P-attribute (ie the size of the
* attribute).
*
* The result of this function is equal to the number of tokens
* in the attribute.
*
* If the attribute's item sequence is compressed, this is read from
* the attribute's Huffman code descriptor block.
*
* Otherwise, it is read from the size member of the Attribute's
* CompCorpus component.
*
* @return The maximum corpus position, or an error code (if less than 0)
*/
int
cl_max_cpos(Attribute *attribute)
{
Component *corpus;
check_arg(attribute, ATT_POS, cl_errno);
if (cl_sequence_compressed(attribute)) {
ensure_component(attribute, CompHuffCodes, 0);
if (attribute->pos.hc == NULL)
return cl_errno = CDA_ENODATA;
cl_errno = CDA_OK;
return attribute->pos.hc->length;
}
/* but if it's not compressed... */
if (!(corpus = ensure_component(attribute, CompCorpus, 0)))
return cl_errno = CDA_ENODATA;
cl_errno = CDA_OK;
return corpus->size;
}
/**
* Gets the maximum id on this P-attribute (ie the range of the attribute's ID codes).
*
* The result of this function is equal to the number of types in this attribute.
*
* @see get_attribute_size
*
* @return The maximum Id, or an error code (if less than 0)
*/
int
cl_max_id(Attribute *attribute)
{
Component *comp;
check_arg(attribute, ATT_POS, cl_errno);
if (!(comp = ensure_component(attribute, CompLexiconIdx, 0)))
return cl_errno = CDA_ENODATA;
cl_errno = CDA_OK;
return comp->size;
}
/* ==================== the relation between ids and the corpus */
/**
* Gets the frequency of an item on this attribute.
*
* @param attribute The P-attribute to look on
* @param id Identifier of an item on this attribute.
* @return The frequency count of the item specified
* by id, or an error code (if less than 0)
*/
int
cl_id2freq(Attribute *attribute, int id)
{
Component *freqs;
check_arg(attribute, ATT_POS, cl_errno);
freqs = ensure_component(attribute, CompCorpusFreqs, 0);
if (freqs == NULL)
return cl_errno = CDA_ENODATA;
if (id >= 0 && id < freqs->size) {
cl_errno = CDA_OK;
return ntohl(freqs->data.data[id]);
}
return cl_errno = CDA_EIDXORNG;
}
/* ============================================================ */
/**
* Gets all the corpus positions where the specified item is
* found on the given P-attribute.
*
* The restrictor list is a set of ranges in which instances of
* the item MUST occur to be collected by this function. If no
* restrictor list is specified (i.e. restrictor_list is NULL),
* then ALL corpus positions where the item occurs are returned.
*
* This restrictor list has the form of a list of ranges {start,end}
* of size restrictor_list_size, that is, the number of ints in
* this area is 2 * restrictor_list_size.
*
* This function is "oldstyle" because in the "newstyle" function,
* there is no restrictor list. (And in fact, the newstyle
* function is implemented as a macro to this one with the last
* two arguments NULL and 0.) It might be better called "restricted".
*
* @see cl_id2cpos
*
* @param attribute The P-attribute to look on.
* @param id The id of the item to look for.
* @param freq The frequency of the specified
* item is written here. This will
* be 0 in the case of errors.
* @param restrictor_list A list of pairs of integers
* specifying ranges {start,end}
* in the corpus
* @param restrictor_list_size The number of PAIRS of ints in
* the restrictor list.
* @return Pointer to the list of corpus
* positions; or NULL in case of
* error.
*/
int *
cl_id2cpos_oldstyle(Attribute *attribute, int id, int *freq, int *restrictor_list, int restrictor_list_size)
{
Component *revcorp, *revcidx;
int *buffer;
int size, range, i;
check_arg(attribute, ATT_POS, NULL);
size = cl_max_cpos(attribute);
if (size <= 0 || !cl_all_ok()) {
/* Rprintf("Cannot determine size of PA %s\n", attribute->any.name); */
return NULL;
}
range = cl_max_id(attribute);
if (range <= 0 || !cl_all_ok()) {
/* Rprintf("Cannot determine ID range of PA %s\n", attribute->any.name); */
return NULL;
}
if (id < 0 || id >= range) {
cl_errno = CDA_EIDORNG;
/* Rprintf("ID %d out of range of PA %s\n", id, attribute->any.name); */
*freq = 0;
return NULL;
}
*freq = cl_id2freq(attribute, id);
if (*freq < 0 || !cl_all_ok()) {
/* Rprintf("Frequency %d of ID %d illegal (PA %s)\n", *freq, id, attribute->any.name); */
return NULL;
}
/* there are no items in a P-Attribute with freq 0 - we don't have to catch that special case. */
buffer = (int *)cl_malloc(*freq * sizeof(int));
if (cl_index_compressed(attribute)) {
BStream bs;
unsigned int i, b, last_pos, gap, offset, ins_ptr, res_ptr;
revcorp = ensure_component(attribute, CompCompRF, 0);
revcidx = ensure_component(attribute, CompCompRFX, 0);
if (!revcorp || !revcidx) {
cl_errno = CDA_ENODATA;
*freq = 0;
return NULL;
}
b = compute_ba(*freq, size);
offset = ntohl(revcidx->data.data[id]); /* byte offset in RFC */
BSopen((unsigned char *)revcorp->data.data, "r", &bs);
BSseek(&bs, offset);
last_pos = 0;
ins_ptr = 0;
res_ptr = 0;
for (i = 0; i < *freq; i++) {
gap = read_golomb_code_bs(b, &bs);
last_pos += gap;
/* TODO : when the end of the restrictor list is reached, we can also leave the for loop above */
if (restrictor_list && restrictor_list_size > 0) {
while (res_ptr < restrictor_list_size && last_pos > restrictor_list[res_ptr * 2 + 1])
/* beyond last restricting range */
res_ptr++;
if (res_ptr < restrictor_list_size &&
last_pos >= restrictor_list[res_ptr * 2] &&
last_pos <= restrictor_list[res_ptr * 2 + 1]) {
buffer[ins_ptr++] = last_pos;
}
}
else
/* no restrictor list: copy */
buffer[ins_ptr++] = last_pos;
}
BSclose(&bs);
/* reduce, if possible */
if (ins_ptr < *freq && ins_ptr != *freq) {
if (ins_ptr == 0) {
assert(buffer);
cl_free(buffer);
}
else
buffer = cl_realloc(buffer, ins_ptr * sizeof(int));
*freq = ins_ptr;
}
} /* endif cl_index_compressed */
else {
revcorp = ensure_component(attribute, CompRevCorpus, 0);
revcidx = ensure_component(attribute, CompRevCorpusIdx, 0);
if (!(revcorp && revcidx)) {
cl_errno = CDA_ENODATA;
/* Rprintf("Cannot load REVCORP or REVCIDX component of %s\n", attribute->any.name); */
*freq = 0;
return NULL;
}
memcpy(buffer, revcorp->data.data + ntohl(revcidx->data.data[id]), *freq * sizeof(int));
/* convert network byte order to native integers */
for (i = 0; i < *freq; i++)
buffer[i] = ntohl(buffer[i]);
if (restrictor_list != NULL && restrictor_list_size > 0) {
/* force all items to be within the restrictor's ranges */
int res_ptr = 0, buf_ptr = 0, ins_ptr = 0;
while (buf_ptr < *freq && res_ptr < restrictor_list_size) {
if (buffer[buf_ptr] < restrictor_list[res_ptr*2])
/* before start */
buf_ptr++;
else if (buffer[buf_ptr] > restrictor_list[res_ptr*2+1])
/* beyond end */
res_ptr++;
else
/* within range */
buffer[ins_ptr++] = buffer[buf_ptr++];
}
if (ins_ptr < *freq && ins_ptr != *freq) {
if (ins_ptr == 0)
cl_free(buffer);
else
buffer = cl_realloc(buffer, ins_ptr * sizeof(int));
*freq = ins_ptr;
}
}
}
cl_errno = CDA_OK;
return buffer;
}
/* ---------------------------------------- stream-like reading */
/**
* Underlying structure for the PositionStream object.
*
* PositionStreams are used for accessing Attributes.
* Each one represents a stream of corpus positions,
* representing positions where a given item occurs.
*
*/
typedef struct _position_stream_rec_ {
Attribute *attribute; /**< The Attribute on which this PositionStream has been opened. */
int id; /**< The item whose positions this PositionStream will read */
int id_freq; /**< id frequency (ie frequency of the item in question);
maximum number of positions that can be read */
int nr_items; /**< how many items delivered so far */
int is_compressed; /**< Boolean: attribute REVCORP is compressed? */
/** for compressed streams, the stream is a BStream object rather than just a pointer. */
BStream bs;
int b; /**< relevent for compressed streams */
int last_pos; /**< relevent for compressed streams */
/** pointer to base of stream for uncompressed streams. */
int *base;
} PositionStreamRecord;
/**
* Creates a new PositionStream object.
*
* @param attribute The P-attribute to open the position stream on
* @param id The id that the new PositionStream will have.
* This the id of an item on the specified attribute.
* @return The new object, or NULL in case of problem.
*/
PositionStream
cl_new_stream(Attribute *attribute, int id)
{
Component *revcorp, *revcidx;
int size, freq, range;
PositionStream ps = NULL;
check_arg(attribute, ATT_POS, NULL);
size = cl_max_cpos(attribute);
if (size <= 0 || !cl_all_ok())
return NULL;
range = cl_max_id(attribute);
if (range <= 0 || !cl_all_ok())
return NULL;
if (id < 0 || id >= range) {
cl_errno = CDA_EIDORNG;
return NULL;
}
freq = cl_id2freq(attribute, id);
if (freq < 0 || !cl_all_ok())
return NULL;
ps = (PositionStream)cl_malloc(sizeof(PositionStreamRecord));
ps->attribute = attribute;
ps->id = id;
ps->id_freq = freq;
ps->nr_items = 0;
ps->is_compressed = 0;
ps->b = 0; ps->last_pos = 0;
ps->base = NULL;
if (cl_index_compressed(attribute)) {
int offset;
ps->is_compressed = 1;
revcorp = ensure_component(attribute, CompCompRF, 0);
revcidx = ensure_component(attribute, CompCompRFX, 0);
if (revcorp == NULL || revcidx == NULL) {
cl_errno = CDA_ENODATA;
cl_free(ps);
return NULL;
}
ps->b = compute_ba(ps->id_freq, size);
offset = ntohl(revcidx->data.data[id]); /* byte offset in RFC */
BSopen((unsigned char *)revcorp->data.data, "r", &(ps->bs));
BSseek(&(ps->bs), offset);
ps->last_pos = 0;
}
else {
ps->is_compressed = 0;
revcorp = ensure_component(attribute, CompRevCorpus, 0);
revcidx = ensure_component(attribute, CompRevCorpusIdx, 0);
if (revcorp == NULL || revcidx == NULL) {
cl_errno = CDA_ENODATA;
cl_free(ps);
return NULL;
}
ps->base = revcorp->data.data + ntohl(revcidx->data.data[ps->id]);
}
return ps;
}
/**
* Deletes a PositionStream object.
*/
int
cl_delete_stream(ClPositionStream *ps)
{
assert(ps && *ps);
(*ps)->attribute = NULL;
(*ps)->id = -1;
(*ps)->id_freq = -1;
(*ps)->nr_items = -1;
(*ps)->is_compressed = 0;
if ((*ps)->is_compressed) {
BSclose(&((*ps)->bs));
(*ps)->b = 0;
(*ps)->last_pos = 0;
}
else
(*ps)->base = NULL;
cl_free(*ps);
return 1;
}
/**
* Reads corpus positions from a position stream to a buffer.
*
* @param ps The position stream to read.
* @param buffer Location to put the resulting item positions.
* @param buffer_size Maximum number of item positions to read.
* (Fewer will be read if fewer are available).
* @return The number of item positions that have been
* read. This may be less than buffer_size (and
* will be 0 if there are no instances of this item left).
*/
int
cl_read_stream(PositionStream ps, int *buffer, int buffer_size)
{
int items_to_read, i;
assert(ps);
assert(buffer);
/* return 0 if we have already read >= freq items */
if (ps->nr_items >= ps->id_freq)
return 0;
if (ps->nr_items + buffer_size > ps->id_freq)
items_to_read = ps->id_freq - ps->nr_items;
else
items_to_read = buffer_size;
assert(items_to_read >= 0);
if (items_to_read == 0)
return 0;
if (ps->is_compressed) {
for (i = 0; i < items_to_read; i++, ps->nr_items++) {
ps->last_pos += read_golomb_code_bs(ps->b, &(ps->bs));
*buffer = ps->last_pos;
buffer++;
}
}
else {
size_t k;
k = items_to_read * sizeof(int);
if (k < PTRDIFF_MAX){
memcpy(buffer, ps->base + ps->nr_items, k);
ps->nr_items += items_to_read;
/* convert network byte order to native integers */
for (i = 0; i < items_to_read; i++)
buffer[i] = ntohl(buffer[i]);
}
}
return items_to_read;
}