6 // functions to convert tag positions to the corresponding tree node and viceversa.
\r
7 // These are implemented in order to be able to change the tree and Tags representations,
\r
8 // without affecting the code so much.
\r
9 // Current implementation corresponds to balanced-parentheses representation for
\r
10 // the tree, and storing 2 tags per tree node (opening and closing tags).
\r
12 // tag position -> tree node
\r
13 inline treeNode tagpos2node(int t)
\r
15 return (treeNode) t;
\r
18 // tree node -> tag position
\r
19 inline int node2tagpos(treeNode x)
\r
24 // returns NULLT if the test is true
\r
25 #define NULLT_IF(x) do { if (x) return NULLT; } while (0)
\r
28 XMLTree::XMLTree( pb * const par, uint npar, vector<string> * const TN, TagIdMap * const tim, uint *empty_texts_bmp, TagType *tags,
\r
29 TextCollection * const TC, bool dis_tc)
\r
32 // creates the data structure for the tree topology
\r
33 Par = (bp *)umalloc(sizeof(bp));
\r
34 bp_construct(Par, npar, (pb*) par, OPT_DEGREE|0);
\r
36 // creates structure for tags
\r
38 TagName = (vector<string>*)TN;
\r
39 tIdMap = (TagIdMap *) tim;
\r
41 uint max_tag = TN->size() - 1;
\r
43 static_bitsequence_builder *bmb = new static_bitsequence_builder_sdarray();
\r
44 alphabet_mapper *am = new alphabet_mapper_none();
\r
45 Tags = new static_sequence_bs((uint*)tags,npar,am,bmb);
\r
47 cout << "Tags test: " << Tags->test((uint*)tags,npar) << endl;
\r
49 tags_blen = bits(max_tag);
\r
50 tags_len = (uint)npar;
\r
51 tags_fix = new uint[uint_len(tags_blen,tags_len)];
\r
52 for(uint i=0;i<(uint)npar;i++)
\r
53 set_field(tags_fix,tags_blen,i,tags[i]);
\r
59 Text = (TextCollection*) TC;
\r
62 EBVector = new static_bitsequence_rrr02(empty_texts_bmp,npar,32);
\r
63 free(empty_texts_bmp);
\r
64 empty_texts_bmp = NULL;
\r
67 disable_tc = dis_tc;
\r
71 // ~XMLTree: frees memory of XML tree.
\r
72 XMLTree::~XMLTree()
\r
77 free(Par); // frees the memory of struct Par
\r
98 void XMLTree::print_stats()
\r
100 uint total_space = Tags->size()+sizeof(static_sequence*);
\r
101 total_space += sizeof(uint*)+sizeof(uint)*(2+uint_len(tags_blen,tags_len));
\r
102 cout << "Space usage for XMLTree:" << endl
\r
103 << " - tags static_sequence: " << Tags->size()+sizeof(static_sequence*) << endl
\r
104 << " - tags access array: " << sizeof(uint*)+sizeof(uint)*(2+uint_len(tags_blen,tags_len)) << endl
\r
105 << " ... add Diego structures ... " << endl
\r
106 << " *total* " << total_space << endl;
\r
109 // Save: saves XML tree data structure to file.
\r
110 void XMLTree::Save(int fd)
\r
113 char filenameaux[1024];
\r
116 fp = fdopen(fd, "wa");
\r
117 // first stores the tree topology
\r
120 // stores the table with tag names
\r
121 int ntags = TagName->size();
\r
123 ufwrite(&ntags, sizeof(int), 1, fp);
\r
124 for (i = 0; i<ntags;i++)
\r
125 fprintf(fp, "%s\n",TagName->at(i).c_str());
\r
130 ufwrite(&tags_blen,sizeof(uint),1,fp);
\r
131 ufwrite(&tags_len,sizeof(uint),1,fp);
\r
132 ufwrite(tags_fix,sizeof(uint),uint_len(tags_blen,tags_len),fp);
\r
135 ufwrite(&disable_tc, sizeof(bool),1,fp);
\r
138 EBVector->save(fp);
\r
140 // stores the texts
\r
149 // Load: loads XML tree data structure from file. Returns
\r
150 // a pointer to the loaded data structure
\r
151 XMLTree *XMLTree::Load(int fd)
\r
160 fp = fdopen(fd, "r");
\r
162 XML_Tree = new XMLTree();
\r
164 // Load the tree structure
\r
165 XML_Tree->Par = (bp *)umalloc(sizeof(bp));
\r
167 loadTree(XML_Tree->Par, fp);
\r
169 XML_Tree->TagName = new vector<string>();
\r
170 XML_Tree->tIdMap = new std::unordered_map<string,int>();
\r
175 // Load the tag names
\r
176 ufread(&ntags, sizeof(int), 1, fp);
\r
178 for (i=0; i<ntags;i++) {
\r
179 char * r = fgets(buffer,1023,fp);
\r
181 throw "Cannot read tag list";
\r
182 s = (const char*) buffer;
\r
183 // remove the trailing \n
\r
184 s.erase(s.size()-1);
\r
185 XML_Tree->TagName->push_back(s);
\r
186 XML_Tree->tIdMap->insert(std::make_pair(s,i));
\r
191 // loads the tag structure
\r
192 XML_Tree->Tags = static_sequence::load(fp);
\r
193 ufread(&XML_Tree->tags_blen,sizeof(uint),1,fp);
\r
194 ufread(&XML_Tree->tags_len,sizeof(uint),1,fp);
\r
195 XML_Tree->tags_fix = new uint[uint_len(XML_Tree->tags_blen,XML_Tree->tags_len)];
\r
196 ufread(XML_Tree->tags_fix,sizeof(uint),uint_len(XML_Tree->tags_blen,XML_Tree->tags_len),fp);
\r
198 // TODO ask francisco about this
\r
199 /// FIXME:UGLY tests!
\r
200 uint * seq = new uint[XML_Tree->tags_len];
\r
201 for(uint i=0;i<XML_Tree->tags_len;i++)
\r
202 seq[i] = get_field(XML_Tree->tags_fix,XML_Tree->tags_blen,i);
\r
203 //cout << "Tags test: " << XML_Tree->Tags->test(seq,XML_Tree->tags_len) << endl;
\r
204 XML_Tree->Tags->test(seq,XML_Tree->tags_len);
\r
211 ufread(&(XML_Tree->disable_tc), sizeof(bool), 1, fp);
\r
213 XML_Tree->EBVector = static_bitsequence_rrr02::load(fp);
\r
216 int sample_rate_text = 64;
\r
218 if (!XML_Tree->disable_tc){
\r
219 XML_Tree->Text = TextCollection::Load(fp,sample_rate_text);
\r
221 else XML_Tree->Text = NULL;
\r
227 // root(): returns the tree root.
\r
228 inline treeNode XMLTree::Root()
\r
230 return 0; //root_node(Par);
\r
233 // SubtreeSize(x): the number of nodes (and attributes) in the subtree of node x.
\r
234 int XMLTree::SubtreeSize(treeNode x)
\r
236 return subtree_size(Par, x);
\r
239 // SubtreeTags(x,tag): the number of occurrences of tag within the subtree of node x.
\r
240 int XMLTree::SubtreeTags(treeNode x, TagType tag)
\r
243 x = first_child(Par,x);
\r
246 int s = x + 2*subtree_size(Par, x) - 1;
\r
248 return Tags->rank(tag, s) - Tags->rank(tag, node2tagpos(x)-1);
\r
251 // IsLeaf(x): returns whether node x is leaf or not. In the succinct representation
\r
252 // this is just a bit inspection.
\r
253 bool XMLTree::IsLeaf(treeNode x)
\r
255 return isleaf(Par, x);
\r
258 // IsAncestor(x,y): returns whether node x is ancestor of node y.
\r
259 bool XMLTree::IsAncestor(treeNode x, treeNode y)
\r
261 return is_ancestor(Par, x, y);
\r
264 // IsChild(x,y): returns whether node x is parent of node y.
\r
265 bool XMLTree::IsChild(treeNode x, treeNode y)
\r
267 if (!is_ancestor(Par, x, y)) return false;
\r
268 return depth(Par, x) == (depth(Par, y) + 1);
\r
271 // IsFirstChild(x): returns whether node x is the first child of its parent.
\r
272 bool XMLTree::IsFirstChild(treeNode x)
\r
274 return ((x != NULLT)&&(x==Root() || prev_sibling(Par,x) == (treeNode)-1));
\r
278 // NumChildren(x): number of children of node x. Constant time with the data structure
\r
280 int XMLTree::NumChildren(treeNode x)
\r
282 return degree(Par, x);
\r
285 // ChildNumber(x): returns i if node x is the i-th children of its parent.
\r
286 int XMLTree::ChildNumber(treeNode x)
\r
288 return child_rank(Par, x);
\r
291 // Depth(x): depth of node x, a simple binary rank on the parentheses sequence.
\r
292 int XMLTree::Depth(treeNode x)
\r
294 return depth(Par, x);
\r
297 // Preorder(x): returns the preorder number of node x, just counting the tree
\r
298 // nodes (i.e., tags, it disregards the texts in the tree).
\r
299 int XMLTree::Preorder(treeNode x)
\r
301 return preorder_rank(Par, x);
\r
304 // Postorder(x): returns the postorder number of node x, just counting the tree
\r
305 // nodes (i.e., tags, it disregards the texts in the tree).
\r
306 int XMLTree::Postorder(treeNode x)
\r
308 return postorder_rank(Par, x);
\r
311 // Tag(x): returns the tag identifier of node x.
\r
312 TagType XMLTree::Tag(treeNode x)
\r
314 return get_field(tags_fix,tags_blen,node2tagpos(x));
\r
317 // DocIds(x): returns the range of text identifiers that descend from node x.
\r
318 // returns {NULLT, NULLT} when there are no texts descending from x.
\r
319 range XMLTree::DocIds(treeNode x)
\r
329 int min = EBVector->rank1(x-1);
\r
330 int max = EBVector->rank1(x+2*subtree_size(Par, x)-2);
\r
331 if (min==max) { // range is empty, no texts within the subtree of x
\r
335 else { // the range is non-empty, there are texts within the subtree of x
\r
343 // Parent(x): returns the parent node of node x.
\r
344 treeNode XMLTree::Parent(treeNode x)
\r
349 return parent(Par, x);
\r
352 // Child(x,i): returns the i-th child of node x, assuming it exists.
\r
353 treeNode XMLTree::Child(treeNode x, int i)
\r
355 if (i <= OPTD) return naive_child(Par, x, i);
\r
356 else return child(Par, x, i);
\r
359 // FirstChild(x): returns the first child of node x, assuming it exists. Very fast in BP.
\r
360 treeNode XMLTree::FirstChild(treeNode x)
\r
362 NULLT_IF(x==NULLT);
\r
363 return first_child(Par, x);
\r
366 treeNode XMLTree::FirstElement(treeNode x)
\r
368 NULLT_IF(x==NULLT);
\r
369 treeNode fc = first_child(Par, x);
\r
371 return ((fc == NULLT || Tag(fc) != PCDATA_TAG_ID) ? fc : next_sibling(Par,fc));
\r
375 treeNode XMLTree::NextElement(treeNode x)
\r
377 NULLT_IF(x==NULLT);
\r
378 treeNode ns = next_sibling(Par, x);
\r
379 return ((ns == NULLT || Tag(ns) != PCDATA_TAG_ID) ? ns : next_sibling(Par,ns));
\r
382 // LastChild(x): returns the last child of node x.
\r
383 treeNode XMLTree::LastChild(treeNode x)
\r
385 NULLT_IF(x==NULLT || x == Root() || isleaf(Par,x));
\r
386 return find_open(Par, find_close(Par, x)-1);
\r
390 // NextSibling(x): returns the next sibling of node x, assuming it exists.
\r
391 treeNode XMLTree::NextSibling(treeNode x)
\r
393 NULLT_IF(x==NULLT || x == Root() );
\r
394 return next_sibling(Par, x);
\r
397 // PrevSibling(x): returns the previous sibling of node x, assuming it exists.
\r
398 treeNode XMLTree::PrevSibling(treeNode x)
\r
400 NULLT_IF(x==NULLT || x == Root());
\r
401 return prev_sibling(Par, x);
\r
404 // TaggedChild(x,tag): returns the first child of node x tagged tag, or NULLT if there is none.
\r
405 // Because of the balanced-parentheses representation of the tree, this operation is not supported
\r
406 // efficiently, just iterating among the children of node x until finding the desired child.
\r
407 treeNode XMLTree::TaggedChild(treeNode x, TagType tag)
\r
410 NULLT_IF(x==NULLT || isleaf(Par,x));
\r
413 child = first_child(Par, x); // starts at first child of node x
\r
414 if (get_field(tags_fix,tags_blen,node2tagpos(child)) == tag)
\r
417 return TaggedFollSibling(child,tag);
\r
420 // TaggedSibling(x,tag): returns the first sibling of node x tagged tag, or NULLT if there is none.
\r
421 treeNode XMLTree::TaggedFollSibling(treeNode x, TagType tag)
\r
423 NULLT_IF(x==NULLT);
\r
424 treeNode sibling = next_sibling(Par, x);
\r
425 while (sibling != NULLT) {
\r
426 if (get_field(tags_fix,tags_blen,node2tagpos(sibling)) == tag) // current sibling is labeled with tag of interest
\r
428 sibling = next_sibling(Par, sibling); // OK, let's try with the next sibling
\r
430 return NULLT; // no such sibling was found
\r
433 treeNode XMLTree::SelectChild(treeNode x, std::unordered_set<int> *tags)
\r
436 NULLT_IF(x==NULLT || isleaf(Par,x));
\r
438 treeNode child = first_child(Par, x);
\r
439 TagType t = get_field(tags_fix, tags_blen, node2tagpos(child));
\r
440 std::unordered_set<int>::const_iterator tagit = tags->find(t);
\r
441 if (tagit != tags->end()) return child;
\r
442 return SelectFollSibling(child,tags);
\r
446 treeNode XMLTree::SelectFollSibling(treeNode x, std::unordered_set<int> *tags)
\r
449 NULLT_IF(x==NULLT);
\r
452 treeNode sibling = next_sibling(Par, x);
\r
453 std::unordered_set<int>::const_iterator tagit;
\r
454 while (sibling != NULLT) {
\r
455 t = get_field(tags_fix, tags_blen, node2tagpos(sibling));
\r
456 tagit = tags->find(t);
\r
457 if (tagit != tags->end()) return sibling;
\r
458 sibling = next_sibling(Par, sibling);
\r
464 // TaggedDesc(x,tag): returns the first node tagged tag with larger preorder than x and within
\r
465 // the subtree of x. Returns NULLT if there is none.
\r
466 treeNode XMLTree::TaggedDesc(treeNode x, TagType tag)
\r
468 NULLT_IF(x==NULLT || isleaf(Par,x));
\r
470 int s = (int) Tags->select_next(tag,node2tagpos(x));
\r
471 NULLT_IF (s == -1);
\r
473 treeNode y = tagpos2node(s); // transforms the tag position into a node position
\r
475 return (is_ancestor(Par,x,y) ? y : NULLT);
\r
479 treeNode XMLTree::SelectDesc(treeNode x, std::unordered_set<int> *tags)
\r
481 NULLT_IF (x ==NULLT || isleaf(Par,x));
\r
483 treeNode min = NULLT;
\r
484 treeNode fc = first_child(Par,x);
\r
486 std::unordered_set<int>::const_iterator tagit;
\r
487 for (tagit = tags->begin(); tagit != tags->end(); tagit++) {
\r
488 aux = TaggedDesc(x, (TagType) *tagit);
\r
489 if (aux == fc) return fc;
\r
490 if (aux == NULLT) continue;
\r
491 if ((min == NULLT) || (aux < min)) min = aux;
\r
498 // TaggedPrec(x,tag): returns the first node tagged tag with smaller preorder than x and not an
\r
499 // ancestor of x. Returns NULLT if there is none.
\r
500 treeNode XMLTree::TaggedPrec(treeNode x, TagType tag)
\r
503 treeNode node_s, root;
\r
504 r = (int)Tags->rank(tag, node2tagpos(x)-1);
\r
505 if (r==0) return NULLT; // there is no such node.
\r
506 s = (int)Tags->select(tag, r);
\r
507 root = root_node(Par);
\r
508 node_s = tagpos2node(s);
\r
509 while (is_ancestor(Par, node_s, x) && (node_s!=root)) { // the one that we found is an ancestor of x
\r
511 if (r==0) return NULLT; // there is no such node
\r
512 s = (int)Tags->select(tag, r); // we should use select_prev instead when provided
\r
513 node_s = tagpos2node(s);
\r
515 return NULLT; // there is no such node
\r
519 // TaggedFoll(x,tag): returns the first node tagged tag with larger preorder than x and not in
\r
520 // the subtree of x. Returns NULLT if there is none.
\r
521 treeNode XMLTree::TaggedFoll(treeNode x, TagType tag)
\r
523 NULLT_IF (x ==NULLT || x == Root());
\r
525 return tagpos2node(Tags->select_next(tag,find_close(Par, x)));
\r
529 // TaggedFollBelow(x,tag,root): returns the first node tagged tag with larger preorder than x
\r
530 // and not in the subtree of x. Returns NULLT if there is none.
\r
531 treeNode XMLTree::TaggedFollBelow(treeNode x, TagType tag, treeNode root)
\r
534 NULLT_IF (x == NULLT || x == Root());
\r
536 treeNode s = tagpos2node(Tags->select_next(tag, find_close(Par, x)));
\r
538 if (root == Root()) return s;
\r
539 NULLT_IF (s == NULLT || s >= find_close(Par, root));
\r
544 /* Here we inline TaggedFoll to find the min globally, and only at the end
\r
545 we check if the min is below the context node */
\r
546 treeNode XMLTree::SelectFollBelow(treeNode x, std::unordered_set<int> *tags, treeNode root)
\r
549 NULLT_IF(x==NULLT || x==Root());
\r
551 treeNode min = NULLT;
\r
552 treeNode ns = next_sibling(Par, x);
\r
554 std::unordered_set<int>::const_iterator tagit;
\r
555 for (tagit = tags->begin(); tagit != tags->end(); tagit++) {
\r
557 aux = tagpos2node(Tags->select_next(*tagit, find_close(Par, x)));
\r
559 // The next sibling of x is guaranteed to be below ctx
\r
560 // and is the node with lowest preorder which is after ctx.
\r
561 // if we find it, we return early;
\r
563 if (aux == ns ) return ns;
\r
564 if (aux == NULLT) continue;
\r
565 if ((min == NULLT) || (aux < min)) min = aux;
\r
568 // found the smallest node in preorder which is after x.
\r
569 // if ctx is the root node, just return what we found.
\r
571 if (root == Root()) return min;
\r
572 // else check whether if is in below the ctx node
\r
574 NULLT_IF (min == NULLT || min >= find_close(Par, root));
\r
581 // TaggedAncestor(x, tag): returns the closest ancestor of x tagged tag. Return
\r
582 // NULLT is there is none.
\r
583 treeNode XMLTree::TaggedAncestor(treeNode x, TagType tag)
\r
585 if (x == NULLT || x == Root())
\r
588 treeNode s = parent(Par, x), r = Root();
\r
590 if (get_field(tags_fix,tags_blen,node2tagpos(s)) /*Tags->access(node2tagpos(s))*/ == tag) return s;
\r
591 s = parent(Par, s);
\r
598 // MyText(x): returns the document identifier of the text below node x,
\r
599 // or NULLT if x is not a leaf node or the text is empty. Assumes Doc
\r
600 // ids start from 0.
\r
601 DocID XMLTree::MyText(treeNode x)
\r
603 TagType tag = Tag(x);
\r
604 // seems faster than testing EBVector->access(x);
\r
606 if (tag == PCDATA_TAG_ID || tag == ATTRIBUTE_DATA_TAG_ID)
\r
607 return (DocID) (EBVector->rank1(x)-1); //-1 because document ids start from 0
\r
609 return (DocID) NULLT;
\r
613 // TextXMLId(d): returns the preorder of document with identifier d in the tree consisting of
\r
614 // all tree nodes and all text nodes. Assumes that the tree root has preorder 1.
\r
615 int XMLTree::TextXMLId(DocID d)
\r
617 NULLT_IF(d == NULLT);
\r
618 int s = EBVector->select1(d+1);
\r
619 return rank_open(Par, s) + d + 1; // +1 because root has preorder 1
\r
623 // NodeXMLId(x): returns the preorder of node x in the tree consisting
\r
624 // of all tree nodes and all text nodes. Assumes that the tree root has
\r
626 int XMLTree::NodeXMLId(treeNode x)
\r
628 NULLT_IF(x == NULLT);
\r
629 if (x == Root()) return 1; // root node has preorder 1
\r
630 return rank_open(Par, x) + EBVector->rank1(x-1);
\r
633 // ParentNode(d): returns the parent node of document identifier d.
\r
634 treeNode XMLTree::ParentNode(DocID d)
\r
636 NULLT_IF (d == NULLT);
\r
637 return (treeNode) EBVector->select1(d+1);
\r
640 // GetTagId: returns the tag identifier corresponding to a given tag name.
\r
641 // Returns NULLT in case that the tag name does not exists.
\r
642 TagType XMLTree::GetTagId(unsigned char *tagname)
\r
645 string s = (char *) tagname;
\r
646 TagIdMapIT it = tIdMap->find(s);
\r
647 return (TagType) ((it != tIdMap->end()) ? it->second : -1);
\r
652 // GetTagName(tagid): returns the tag name of a given tag identifier.
\r
653 // Returns NULL in case that the tag identifier is not valid.
\r
654 unsigned char *XMLTree::GetTagName(TagType tagid)
\r
657 if ( tagid < 0 || tagid >= TagName->size())
\r
658 return (unsigned char *) "<INVALID TAG>";
\r
659 strcpy((char *)s, TagName->at(tagid).c_str());
\r
661 return (s == NULL ? (unsigned char*) "<INVALID TAG>" : s);
\r
665 const unsigned char *XMLTree::GetTagNameByRef(TagType tagid)
\r
669 if ( tagid < 0 || tagid >= TagName->size())
\r
670 return (unsigned char *) "<INVALID TAG>";
\r
672 return (const unsigned char *) TagName->at(tagid).c_str();
\r
678 TagType XMLTree::RegisterTag(unsigned char *tagname)
\r
680 TagType id = XMLTree::GetTagId(tagname);
\r
682 string s = (char *) tagname;
\r
683 REGISTER_TAG(TagName,tIdMap,s);
\r