10 // functions to convert tag positions to the corresponding tree node and viceversa.
\r
11 // These are implemented in order to be able to change the tree and Tags representations,
\r
12 // without affecting the code so much.
\r
13 // Current implementation corresponds to balanced-parentheses representation for
\r
14 // the tree, and storing 2 tags per tree node (opening and closing tags).
\r
17 static int bits8 (int t ) {
\r
30 static treeNode fast_sibling(bp* Par,treeNode x,TagType tag){
\r
32 if (tag == PCDATA_TAG_ID){
\r
34 return fast_inspect(Par,x)==OP ? x : NULLT;
\r
35 } else return fast_next_sibling(Par,x);
\r
39 static bool fast_isleaf(bp* Par,treeNode x){
\r
40 return (fast_inspect(Par,x+1) == CP ? true : false);
\r
44 inline uint get_field_no_power(uint *A, uint len, uint index) {
\r
46 register uint i=index*len/W, j=index*len-W*i;
\r
47 return (j+len <= W) ? (A[i] << (W-j-len)) >> (W-len) : (A[i] >> j) | (A[i+1] << (WW-j-len)) >> (W-len);
\r
51 static uint fast_get_field(uint* A,int len, int idx)
\r
56 return (uint) (((uchar*)A)[idx]);
\r
58 f2 = ((unsigned short*)A)[idx];
\r
59 f1 = ((unsigned short*)A)[idx+1];
\r
60 return (f1 << 16) + f2;
\r
62 return get_field_no_power (A,len,idx);
\r
70 XMLTree::XMLTree( pb * const par, uint npar, vector<string> * const TN, TagIdMap * const tim,
\r
71 uint *empty_texts_bmp, TagType *tags,
\r
72 TextCollection * const TC, bool dis_tc)
\r
75 // creates the data structure for the tree topology
\r
76 Par = (bp *)umalloc(sizeof(bp));
\r
78 bp_construct(Par, npar, (pb*) par, OPT_DEGREE|0);
\r
79 STOPTIMER(Building);
\r
80 PRINTTIME("Building parenthesis struct", Building);
\r
84 // creates structure for tags
\r
86 TagName = (vector<string>*)TN;
\r
87 tIdMap = (TagIdMap *) tim;
\r
89 uint max_tag = TN->size() - 1;
\r
92 static_bitsequence_builder *bmb = new static_bitsequence_builder_sdarray();
\r
93 alphabet_mapper *am = new alphabet_mapper_none();
\r
94 Tags = new static_sequence_bs((uint*)tags,npar,am,bmb);
\r
96 //cout << "Tags test: " << Tags->test((uint*)tags,npar) << endl;
\r
98 //Ensures that for small tag numbers, we are on an 8bit boundary.
\r
99 //Makes tag access way faster with negligeable waste of space.
\r
100 tags_blen = bits8(max_tag);
\r
101 std::cerr << "Tags blen is " << tags_blen << "\n";
\r
102 tags_len = (uint)npar;
\r
103 tags_fix = new uint[uint_len(tags_blen,tags_len)];
\r
104 for(uint i=0;i<(uint)npar;i++)
\r
105 set_field(tags_fix,tags_blen,i,tags[i]);
\r
110 STOPTIMER(Building);
\r
111 PRINTTIME("Building Tag Structure", Building);
\r
113 Text = (TextCollection*) TC;
\r
116 EBVector = new static_bitsequence_rrr02(empty_texts_bmp,npar,32);
\r
117 //EBVector = new static_bitsequence_sdarray(empty_texts_bmp,npar);
\r
118 free(empty_texts_bmp);
\r
119 empty_texts_bmp = NULL;
\r
122 disable_tc = dis_tc;
\r
125 std::cerr << "Number of distinct tags " << TagName->size() << "\n";
\r
126 //std::cerr.flush();
\r
130 // ~XMLTree: frees memory of XML tree.
\r
131 XMLTree::~XMLTree()
\r
136 free(Par); // frees the memory of struct Par
\r
152 if (stream != NULL){
\r
161 void XMLTree::print_stats()
\r
163 uint total_space = Tags->size()+sizeof(static_sequence*);
\r
164 total_space += sizeof(uint*)+sizeof(uint)*(2+uint_len(tags_blen,tags_len));
\r
165 cout << "Space usage for XMLTree:" << endl
\r
166 << " - tags static_sequence: " << Tags->size()+sizeof(static_sequence*) << endl
\r
167 << " - tags access array: " << sizeof(uint*)+sizeof(uint)*(2+uint_len(tags_blen,tags_len)) << endl
\r
168 << " ... add Diego structures ... " << endl
\r
169 << " *total* " << total_space << endl;
\r
172 // Save: saves XML tree data structure to file.
\r
173 void XMLTree::Save(int fd, char *filename)
\r
176 char filenameaux[1024];
\r
179 fp = fdopen(fd, "wa");
\r
180 // first stores the tree topology
\r
183 // stores the table with tag names
\r
184 int ntags = TagName->size();
\r
186 ufwrite(&ntags, sizeof(int), 1, fp);
\r
187 for (i = 0; i<ntags;i++)
\r
188 fprintf(fp, "%s\n",TagName->at(i).c_str());
\r
193 ufwrite(&tags_blen,sizeof(uint),1,fp);
\r
194 ufwrite(&tags_len,sizeof(uint),1,fp);
\r
195 ufwrite(tags_fix,sizeof(uint),uint_len(tags_blen,tags_len),fp);
\r
198 ufwrite(&disable_tc, sizeof(bool),1,fp);
\r
201 EBVector->save(fp);
\r
203 // stores the texts
\r
205 Text->Save(fp, filename);
\r
210 // Load: loads XML tree data structure from file. Returns
\r
211 // a pointer to the loaded data structure
\r
212 XMLTree *XMLTree::Load(int fd, char *filename, bool load_tc,int sample_factor)
\r
220 buffer[1023] = '\0';
\r
222 fp = fdopen(fd, "r");
\r
224 XML_Tree = new XMLTree();
\r
226 // Load the tree structure
\r
227 XML_Tree->Par = (bp *)umalloc(sizeof(bp));
\r
229 loadTree(XML_Tree->Par, fp);
\r
230 STOPTIMER(Loading);
\r
231 PRINTTIME("Loading parenthesis struct", Loading);
\r
234 XML_Tree->TagName = new std::vector<std::string>();
\r
235 XML_Tree->tIdMap = new std::unordered_map<std::string,int>();
\r
239 // Load the tag names
\r
240 ufread(&ntags, sizeof(int), 1, fp);
\r
242 for (i=0; i<ntags;i++) {
\r
243 if (fgets(buffer,1022,fp) != buffer)
\r
244 throw "Cannot read tag list";
\r
246 // remove the trailing \n
\r
247 s.erase(s.size()-1);
\r
248 XML_Tree->TagName->push_back(s);
\r
249 XML_Tree->tIdMap->insert(std::make_pair(s,i));
\r
252 STOPTIMER(Loading);
\r
253 PRINTTIME("Loading tag names struct", Loading);
\r
256 // loads the tag structure
\r
257 XML_Tree->Tags = static_sequence::load(fp);
\r
258 ufread(&XML_Tree->tags_blen,sizeof(uint),1,fp);
\r
259 std::cerr << "tags_blen is "<< XML_Tree->tags_blen <<"\n";
\r
260 ufread(&XML_Tree->tags_len,sizeof(uint),1,fp);
\r
261 XML_Tree->tags_fix = new uint[uint_len(XML_Tree->tags_blen,XML_Tree->tags_len)];
\r
262 ufread(XML_Tree->tags_fix,sizeof(uint),uint_len(XML_Tree->tags_blen,XML_Tree->tags_len),fp);
\r
264 // TODO ask francisco about this
\r
265 /// FIXME:UGLY tests!
\r
266 //uint * seq = new uint[XML_Tree->tags_len];
\r
267 //for(uint i=0;i<XML_Tree->tags_len;i++)
\r
268 // seq[i] = get_field(XML_Tree->tags_fix,XML_Tree->tags_blen,i);
\r
269 //cout << "Tags test: " << XML_Tree->Tags->test(seq,XML_Tree->tags_len) << endl;
\r
270 //XML_Tree->Tags->test(seq,XML_Tree->tags_len);
\r
274 STOPTIMER(Loading);
\r
275 std::cerr << (uint_len(XML_Tree->tags_blen,XML_Tree->tags_len)*sizeof(uint))/(1024*1024) << " MB for tag sequence" << std::endl;
\r
276 PRINTTIME("Loading tag struct", Loading);
\r
281 ufread(&(XML_Tree->disable_tc), sizeof(bool), 1, fp);
\r
283 XML_Tree->EBVector = static_bitsequence_rrr02::load(fp);
\r
284 //XML_Tree->EBVector = static_bitsequence_sdarray::load(fp);
\r
286 STOPTIMER(Loading);
\r
287 PRINTTIME("Loading text bitvector struct", Loading);
\r
292 if (!XML_Tree->disable_tc){
\r
293 XML_Tree->Text = TextCollection::Load(fp, filename, TextCollection::index_mode_default, sample_factor);
\r
295 else XML_Tree->Text = NULL;
\r
296 STOPTIMER(Loading);
\r
297 PRINTTIME("Loading TextCollection", Loading);
\r
301 XML_Tree->EBVector = NULL;
\r
302 XML_Tree->Text = NULL;
\r
303 XML_Tree->disable_tc = true;
\r
306 XML_Tree->stream = NULL;
\r
307 XML_Tree->stream_fd = 0;
\r
314 // SubtreeSize(x): the number of nodes (and attributes) in the subtree of node x.
\r
315 int XMLTree::SubtreeSize(treeNode x)
\r
317 return subtree_size(Par, x);
\r
320 // SubtreeTags(x,tag): the number of occurrences of tag within the subtree of node x.
\r
321 int XMLTree::SubtreeTags(treeNode x, TagType tag)
\r
324 x = fast_first_child(Par,x);
\r
327 int s = x + 2*subtree_size(Par, x) - 1;
\r
329 return (Tags->rank(tag, s) - Tags->rank(tag, node2tagpos(x)-1))+1;
\r
331 int XMLTree::SubtreeElements(treeNode x)
\r
334 int size = subtree_size(Par,x);
\r
336 x = fast_first_child(Par,x);
\r
340 int s = x + 2*size - 1;
\r
341 int ntext = Tags->rank(PCDATA_TAG_ID, s) - Tags->rank(PCDATA_TAG_ID, node2tagpos(x)-1);
\r
342 size = size - ntext;
\r
343 treeNode fin = fast_find_close(Par,x);
\r
344 treeNode y = Tags->select_next(ATTRIBUTE_TAG_ID,node2tagpos(x));
\r
345 while (y != NULLT && y < fin){
\r
346 size -= SubtreeSize(y);
\r
347 y = Tags->select_next(ATTRIBUTE_TAG_ID,node2tagpos(y));
\r
352 // IsLeaf(x): returns whether node x is leaf or not. In the succinct representation
\r
353 // this is just a bit inspection.
\r
354 bool XMLTree::IsLeaf(treeNode x)
\r
356 NULLT_IF(x==NULLT);
\r
357 return fast_isleaf(Par, x);
\r
360 // IsAncestor(x,y): returns whether node x is ancestor of node y.
\r
361 bool XMLTree::IsAncestor(treeNode x, treeNode y)
\r
363 return fast_is_ancestor(Par, x, y);
\r
366 // IsChild(x,y): returns whether node x is parent of node y.
\r
367 bool XMLTree::IsChild(treeNode x, treeNode y)
\r
369 if (!fast_is_ancestor(Par, x, y)) return false;
\r
370 return depth(Par, x) == (depth(Par, y) + 1);
\r
373 // IsFirstChild(x): returns whether node x is the first child of its parent.
\r
374 /*bool XMLTree::IsFirstChild(treeNode x)
\r
376 return ((x != NULLT)&&(x==Root() || prev_sibling(Par,x) == (treeNode)-1));
\r
380 // NumChildren(x): number of children of node x. Constant time with the data structure
\r
382 int XMLTree::NumChildren(treeNode x)
\r
384 return degree(Par, x);
\r
387 // ChildNumber(x): returns i if node x is the i-th children of its parent.
\r
388 int XMLTree::ChildNumber(treeNode x)
\r
390 return child_rank(Par, x);
\r
393 // Depth(x): depth of node x, a simple binary rank on the parentheses sequence.
\r
394 int XMLTree::Depth(treeNode x)
\r
396 return depth(Par, x);
\r
399 // Preorder(x): returns the preorder number of node x, just counting the tree
\r
400 // nodes (i.e., tags, it disregards the texts in the tree).
\r
401 int XMLTree::Preorder(treeNode x)
\r
403 return preorder_rank(Par, x);
\r
406 // Postorder(x): returns the postorder number of node x, just counting the tree
\r
407 // nodes (i.e., tags, it disregards the texts in the tree).
\r
408 int XMLTree::Postorder(treeNode x)
\r
410 return postorder_rank(Par, x);
\r
413 // Tag(x): returns the tag identifier of node x.
\r
414 TagType XMLTree::Tag(treeNode x)
\r
416 return fast_get_field(tags_fix,tags_blen,node2tagpos(x));
\r
419 // DocIds(x): returns the range of text identifiers that descend from node x.
\r
420 // returns {NULLT, NULLT} when there are no texts descending from x.
\r
421 range XMLTree::DocIds(treeNode x)
\r
429 int min = EBVector->rank1(x-1);
\r
430 int max = EBVector->rank1(x+2*subtree_size(Par, x)-2);
\r
431 if (min==max) { // range is empty, no texts within the subtree of x
\r
435 else { // the range is non-empty, there are texts within the subtree of x
\r
442 // Parent(x): returns the parent node of node x.
\r
444 treeNode XMLTree::Parent(treeNode x)
\r
449 return parent(Par, x);;
\r
452 // Child(x,i): returns the i-th child of node x, assuming it exists.
\r
453 treeNode XMLTree::Child(treeNode x, int i)
\r
455 if (i <= OPTD) return naive_child(Par, x, i);
\r
456 else return child(Par, x, i);
\r
459 // FirstChild(x): returns the first child of node x, assuming it exists. Very fast in BP.
\r
461 treeNode XMLTree::FirstChild(treeNode x)
\r
463 NULLT_IF(x==NULLT);
\r
464 return fast_first_child(Par, x);
\r
468 treeNode XMLTree::FirstElement(treeNode x)
\r
470 NULLT_IF(x==NULLT);
\r
471 x = fast_first_child(Par, x);
\r
472 NULLT_IF(x == NULLT);
\r
475 case PCDATA_TAG_ID:
\r
477 return (fast_inspect(Par,x)==OP)? x : NULLT;
\r
479 case ATTRIBUTE_TAG_ID:
\r
480 x = fast_next_sibling(Par,x);
\r
481 if (x != NULLT && Tag(x) == PCDATA_TAG_ID){
\r
483 return (fast_inspect(Par,x)==OP)? x : NULLT;
\r
491 treeNode XMLTree::NextElement(treeNode x)
\r
493 NULLT_IF(x==NULLT);
\r
494 x = fast_next_sibling(Par, x);
\r
495 NULLT_IF(x == NULLT);
\r
496 if (Tag(x) == PCDATA_TAG_ID){
\r
498 return (fast_inspect(Par,x)==OP)? x : NULLT;
\r
503 // LastChild(x): returns the last child of node x.
\r
504 treeNode XMLTree::LastChild(treeNode x)
\r
506 NULLT_IF(x == NULLT || fast_isleaf(Par,x));
\r
507 return find_open(Par, fast_find_close(Par, x)-1);
\r
510 // NextSibling(x): returns the next sibling of node x, assuming it exists.
\r
511 /*treeNode XMLTree::NextSibling(treeNode x)
\r
513 NULLT_IF(x==NULLT || x == Root() );
\r
514 x = fast_find_close(Par,x)+1;
\r
515 return (fast_inspect(Par,x) == CP ? NULLT : x);
\r
519 // PrevSibling(x): returns the previous sibling of node x, assuming it exists.
\r
520 treeNode XMLTree::PrevSibling(treeNode x)
\r
522 NULLT_IF(x==NULLT);
\r
523 return prev_sibling(Par, x);
\r
526 // TaggedChild(x,tag): returns the first child of node x tagged tag, or NULLT if there is none.
\r
527 // Because of the balanced-parentheses representation of the tree, this operation is not supported
\r
528 // efficiently, just iterating among the children of node x until finding the desired child.
\r
529 treeNode XMLTree::TaggedChild(treeNode x, TagType tag)
\r
532 NULLT_IF(x==NULLT || fast_isleaf(Par,x));
\r
534 child = fast_first_child(Par, x); // starts at first child of node x
\r
535 if (Tag(child) == tag)
\r
538 return TaggedFollowingSibling(child,tag);
\r
541 // TaggedSibling(x,tag): returns the first sibling of node x tagged tag, or NULLT if there is none.
\r
542 treeNode XMLTree::TaggedFollowingSibling(treeNode x, TagType tag)
\r
544 NULLT_IF(x==NULLT);
\r
545 treeNode sibling = fast_next_sibling(Par, x);
\r
547 while (sibling != NULLT) {
\r
548 ctag = Tag(sibling);
\r
549 if (ctag == tag) // current sibling is labeled with tag of interest
\r
551 sibling = fast_sibling(Par, sibling, ctag); // OK, let's try with the next sibling
\r
553 return NULLT; // no such sibling was found
\r
556 treeNode XMLTree::SelectChild(treeNode x, TagIdSet *tags)
\r
559 NULLT_IF(x==NULLT || fast_isleaf(Par,x));
\r
561 treeNode child = fast_first_child(Par, x);
\r
563 while (child != NULLT) {
\r
565 if (tags->find(t) != tags->end()) return child;
\r
566 child = fast_sibling(Par, child,t);
\r
572 treeNode XMLTree::SelectFollowingSibling(treeNode x, TagIdSet *tags)
\r
575 NULLT_IF(x==NULLT);
\r
578 treeNode sibling = fast_next_sibling(Par, x);
\r
579 while (sibling != NULLT) {
\r
581 if (tags->find(t) != tags->end()) return sibling;
\r
582 sibling = fast_sibling(Par, sibling,t);
\r
588 // TaggedDescendant(x,tag): returns the first node tagged tag with larger preorder than x and within
\r
589 // the subtree of x. Returns NULLT if there is none.
\r
591 treeNode XMLTree::TaggedDescendant(treeNode x, TagType tag)
\r
593 //NULLT_IF(x==NULLT || fast_isleaf(Par,x));
\r
595 int s = (int) Tags->select_next(tag,node2tagpos(x));
\r
596 NULLT_IF (s == -1);
\r
598 treeNode y = tagpos2node(s); // transforms the tag position into a node position
\r
600 return (fast_is_ancestor(Par,x,y) ? y : NULLT);
\r
604 treeNode XMLTree::SelectDescendant(treeNode x, TagIdSet *tags)
\r
606 NULLT_IF (x ==NULLT || fast_isleaf(Par,x));
\r
608 treeNode min = NULLT;
\r
609 treeNode fc = fast_first_child(Par,x);
\r
611 TagIdSet::const_iterator tagit;
\r
612 for (tagit = tags->begin(); tagit != tags->end(); tagit++) {
\r
613 aux = TaggedDescendant(x, (TagType) *tagit);
\r
614 if (aux == fc) return fc;
\r
615 if (aux == NULLT) continue;
\r
616 if ((min == NULLT) || (aux < min)) min = aux;
\r
623 // TaggedPrec(x,tag): returns the first node tagged tag with smaller preorder than x and not an
\r
624 // ancestor of x. Returns NULLT if there is none.
\r
625 treeNode XMLTree::TaggedPreceding(treeNode x, TagType tag)
\r
628 treeNode node_s, root;
\r
629 r = (int)Tags->rank(tag, node2tagpos(x)-1);
\r
630 if (r==0) return NULLT; // there is no such node.
\r
631 s = (int)Tags->select(tag, r);
\r
632 root = root_node(Par);
\r
633 node_s = tagpos2node(s);
\r
634 while (fast_is_ancestor(Par, node_s, x) && (node_s!=root)) { // the one that we found is an ancestor of x
\r
636 if (r==0) return NULLT; // there is no such node
\r
637 s = (int)Tags->select(tag, r); // we should use select_prev instead when provided
\r
638 node_s = tagpos2node(s);
\r
640 return NULLT; // there is no such node
\r
644 // TaggedFoll(x,tag): returns the first node tagged tag with larger preorder than x and not in
\r
645 // the subtree of x. Returns NULLT if there is none.
\r
646 treeNode XMLTree::TaggedFollowing(treeNode x, TagType tag)
\r
648 NULLT_IF (x ==NULLT || x == Root());
\r
649 return tagpos2node(Tags->select_next(tag,fast_find_close(Par, x)));
\r
653 // TaggedFollBelow(x,tag,root): returns the first node tagged tag with larger preorder than x
\r
654 // and not in the subtree of x. Returns NULLT if there is none.
\r
656 treeNode XMLTree::TaggedFollowingBelow(treeNode x, TagType tag, treeNode ancestor)
\r
658 // NULLT_IF (x == NULLT || x == Root() || x == ancestor);
\r
660 //Special optimisation, test for the following sibling first
\r
661 treeNode close = fast_find_close(Par, x);
\r
662 treeNode s = tagpos2node(Tags->select_next(tag, close));
\r
664 if (ancestor == Root() || s==NULLT || s < fast_find_close(Par,ancestor)) return s;
\r
669 treeNode XMLTree::TaggedFollowingBefore(treeNode x, TagType tag, treeNode closing)
\r
672 NULLT_IF (x == NULLT || x == Root());
\r
674 treeNode s = tagpos2node(Tags->select_next(tag, fast_find_close(Par, x)));
\r
675 NULLT_IF (s == NULLT || s >= closing);
\r
680 /* Here we inline TaggedFoll to find the min globally, and only at the end
\r
681 we check if the min is below the context node */
\r
682 treeNode XMLTree::SelectFollowingBelow(treeNode x, TagIdSet *tags, treeNode ancestor)
\r
685 NULLT_IF(x==NULLT || x==Root());
\r
687 treeNode close = fast_find_close(Par,x);
\r
688 treeNode ns = close+1;
\r
689 if ( (fast_inspect(Par,ns) == OP) && (tags->find(Tag(ns)) != tags->end()))
\r
693 treeNode min = NULLT;
\r
697 TagIdSet::const_iterator tagit;
\r
698 for (tagit = tags->begin(); tagit != tags->end(); tagit++) {
\r
700 aux = tagpos2node(Tags->select_next(*tagit, close));
\r
702 // The next sibling of x is guaranteed to be below ctx
\r
703 // and is the node with lowest preorder which is after ctx.
\r
704 // if we find it, we return early;
\r
705 if (aux == NULLT) continue;
\r
706 if ((min == NULLT) || (aux < min)) min = aux;
\r
709 // found the smallest node in preorder which is after x.
\r
710 // if ctx is the root node, just return what we found.
\r
712 if (ancestor == Root()) return min;
\r
713 // else check whether if is in below the ctx node
\r
715 NULLT_IF (min == NULLT || min >= fast_find_close(Par, ancestor));
\r
720 treeNode XMLTree::SelectFollowingBefore(treeNode x, TagIdSet *tags, treeNode closing)
\r
723 NULLT_IF(x==NULLT || x==Root());
\r
725 treeNode min = NULLT;
\r
726 treeNode ns = fast_next_sibling(Par, x);
\r
727 treeNode close = ns - 1;
\r
729 TagIdSet::const_iterator tagit;
\r
730 for (tagit = tags->begin(); tagit != tags->end(); tagit++) {
\r
732 aux = tagpos2node(Tags->select_next(*tagit, close));
\r
734 // The next sibling of x is guaranteed to be below ctx
\r
735 // and is the node with lowest preorder which is after ctx.
\r
736 // if we find it, we return early;
\r
738 if (aux == ns ) return ns;
\r
739 if (aux == NULLT) continue;
\r
740 if ((min == NULLT) || (aux < min)) min = aux;
\r
743 // found the smallest node in preorder which is after x.
\r
744 // if ctx is the root node, just return what we found.
\r
746 NULLT_IF (min == NULLT || min >= closing);
\r
753 // TaggedAncestor(x, tag): returns the closest ancestor of x tagged tag. Return
\r
754 // NULLT is there is none.
\r
755 treeNode XMLTree::TaggedAncestor(treeNode x, TagType tag)
\r
757 if (x == NULLT || x == Root())
\r
760 treeNode s = parent(Par, x), r = Root();
\r
762 if (Tag(s) == tag) return s;
\r
763 s = parent(Par, s);
\r
770 // MyText(x): returns the document identifier of the text below node x,
\r
771 // or NULLT if x is not a leaf node or the text is empty. Assumes Doc
\r
772 // ids start from 0.
\r
773 DocID XMLTree::MyText(treeNode x)
\r
775 TagType tag = Tag(x);
\r
776 // seems faster than testing EBVector->access(x);
\r
778 if (tag == PCDATA_TAG_ID || tag == ATTRIBUTE_DATA_TAG_ID)
\r
779 //if (EBVector->access(x))
\r
780 return (DocID) (EBVector->rank1(x)-1); //-1 because document ids start from 0
\r
782 return (DocID) NULLT;
\r
785 // MyText(x): returns the document identifier of the text below node x,
\r
786 // or NULLT if x is not a leaf node or the text is empty. Assumes Doc
\r
787 // ids start from 0.
\r
788 DocID XMLTree::MyTextUnsafe(treeNode x)
\r
790 return (DocID) (EBVector->rank1(x)-1); //-1 because document ids start from 0
\r
792 // TextXMLId(d): returns the preorder of document with identifier d in the tree consisting of
\r
793 // all tree nodes and all text nodes. Assumes that the tree root has preorder 1.
\r
794 int XMLTree::TextXMLId(DocID d)
\r
796 NULLT_IF(d == NULLT);
\r
797 int s = EBVector->select1(d+1);
\r
798 return rank_open(Par, s) + d + 1; // +1 because root has preorder 1
\r
802 // NodeXMLId(x): returns the preorder of node x in the tree consisting
\r
803 // of all tree nodes and all text nodes. Assumes that the tree root has
\r
805 int XMLTree::NodeXMLId(treeNode x)
\r
807 NULLT_IF(x == NULLT);
\r
808 if (x == Root()) return 1; // root node has preorder 1
\r
809 return rank_open(Par, x) + EBVector->rank1(x-1);
\r
812 // ParentNode(d): returns the parent node of document identifier d.
\r
813 treeNode XMLTree::ParentNode(DocID d)
\r
815 NULLT_IF (d == NULLT);
\r
816 return (treeNode) EBVector->select1(d+1);
\r
819 // GetTagId: returns the tag identifier corresponding to a given tag name.
\r
820 // Returns NULLT in case that the tag name does not exists.
\r
821 TagType XMLTree::GetTagId(unsigned char *tagname)
\r
824 string s = (char *) tagname;
\r
825 TagIdMapIT it = tIdMap->find(s);
\r
826 return (TagType) ((it != tIdMap->end()) ? it->second : -1);
\r
831 // GetTagName(tagid): returns the tag name of a given tag identifier.
\r
832 // Returns NULL in case that the tag identifier is not valid.
\r
833 unsigned char *XMLTree::GetTagName(TagType tagid)
\r
836 if ( tagid < 0 || tagid >= TagName->size())
\r
837 return (unsigned char *) "<INVALID TAG>";
\r
838 strcpy((char *)s, (*TagName)[tagid].c_str());
\r
840 return (s == NULL ? (unsigned char*) "<INVALID TAG>" : s);
\r
844 const unsigned char *XMLTree::GetTagNameByRef(TagType tagid)
\r
848 if ( tagid < 0 || tagid >= TagName->size())
\r
849 return (unsigned char *) "<INVALID TAG>";
\r
851 return (const unsigned char *) (*TagName)[tagid].c_str();
\r
857 TagType XMLTree::RegisterTag(unsigned char *tagname)
\r
859 TagType id = XMLTree::GetTagId(tagname);
\r
861 string s = (char *) tagname;
\r
862 REGISTER_TAG(TagName,tIdMap,s);
\r
869 treeNode XMLTree::Closing(treeNode x) {
\r
870 return fast_find_close(Par,x);
\r
872 bool XMLTree::IsOpen(treeNode x) { return fast_inspect(Par,x); }
\r
874 //WARNING this uses directly the underlying implementation for plain text
\r
877 void XMLTree::Print(int fd,treeNode x, bool no_text){
\r
879 int newfd = dup(fd);
\r
880 stream = fdopen(newfd,"wa");
\r
887 buffer = new string();
\r
890 treeNode fin = fast_find_close(Par,x);
\r
892 TagType tag = Tag(n);
\r
894 range r = DocIds(x);
\r
895 treeNode first_idx;
\r
896 treeNode first_text = (tag == PCDATA_TAG_ID ? x : ParentNode(r.min-1));
\r
897 treeNode first_att = NULLT;
\r
899 if (first_att == NULLT)
\r
900 first_idx = first_text;
\r
901 else if (first_text == NULLT)
\r
902 first_idx = first_att;
\r
904 first_idx = min(first_att,first_text);
\r
906 uchar * current_text=NULL;
\r
907 if (first_idx != NULLT)
\r
908 current_text = GetText(MyText(first_idx));
\r
910 std::vector<uchar*> st;
\r
912 if (fast_inspect(Par,n)){
\r
913 if (tag == PCDATA_TAG_ID ) {
\r
916 myfputs("<$/>",fp);
\r
918 read = myfprintf((const char*) current_text, fp);
\r
919 current_text += (read + 1);
\r
921 n+=2; // skip closing $
\r
927 tagstr = (uchar*) GetTagNameByRef(tag);
\r
928 myfputs((const char*) tagstr ,fp);
\r
930 if (fast_inspect(Par,n)) {
\r
931 st.push_back(tagstr);
\r
933 if (tag == ATTRIBUTE_TAG_ID){
\r
935 if (no_text) myfputs("><@@>",fp);
\r
936 while (fast_inspect(Par,n)){
\r
939 myfputs((const char*) &(GetTagNameByRef(Tag(n))[3]),fp);
\r
941 myfputs("<$@/></",fp);
\r
942 myfputs((const char*) &(GetTagNameByRef(Tag(n))[3]),fp);
\r
948 myfputs((const char*) &(GetTagNameByRef(Tag(n))[3]),fp);
\r
951 read = myfprintf((const char*) current_text,fp);
\r
952 current_text += (read + 1);
\r
958 myfputs("</@@>",fp);
\r
959 else myfputc('>',fp);
\r
967 else {// <foo /> tag
\r
977 myfputs((const char*)st.back(),fp);
\r
981 }while (!fast_inspect(Par,n) && !st.empty());
\r