Added some debug printing to the ::Load method
[SXSI/XMLTree.git] / XMLTree.cpp
1 #include "XMLTree.h"\r
2 #include <cstring>\r
3 \r
4 // functions to convert tag positions to the corresponding tree node and viceversa. \r
5 // These are implemented in order to be able to change the tree and Tags representations, \r
6 // without affecting the code so much.\r
7 // Current implementation corresponds to balanced-parentheses representation for\r
8 // the tree, and storing 2 tags per tree node (opening and closing tags).\r
9 \r
10 // tag position -> tree node\r
11 inline treeNode tagpos2node(int t) {\r
12    return (treeNode)t;\r
13 }\r
14 \r
15 // tree node -> tag position\r
16 inline int node2tagpos(treeNode x) {\r
17    return (int)x;\r
18 }\r
19 \r
20 \r
21 //KIM OJO to prevent suprious "unused result" warnings\r
22 \r
23 inline void ufread(void *ptr, size_t size, size_t nmemb, FILE *stream){\r
24   size_t res;\r
25   res = fread(ptr,size,nmemb,stream);\r
26   if (res < nmemb)\r
27     throw "ufread I/O error";\r
28 \r
29   return;\r
30 }\r
31 \r
32 inline void ufwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream){\r
33   size_t res;\r
34   res = fwrite(ptr,size,nmemb,stream);\r
35   if (res < nmemb)\r
36     throw "ufwrite I/O error";\r
37   return;\r
38 }\r
39 \r
40 // OJO to fail cleanly while doing a realloc\r
41 // if we can't realloc we are pretty much screwed anyway but\r
42 // it makes the code clearer to not have a bunch of if (!ptr) { printf("..."); exit(1); };\r
43 inline void * urealloc(void *ptr, size_t size){\r
44 \r
45   void * dest = realloc(ptr,size);\r
46   //don't fail if we requested size 0\r
47   if (dest == NULL && size > 0 )\r
48     throw std::bad_alloc();\r
49   return dest;\r
50 \r
51 }\r
52 \r
53 inline void * ucalloc(size_t nmemb, size_t size){\r
54 \r
55   void * dest = calloc(nmemb,size);\r
56   //don't fail if we requested size 0\r
57   if (dest == NULL && nmemb > 0 && size > 0 )\r
58     throw std::bad_alloc();\r
59   return dest;\r
60 \r
61 }\r
62 \r
63 inline void * umalloc(size_t size){\r
64   void * dest = malloc(size);\r
65   if (dest == NULL && size > 0)\r
66     throw std::bad_alloc();\r
67   return dest;\r
68 }\r
69 \r
70 // Save: saves XML tree data structure to file. \r
71 void XMLTree::Save(unsigned char *filename) \r
72  {\r
73 \r
74     FILE *fp;\r
75     char filenameaux[1024];\r
76     int i;\r
77    \r
78     sprintf(filenameaux, "%s.srx", filename);\r
79     fp = fopen(filenameaux, "w");\r
80     if (fp == NULL) {\r
81        printf("Error: cannot create file %s to store the tree structure of XML collection\n", filenameaux);\r
82        exit(1);\r
83     } \r
84     \r
85     // first stores the tree topology\r
86     saveTree(Par, fp);\r
87  \r
88     // stores the table with tag names\r
89     ufwrite(&ntagnames, sizeof(int), 1, fp);\r
90     for (i=0; i<ntagnames;i++)\r
91       fprintf(fp, "%s\n",TagName[i]);\r
92     \r
93     \r
94     // stores the flags\r
95     ufwrite(&indexing_empty_texts, sizeof(bool), 1, fp);\r
96     ufwrite(&initialized, sizeof(bool), 1, fp);\r
97     ufwrite(&finished, sizeof(bool), 1, fp);\r
98     ufwrite(&disable_tc, sizeof(bool),1,fp);\r
99     \r
100     if (!indexing_empty_texts) EBVector->save(fp);\r
101     \r
102     // stores the tags\r
103     Tags->save(fp);\r
104 \r
105     // stores the texts   \r
106     if (!disable_tc)\r
107       Text->Save(fp);\r
108 \r
109     fclose(fp);\r
110 \r
111  }\r
112 \r
113 \r
114 // Load: loads XML tree data structure from file. Returns\r
115 // a pointer to the loaded data structure\r
116 XMLTree *XMLTree::Load(unsigned char *filename, int sample_rate_text) \r
117  {\r
118 \r
119     FILE *fp;\r
120     char buffer[1024];\r
121     XMLTree *XML_Tree;\r
122     int i;\r
123     size_t s_tree = 0;\r
124     long s_text = 0;\r
125     size_t s_tags = 0;\r
126 \r
127     // first load the tree topology\r
128     sprintf(buffer, "%s.srx", filename);\r
129     fp = fopen(buffer, "r");\r
130     if (fp == NULL) {\r
131        printf("Error: cannot open file %s to load the tree structure of XML collection\n", buffer);\r
132        exit(1);\r
133     } \r
134 \r
135     XML_Tree = new XMLTree();\r
136 \r
137     XML_Tree->Par = (bp *)umalloc(sizeof(bp));\r
138 \r
139     loadTree(XML_Tree->Par, fp); \r
140 \r
141     s_tree += sizeof(bp);\r
142 \r
143     // stores the table with tag names\r
144     ufread(&XML_Tree->ntagnames, sizeof(int), 1, fp);\r
145     \r
146     s_tree += sizeof(int);\r
147 \r
148     XML_Tree->TagName = (unsigned char **)umalloc(XML_Tree->ntagnames*sizeof(unsigned char *));\r
149     \r
150     s_tags += sizeof(unsigned char*)*XML_Tree->ntagnames;\r
151 \r
152 \r
153     for (i=0; i<XML_Tree->ntagnames;i++) {\r
154       \r
155       // OJO Kim is it needed ?\r
156       int k = feof(fp);\r
157 \r
158       \r
159        // fscanf chokes on "\n" which is the case for the root element\r
160        char * r = fgets(buffer,1023,fp);\r
161        //       int r = fscanf(fp, "%s\n",buffer);\r
162        if (r==NULL)\r
163          throw "Cannot read tag list";\r
164 \r
165        // strlen is actually the right size, since there is a trailing '\n'\r
166        int len = strlen((const char*)buffer);\r
167        XML_Tree->TagName[i] = (unsigned char *)ucalloc(len,sizeof(char));\r
168        strncpy((char *)XML_Tree->TagName[i], (const char *)buffer,len - 1);\r
169        s_tags+= len*sizeof(char);\r
170     }\r
171         \r
172     // loads the flags\r
173 \r
174     ufread(&(XML_Tree->indexing_empty_texts), sizeof(bool), 1, fp);\r
175     ufread(&(XML_Tree->initialized), sizeof(bool), 1, fp);\r
176     ufread(&(XML_Tree->finished), sizeof(bool), 1, fp);\r
177     ufread(&(XML_Tree->disable_tc), sizeof(bool), 1, fp);\r
178     \r
179     s_tree+=sizeof(bool)*4;\r
180 \r
181     if (!(XML_Tree->indexing_empty_texts)) XML_Tree->EBVector = static_bitsequence_rrr02::load(fp);\r
182     \r
183     s_tree+= XML_Tree->EBVector->size();\r
184     \r
185     // loads the tags\r
186     XML_Tree->Tags = static_sequence::load(fp);\r
187 \r
188     s_tree+= XML_Tree->Tags->size();\r
189 \r
190     s_text = ftell(fp);\r
191 \r
192     // loads the texts\r
193     if (!XML_Tree->disable_tc){\r
194       XML_Tree->Text = TextCollection::InitTextCollection(sample_rate_text);\r
195       XML_Tree->Text->Load(fp,sample_rate_text);\r
196     }\r
197     else\r
198       XML_Tree->Text = NULL;\r
199 \r
200     s_text = ftell(fp) - s_text;\r
201     fclose(fp);\r
202 \r
203     std::cerr << "Tree part is " << s_tree/1024 << " Kbytes,\n"\r
204               << "Tag part is " << s_tags/1024 << " Kbytes,\n"\r
205               << "Text collection is " << s_text/1024 << " Kbytes \n";\r
206     return XML_Tree;\r
207  }\r
208 \r
209 \r
210 // ~XMLTree: frees memory of XML tree.\r
211 XMLTree::~XMLTree() \r
212  { \r
213     int i;\r
214 \r
215     destroyTree(Par);\r
216     free(Par); // frees the memory of struct Par\r
217    \r
218     for (i=0; i<ntagnames;i++) \r
219        free(TagName[i]);\r
220     \r
221     free(TagName);\r
222 \r
223     if (!indexing_empty_texts) {\r
224        //EBVector->~static_bitsequence_rrr02();\r
225        delete EBVector;\r
226        EBVector = NULL;\r
227     }\r
228 \r
229     //Tags->~static_sequence_wvtree();\r
230     delete Tags;\r
231     Tags = NULL;\r
232 \r
233     //Text->~TextCollection();\r
234     delete Text;\r
235     Text = NULL;\r
236 \r
237     initialized = false;\r
238     finished = false;\r
239  }\r
240 \r
241 // root(): returns the tree root.\r
242 treeNode XMLTree::Root() \r
243  {\r
244     if (!finished) {\r
245        fprintf(stderr, "Root() : Error: data structure has not been constructed properly\n");\r
246        exit(1);\r
247     }\r
248     return root_node(Par);\r
249  }\r
250 \r
251 // SubtreeSize(x): the number of nodes (and attributes) in the subtree of node x.\r
252 int XMLTree::SubtreeSize(treeNode x) \r
253  {\r
254     if (!finished) {\r
255        fprintf(stderr, "Error: data structure has not been constructed properly\n");\r
256        exit(1);\r
257     }\r
258     return subtree_size(Par, x);\r
259  }\r
260 \r
261 // SubtreeTags(x,tag): the number of occurrences of tag within the subtree of node x.\r
262 int XMLTree::SubtreeTags(treeNode x, TagType tag) \r
263  {\r
264     if (!finished) {\r
265        fprintf(stderr, "Error: data structure has not been constructed properly\n");\r
266        exit(1);\r
267     }\r
268 \r
269     int s = x + 2*subtree_size(Par, x) - 1;\r
270  \r
271     return Tags->rank(tag, s) - Tags->rank(tag, node2tagpos(x)-1);\r
272  }\r
273 \r
274 // IsLeaf(x): returns whether node x is leaf or not. In the succinct representation\r
275 // this is just a bit inspection.\r
276 bool XMLTree::IsLeaf(treeNode x) \r
277  {\r
278     return isleaf(Par, x);\r
279  } \r
280 \r
281 // IsAncestor(x,y): returns whether node x is ancestor of node y.\r
282 bool XMLTree::IsAncestor(treeNode x, treeNode y) \r
283  {\r
284     if (!finished) {\r
285        fprintf(stderr, "Error: data structure has not been constructed properly\n");\r
286        exit(1);\r
287     }\r
288 \r
289     return is_ancestor(Par, x, y);\r
290  }\r
291 \r
292 // IsChild(x,y): returns whether node x is parent of node y.\r
293 bool XMLTree::IsChild(treeNode x, treeNode y) \r
294  {\r
295     if (!finished) {\r
296        fprintf(stderr, "Error: data structure has not been constructed properly\n");\r
297        exit(1);\r
298     }\r
299 \r
300     if (!is_ancestor(Par, x, y)) return false;\r
301     return depth(Par, x) == (depth(Par, y) + 1);\r
302  }\r
303 \r
304 // NumChildren(x): number of children of node x. Constant time with the data structure\r
305 // of Sadakane.\r
306 int XMLTree::NumChildren(treeNode x) \r
307  {\r
308     if (!finished) {\r
309        fprintf(stderr, "Error: data structure has not been constructed properly\n");\r
310        exit(1);\r
311     }\r
312 \r
313     return degree(Par, x);\r
314  }\r
315 \r
316 // ChildNumber(x): returns i if node x is the i-th children of its parent.\r
317 int XMLTree::ChildNumber(treeNode x) \r
318  {\r
319     if (!finished) {\r
320        fprintf(stderr, "Error: data structure has not been constructed properly\n");\r
321        exit(1);\r
322     }\r
323 \r
324     return child_rank(Par, x);\r
325  }\r
326 \r
327 // Depth(x): depth of node x, a simple binary rank on the parentheses sequence.\r
328 int XMLTree::Depth(treeNode x) \r
329  {\r
330     if (!finished) {\r
331        fprintf(stderr, "Error: data structure has not been constructed properly\n");\r
332        exit(1);\r
333     }\r
334 \r
335     return depth(Par, x);\r
336  }\r
337 \r
338 // Preorder(x): returns the preorder number of node x, just counting the tree\r
339 // nodes (i.e., tags, it disregards the texts in the tree).\r
340 int XMLTree::Preorder(treeNode x) \r
341  {\r
342     if (!finished) {\r
343        fprintf(stderr, "Error: data structure has not been constructed properly\n");\r
344        exit(1);\r
345     }\r
346 \r
347     return preorder_rank(Par, x);\r
348  }\r
349 \r
350 // Postorder(x): returns the postorder number of node x, just counting the tree\r
351 // nodes (i.e., tags, it disregards the texts in the tree).\r
352 int XMLTree::Postorder(treeNode x) \r
353  {\r
354     if (!finished) {\r
355        fprintf(stderr, "Error: data structure has not been constructed properly\n");\r
356        exit(1);\r
357     }\r
358     return postorder_rank(Par, x);\r
359  }\r
360 \r
361 // Tag(x): returns the tag identifier of node x.\r
362 TagType XMLTree::Tag(treeNode x) \r
363  {\r
364     if (!finished) {\r
365        fprintf(stderr, "Error: data structure has not been constructed properly\n");\r
366        exit(1);\r
367     }\r
368 \r
369     return Tags->access(node2tagpos(x));\r
370  }\r
371 \r
372 // DocIds(x): returns the range of text identifiers that descend from node x.\r
373 // returns {NULLT, NULLT} when there are no texts descending from x.\r
374 range XMLTree::DocIds(treeNode x) \r
375  {\r
376     if (!finished) {\r
377        fprintf(stderr, "Error: data structure has not been constructed properly\n");\r
378        exit(1);\r
379     }\r
380 \r
381     range r;\r
382     if (indexing_empty_texts) { // faster, no rank needed\r
383        r.min = x;\r
384        r.max = x+2*subtree_size(Par, x)-2;\r
385     }\r
386     else { // we are not indexing empty texts, we need rank\r
387        int min = EBVector->rank1(x-1);                          \r
388        int max = EBVector->rank1(x+2*subtree_size(Par, x)-2); \r
389        if (min==max) { // range is empty, no texts within the subtree of x\r
390           r.min = NULLT;\r
391           r.max = NULLT;\r
392        }\r
393        else { // the range is non-empty, there are texts within the subtree of x\r
394           r.min = min+1;\r
395           r.max = max;\r
396        }\r
397     }\r
398     return r;\r
399  }\r
400 \r
401 // Parent(x): returns the parent node of node x.\r
402 treeNode XMLTree::Parent(treeNode x) \r
403  {\r
404     if (!finished) {\r
405        fprintf(stderr, "Error: data structure has not been constructed properly\n");\r
406        exit(1);\r
407     }\r
408     if (x == Root())\r
409       return NULLT;\r
410     else\r
411       return parent(Par, x);\r
412  }\r
413 \r
414 // Child(x,i): returns the i-th child of node x, assuming it exists.\r
415 treeNode XMLTree::Child(treeNode x, int i) \r
416 {\r
417     if (!finished) {\r
418        fprintf(stderr, "Error: data structure has not been constructed properly\n");\r
419        exit(1);\r
420     }\r
421 \r
422     if (i <= OPTD) return naive_child(Par, x, i);\r
423     else return child(Par, x, i);\r
424  }\r
425 \r
426 // FirstChild(x): returns the first child of node x, assuming it exists. Very fast in BP.\r
427 treeNode XMLTree::FirstChild(treeNode x) \r
428  {\r
429     if (!finished) {\r
430        fprintf(stderr, "Error: data structure has not been constructed properly\n");\r
431        exit(1);\r
432     }\r
433 \r
434     return first_child(Par, x);\r
435  }\r
436 \r
437 // NextSibling(x): returns the next sibling of node x, assuming it exists.\r
438 treeNode XMLTree::NextSibling(treeNode x) \r
439  {\r
440     if (!finished) {\r
441        fprintf(stderr, "Error: data structure has not been constructed properly\n");\r
442        exit(1);\r
443     }\r
444     if (x == Root())\r
445       return NULLT;\r
446  \r
447     return next_sibling(Par, x);\r
448  }\r
449 \r
450 // PrevSibling(x): returns the previous sibling of node x, assuming it exists.\r
451 treeNode XMLTree::PrevSibling(treeNode x) \r
452  {\r
453     if (!finished) {\r
454        fprintf(stderr, "Error: data structure has not been constructed properly\n");\r
455        exit(1);\r
456     }\r
457 \r
458     return prev_sibling(Par, x);\r
459  }\r
460 \r
461 // TaggedChild(x,i,tag): returns the i-th child of node x tagged tag, or NULLT if there is none.\r
462 // Because of the balanced-parentheses representation of the tree, this operation is not supported\r
463 // efficiently, just iterating among the children of node x until finding the desired child.\r
464 treeNode XMLTree::TaggedChild(treeNode x, int i, TagType tag) \r
465  {\r
466     if (!finished) {\r
467        fprintf(stderr, "Error: data structure has not been constructed properly\n");\r
468        exit(1);\r
469     }\r
470 \r
471     treeNode child;\r
472    \r
473     child = first_child(Par, x); // starts at first child of node x\r
474     if (child==(treeNode)-1) return NULLT; // node x is a leaf, there is no such child\r
475     while (child!=(treeNode)-1) {\r
476        if (Tags->access(node2tagpos(child)) == tag) { // current child is labeled with tag of interest\r
477           i--;\r
478           if (i==0) return child; // we have seen i children of x tagged tag, this is the one we are looking for\r
479        }\r
480        child = next_sibling(Par, x); // OK, let's try with the next child\r
481     }\r
482     return NULLT; // no such child was found  \r
483  }\r
484 \r
485 // TaggedDesc(x,tag): returns the first node tagged tag with larger preorder than x and within\r
486 // the subtree of x. Returns NULLT if there is none.\r
487 treeNode XMLTree::TaggedDesc(treeNode x, TagType tag) \r
488  {\r
489     if (!finished) {\r
490        fprintf(stderr, "Error: data structure has not been constructed properly\n");\r
491        exit(1);\r
492     }\r
493 \r
494     int r, s;\r
495     treeNode y;\r
496     r = (int) Tags->rank(tag, node2tagpos(x));\r
497     s = (int) Tags->select(tag, r+1);\r
498     if (s == -1) return NULLT; // there is no such node\r
499     y = tagpos2node(s); // transforms the tag position into a node position\r
500     if (!is_ancestor(Par, x, y)) return NULLT; // the next node tagged tag (in preorder) is not within the subtree of x.\r
501     else return y;\r
502  }\r
503 \r
504 // TaggedPrec(x,tag): returns the first node tagged tag with smaller preorder than x and not an\r
505 // ancestor of x. Returns NULLT if there is none.\r
506 treeNode XMLTree::TaggedPrec(treeNode x, TagType tag) \r
507  {\r
508     if (!finished) {\r
509        fprintf(stderr, "Error: data structure has not been constructed properly\n");\r
510        exit(1);\r
511     }\r
512     \r
513     int r, s;\r
514     treeNode node_s, root;\r
515     r = (int)Tags->rank(tag, node2tagpos(x)-1);\r
516     if (r==0) return NULLT; // there is no such node.\r
517     s = (int)Tags->select(tag, r);\r
518     root = root_node(Par);\r
519     node_s = tagpos2node(s);\r
520     while (is_ancestor(Par, node_s, x) && (node_s!=root)) { // the one that we found is an ancestor of x\r
521        r--;\r
522        if (r==0) return NULLT; // there is no such node\r
523        s = (int)Tags->select(tag, r);  // we should use select_prev instead when provided\r
524        node_s = tagpos2node(s);\r
525     }\r
526     return NULLT; // there is no such node \r
527  }\r
528 \r
529 // TaggedFoll(x,tag): returns the first node tagged tag with larger preorder than x and not in\r
530 // the subtree of x. Returns NULLT if there is none.\r
531 treeNode XMLTree::TaggedFoll(treeNode x, TagType tag) \r
532  {\r
533     if (!finished) {\r
534        fprintf(stderr, "Error: data structure has not been constructed properly\n");\r
535        exit(1);\r
536     }\r
537 \r
538     int r, s;\r
539     r = (int) Tags->rank(tag, node2tagpos(next_sibling(Par, x))-1);\r
540     s = (int) Tags->select(tag, r+1);  // select returns -1 in case that there is no r+1-th tag.\r
541     if (s==-1) return NULLT;\r
542     else return tagpos2node(s);\r
543  }\r
544 \r
545 // PrevText(x): returns the document identifier of the text to the left \r
546 // of node x, or NULLT if x is the root node or the text is empty.\r
547 // Assumes Doc ids start from 0.\r
548 DocID XMLTree::PrevText(treeNode x) \r
549  {\r
550     if (!finished) {\r
551        fprintf(stderr, "Error: data structure has not been constructed properly\n");\r
552        exit(1);\r
553     }\r
554 \r
555     if (x == Root()) return NULLT;\r
556     if (indexing_empty_texts)  // faster, no rank needed\r
557        return (DocID)x-1;\r
558     else { // we are not indexing empty texts, rank is needed\r
559        if (EBVector->access(x-1) == 0) \r
560           return (DocID)NULLT;  // there is no text to the left of node (text is empty)\r
561        else\r
562           return (DocID)EBVector->rank1(x-1)-1;  //-1 because document ids start from 0\r
563     }\r
564  }\r
565 \r
566 // NextText(x): returns the document identifier of the text to the right\r
567 // of node x, or NULLT if x is the root node. Assumes Doc ids start from 0.\r
568 DocID XMLTree::NextText(treeNode x) \r
569  {\r
570     if (!finished) {\r
571        fprintf(stderr, "Error: data structure has not been constructed properly\n");\r
572        exit(1);\r
573     }\r
574 \r
575     if (x == Root()) return NULLT;\r
576     if (indexing_empty_texts)  // faster, no rank needed\r
577        return (DocID)x+2*subtree_size(Par, x)-1;\r
578     else { // we are not indexing empty texts, rank is needed\r
579        int p = x+2*subtree_size(Par, x)-1;\r
580        if (EBVector->access(p) == 0) // there is no text to the right of node\r
581           return (DocID)NULLT;\r
582        else\r
583           return (DocID)EBVector->rank1(p)-1; //-1 because document ids start from 0\r
584     }\r
585  }\r
586 \r
587 // MyText(x): returns the document identifier of the text below node x, \r
588 // or NULLT if x is not a leaf node or the text is empty. Assumes Doc \r
589 // ids start from 0.\r
590 DocID XMLTree::MyText(treeNode x) \r
591  {\r
592     if (!finished) {\r
593        fprintf(stderr, "Error: data structure has not been constructed properly\n");\r
594        exit(1);\r
595     }\r
596 \r
597     if (!IsLeaf(x)) return NULLT;\r
598     if (indexing_empty_texts) // faster, no rank needed\r
599        return (DocID)x;\r
600     else { // we are not indexing empty texts, rank is needed\r
601        if (EBVector->access(x) == 0)  // there is no text below node x\r
602           return (DocID)NULLT;\r
603        else\r
604           return (DocID)EBVector->rank1(x)-1; //-1 because document ids start from 0\r
605     } \r
606  }\r
607 \r
608 // TextXMLId(d): returns the preorder of document with identifier d in the tree consisting of\r
609 // all tree nodes and all text nodes. Assumes that the tree root has preorder 1.\r
610 int XMLTree::TextXMLId(DocID d) \r
611  {\r
612     if (!finished) {\r
613        fprintf(stderr, "Error: data structure has not been constructed properly\n");\r
614        exit(1);\r
615     }\r
616 \r
617     if (indexing_empty_texts) \r
618        return d + rank_open(Par, d)+1; // +1 because root has preorder 1\r
619     else { // slower, needs rank and select\r
620        int s = EBVector->select1(d+1);\r
621        return rank_open(Par, s) + d + 1; // +1 because root has preorder 1\r
622     }\r
623  }\r
624 \r
625 // NodeXMLId(x): returns the preorder of node x in the tree consisting \r
626 // of all tree nodes and all text nodes. Assumes that the tree root has\r
627 // preorder 0;\r
628 int XMLTree::NodeXMLId(treeNode x) \r
629  {\r
630     if (!finished) {\r
631        fprintf(stderr, "Error: data structure has not been constructed properly\n");\r
632        exit(1);\r
633     }\r
634 \r
635     if (indexing_empty_texts)\r
636        return x - 1 + rank_open(Par, x);\r
637     else {\r
638        if (x == Root()) return 1; // root node has preorder 1\r
639        else\r
640           return rank_open(Par, x) + EBVector->rank1(x-1);\r
641     }\r
642  }\r
643 \r
644 // ParentNode(d): returns the parent node of document identifier d.\r
645 treeNode XMLTree::ParentNode(DocID d) \r
646  {\r
647     if (!finished) {\r
648        fprintf(stderr, "Error: data structure has not been constructed properly\n");\r
649        exit(1);\r
650     }\r
651     \r
652     if (d == NULLT)\r
653       return NULLT;\r
654     \r
655     int s;\r
656     // OJO : Kim : I added the d+1. before that, else branch was \r
657     // EBVector->select1(d)\r
658     // and gave wrong results (I'm really poking a bear with a stick here).\r
659     if (indexing_empty_texts) s = d;\r
660     else s = EBVector->select1(d+1);\r
661     \r
662     if (inspect(Par,s) == CP) // is a closing parenthesis\r
663        return parent(Par, find_open(Par, s));\r
664     else // is an opening parenthesis\r
665        return (treeNode)s;\r
666      \r
667  }\r
668 \r
669 \r
670 // OpenDocument(empty_texts): it starts the construction of the data structure for\r
671 // the XML document. Parameter empty_texts indicates whether we index empty texts\r
672 // in document or not. Returns a non-zero value upon success, NULLT in case of error.\r
673 int XMLTree::OpenDocument(bool empty_texts, int sample_rate_text,bool dtc)\r
674  {\r
675     initialized = true;\r
676     finished = false;\r
677     found_attributes = false;\r
678     npar = 0;\r
679     parArraySize = 1;\r
680     ntagnames = 2;    \r
681     disable_tc = dtc;\r
682     \r
683     indexing_empty_texts = empty_texts;\r
684     \r
685     par_aux = (pb *)umalloc(sizeof(pb)*parArraySize);\r
686     \r
687     tags_aux = (TagType *) umalloc(sizeof(TagType));\r
688     \r
689     TagName = (unsigned char **) umalloc(2*sizeof(unsigned char*));\r
690 \r
691     TagName[0] = (unsigned char *) umalloc(4*sizeof(unsigned char));\r
692 \r
693     strcpy((char *) TagName[0], "<@>");\r
694 \r
695     TagName[1] = (unsigned char *) umalloc(4*sizeof(unsigned char));\r
696 \r
697     strcpy((char *) TagName[1], "<$>");\r
698 \r
699 \r
700     if (!indexing_empty_texts) \r
701       empty_texts_aux = (unsigned int *)umalloc(sizeof(unsigned int));\r
702        \r
703     \r
704     \r
705     Text = TextCollection::InitTextCollection((unsigned)sample_rate_text);\r
706     \r
707     return 1;  // indicates success in the initialization of the data structure\r
708  }\r
709 \r
710 // CloseDocument(): it finishes the construction of the data structure for the XML\r
711 // document. Tree and tags are represented in the final form, dynamic data \r
712 // structures are made static, and the flag "finished" is set to true. After that, \r
713 // the data structure can be queried.\r
714 int XMLTree::CloseDocument()\r
715  {\r
716     if (!initialized) {  // data structure has not been initialized properly\r
717        fprintf(stderr, "Error: data structure has not been initialized properly (by calling method OpenDocument)\n");\r
718        return NULLT;\r
719     }\r
720     \r
721     // closing parenthesis for the tree root\r
722     par_aux = (pb *)urealloc(par_aux, sizeof(pb)*(1+npar/(8*sizeof(pb))));\r
723     \r
724     // creates the data structure for the tree topology\r
725     Par = (bp *)umalloc(sizeof(bp));\r
726     bp_construct(Par, npar, par_aux, OPT_DEGREE|0);    \r
727     // creates structure for tags\r
728     static_bitsequence_builder * bmb = new static_bitsequence_builder_brw32(20);\r
729     static_permutation_builder * pmb = new static_permutation_builder_mrrr(PERM_SAMPLE, bmb);\r
730     static_sequence_builder * ssb = new static_sequence_builder_gmr_chunk(bmb, pmb);\r
731 \r
732 \r
733     // If we found an attribute then "<@>" is present in the tree\r
734     // if we didn't then it is not. "<$>" is never present in the tree\r
735     int ntagsize = found_attributes ? 2*ntagnames-1 : 2*ntagnames - 2;\r
736 \r
737     Tags = new static_sequence_gmr((uint *) tags_aux, (uint) npar-1,ntagsize, bmb, ssb);\r
738     \r
739     delete bmb;\r
740     delete pmb;\r
741     delete ssb;\r
742     // makes the text collection static\r
743     if (!disable_tc)\r
744       Text->MakeStatic();\r
745     \r
746     // creates the data structure marking the non-empty texts (just in the case it is necessary)\r
747     if (!indexing_empty_texts)  {\r
748        EBVector = new static_bitsequence_rrr02((uint *)empty_texts_aux,(ulong)npar,(uint)32);\r
749        free (empty_texts_aux);\r
750        empty_texts_aux = NULL;\r
751     }\r
752    \r
753     // OJO was leaked before, found by valgrind\r
754     free(tags_aux);\r
755 \r
756     tags_aux = NULL;\r
757 \r
758     finished = true;\r
759 \r
760     return 1; // indicates success in the inicialization\r
761  }\r
762 \r
763 \r
764 // NewOpenTag(tagname): indicates the event of finding a new opening tag in the document.\r
765 // Tag name is given. Returns a non-zero value upon success, and returns NULLT\r
766 // in case of failing when trying to insert the new tag.\r
767 int XMLTree::NewOpenTag(unsigned char *tagname)\r
768  {\r
769     int i;\r
770 \r
771     if (!initialized) {  // data structure has not been initialized properly\r
772        fprintf(stderr, "Error: you cannot insert a new opening tag without first calling method OpenDocument first\n");\r
773        return NULLT;\r
774     }\r
775     \r
776     // inserts a new opening parentheses in the bit sequence\r
777     if (sizeof(pb)*8*parArraySize == npar) { // no space left for the new parenthesis\r
778        par_aux = (pb *)urealloc(par_aux, sizeof(pb)*2*parArraySize);\r
779        parArraySize *= 2;\r
780     }\r
781     \r
782     setbit(par_aux,npar,OP);  // marks a new opening parenthesis\r
783 \r
784     // transforms the tagname into a tag identifier. If the tag is new, we insert\r
785     // it in the table.\r
786     for (i=0; i<ntagnames; i++)\r
787       if (strcmp((const char *)tagname,(const char *)TagName[i])==0) break;\r
788  \r
789 \r
790     // NewOpenTag("<@>") was called\r
791     if (i==0) \r
792       found_attributes=true;\r
793 \r
794     if (i==ntagnames) { // the tag is a new one, then we insert it\r
795        TagName = (unsigned char **)urealloc(TagName, sizeof(char *)*(ntagnames+1));\r
796        \r
797        if (!TagName) {\r
798           fprintf(stderr, "Error: not enough memory\n");\r
799           return NULLT;\r
800        }\r
801        \r
802        ntagnames++;\r
803        TagName[i] = (unsigned char *)umalloc(sizeof(unsigned char)*(strlen((const char *)tagname)+1));\r
804        strcpy((char *)TagName[i], (const char *)tagname);\r
805     } \r
806     tags_aux = (TagType *) urealloc(tags_aux, sizeof(TagType)*(npar + 1));\r
807 \r
808     tags_aux[npar] = i; // inserts the new tag id within the preorder sequence of tags\r
809     \r
810     npar++;\r
811 \r
812     return 1;\r
813     \r
814  }\r
815 \r
816 \r
817 // NewClosingTag(tagname): indicates the event of finding a new closing tag in the document.\r
818 // Tag name is given. Returns a non-zero value upon success, and returns NULLT\r
819 // in case of failing when trying to insert the new tag.\r
820 int XMLTree::NewClosingTag(unsigned char *tagname)\r
821  {\r
822     int i;\r
823 \r
824     if (!initialized) {  // data structure has not been initialized properly\r
825        fprintf(stderr, "Error: you cannot insert a new closing tag without first calling method OpenDocument first\n");\r
826        return NULLT;\r
827     }\r
828     \r
829     // inserts a new closing parentheses in the bit sequence\r
830     if (sizeof(pb)*8*parArraySize == npar) { // no space left for the new parenthesis\r
831        par_aux = (pb *)urealloc(par_aux, sizeof(pb)*2*parArraySize);\r
832        parArraySize *= 2;\r
833     }\r
834     \r
835     setbit(par_aux,npar,CP);  // marks a new closing opening parenthesis\r
836 \r
837     // transforms the tagname into a tag identifier. If the tag is new, we insert\r
838     // it in the table.\r
839     for (i=0; i<ntagnames; i++)\r
840        if (strcmp((const char *)tagname,(const char *)TagName[i])==0) break;\r
841  \r
842     if (i==ntagnames) { // the tag is a new one, then we insert it\r
843        TagName = (unsigned char **)urealloc(TagName, sizeof(char *)*(ntagnames+1));\r
844        \r
845        \r
846        ntagnames++;\r
847        TagName[i] = (unsigned char *)umalloc(sizeof(char)*(strlen((const char *)tagname)+2));\r
848        TagName[i][0] = '/';\r
849        strcpy((char *)&(TagName[i][1]), (const char *)tagname);\r
850     } \r
851 \r
852     tags_aux = (TagType *)urealloc(tags_aux, sizeof(TagType)*(npar + 1));\r
853 \r
854     tags_aux[npar] = i; // inserts the new tag id within the preorder sequence of tags\r
855     \r
856     npar++;\r
857 \r
858     return 1; // success\r
859     \r
860  }\r
861 \r
862 \r
863 // NewText(s): indicates the event of finding a new (non-empty) text s in the document.\r
864 // The new text is inserted within the text collection. Returns a non-zero value upon\r
865 // success, NULLT in case of error.\r
866 int XMLTree::NewText(unsigned char *s)\r
867  {\r
868     if (!initialized) {  // data structure has not been initialized properly\r
869        fprintf(stderr, "Error: you cannot insert a new text without first calling method OpenDocument first\n");\r
870        return NULLT;\r
871     }\r
872 \r
873     if (disable_tc) {\r
874       XMLTree::NewEmptyText();\r
875       return 1;\r
876     };\r
877 \r
878     if (!indexing_empty_texts) {\r
879        empty_texts_aux = (unsigned int *)urealloc(empty_texts_aux, sizeof(pb)*(1+(npar-1)/(8*sizeof(pb))));\r
880               bitset(empty_texts_aux, npar-1);  // marks the non-empty text with a 1 in the bit vector\r
881     }\r
882     \r
883     Text->InsertText(s);\r
884     \r
885     return 1; // success\r
886  }\r
887 \r
888 // NewEmptyText(): indicates the event of finding a new empty text in the document.\r
889 // In case of indexing empty and non-empty texts, we insert the empty texts into the\r
890 // text collection. In case of indexing only non-empty texts, it just indicates an\r
891 // empty text in the bit vector of empty texts. Returns a non-zero value upon\r
892 // success, NULLT in case of error.\r
893 int XMLTree::NewEmptyText() \r
894  {\r
895     unsigned char c = 0;\r
896     if (!initialized) {  // data structure has not been initialized properly\r
897        fprintf(stderr, "Error: you cannot insert a new empty text without first calling method OpenDocument first\n");\r
898        return NULLT;\r
899     }\r
900 \r
901     if (!indexing_empty_texts) {\r
902        empty_texts_aux = (unsigned int *)urealloc(empty_texts_aux, sizeof(pb)*(1+(npar-1)/(8*sizeof(pb))));\r
903        \r
904        bitclean(empty_texts_aux, npar-1);  // marks the empty text with a 0 in the bit vector\r
905     }\r
906     else Text->InsertText(&c); // we insert the empty text just in case we index all the texts\r
907     \r
908     return 1; // success    \r
909  }\r
910 \r
911 \r
912 // GetTagId: returns the tag identifier corresponding to a given tag name.\r
913 // Returns NULLT in case that the tag name does not exists.\r
914 TagType XMLTree::GetTagId(unsigned char *tagname)\r
915  {\r
916     int i;\r
917     // this should be changed for more efficient processing\r
918     for (i=0; i<ntagnames; i++)\r
919        if (strcmp((const char *)tagname,(const char *)TagName[i])==0) break; \r
920     if (i==ntagnames) return (TagType)NULLT; // tagname does not exists in the table\r
921     else return i;\r
922  }\r
923 \r
924 \r
925 // GetTagName(tagid): returns the tag name of a given tag identifier.\r
926 // Returns NULL in case that the tag identifier is not valid.\r
927 unsigned char *XMLTree::GetTagName(TagType tagid)\r
928  {\r
929     unsigned char *s;\r
930 \r
931     if (tagid >= ntagnames) return NULL; // invalid tag identifier\r
932     s = (unsigned char *)umalloc((strlen((const char *)TagName[tagid])+1)*sizeof(unsigned char));\r
933     strcpy((char *)s, (const char *)TagName[tagid]);\r
934     return s;\r
935  }\r
936 \r
937 \r
938 //KIM : OJO need the two following methods\r
939 \r
940 const unsigned char *XMLTree::GetTagNameByRef(TagType tagid)\r
941  {\r
942     if (tagid >= ntagnames) return NULL; // invalid tag identifier\r
943     return ((const unsigned char*)  TagName[tagid]);\r
944  }\r
945 \r
946 \r
947 \r
948 TagType XMLTree::RegisterTag(unsigned char *tagname)\r
949 {\r
950   if (!finished)\r
951     return NULLT;\r
952   \r
953   TagType id = XMLTree::GetTagId(tagname);\r
954   if (id == NULLT){\r
955     id = ntagnames;\r
956     ntagnames = ntagnames + 1;    \r
957     TagName = (unsigned char **) urealloc(TagName,ntagnames*(sizeof(unsigned char*)));\r
958     TagName[id] = (unsigned char *) umalloc(sizeof(unsigned char)*strlen( (const char*) tagname)+1);\r
959     strcpy((char*)TagName[id], (const char *)tagname);  \r
960   };\r
961 \r
962   return id;\r
963 }\r