Diego: added method TaggedAncestor, renamed former method TaggedFoll to TaggedFollowi...
[SXSI/XMLTree.git] / XMLTree.cpp
index cdfe952..a37263a 100644 (file)
@@ -581,9 +581,30 @@ treeNode XMLTree::TaggedPrec(treeNode x, TagType tag)
     return NULLT; // there is no such node \r
  }\r
 \r
+\r
 // TaggedFoll(x,tag): returns the first node tagged tag with larger preorder than x and not in\r
 // the subtree of x. Returns NULLT if there is none.\r
-treeNode XMLTree::TaggedFoll(treeNode x, TagType tag) \r
+treeNode XMLTree::TaggedFoll(treeNode x, TagType tag)\r
+ {\r
+    if (!finished) {\r
+       fprintf(stderr, "Error: data structure has not been constructed properly\n");\r
+       exit(1);\r
+    }\r
+\r
+    int r, s;\r
+    if (x ==NULLT || x == Root())\r
+       return NULLT;\r
+                   \r
+    r = (int) Tags->rank(tag, find_close(Par, x));\r
+    s = (int) Tags->select(tag, r+1);  // select returns -1 in case that there is no r+1-th tag.\r
+    if (s==-1) return NULLT;\r
+    else return tagpos2node(s);\r
+ } \r
+\r
+\r
+// TaggedFollowingSibling(x,tag): returns the first node tagged tag with larger preorder than x and not in\r
+// the subtree of x. Returns NULLT if there is none.\r
+treeNode XMLTree::TaggedFollowingSibling(treeNode x, TagType tag) \r
  {\r
     if (!finished) {\r
        fprintf(stderr, "Error: data structure has not been constructed properly\n");\r
@@ -591,15 +612,39 @@ treeNode XMLTree::TaggedFoll(treeNode x, TagType tag)
     }\r
 \r
     int r, s;\r
-    if (x ==NULLT || x == Root()|| (next_sibling(Par,x) == -1 ))\r
+    treeNode ns = next_sibling(Par,x);\r
+\r
+    if (x == NULLT || x == Root() || ns == -1)\r
       return NULLT;\r
 \r
-    r = (int) Tags->rank(tag, node2tagpos(next_sibling(Par, x))-1);\r
+    r = (int) Tags->rank(tag, node2tagpos(ns)-1);\r
     s = (int) Tags->select(tag, r+1);  // select returns -1 in case that there is no r+1-th tag.\r
     if (s==-1) return NULLT;\r
     else return tagpos2node(s);\r
  }\r
 \r
+\r
+// TaggedAncestor(x, tag): returns the closest ancestor of x tagged tag. Return\r
+// NULLT is there is none.\r
+treeNode XMLTree::TaggedAncestor(treeNode x, TagType tag)\r
+ {\r
+    if (!finished) {\r
+       fprintf(stderr, "Error: data structure has not been constructed properly\n");\r
+       exit(1);\r
+    }\r
+    \r
+    if (x == NULLT || x == Root())\r
+       return NULLT;\r
+    \r
+    treeNode s = parent(Par, x), r = Root();\r
+    while (s != r) {\r
+       if (Tags->access(node2tagpos(s)) == tag) return s;\r
+       s = parent(Par, s);\r
+    }\r
+    return NULLT;\r
+ }\r
+\r
+\r
 // PrevText(x): returns the document identifier of the text to the left \r
 // of node x, or NULLT if x is the root node or the text is empty.\r
 // Assumes Doc ids start from 0.\r
@@ -890,17 +935,16 @@ int XMLTree::NewClosingTag(unsigned char *tagname)
        parArraySize *= 2;\r
     }\r
     \r
-    setbit(par_aux,npar,CP);  // marks a new closing opening parenthesis\r
+    setbit(par_aux,npar,CP);  // marks a new closing parenthesis\r
 \r
     // transforms the tagname into a tag identifier. If the tag is new, we insert\r
     // it in the table.\r
     for (i=0; i<ntagnames; i++)\r
-       if (strcmp((const char *)tagname,(const char *)TagName[i])==0) break;\r
+       if ((strcmp((const char *)tagname,(const char *)(TagName[i]+1))==0) && (TagName[i][0]=='/')) break;\r
  \r
     if (i==ntagnames) { // the tag is a new one, then we insert it\r
        TagName = (unsigned char **)urealloc(TagName, sizeof(char *)*(ntagnames+1));\r
        \r
-       \r
        ntagnames++;\r
        TagName[i] = (unsigned char *)umalloc(sizeof(char)*(strlen((const char *)tagname)+2));\r
        TagName[i][0] = '/';\r