7 // functions to convert tag positions to the corresponding tree node and viceversa.
\r
8 // These are implemented in order to be able to change the tree and Tags representations,
\r
9 // without affecting the code so much.
\r
10 // Current implementation corresponds to balanced-parentheses representation for
\r
11 // the tree, and storing 2 tags per tree node (opening and closing tags).
\r
13 // tag position -> tree node
\r
14 static treeNode tagpos2node(int t)
\r
16 return (treeNode) t;
\r
19 static int bits8 (int t ) {
\r
29 // tree node -> tag position
\r
30 static int node2tagpos(treeNode x)
\r
35 static int fast_find_close(bp *b,int s)
\r
37 return fwd_excess(b,s,-1);
\r
40 static int fast_inspect(bp* Par,treeNode i)
\r
45 return (Par->B[j] >> (D-1-l)) & 1;
\r
48 static treeNode fast_first_child(bp *Par, treeNode x)
\r
51 return (fast_inspect(Par,x) == OP) ? x : NULLT;
\r
54 static treeNode fast_next_sibling(bp* Par,treeNode x)
\r
56 x = fast_find_close(Par,x)+1;
\r
57 return (fast_inspect(Par,x) == OP) ? x : NULLT;
\r
61 static treeNode fast_sibling(bp* Par,treeNode x,TagType tag){
\r
63 if (tag == PCDATA_TAG_ID){
\r
65 return fast_inspect(Par,x)==OP ? x : NULLT;
\r
66 } else return fast_next_sibling(Par,x);
\r
70 static bool fast_isleaf(bp* Par,treeNode x){
\r
71 return (fast_inspect(Par,x+1) == CP ? true : false);
\r
75 inline uint get_field_no_power(uint *A, uint len, uint index) {
\r
77 register uint i=index*len/W, j=index*len-W*i;
\r
78 return (j+len <= W) ? (A[i] << (W-j-len)) >> (W-len) : (A[i] >> j) | (A[i+1] << (WW-j-len)) >> (W-len);
\r
82 static uint fast_get_field(uint* A,int len, int idx)
\r
87 return (uint) (((uchar*)A)[idx]);
\r
89 f2 = ((unsigned short*)A)[idx];
\r
90 f1 = ((unsigned short*)A)[idx+1];
\r
91 return (f1 << 16) + f2;
\r
93 return get_field_no_power (A,len,idx);
\r
98 inline bool fast_is_ancestor(bp * Par,treeNode x,treeNode y){
\r
102 return (x==0) || (y <= fast_find_close(Par,x));
\r
106 XMLTree::XMLTree( pb * const par, uint npar, vector<string> * const TN, TagIdMap * const tim,
\r
107 uint *empty_texts_bmp, TagType *tags,
\r
108 TextCollection * const TC, bool dis_tc)
\r
111 // creates the data structure for the tree topology
\r
112 Par = (bp *)umalloc(sizeof(bp));
\r
114 bp_construct(Par, npar, (pb*) par, OPT_DEGREE|0);
\r
115 STOPTIMER(Building);
\r
116 PRINTTIME("Building parenthesis struct", Building);
\r
120 // creates structure for tags
\r
122 TagName = (vector<string>*)TN;
\r
123 tIdMap = (TagIdMap *) tim;
\r
125 uint max_tag = TN->size() - 1;
\r
128 static_bitsequence_builder *bmb = new static_bitsequence_builder_sdarray();
\r
129 alphabet_mapper *am = new alphabet_mapper_none();
\r
130 Tags = new static_sequence_bs((uint*)tags,npar,am,bmb);
\r
132 //cout << "Tags test: " << Tags->test((uint*)tags,npar) << endl;
\r
134 //Ensures that for small tag numbers, we are on an 8bit boundary.
\r
135 //Makes tag access way faster with negligeable waste of space.
\r
136 tags_blen = bits8(max_tag);
\r
137 std::cerr << "Tags blen is " << tags_blen << "\n";
\r
138 tags_len = (uint)npar;
\r
139 tags_fix = new uint[uint_len(tags_blen,tags_len)];
\r
140 for(uint i=0;i<(uint)npar;i++)
\r
141 set_field(tags_fix,tags_blen,i,tags[i]);
\r
146 STOPTIMER(Building);
\r
147 PRINTTIME("Building Tag Structure", Building);
\r
149 Text = (TextCollection*) TC;
\r
152 EBVector = new static_bitsequence_rrr02(empty_texts_bmp,npar,32);
\r
153 //EBVector = new static_bitsequence_sdarray(empty_texts_bmp,npar);
\r
154 free(empty_texts_bmp);
\r
155 empty_texts_bmp = NULL;
\r
158 disable_tc = dis_tc;
\r
161 std::cerr << "Number of distinct tags " << TagName->size() << "\n";
\r
162 //std::cerr.flush();
\r
166 // ~XMLTree: frees memory of XML tree.
\r
167 XMLTree::~XMLTree()
\r
172 free(Par); // frees the memory of struct Par
\r
188 if (stream != NULL){
\r
197 void XMLTree::print_stats()
\r
199 uint total_space = Tags->size()+sizeof(static_sequence*);
\r
200 total_space += sizeof(uint*)+sizeof(uint)*(2+uint_len(tags_blen,tags_len));
\r
201 cout << "Space usage for XMLTree:" << endl
\r
202 << " - tags static_sequence: " << Tags->size()+sizeof(static_sequence*) << endl
\r
203 << " - tags access array: " << sizeof(uint*)+sizeof(uint)*(2+uint_len(tags_blen,tags_len)) << endl
\r
204 << " ... add Diego structures ... " << endl
\r
205 << " *total* " << total_space << endl;
\r
208 // Save: saves XML tree data structure to file.
\r
209 void XMLTree::Save(int fd)
\r
212 char filenameaux[1024];
\r
215 fp = fdopen(fd, "wa");
\r
216 // first stores the tree topology
\r
219 // stores the table with tag names
\r
220 int ntags = TagName->size();
\r
222 ufwrite(&ntags, sizeof(int), 1, fp);
\r
223 for (i = 0; i<ntags;i++)
\r
224 fprintf(fp, "%s\n",TagName->at(i).c_str());
\r
229 ufwrite(&tags_blen,sizeof(uint),1,fp);
\r
230 ufwrite(&tags_len,sizeof(uint),1,fp);
\r
231 ufwrite(tags_fix,sizeof(uint),uint_len(tags_blen,tags_len),fp);
\r
234 ufwrite(&disable_tc, sizeof(bool),1,fp);
\r
237 EBVector->save(fp);
\r
239 // stores the texts
\r
248 // Load: loads XML tree data structure from file. Returns
\r
249 // a pointer to the loaded data structure
\r
250 XMLTree *XMLTree::Load(int fd, bool load_tc,int sample_factor)
\r
260 fp = fdopen(fd, "r");
\r
262 XML_Tree = new XMLTree();
\r
264 // Load the tree structure
\r
265 XML_Tree->Par = (bp *)umalloc(sizeof(bp));
\r
267 loadTree(XML_Tree->Par, fp);
\r
268 STOPTIMER(Loading);
\r
269 PRINTTIME("Loading parenthesis struct", Loading);
\r
272 XML_Tree->TagName = new vector<string>();
\r
273 XML_Tree->tIdMap = new std::unordered_map<string,int>();
\r
278 // Load the tag names
\r
279 ufread(&ntags, sizeof(int), 1, fp);
\r
281 for (i=0; i<ntags;i++) {
\r
282 char * r = fgets(buffer,1023,fp);
\r
284 throw "Cannot read tag list";
\r
285 s = (const char*) buffer;
\r
286 // remove the trailing \n
\r
287 s.erase(s.size()-1);
\r
288 XML_Tree->TagName->push_back(s);
\r
289 XML_Tree->tIdMap->insert(std::make_pair(s,i));
\r
292 STOPTIMER(Loading);
\r
293 PRINTTIME("Loading tag names struct", Loading);
\r
296 // loads the tag structure
\r
297 XML_Tree->Tags = static_sequence::load(fp);
\r
298 ufread(&XML_Tree->tags_blen,sizeof(uint),1,fp);
\r
299 std::cerr << "tags_blen is "<< XML_Tree->tags_blen <<"\n";
\r
300 ufread(&XML_Tree->tags_len,sizeof(uint),1,fp);
\r
301 XML_Tree->tags_fix = new uint[uint_len(XML_Tree->tags_blen,XML_Tree->tags_len)];
\r
302 ufread(XML_Tree->tags_fix,sizeof(uint),uint_len(XML_Tree->tags_blen,XML_Tree->tags_len),fp);
\r
304 // TODO ask francisco about this
\r
305 /// FIXME:UGLY tests!
\r
306 //uint * seq = new uint[XML_Tree->tags_len];
\r
307 //for(uint i=0;i<XML_Tree->tags_len;i++)
\r
308 // seq[i] = get_field(XML_Tree->tags_fix,XML_Tree->tags_blen,i);
\r
309 //cout << "Tags test: " << XML_Tree->Tags->test(seq,XML_Tree->tags_len) << endl;
\r
310 //XML_Tree->Tags->test(seq,XML_Tree->tags_len);
\r
314 STOPTIMER(Loading);
\r
315 std::cerr << (uint_len(XML_Tree->tags_blen,XML_Tree->tags_len)*sizeof(uint))/(1024*1024) << " MB for tag sequence" << std::endl;
\r
316 PRINTTIME("Loading tag struct", Loading);
\r
321 ufread(&(XML_Tree->disable_tc), sizeof(bool), 1, fp);
\r
323 XML_Tree->EBVector = static_bitsequence_rrr02::load(fp);
\r
324 //XML_Tree->EBVector = static_bitsequence_sdarray::load(fp);
\r
326 STOPTIMER(Loading);
\r
327 PRINTTIME("Loading text bitvector struct", Loading);
\r
332 if (!XML_Tree->disable_tc){
\r
333 XML_Tree->Text = TextCollection::Load(fp,sample_factor);
\r
335 else XML_Tree->Text = NULL;
\r
336 STOPTIMER(Loading);
\r
337 PRINTTIME("Loading TextCollection", Loading);
\r
341 XML_Tree->EBVector = NULL;
\r
342 XML_Tree->Text = NULL;
\r
343 XML_Tree->disable_tc = true;
\r
346 XML_Tree->stream = NULL;
\r
347 XML_Tree->stream_fd = 0;
\r
354 // SubtreeSize(x): the number of nodes (and attributes) in the subtree of node x.
\r
355 int XMLTree::SubtreeSize(treeNode x)
\r
357 return subtree_size(Par, x);
\r
360 // SubtreeTags(x,tag): the number of occurrences of tag within the subtree of node x.
\r
361 int XMLTree::SubtreeTags(treeNode x, TagType tag)
\r
364 x = fast_first_child(Par,x);
\r
367 int s = x + 2*subtree_size(Par, x) - 1;
\r
369 return Tags->rank(tag, s) - Tags->rank(tag, node2tagpos(x)-1);
\r
371 int XMLTree::SubtreeElements(treeNode x)
\r
374 int size = subtree_size(Par,x);
\r
376 x = fast_first_child(Par,x);
\r
380 int s = x + 2*size - 1;
\r
381 int ntext = Tags->rank(PCDATA_TAG_ID, s) - Tags->rank(PCDATA_TAG_ID, node2tagpos(x)-1);
\r
382 size = size - ntext;
\r
383 treeNode fin = fast_find_close(Par,x);
\r
384 treeNode y = Tags->select_next(ATTRIBUTE_TAG_ID,node2tagpos(x));
\r
385 while (y != NULLT && y < fin){
\r
386 size -= SubtreeSize(y);
\r
387 y = Tags->select_next(ATTRIBUTE_TAG_ID,node2tagpos(y));
\r
392 // IsLeaf(x): returns whether node x is leaf or not. In the succinct representation
\r
393 // this is just a bit inspection.
\r
394 bool XMLTree::IsLeaf(treeNode x)
\r
396 NULLT_IF(x==NULLT);
\r
397 return fast_isleaf(Par, x);
\r
400 // IsAncestor(x,y): returns whether node x is ancestor of node y.
\r
401 bool XMLTree::IsAncestor(treeNode x, treeNode y)
\r
403 return fast_is_ancestor(Par, x, y);
\r
406 // IsChild(x,y): returns whether node x is parent of node y.
\r
407 bool XMLTree::IsChild(treeNode x, treeNode y)
\r
409 if (!fast_is_ancestor(Par, x, y)) return false;
\r
410 return depth(Par, x) == (depth(Par, y) + 1);
\r
413 // IsFirstChild(x): returns whether node x is the first child of its parent.
\r
414 bool XMLTree::IsFirstChild(treeNode x)
\r
416 return ((x != NULLT)&&(x==Root() || prev_sibling(Par,x) == (treeNode)-1));
\r
420 // NumChildren(x): number of children of node x. Constant time with the data structure
\r
422 int XMLTree::NumChildren(treeNode x)
\r
424 return degree(Par, x);
\r
427 // ChildNumber(x): returns i if node x is the i-th children of its parent.
\r
428 int XMLTree::ChildNumber(treeNode x)
\r
430 return child_rank(Par, x);
\r
433 // Depth(x): depth of node x, a simple binary rank on the parentheses sequence.
\r
434 int XMLTree::Depth(treeNode x)
\r
436 return depth(Par, x);
\r
439 // Preorder(x): returns the preorder number of node x, just counting the tree
\r
440 // nodes (i.e., tags, it disregards the texts in the tree).
\r
441 int XMLTree::Preorder(treeNode x)
\r
443 return preorder_rank(Par, x);
\r
446 // Postorder(x): returns the postorder number of node x, just counting the tree
\r
447 // nodes (i.e., tags, it disregards the texts in the tree).
\r
448 int XMLTree::Postorder(treeNode x)
\r
450 return postorder_rank(Par, x);
\r
453 // Tag(x): returns the tag identifier of node x.
\r
454 TagType XMLTree::Tag(treeNode x)
\r
456 return fast_get_field(tags_fix,tags_blen,node2tagpos(x));
\r
459 // DocIds(x): returns the range of text identifiers that descend from node x.
\r
460 // returns {NULLT, NULLT} when there are no texts descending from x.
\r
461 range XMLTree::DocIds(treeNode x)
\r
469 int min = EBVector->rank1(x-1);
\r
470 int max = EBVector->rank1(x+2*subtree_size(Par, x)-2);
\r
471 if (min==max) { // range is empty, no texts within the subtree of x
\r
475 else { // the range is non-empty, there are texts within the subtree of x
\r
482 // Parent(x): returns the parent node of node x.
\r
484 treeNode XMLTree::Parent(treeNode x)
\r
489 return parent(Par, x);;
\r
492 // Child(x,i): returns the i-th child of node x, assuming it exists.
\r
493 treeNode XMLTree::Child(treeNode x, int i)
\r
495 if (i <= OPTD) return naive_child(Par, x, i);
\r
496 else return child(Par, x, i);
\r
499 // FirstChild(x): returns the first child of node x, assuming it exists. Very fast in BP.
\r
501 treeNode XMLTree::FirstChild(treeNode x)
\r
503 NULLT_IF(x==NULLT);
\r
504 return fast_first_child(Par, x);
\r
507 treeNode XMLTree::FirstElement(treeNode x)
\r
509 NULLT_IF(x==NULLT);
\r
510 x = fast_first_child(Par, x);
\r
511 NULLT_IF(x == NULLT);
\r
514 case PCDATA_TAG_ID:
\r
516 return (fast_inspect(Par,x)==OP)? x : NULLT;
\r
518 case ATTRIBUTE_TAG_ID:
\r
519 x = fast_next_sibling(Par,x);
\r
520 if (x != NULLT && Tag(x) == PCDATA_TAG_ID){
\r
522 return (fast_inspect(Par,x)==OP)? x : NULLT;
\r
530 treeNode XMLTree::NextElement(treeNode x)
\r
532 NULLT_IF(x==NULLT);
\r
533 x = fast_next_sibling(Par, x);
\r
534 NULLT_IF(x == NULLT);
\r
535 if (Tag(x) == PCDATA_TAG_ID){
\r
537 return (fast_inspect(Par,x)==OP)? x : NULLT;
\r
541 value XMLTree::CamlFirstElement(value x)
\r
543 return Val_int(FirstElement(Int_val(x)));
\r
545 value XMLTree::CamlNextElement(value x)
\r
547 return Val_int(NextElement(Int_val(x)));
\r
550 extern "C" value caml_cpp_fast_first_element(value xmltree, value node){
\r
551 return XMLTREE(xmltree)->CamlFirstElement(node);
\r
554 extern "C" value caml_cpp_fast_next_element(value xmltree, value node){
\r
555 return XMLTREE(xmltree)->CamlNextElement(node);
\r
558 // LastChild(x): returns the last child of node x.
\r
559 treeNode XMLTree::LastChild(treeNode x)
\r
561 NULLT_IF(x == NULLT || fast_isleaf(Par,x));
\r
562 return find_open(Par, fast_find_close(Par, x)-1);
\r
565 // NextSibling(x): returns the next sibling of node x, assuming it exists.
\r
566 treeNode XMLTree::NextSibling(treeNode x)
\r
568 NULLT_IF(x==NULLT || x == Root() );
\r
569 x = fast_find_close(Par,x)+1;
\r
570 return (fast_inspect(Par,x) == CP ? NULLT : x);
\r
574 // PrevSibling(x): returns the previous sibling of node x, assuming it exists.
\r
575 treeNode XMLTree::PrevSibling(treeNode x)
\r
577 NULLT_IF(x==NULLT);
\r
578 return prev_sibling(Par, x);
\r
581 // TaggedChild(x,tag): returns the first child of node x tagged tag, or NULLT if there is none.
\r
582 // Because of the balanced-parentheses representation of the tree, this operation is not supported
\r
583 // efficiently, just iterating among the children of node x until finding the desired child.
\r
584 treeNode XMLTree::TaggedChild(treeNode x, TagType tag)
\r
587 NULLT_IF(x==NULLT || fast_isleaf(Par,x));
\r
589 child = fast_first_child(Par, x); // starts at first child of node x
\r
590 if (Tag(child) == tag)
\r
593 return TaggedFollowingSibling(child,tag);
\r
596 // TaggedSibling(x,tag): returns the first sibling of node x tagged tag, or NULLT if there is none.
\r
597 treeNode XMLTree::TaggedFollowingSibling(treeNode x, TagType tag)
\r
599 NULLT_IF(x==NULLT);
\r
600 treeNode sibling = fast_next_sibling(Par, x);
\r
602 while (sibling != NULLT) {
\r
603 ctag = Tag(sibling);
\r
604 if (ctag == tag) // current sibling is labeled with tag of interest
\r
606 sibling = fast_sibling(Par, sibling, ctag); // OK, let's try with the next sibling
\r
608 return NULLT; // no such sibling was found
\r
611 treeNode XMLTree::SelectChild(treeNode x, TagIdSet *tags)
\r
614 NULLT_IF(x==NULLT || fast_isleaf(Par,x));
\r
616 treeNode child = fast_first_child(Par, x);
\r
618 while (child != NULLT) {
\r
620 if (tags->find(t) != tags->end()) return child;
\r
621 child = fast_sibling(Par, child,t);
\r
627 treeNode XMLTree::SelectFollowingSibling(treeNode x, TagIdSet *tags)
\r
630 NULLT_IF(x==NULLT);
\r
633 treeNode sibling = fast_next_sibling(Par, x);
\r
634 while (sibling != NULLT) {
\r
636 if (tags->find(t) != tags->end()) return sibling;
\r
637 sibling = fast_sibling(Par, sibling,t);
\r
643 // TaggedDescendant(x,tag): returns the first node tagged tag with larger preorder than x and within
\r
644 // the subtree of x. Returns NULLT if there is none.
\r
645 treeNode XMLTree::TaggedDescendant(treeNode x, TagType tag)
\r
647 //NULLT_IF(x==NULLT || fast_isleaf(Par,x));
\r
649 int s = (int) Tags->select_next(tag,node2tagpos(x));
\r
650 NULLT_IF (s == -1);
\r
652 treeNode y = tagpos2node(s); // transforms the tag position into a node position
\r
654 return (fast_is_ancestor(Par,x,y) ? y : NULLT);
\r
658 treeNode XMLTree::SelectDescendant(treeNode x, TagIdSet *tags)
\r
660 NULLT_IF (x ==NULLT || fast_isleaf(Par,x));
\r
662 treeNode min = NULLT;
\r
663 treeNode fc = fast_first_child(Par,x);
\r
665 TagIdSet::const_iterator tagit;
\r
666 for (tagit = tags->begin(); tagit != tags->end(); tagit++) {
\r
667 aux = TaggedDescendant(x, (TagType) *tagit);
\r
668 if (aux == fc) return fc;
\r
669 if (aux == NULLT) continue;
\r
670 if ((min == NULLT) || (aux < min)) min = aux;
\r
677 // TaggedPrec(x,tag): returns the first node tagged tag with smaller preorder than x and not an
\r
678 // ancestor of x. Returns NULLT if there is none.
\r
679 treeNode XMLTree::TaggedPreceding(treeNode x, TagType tag)
\r
682 treeNode node_s, root;
\r
683 r = (int)Tags->rank(tag, node2tagpos(x)-1);
\r
684 if (r==0) return NULLT; // there is no such node.
\r
685 s = (int)Tags->select(tag, r);
\r
686 root = root_node(Par);
\r
687 node_s = tagpos2node(s);
\r
688 while (fast_is_ancestor(Par, node_s, x) && (node_s!=root)) { // the one that we found is an ancestor of x
\r
690 if (r==0) return NULLT; // there is no such node
\r
691 s = (int)Tags->select(tag, r); // we should use select_prev instead when provided
\r
692 node_s = tagpos2node(s);
\r
694 return NULLT; // there is no such node
\r
698 // TaggedFoll(x,tag): returns the first node tagged tag with larger preorder than x and not in
\r
699 // the subtree of x. Returns NULLT if there is none.
\r
700 treeNode XMLTree::TaggedFollowing(treeNode x, TagType tag)
\r
702 NULLT_IF (x ==NULLT || x == Root());
\r
703 return tagpos2node(Tags->select_next(tag,fast_find_close(Par, x)));
\r
707 // TaggedFollBelow(x,tag,root): returns the first node tagged tag with larger preorder than x
\r
708 // and not in the subtree of x. Returns NULLT if there is none.
\r
709 treeNode XMLTree::TaggedFollowingBelow(treeNode x, TagType tag, treeNode ancestor)
\r
711 // NULLT_IF (x == NULLT || x == Root() || x == ancestor);
\r
713 //Special optimisation, test for the following sibling first
\r
714 treeNode close = fast_find_close(Par, x);
\r
716 treeNode ns = close+1;
\r
717 if (fast_inspect(Par,ns) == OP) {
\r
718 TagType tagns = Tag(ns);
\r
719 // cout << GetTagNameByRef(tagns) << endl;
\r
721 if (tagns == PCDATA_TAG_ID){
\r
724 if (fast_inspect(Par,ns) != OP)
\r
733 treeNode s = tagpos2node(Tags->select_next(tag, close));
\r
735 if (ancestor == Root() || s==NULLT || s < fast_find_close(Par,ancestor)) return s;
\r
739 treeNode XMLTree::TaggedFollowingBefore(treeNode x, TagType tag, treeNode closing)
\r
742 NULLT_IF (x == NULLT || x == Root());
\r
744 treeNode s = tagpos2node(Tags->select_next(tag, fast_find_close(Par, x)));
\r
745 NULLT_IF (s == NULLT || s >= closing);
\r
750 /* Here we inline TaggedFoll to find the min globally, and only at the end
\r
751 we check if the min is below the context node */
\r
752 treeNode XMLTree::SelectFollowingBelow(treeNode x, TagIdSet *tags, treeNode ancestor)
\r
755 NULLT_IF(x==NULLT || x==Root());
\r
757 treeNode close = fast_find_close(Par,x);
\r
758 treeNode ns = close+1;
\r
759 if ( (fast_inspect(Par,ns) == OP) && (tags->find(Tag(ns)) != tags->end()))
\r
763 treeNode min = NULLT;
\r
767 TagIdSet::const_iterator tagit;
\r
768 for (tagit = tags->begin(); tagit != tags->end(); tagit++) {
\r
770 aux = tagpos2node(Tags->select_next(*tagit, close));
\r
772 // The next sibling of x is guaranteed to be below ctx
\r
773 // and is the node with lowest preorder which is after ctx.
\r
774 // if we find it, we return early;
\r
775 if (aux == NULLT) continue;
\r
776 if ((min == NULLT) || (aux < min)) min = aux;
\r
779 // found the smallest node in preorder which is after x.
\r
780 // if ctx is the root node, just return what we found.
\r
782 if (ancestor == Root()) return min;
\r
783 // else check whether if is in below the ctx node
\r
785 NULLT_IF (min == NULLT || min >= fast_find_close(Par, ancestor));
\r
790 treeNode XMLTree::SelectFollowingBefore(treeNode x, TagIdSet *tags, treeNode closing)
\r
793 NULLT_IF(x==NULLT || x==Root());
\r
795 treeNode min = NULLT;
\r
796 treeNode ns = fast_next_sibling(Par, x);
\r
797 treeNode close = ns - 1;
\r
799 TagIdSet::const_iterator tagit;
\r
800 for (tagit = tags->begin(); tagit != tags->end(); tagit++) {
\r
802 aux = tagpos2node(Tags->select_next(*tagit, close));
\r
804 // The next sibling of x is guaranteed to be below ctx
\r
805 // and is the node with lowest preorder which is after ctx.
\r
806 // if we find it, we return early;
\r
808 if (aux == ns ) return ns;
\r
809 if (aux == NULLT) continue;
\r
810 if ((min == NULLT) || (aux < min)) min = aux;
\r
813 // found the smallest node in preorder which is after x.
\r
814 // if ctx is the root node, just return what we found.
\r
816 NULLT_IF (min == NULLT || min >= closing);
\r
823 // TaggedAncestor(x, tag): returns the closest ancestor of x tagged tag. Return
\r
824 // NULLT is there is none.
\r
825 treeNode XMLTree::TaggedAncestor(treeNode x, TagType tag)
\r
827 if (x == NULLT || x == Root())
\r
830 treeNode s = parent(Par, x), r = Root();
\r
832 if (Tag(s) == tag) return s;
\r
833 s = parent(Par, s);
\r
840 // MyText(x): returns the document identifier of the text below node x,
\r
841 // or NULLT if x is not a leaf node or the text is empty. Assumes Doc
\r
842 // ids start from 0.
\r
843 DocID XMLTree::MyText(treeNode x)
\r
845 TagType tag = Tag(x);
\r
846 // seems faster than testing EBVector->access(x);
\r
848 if (tag == PCDATA_TAG_ID || tag == ATTRIBUTE_DATA_TAG_ID)
\r
849 //if (EBVector->access(x))
\r
850 return (DocID) (EBVector->rank1(x)-1); //-1 because document ids start from 0
\r
852 return (DocID) NULLT;
\r
855 // MyText(x): returns the document identifier of the text below node x,
\r
856 // or NULLT if x is not a leaf node or the text is empty. Assumes Doc
\r
857 // ids start from 0.
\r
858 DocID XMLTree::MyTextUnsafe(treeNode x)
\r
860 return (DocID) (EBVector->rank1(x)-1); //-1 because document ids start from 0
\r
862 // TextXMLId(d): returns the preorder of document with identifier d in the tree consisting of
\r
863 // all tree nodes and all text nodes. Assumes that the tree root has preorder 1.
\r
864 int XMLTree::TextXMLId(DocID d)
\r
866 NULLT_IF(d == NULLT);
\r
867 int s = EBVector->select1(d+1);
\r
868 return rank_open(Par, s) + d + 1; // +1 because root has preorder 1
\r
872 // NodeXMLId(x): returns the preorder of node x in the tree consisting
\r
873 // of all tree nodes and all text nodes. Assumes that the tree root has
\r
875 int XMLTree::NodeXMLId(treeNode x)
\r
877 NULLT_IF(x == NULLT);
\r
878 if (x == Root()) return 1; // root node has preorder 1
\r
879 return rank_open(Par, x) + EBVector->rank1(x-1);
\r
882 // ParentNode(d): returns the parent node of document identifier d.
\r
883 treeNode XMLTree::ParentNode(DocID d)
\r
885 NULLT_IF (d == NULLT);
\r
886 return (treeNode) EBVector->select1(d+1);
\r
889 // GetTagId: returns the tag identifier corresponding to a given tag name.
\r
890 // Returns NULLT in case that the tag name does not exists.
\r
891 TagType XMLTree::GetTagId(unsigned char *tagname)
\r
894 string s = (char *) tagname;
\r
895 TagIdMapIT it = tIdMap->find(s);
\r
896 return (TagType) ((it != tIdMap->end()) ? it->second : -1);
\r
901 // GetTagName(tagid): returns the tag name of a given tag identifier.
\r
902 // Returns NULL in case that the tag identifier is not valid.
\r
903 unsigned char *XMLTree::GetTagName(TagType tagid)
\r
906 if ( tagid < 0 || tagid >= TagName->size())
\r
907 return (unsigned char *) "<INVALID TAG>";
\r
908 strcpy((char *)s, (*TagName)[tagid].c_str());
\r
910 return (s == NULL ? (unsigned char*) "<INVALID TAG>" : s);
\r
914 const unsigned char *XMLTree::GetTagNameByRef(TagType tagid)
\r
918 if ( tagid < 0 || tagid >= TagName->size())
\r
919 return (unsigned char *) "<INVALID TAG>";
\r
921 return (const unsigned char *) (*TagName)[tagid].c_str();
\r
927 TagType XMLTree::RegisterTag(unsigned char *tagname)
\r
929 TagType id = XMLTree::GetTagId(tagname);
\r
931 string s = (char *) tagname;
\r
932 REGISTER_TAG(TagName,tIdMap,s);
\r
939 treeNode XMLTree::Closing(treeNode x) {
\r
940 return fast_find_close(Par,x);
\r
942 bool XMLTree::IsOpen(treeNode x) { return fast_inspect(Par,x); }
\r
944 //WARNING this uses directly the underlying implementation for plain text
\r
947 void XMLTree::Print(int fd,treeNode x, bool no_text){
\r
949 int newfd = dup(fd);
\r
950 stream = fdopen(newfd,"wa");
\r
957 buffer = new string();
\r
960 treeNode fin = fast_find_close(Par,x);
\r
962 TagType tag = Tag(n);
\r
964 range r = DocIds(x);
\r
965 treeNode first_idx;
\r
966 treeNode first_text = (tag == PCDATA_TAG_ID ? x : ParentNode(r.min-1));
\r
967 treeNode first_att = NULLT;
\r
969 if (first_att == NULLT)
\r
970 first_idx = first_text;
\r
971 else if (first_text == NULLT)
\r
972 first_idx = first_att;
\r
974 first_idx = min(first_att,first_text);
\r
976 uchar * current_text=NULL;
\r
977 if (first_idx != NULLT)
\r
978 current_text = GetText(MyText(first_idx));
\r
980 std::vector<uchar*> st;
\r
982 if (fast_inspect(Par,n)){
\r
983 if (tag == PCDATA_TAG_ID ) {
\r
986 myfputs("<$/>",fp);
\r
988 read = myfprintf((const char*) current_text, fp);
\r
989 current_text += (read + 1);
\r
991 n+=2; // skip closing $
\r
997 tagstr = (uchar*) GetTagNameByRef(tag);
\r
998 myfputs((const char*) tagstr ,fp);
\r
1000 if (fast_inspect(Par,n)) {
\r
1001 st.push_back(tagstr);
\r
1003 if (tag == ATTRIBUTE_TAG_ID){
\r
1005 if (no_text) myfputs("><@@>",fp);
\r
1006 while (fast_inspect(Par,n)){
\r
1009 myfputs((const char*) &(GetTagNameByRef(Tag(n))[3]),fp);
\r
1011 myfputs("<$@/></",fp);
\r
1012 myfputs((const char*) &(GetTagNameByRef(Tag(n))[3]),fp);
\r
1018 myfputs((const char*) &(GetTagNameByRef(Tag(n))[3]),fp);
\r
1020 myfputs("=\"",fp);
\r
1021 read = myfprintf((const char*) current_text,fp);
\r
1022 current_text += (read + 1);
\r
1028 myfputs("</@@>",fp);
\r
1029 else myfputc('>',fp);
\r
1037 else {// <foo /> tag
\r
1047 myfputs((const char*)st.back(),fp);
\r
1051 }while (!fast_inspect(Par,n) && !st.empty());
\r
1055 mybufferflush(fp);
\r