Clean-up makefile
[SXSI/XMLTree.git] / XMLTree.cpp
index 248185d..fd46677 100644 (file)
@@ -13,11 +13,6 @@ using std::string;
 // Current implementation corresponds to balanced-parentheses representation for\r
 // the tree, and storing 2 tags per tree node (opening and closing tags).\r
 \r
-// tag position -> tree node\r
-static treeNode tagpos2node(int t) \r
- {\r
-    return (treeNode) t;\r
- }\r
 \r
 static int bits8 (int t ) {\r
   int r = bits(t);\r
@@ -29,36 +24,7 @@ static int bits8 (int t ) {
     return r;\r
 }\r
 \r
-// tree node -> tag position\r
-static int node2tagpos(treeNode x) \r
-{\r
-  return (int)x;\r
-}\r
-\r
-static int fast_find_close(bp *b,int s)\r
-{\r
-  return fwd_excess(b,s,-1);\r
-}\r
-\r
-static int fast_inspect(bp* Par,treeNode i)\r
-{\r
-  int j,l;\r
-  j = i >> logD;\r
-  l = i & (D-1);\r
-  return (Par->B[j] >> (D-1-l)) & 1;\r
-}\r
-\r
-static treeNode fast_first_child(bp *Par, treeNode x)\r
-{\r
-  x = x+1;\r
-  return (fast_inspect(Par,x) == OP) ? x : NULLT;\r
-}\r
 \r
-static treeNode fast_next_sibling(bp* Par,treeNode x)\r
-{\r
-  x = fast_find_close(Par,x)+1;\r
-  return (fast_inspect(Par,x) == OP) ? x : NULLT;\r
-}\r
 \r
 \r
 static treeNode fast_sibling(bp* Par,treeNode x,TagType tag){\r
@@ -98,12 +64,7 @@ static uint fast_get_field(uint* A,int len, int idx)
 \r
 }\r
 \r
-inline bool fast_is_ancestor(bp * Par,treeNode x,treeNode y){\r
-  if (x > y) \r
-    return false;\r
-  else\r
-    return (x==0) || (y <= fast_find_close(Par,x));\r
-}\r
+\r
 \r
 \r
 XMLTree::XMLTree( pb * const par, uint npar,  vector<string> * const TN,  TagIdMap * const tim, \r
@@ -209,7 +170,7 @@ void XMLTree::print_stats()
  }\r
 \r
 // Save: saves XML tree data structure to file. \r
-void XMLTree::Save(int fd) \r
+void XMLTree::Save(int fd, char *filename\r
  {\r
     FILE *fp;\r
     char filenameaux[1024];\r
@@ -241,16 +202,14 @@ void XMLTree::Save(int fd)
     \r
     // stores the texts   \r
     if (!disable_tc) {\r
-      Text->Save(fp);\r
+        Text->Save(fp, filename);\r
     };\r
-\r
-\r
  }\r
 \r
 \r
 // Load: loads XML tree data structure from file. Returns\r
 // a pointer to the loaded data structure\r
-XMLTree *XMLTree::Load(int fd, bool load_tc,int sample_factor) \r
+XMLTree *XMLTree::Load(int fd, char *filename, bool load_tc,int sample_factor) \r
  {\r
 \r
     FILE *fp;\r
@@ -331,7 +290,7 @@ XMLTree *XMLTree::Load(int fd, bool load_tc,int sample_factor)
     // Not used  \r
     // loads the texts\r
     if (!XML_Tree->disable_tc){\r
-        XML_Tree->Text = TextCollection::Load(fp, TextCollection::index_mode_default, sample_factor);\r
+        XML_Tree->Text = TextCollection::Load(fp, filename, TextCollection::index_mode_default, sample_factor);\r
     }\r
     else XML_Tree->Text = NULL;\r
     STOPTIMER(Loading);\r
@@ -412,11 +371,11 @@ bool XMLTree::IsChild(treeNode x, treeNode y)
  }\r
 \r
 // IsFirstChild(x): returns whether node x is the first child of its parent.\r
-bool XMLTree::IsFirstChild(treeNode x)\r
+/*bool XMLTree::IsFirstChild(treeNode x)\r
  {\r
     return ((x != NULLT)&&(x==Root() || prev_sibling(Par,x) == (treeNode)-1));\r
  }\r
-\r
+*/\r
 \r
 // NumChildren(x): number of children of node x. Constant time with the data structure\r
 // of Sadakane.\r
@@ -481,14 +440,14 @@ range XMLTree::DocIds(treeNode x)
  }\r
 \r
 // Parent(x): returns the parent node of node x.\r
-\r
+/*\r
 treeNode XMLTree::Parent(treeNode x) \r
  {\r
     if (x == Root())\r
       return NULLT;\r
     else\r
       return  parent(Par, x);;\r
- }\r
+ }*/\r
 \r
 // Child(x,i): returns the i-th child of node x, assuming it exists.\r
 treeNode XMLTree::Child(treeNode x, int i) \r
@@ -498,13 +457,14 @@ treeNode XMLTree::Child(treeNode x, int i)
 }\r
 \r
 // FirstChild(x): returns the first child of node x, assuming it exists. Very fast in BP.\r
-\r
+/*\r
 treeNode XMLTree::FirstChild(treeNode x) \r
  {\r
    NULLT_IF(x==NULLT);\r
    return fast_first_child(Par, x);\r
  }\r
-\r
+*/\r
+/*\r
 treeNode XMLTree::FirstElement(treeNode x) \r
  {\r
    NULLT_IF(x==NULLT);\r
@@ -527,7 +487,7 @@ treeNode XMLTree::FirstElement(treeNode x)
      return x;\r
    }\r
  }\r
-\r
+*//*\r
 treeNode XMLTree::NextElement(treeNode x) \r
 {\r
   NULLT_IF(x==NULLT);\r
@@ -538,7 +498,7 @@ treeNode XMLTree::NextElement(treeNode x)
      return (fast_inspect(Par,x)==OP)? x : NULLT;\r
   }\r
   else return x;  \r
-}\r
+  }*/\r
 \r
 // LastChild(x): returns the last child of node x.\r
 treeNode XMLTree::LastChild(treeNode x)\r
@@ -548,13 +508,13 @@ treeNode XMLTree::LastChild(treeNode x)
  }\r
 \r
 // NextSibling(x): returns the next sibling of node x, assuming it exists.\r
-treeNode XMLTree::NextSibling(treeNode x) \r
+/*treeNode XMLTree::NextSibling(treeNode x) \r
  {\r
    NULLT_IF(x==NULLT || x == Root() );\r
    x = fast_find_close(Par,x)+1;\r
    return (fast_inspect(Par,x) == CP ? NULLT : x);\r
  }\r
-\r
+*/\r
 \r
 // PrevSibling(x): returns the previous sibling of node x, assuming it exists.\r
 treeNode XMLTree::PrevSibling(treeNode x) \r
@@ -627,6 +587,7 @@ treeNode XMLTree::SelectFollowingSibling(treeNode x, TagIdSet *tags)
 \r
 // TaggedDescendant(x,tag): returns the first node tagged tag with larger preorder than x and within\r
 // the subtree of x. Returns NULLT if there is none.\r
+/*\r
 treeNode XMLTree::TaggedDescendant(treeNode x, TagType tag) \r
  {\r
    //NULLT_IF(x==NULLT || fast_isleaf(Par,x));\r
@@ -638,7 +599,7 @@ treeNode XMLTree::TaggedDescendant(treeNode x, TagType tag)
    \r
    return (fast_is_ancestor(Par,x,y) ? y : NULLT);\r
  }\r
-\r
+*/\r
 \r
 treeNode XMLTree::SelectDescendant(treeNode x, TagIdSet *tags)\r
  {\r
@@ -691,36 +652,20 @@ treeNode XMLTree::TaggedFollowing(treeNode x, TagType tag)
 \r
 // TaggedFollBelow(x,tag,root): returns the first node tagged tag with larger preorder than x \r
 // and not in the subtree of x. Returns NULLT if there is none.\r
+/*\r
 treeNode XMLTree::TaggedFollowingBelow(treeNode x, TagType tag, treeNode ancestor)\r
 {\r
   // NULLT_IF (x == NULLT || x == Root() || x == ancestor); \r
 \r
   //Special optimisation, test for the following sibling first\r
   treeNode close = fast_find_close(Par, x);\r
-  /*\r
-   treeNode ns = close+1;\r
-  if (fast_inspect(Par,ns) == OP) {\r
-    TagType tagns = Tag(ns);\r
-    //    cout << GetTagNameByRef(tagns) << endl;\r
-    //cout.flush();\r
-    if (tagns == PCDATA_TAG_ID){\r
-      close = ns+1;\r
-      ns = ns+2;\r
-      if (fast_inspect(Par,ns) != OP)\r
-       goto after;\r
-      tagns = Tag(ns);      \r
-    };\r
-    if (tagns == tag)\r
-      return ns;\r
-  };\r
- after:\r
-  */\r
   treeNode s = tagpos2node(Tags->select_next(tag, close));\r
   \r
   if (ancestor == Root() || s==NULLT || s < fast_find_close(Par,ancestor)) return s;\r
   else return NULLT;\r
 } \r
-\r
+*/\r
+ /*\r
 treeNode XMLTree::TaggedFollowingBefore(treeNode x, TagType tag, treeNode closing)\r
 {\r
 \r
@@ -731,7 +676,7 @@ treeNode XMLTree::TaggedFollowingBefore(treeNode x, TagType tag, treeNode closin
   \r
   return s;\r
 } \r
-\r
+ */\r
 /* Here we inline TaggedFoll to find the min globally, and only at the end\r
    we check if the min is below the context node */\r
 treeNode XMLTree::SelectFollowingBelow(treeNode x, TagIdSet *tags, treeNode ancestor)\r
@@ -750,13 +695,9 @@ treeNode XMLTree::SelectFollowingBelow(treeNode x, TagIdSet *tags, treeNode ance
   \r
 \r
    TagIdSet::const_iterator tagit;\r
-   for (tagit = tags->begin(); tagit != tags->end(); tagit++) {\r
+   for (tagit = tags->begin(); tagit != tags->end(); ++tagit) {\r
 \r
      aux = tagpos2node(Tags->select_next(*tagit, close));\r
-     \r
-     // The next sibling of x is guaranteed to be below ctx\r
-     // and is the node with lowest preorder which is after ctx.\r
-     // if we find it, we return early;         \r
      if (aux == NULLT) continue;\r
      if ((min == NULLT) || (aux < min)) min = aux;\r
    };\r
@@ -764,14 +705,43 @@ treeNode XMLTree::SelectFollowingBelow(treeNode x, TagIdSet *tags, treeNode ance
    // found the smallest node in preorder which is after x.\r
    // if ctx is the root node, just return what we found.\r
 \r
-   if (ancestor == Root()) return min;\r
-   // else check whether if is in below the ctx node\r
-\r
-   NULLT_IF (min == NULLT || min >= fast_find_close(Par, ancestor));\r
+   if (ancestor == Root() || min == NULLT || min < fast_find_close(Par, ancestor)) return min;\r
+   else return NULLT;\r
    \r
-   return min;\r
+ }\r
+\r
+treeNode XMLTree::SelectFollowingBefore(treeNode x, TagIdSet *tags, treeNode ancestor_closing)\r
+ {\r
+\r
+   NULLT_IF(x==NULLT || x==Root());\r
+\r
+   treeNode close = fast_find_close(Par,x);\r
+   treeNode ns = close+1;\r
+   if ( (fast_inspect(Par,ns) == OP) && (tags->find(Tag(ns)) != tags->end()))\r
+     return ns;\r
+\r
+   int i;\r
+   treeNode min = NULLT;\r
+   treeNode aux;\r
+  \r
+\r
+   TagIdSet::const_iterator tagit;\r
+   for (tagit = tags->begin(); tagit != tags->end(); ++tagit) {\r
+\r
+     aux = tagpos2node(Tags->select_next(*tagit, close));\r
+     if (aux == NULLT) continue;\r
+     if ((min == NULLT) || (aux < min)) min = aux;\r
+   };\r
+     \r
+   // found the smallest node in preorder which is after x.\r
+   // if ctx is the root node, just return what we found.\r
+\r
+   if (ancestor_closing == Root() || min == NULLT || min < ancestor_closing) return min;\r
+   else return NULLT;\r
    \r
  }\r
+\r
+/*\r
 treeNode XMLTree::SelectFollowingBefore(treeNode x, TagIdSet *tags, treeNode closing)\r
  {\r
 \r
@@ -803,7 +773,7 @@ treeNode XMLTree::SelectFollowingBefore(treeNode x, TagIdSet *tags, treeNode clo
    return min;\r
    \r
  }\r
-\r
+*/\r
 \r
 // TaggedAncestor(x, tag): returns the closest ancestor of x tagged tag. Return\r
 // NULLT is there is none.\r