- Implement popcount in ASM if available
authorkim <kim@3cdefd35-fc62-479d-8e8d-bae585ffb9ca>
Thu, 8 Sep 2011 02:44:06 +0000 (02:44 +0000)
committerkim <kim@3cdefd35-fc62-479d-8e8d-bae585ffb9ca>
Thu, 8 Sep 2011 02:44:06 +0000 (02:44 +0000)
- Pass around extra file name to load/save for tc extra-index files.

git-svn-id: svn+ssh://idea.nguyen.vg/svn/sxsi/trunk/XMLTree@1102 3cdefd35-fc62-479d-8e8d-bae585ffb9ca

Makefile
XMLTree.cpp
XMLTree.h
bpcore.c
darray.c
darray.h

index fd24873..035d0d9 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -1,4 +1,4 @@
-OPT_FLAGS=-O6 -DPOPCOUNT_TABLE -DHAS_NATIVE_POPCOUNT -fno-PIC -static
+OPT_FLAGS=-O4 -DPOPCOUNT_TABLE -DHAS_NATIVE_POPCOUNT -fno-PIC -static
 INC_FLAGS=-I./libcds/includes/ -I.
 CFLAGS= $(INC_FLAGS) $(OPT_FLAGS)
 CXXFLAGS= -std=c++0x $(INC_FLAGS) $(OPT_FLAGS)
index 119048d..a1eb13c 100644 (file)
@@ -7,8 +7,8 @@ using std::endl;
 using std::min;\r
 using std::string;\r
 \r
-// functions to convert tag positions to the corresponding tree node and viceversa. \r
-// These are implemented in order to be able to change the tree and Tags representations, \r
+// functions to convert tag positions to the corresponding tree node and viceversa.\r
+// These are implemented in order to be able to change the tree and Tags representations,\r
 // without affecting the code so much.\r
 // Current implementation corresponds to balanced-parentheses representation for\r
 // the tree, and storing 2 tags per tree node (opening and closing tags).\r
@@ -20,7 +20,7 @@ static int bits8 (int t ) {
     return 8;\r
   else if (r <= 16)\r
     return 16;\r
-  else \r
+  else\r
     return r;\r
 }\r
 \r
@@ -39,7 +39,7 @@ static treeNode fast_sibling(bp* Par,treeNode x,TagType tag){
 \r
 \r
 inline uint get_field_no_power(uint *A, uint len, uint index) {\r
-  \r
+\r
   register uint i=index*len/W, j=index*len-W*i;\r
   return (j+len <= W) ? (A[i] << (W-j-len)) >> (W-len) : (A[i] >> j) | (A[i+1] << (WW-j-len)) >> (W-len);\r
 \r
@@ -64,7 +64,7 @@ static uint fast_get_field(uint* A,int len, int idx)
 \r
 \r
 \r
-XMLTree::XMLTree( pb * const par, uint npar,  vector<string> * const TN,  TagIdMap * const tim, \r
+XMLTree::XMLTree( pb * const par, uint npar,  vector<string> * const TN,  TagIdMap * const tim,\r
                  uint *empty_texts_bmp, TagType *tags,\r
                  TextCollection * const TC, bool dis_tc,\r
                  TextCollectionBuilder::index_type_t _index_type )\r
@@ -79,19 +79,19 @@ XMLTree::XMLTree( pb * const par, uint npar,  vector<string> * const TN,  TagIdM
     PRINTTIME("Building parenthesis struct", Building);\r
     STARTTIMER();\r
 \r
-   \r
+\r
     // creates structure for tags\r
 \r
     TagName = (vector<string>*)TN;\r
     tIdMap = (TagIdMap *) tim;\r
-    \r
+\r
     uint max_tag = TN->size() - 1;\r
-    \r
-      \r
+\r
+\r
     static_bitsequence_builder *bmb = new static_bitsequence_builder_sdarray();\r
     alphabet_mapper *am = new alphabet_mapper_none();\r
     Tags = new static_sequence_bs((uint*)tags,npar,am,bmb);\r
-    \r
+\r
     //cout << "Tags test: " << Tags->test((uint*)tags,npar) << endl;\r
 \r
     //Ensures that for small tag numbers, we are on an 8bit boundary.\r
@@ -102,13 +102,13 @@ XMLTree::XMLTree( pb * const par, uint npar,  vector<string> * const TN,  TagIdM
     tags_fix = new uint[uint_len(tags_blen,tags_len)];\r
     for(uint i=0;i<(uint)npar;i++)\r
        set_field(tags_fix,tags_blen,i,tags[i]);\r
-    delete bmb;    \r
+    delete bmb;\r
     free(tags);\r
     tags = NULL;\r
 \r
     STOPTIMER(Building);\r
     PRINTTIME("Building Tag Structure", Building);\r
-    \r
+\r
     Text = (TextCollection*) TC;\r
 \r
 \r
@@ -117,32 +117,32 @@ XMLTree::XMLTree( pb * const par, uint npar,  vector<string> * const TN,  TagIdM
     free(empty_texts_bmp);\r
     empty_texts_bmp = NULL;\r
 \r
-    \r
+\r
     disable_tc = dis_tc;\r
-    text_index_type = _index_type;    \r
+    text_index_type = _index_type;\r
     std::cerr << "Number of distinct tags " << TagName->size() << "\n";\r
     //std::cerr.flush();\r
  }\r
 \r
 \r
 // ~XMLTree: frees memory of XML tree.\r
-XMLTree::~XMLTree() \r
- { \r
+XMLTree::~XMLTree()\r
+ {\r
     int i;\r
 \r
     destroyTree(Par);\r
     free(Par); // frees the memory of struct Par\r
-   \r
+\r
     delete tIdMap;\r
     tIdMap = NULL;\r
-    \r
+\r
     delete TagName;\r
     TagName = NULL;\r
-    \r
+\r
     delete Tags;\r
     Tags = NULL;\r
 \r
-    delete Text; \r
+    delete Text;\r
     Text = NULL;\r
 \r
     delete EBVector;\r
@@ -151,7 +151,7 @@ XMLTree::~XMLTree()
  }\r
 \r
 \r
-void XMLTree::print_stats() \r
+void XMLTree::print_stats()\r
  {\r
     uint total_space = Tags->size()+sizeof(static_sequence*);\r
     total_space += sizeof(uint*)+sizeof(uint)*(2+uint_len(tags_blen,tags_len));\r
@@ -162,11 +162,10 @@ void XMLTree::print_stats()
          << " *total* " << total_space << endl;\r
  }\r
 \r
-// Save: saves XML tree data structure to file. \r
-void XMLTree::Save(int fd\r
+// Save: saves XML tree data structure to file.\r
+void XMLTree::Save(int fd, char * name)\r
  {\r
     FILE *fp;\r
-    char filenameaux[1024];\r
     int i;\r
 \r
     fp = fdopen(fd, "wa");\r
@@ -179,7 +178,7 @@ void XMLTree::Save(int fd)
     ufwrite(&ntags, sizeof(int), 1, fp);\r
     for (i = 0; i<ntags;i++)\r
       fprintf(fp, "%s\n",TagName->at(i).c_str());\r
-    \r
+\r
 \r
     // stores the tags\r
     Tags->save(fp);\r
@@ -187,39 +186,40 @@ void XMLTree::Save(int fd)
     ufwrite(&tags_len,sizeof(uint),1,fp);\r
     ufwrite(tags_fix,sizeof(uint),uint_len(tags_blen,tags_len),fp);\r
 \r
-    // flags \r
+    // flags\r
     ufwrite(&disable_tc, sizeof(bool),1,fp);\r
-    \r
+\r
     //text positions\r
     EBVector->save(fp);\r
-    \r
-    // stores the texts   \r
+\r
+    // stores the texts\r
     if (!disable_tc) {\r
 \r
       ufwrite(&text_index_type, sizeof(TextCollectionBuilder::index_type_t), 1, fp);\r
-     \r
-      const char * pref;\r
+\r
+\r
+      string file(name);\r
       switch (text_index_type){\r
       case TextCollectionBuilder::index_type_default:\r
-       pref = "default_";\r
+       file.append(".default");\r
        break;\r
       case TextCollectionBuilder::index_type_swcsa:\r
-       pref = "swcsa_";\r
+       file.append(".swcsa");\r
        break;\r
       case TextCollectionBuilder::index_type_rlcsa:\r
-       pref = "rlcsa_";\r
+       file.append(".rlcsa");\r
        break;\r
       };\r
-      \r
-      Text->Save(fp, pref);\r
-          \r
+\r
+      Text->Save(fp, file.c_str());\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, bool load_tc,int sample_factor, char * name)\r
  {\r
 \r
     FILE *fp;\r
@@ -236,7 +236,7 @@ XMLTree *XMLTree::Load(int fd, bool load_tc,int sample_factor)
     // Load the tree structure\r
     XML_Tree->Par = (bp *)umalloc(sizeof(bp));\r
 \r
-    loadTree(XML_Tree->Par, fp); \r
+    loadTree(XML_Tree->Par, fp);\r
     STOPTIMER(Loading);\r
     PRINTTIME("Loading parenthesis struct", Loading);\r
     STARTTIMER();\r
@@ -245,7 +245,7 @@ XMLTree *XMLTree::Load(int fd, bool load_tc,int sample_factor)
     XML_Tree->tIdMap = new std::unordered_map<std::string,int>();\r
     std::string s;\r
     int ntags;\r
-    \r
+\r
     // Load the tag names\r
     ufread(&ntags, sizeof(int), 1, fp);\r
 \r
@@ -254,10 +254,10 @@ XMLTree *XMLTree::Load(int fd, bool load_tc,int sample_factor)
         throw "Cannot read tag list";\r
        s = buffer;\r
        // remove the trailing \n\r
-       s.erase(s.size()-1);       \r
-       XML_Tree->TagName->push_back(s);       \r
+       s.erase(s.size()-1);\r
+       XML_Tree->TagName->push_back(s);\r
        XML_Tree->tIdMap->insert(std::make_pair(s,i));\r
-       \r
+\r
     };\r
     STOPTIMER(Loading);\r
     PRINTTIME("Loading tag names struct", Loading);\r
@@ -266,7 +266,7 @@ XMLTree *XMLTree::Load(int fd, bool load_tc,int sample_factor)
     // loads the tag structure\r
     XML_Tree->Tags = static_sequence::load(fp);\r
     ufread(&XML_Tree->tags_blen,sizeof(uint),1,fp);\r
-    std::cerr << "tags_blen is "<< XML_Tree->tags_blen <<"\n";    \r
+    std::cerr << "tags_blen is "<< XML_Tree->tags_blen <<"\n";\r
     ufread(&XML_Tree->tags_len,sizeof(uint),1,fp);\r
     XML_Tree->tags_fix = new uint[uint_len(XML_Tree->tags_blen,XML_Tree->tags_len)];\r
     ufread(XML_Tree->tags_fix,sizeof(uint),uint_len(XML_Tree->tags_blen,XML_Tree->tags_len),fp);\r
@@ -280,14 +280,14 @@ XMLTree *XMLTree::Load(int fd, bool load_tc,int sample_factor)
     //XML_Tree->Tags->test(seq,XML_Tree->tags_len);\r
     //delete [] seq;\r
     /// End ugly tests\r
-    \r
+\r
     STOPTIMER(Loading);\r
     std::cerr << (uint_len(XML_Tree->tags_blen,XML_Tree->tags_len)*sizeof(uint))/(1024*1024) << " MB for tag sequence" << std::endl;\r
     PRINTTIME("Loading tag struct", Loading);\r
     STARTTIMER();\r
 \r
     // loads the flags\r
-    \r
+\r
     ufread(&(XML_Tree->disable_tc), sizeof(bool), 1, fp);\r
     if (load_tc) {\r
       XML_Tree->EBVector = static_bitsequence_rrr02::load(fp);\r
@@ -295,38 +295,38 @@ XMLTree *XMLTree::Load(int fd, bool load_tc,int sample_factor)
       STOPTIMER(Loading);\r
       PRINTTIME("Loading text bitvector struct", Loading);\r
       STARTTIMER();\r
-      \r
-      // Not used  \r
+\r
+      // Not used\r
       // loads the texts\r
       if (!XML_Tree->disable_tc){\r
        ufread(&(XML_Tree->text_index_type),\r
               sizeof(TextCollectionBuilder::index_type_t), 1, fp);\r
-       const char * pref;\r
-       switch (!XML_Tree->text_index_type){\r
+       string file(name);\r
+       switch (XML_Tree->text_index_type){\r
        case TextCollectionBuilder::index_type_default:\r
-         pref = "default_";\r
+         file.append(".default");\r
          break;\r
        case TextCollectionBuilder::index_type_swcsa:\r
-         pref = "swcsa_";\r
+         file.append(".swcsa");\r
          break;\r
        case TextCollectionBuilder::index_type_rlcsa:\r
-         pref = "rlcsa_";\r
+         file.append(".rlcsa");\r
          break;\r
-       };      \r
-        XML_Tree->Text = TextCollection::Load(fp, pref, TextCollection::index_mode_default, sample_factor);\r
+       };\r
+        XML_Tree->Text = TextCollection::Load(fp, file.c_str(), TextCollection::index_mode_default, sample_factor);\r
 \r
       }\r
       else XML_Tree->Text = NULL;\r
       STOPTIMER(Loading);\r
       PRINTTIME("Loading TextCollection", Loading);\r
-      STARTTIMER(); \r
+      STARTTIMER();\r
     }\r
     else {\r
       XML_Tree->EBVector = NULL;\r
       XML_Tree->Text = NULL;\r
       XML_Tree->disable_tc = true;\r
     };\r
-    \r
+\r
 \r
     return XML_Tree;\r
  }\r
@@ -334,27 +334,27 @@ XMLTree *XMLTree::Load(int fd, bool load_tc,int sample_factor)
 \r
 \r
 // SubtreeSize(x): the number of nodes (and attributes) in the subtree of node x.\r
-/*int XMLTree::SubtreeSize(treeNode x) \r
+/*int XMLTree::SubtreeSize(treeNode x)\r
  {\r
     return subtree_size(Par, x);\r
  }\r
 */\r
 // SubtreeTags(x,tag): the number of occurrences of tag within the subtree of node x.\r
 /*\r
-int XMLTree::SubtreeTags(treeNode x, TagType tag) \r
+int XMLTree::SubtreeTags(treeNode x, TagType tag)\r
  {\r
     if (x == Root())\r
       x = fast_first_child(Par,x);\r
-    \r
+\r
 \r
     int s = x + 2*subtree_size(Par, x) - 1;\r
\r
+\r
     return (Tags->rank(tag, s) - Tags->rank(tag, node2tagpos(x)-1))+1;\r
  }\r
 */\r
-int XMLTree::SubtreeElements(treeNode x) \r
+int XMLTree::SubtreeElements(treeNode x)\r
  {\r
-    \r
+\r
     int size = subtree_size(Par,x);\r
     if (x == Root()){\r
       x = fast_first_child(Par,x);\r
@@ -370,25 +370,25 @@ int XMLTree::SubtreeElements(treeNode x)
       size -= SubtreeSize(y);\r
       y = Tags->select_next(ATTRIBUTE_TAG_ID,node2tagpos(y));\r
     };\r
-    return size;    \r
+    return size;\r
  }\r
 \r
 // IsLeaf(x): returns whether node x is leaf or not. In the succinct representation\r
 // this is just a bit inspection.\r
-bool XMLTree::IsLeaf(treeNode x) \r
+bool XMLTree::IsLeaf(treeNode x)\r
  {\r
    NULLT_IF(x==NULLT);\r
    return fast_isleaf(Par, x);\r
- } \r
+ }\r
 \r
 // IsAncestor(x,y): returns whether node x is ancestor of node y.\r
-bool XMLTree::IsAncestor(treeNode x, treeNode y) \r
+bool XMLTree::IsAncestor(treeNode x, treeNode y)\r
  {\r
     return fast_is_ancestor(Par, x, y);\r
  }\r
 \r
 // IsChild(x,y): returns whether node x is parent of node y.\r
-bool XMLTree::IsChild(treeNode x, treeNode y) \r
+bool XMLTree::IsChild(treeNode x, treeNode y)\r
  {\r
     if (!fast_is_ancestor(Par, x, y)) return false;\r
     return depth(Par, x) == (depth(Par, y) + 1);\r
@@ -403,46 +403,46 @@ bool XMLTree::IsChild(treeNode x, treeNode y)
 \r
 // NumChildren(x): number of children of node x. Constant time with the data structure\r
 // of Sadakane.\r
-int XMLTree::NumChildren(treeNode x) \r
+int XMLTree::NumChildren(treeNode x)\r
  {\r
     return degree(Par, x);\r
  }\r
 \r
 // ChildNumber(x): returns i if node x is the i-th children of its parent.\r
-int XMLTree::ChildNumber(treeNode x) \r
+int XMLTree::ChildNumber(treeNode x)\r
  {\r
     return child_rank(Par, x);\r
  }\r
 \r
 // Depth(x): depth of node x, a simple binary rank on the parentheses sequence.\r
-int XMLTree::Depth(treeNode x) \r
+int XMLTree::Depth(treeNode x)\r
  {\r
     return depth(Par, x);\r
  }\r
 \r
 // Preorder(x): returns the preorder number of node x, just counting the tree\r
 // nodes (i.e., tags, it disregards the texts in the tree).\r
-int XMLTree::Preorder(treeNode x) \r
+int XMLTree::Preorder(treeNode x)\r
  {\r
     return preorder_rank(Par, x);\r
  }\r
 \r
 // Postorder(x): returns the postorder number of node x, just counting the tree\r
 // nodes (i.e., tags, it disregards the texts in the tree).\r
-int XMLTree::Postorder(treeNode x) \r
+int XMLTree::Postorder(treeNode x)\r
  {\r
     return postorder_rank(Par, x);\r
  }\r
 /*\r
 // Tag(x): returns the tag identifier of node x.\r
-TagType XMLTree::Tag(treeNode x) \r
+TagType XMLTree::Tag(treeNode x)\r
  {\r
     return fast_get_field(tags_fix,tags_blen,node2tagpos(x));\r
  }\r
 */\r
 // DocIds(x): returns the range of text identifiers that descend from node x.\r
 // returns {NULLT, NULLT} when there are no texts descending from x.\r
-range XMLTree::DocIds(treeNode x) \r
+range XMLTree::DocIds(treeNode x)\r
  {\r
    range r;\r
    if (x == NULLT) {\r
@@ -450,8 +450,8 @@ range XMLTree::DocIds(treeNode x)
      r.max = NULLT;\r
      return r;\r
    };\r
-   int min = EBVector->rank1(x-1);                          \r
-   int max = EBVector->rank1(x+2*subtree_size(Par, x)-2); \r
+   int min = EBVector->rank1(x-1);\r
+   int max = EBVector->rank1(x+2*subtree_size(Par, x)-2);\r
    if (min==max) { // range is empty, no texts within the subtree of x\r
      r.min = NULLT;\r
      r.max = NULLT;\r
@@ -465,7 +465,7 @@ range XMLTree::DocIds(treeNode x)
 \r
 // Parent(x): returns the parent node of node x.\r
 /*\r
-treeNode XMLTree::Parent(treeNode x) \r
+treeNode XMLTree::Parent(treeNode x)\r
  {\r
     if (x == Root())\r
       return NULLT;\r
@@ -474,7 +474,7 @@ treeNode XMLTree::Parent(treeNode x)
  }*/\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
+treeNode XMLTree::Child(treeNode x, int i)\r
 {\r
     if (i <= OPTD) return naive_child(Par, x, i);\r
     else return child(Par, x, i);\r
@@ -482,46 +482,46 @@ treeNode XMLTree::Child(treeNode x, int i)
 \r
 // FirstChild(x): returns the first child of node x, assuming it exists. Very fast in BP.\r
 /*\r
-treeNode XMLTree::FirstChild(treeNode x) \r
+treeNode XMLTree::FirstChild(treeNode x)\r
  {\r
    NULLT_IF(x==NULLT);\r
    return fast_first_child(Par, x);\r
  }\r
 */\r
 /*\r
-treeNode XMLTree::FirstElement(treeNode x) \r
+treeNode XMLTree::FirstElement(treeNode x)\r
  {\r
    NULLT_IF(x==NULLT);\r
    x = fast_first_child(Par, x);\r
    NULLT_IF(x == NULLT);\r
    switch (Tag(x)){\r
-  \r
+\r
    case PCDATA_TAG_ID:\r
      x = x+2;\r
      return (fast_inspect(Par,x)==OP)? x : NULLT;\r
-     \r
-   case ATTRIBUTE_TAG_ID:  \r
+\r
+   case ATTRIBUTE_TAG_ID:\r
      x = fast_next_sibling(Par,x);\r
      if (x != NULLT && Tag(x) == PCDATA_TAG_ID){\r
        x = x+2;\r
        return (fast_inspect(Par,x)==OP)? x : NULLT;\r
-     } \r
-     else return x;     \r
+     }\r
+     else return x;\r
    default:\r
      return x;\r
    }\r
  }\r
 *//*\r
-treeNode XMLTree::NextElement(treeNode x) \r
+treeNode XMLTree::NextElement(treeNode x)\r
 {\r
   NULLT_IF(x==NULLT);\r
   x = fast_next_sibling(Par, x);\r
-  NULLT_IF(x == NULLT);   \r
+  NULLT_IF(x == NULLT);\r
   if (Tag(x) == PCDATA_TAG_ID){\r
     x = x+2;\r
      return (fast_inspect(Par,x)==OP)? x : NULLT;\r
   }\r
-  else return x;  \r
+  else return x;\r
   }*/\r
 \r
 // LastChild(x): returns the last child of node x.\r
@@ -532,7 +532,7 @@ treeNode XMLTree::NextElement(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
@@ -541,7 +541,7 @@ treeNode XMLTree::NextElement(treeNode x)
 */\r
 \r
 // PrevSibling(x): returns the previous sibling of node x, assuming it exists.\r
-/*treeNode XMLTree::PrevSibling(treeNode x) \r
+/*treeNode XMLTree::PrevSibling(treeNode x)\r
  {\r
    NULLT_IF(x==NULLT);\r
    return prev_sibling(Par, x);\r
@@ -551,11 +551,11 @@ treeNode XMLTree::NextElement(treeNode x)
 // Because of the balanced-parentheses representation of the tree, this operation is not supported\r
 // efficiently, just iterating among the children of node x until finding the desired child.\r
 /*\r
-treeNode XMLTree::TaggedChild(treeNode x, TagType tag) \r
+treeNode XMLTree::TaggedChild(treeNode x, TagType tag)\r
  {\r
-   \r
+\r
    NULLT_IF(x==NULLT || fast_isleaf(Par,x));\r
-   treeNode child;   \r
+   treeNode child;\r
    child = fast_first_child(Par, x); // starts at first child of node x\r
    if (Tag(child) == tag)\r
      return child;\r
@@ -572,25 +572,25 @@ treeNode XMLTree::TaggedFollowingSibling(treeNode x, TagType tag)
   while (sibling != NULLT) {\r
     ctag = Tag(sibling);\r
     if (ctag == tag) // current sibling is labeled with tag of interest\r
-      return sibling; \r
+      return sibling;\r
     sibling = fast_sibling(Par, sibling, ctag); // OK, let's try with the next sibling\r
   }\r
-  return NULLT; // no such sibling was found   \r
+  return NULLT; // no such sibling was found\r
 }\r
 */\r
 treeNode XMLTree::SelectChild(treeNode x, TagIdSet *tags)\r
 {\r
-  \r
+\r
   NULLT_IF(x==NULLT || fast_isleaf(Par,x));\r
   int i;\r
-  treeNode child = fast_first_child(Par, x);  \r
+  treeNode child = fast_first_child(Par, x);\r
   TagType t;\r
   while (child != NULLT) {\r
     t = Tag(child);\r
     if (tags->find(t) != tags->end()) return child;\r
     child = fast_sibling(Par, child,t);\r
   }\r
-  return NULLT;  \r
+  return NULLT;\r
 }\r
 \r
 \r
@@ -606,14 +606,14 @@ treeNode XMLTree::SelectFollowingSibling(treeNode x, TagIdSet *tags)
      if (tags->find(t) != tags->end()) return sibling;\r
      sibling = fast_sibling(Par, sibling,t);\r
    }\r
-   return NULLT;    \r
+   return NULLT;\r
  }\r
 \r
 \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
+treeNode XMLTree::TaggedDescendant(treeNode x, TagType tag)\r
  {\r
    //NULLT_IF(x==NULLT || fast_isleaf(Par,x));\r
 \r
@@ -621,7 +621,7 @@ treeNode XMLTree::TaggedDescendant(treeNode x, TagType tag)
    NULLT_IF (s == -1);\r
 \r
    treeNode y = tagpos2node(s); // transforms the tag position into a node position\r
-   \r
+\r
    return (fast_is_ancestor(Par,x,y) ? y : NULLT);\r
  }\r
 */\r
@@ -647,8 +647,8 @@ treeNode XMLTree::SelectDescendant(treeNode x, TagIdSet *tags)
 \r
 // TaggedPrec(x,tag): returns the first node tagged tag with smaller preorder than x and not an\r
 // ancestor of x. Returns NULLT if there is none.\r
-treeNode XMLTree::TaggedPreceding(treeNode x, TagType tag) \r
- {    \r
+treeNode XMLTree::TaggedPreceding(treeNode x, TagType tag)\r
+ {\r
     int r, s;\r
     treeNode node_s, root;\r
     r = (int)Tags->rank(tag, node2tagpos(x)-1);\r
@@ -662,7 +662,7 @@ treeNode XMLTree::TaggedPreceding(treeNode x, TagType tag)
        s = (int)Tags->select(tag, r);  // we should use select_prev instead when provided\r
        node_s = tagpos2node(s);\r
     }\r
-    return NULLT; // there is no such node \r
+    return NULLT; // there is no such node\r
  }\r
 \r
 \r
@@ -670,37 +670,37 @@ treeNode XMLTree::TaggedPreceding(treeNode x, TagType tag)
 // the subtree of x. Returns NULLT if there is none.\r
 treeNode XMLTree::TaggedFollowing(treeNode x, TagType tag)\r
  {\r
-   NULLT_IF (x ==NULLT || x == Root());   \r
+   NULLT_IF (x ==NULLT || x == Root());\r
    return tagpos2node(Tags->select_next(tag,fast_find_close(Par, x)));\r
 \r
- } \r
+ }\r
 \r
-// TaggedFollBelow(x,tag,root): returns the first node tagged tag with larger preorder than x \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
+  // 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
   treeNode s = tagpos2node(Tags->select_next(tag, close));\r
-  \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
   NULLT_IF (x == NULLT || x == Root());\r
-  \r
-  treeNode s = tagpos2node(Tags->select_next(tag, fast_find_close(Par, x)));  \r
+\r
+  treeNode s = tagpos2node(Tags->select_next(tag, fast_find_close(Par, x)));\r
   NULLT_IF (s == NULLT || s >= closing);\r
-  \r
+\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
@@ -717,7 +717,7 @@ treeNode XMLTree::SelectFollowingBelow(treeNode x, TagIdSet *tags, treeNode ance
    int i;\r
    treeNode min = NULLT;\r
    treeNode aux;\r
-  \r
+\r
 \r
    TagIdSet::const_iterator tagit;\r
    for (tagit = tags->begin(); tagit != tags->end(); ++tagit) {\r
@@ -726,13 +726,13 @@ treeNode XMLTree::SelectFollowingBelow(treeNode x, TagIdSet *tags, treeNode ance
      if (aux == NULLT) continue;\r
      if ((min == NULLT) || (aux < min)) min = aux;\r
    };\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 == Root() || min == NULLT || min < fast_find_close(Par, ancestor)) return min;\r
    else return NULLT;\r
-   \r
+\r
  }\r
 /*\r
 treeNode XMLTree::SelectFollowingBefore(treeNode x, TagIdSet *tags, treeNode ancestor_closing)\r
@@ -748,7 +748,7 @@ treeNode XMLTree::SelectFollowingBefore(treeNode x, TagIdSet *tags, treeNode anc
    int i;\r
    treeNode min = NULLT;\r
    treeNode aux;\r
-  \r
+\r
 \r
    TagIdSet::const_iterator tagit;\r
    for (tagit = tags->begin(); tagit != tags->end(); ++tagit) {\r
@@ -757,13 +757,13 @@ treeNode XMLTree::SelectFollowingBefore(treeNode x, TagIdSet *tags, treeNode anc
      if (aux == NULLT) continue;\r
      if ((min == NULLT) || (aux < min)) min = aux;\r
    };\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
 /*\r
@@ -780,33 +780,33 @@ treeNode XMLTree::SelectFollowingBefore(treeNode x, TagIdSet *tags, treeNode clo
    for (tagit = tags->begin(); tagit != tags->end(); tagit++) {\r
 \r
      aux = tagpos2node(Tags->select_next(*tagit, close));\r
-     \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
-     \r
+\r
      if (aux == ns ) return ns;\r
      if (aux == NULLT) continue;\r
      if ((min == NULLT) || (aux < min)) min = aux;\r
    };\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
    NULLT_IF (min == NULLT || min >= closing);\r
-   \r
+\r
    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
 treeNode XMLTree::TaggedAncestor(treeNode x, TagType tag)\r
- {    \r
+ {\r
     if (x == NULLT || x == Root())\r
        return NULLT;\r
-    \r
+\r
     treeNode s = parent(Par, x), r = Root();\r
     while (s != r) {\r
       if (Tag(s) == tag) return s;\r
@@ -817,42 +817,42 @@ treeNode XMLTree::TaggedAncestor(treeNode x, TagType tag)
 \r
 \r
 \r
-// MyText(x): returns the document identifier of the text below node x, \r
-// or NULLT if x is not a leaf node or the text is empty. Assumes Doc \r
+// MyText(x): returns the document identifier of the text below node x,\r
+// or NULLT if x is not a leaf node or the text is empty. Assumes Doc\r
 // ids start from 0.\r
-DocID XMLTree::MyText(treeNode x) \r
+DocID XMLTree::MyText(treeNode x)\r
 {\r
   TagType tag = Tag(x);\r
   // seems faster than testing EBVector->access(x);\r
 \r
   if (tag == PCDATA_TAG_ID || tag == ATTRIBUTE_DATA_TAG_ID)\r
-    return (DocID) (EBVector->rank1(x)-1);  \r
-  else   \r
+    return (DocID) (EBVector->rank1(x)-1);\r
+  else\r
     return (DocID) NULLT;\r
-    \r
+\r
 }\r
-// MyText(x): returns the document identifier of the text below node x, \r
-// or NULLT if x is not a leaf node or the text is empty. Assumes Doc \r
+// MyText(x): returns the document identifier of the text below node x,\r
+// or NULLT if x is not a leaf node or the text is empty. Assumes Doc\r
 // ids start from 0.\r
-DocID XMLTree::MyTextUnsafe(treeNode x) \r
+DocID XMLTree::MyTextUnsafe(treeNode x)\r
 {\r
   return (DocID) (EBVector->rank1(x)-1); //-1 because document ids start from 0\r
-  \r
+\r
 }\r
 // TextXMLId(d): returns the preorder of document with identifier d in the tree consisting of\r
 // all tree nodes and all text nodes. Assumes that the tree root has preorder 1.\r
-int XMLTree::TextXMLId(DocID d) \r
+int XMLTree::TextXMLId(DocID d)\r
  {\r
    NULLT_IF(d == NULLT);\r
      int s = EBVector->select1(d+1);\r
    return rank_open(Par, s) + d + 1; // +1 because root has preorder 1\r
-   \r
+\r
  }\r
 \r
-// NodeXMLId(x): returns the preorder of node x in the tree consisting \r
+// NodeXMLId(x): returns the preorder of node x in the tree consisting\r
 // of all tree nodes and all text nodes. Assumes that the tree root has\r
 // preorder 0;\r
-int XMLTree::NodeXMLId(treeNode x) \r
+int XMLTree::NodeXMLId(treeNode x)\r
  {\r
    NULLT_IF(x == NULLT);\r
    if (x == Root()) return 1; // root node has preorder 1\r
@@ -860,21 +860,21 @@ int XMLTree::NodeXMLId(treeNode x)
  }\r
 \r
 // ParentNode(d): returns the parent node of document identifier d.\r
-treeNode XMLTree::ParentNode(DocID d) \r
- {    \r
-   NULLT_IF (d == NULLT);   \r
-   return (treeNode) EBVector->select1(d+1);     \r
+treeNode XMLTree::ParentNode(DocID d)\r
+ {\r
+   NULLT_IF (d == NULLT);\r
+   return (treeNode) EBVector->select1(d+1);\r
  }\r
 \r
 // GetTagId: returns the tag identifier corresponding to a given tag name.\r
 // Returns NULLT in case that the tag name does not exists.\r
 TagType XMLTree::GetTagId(unsigned char *tagname)\r
  {\r
-  \r
+\r
    string s = (char *) tagname;\r
-   TagIdMapIT it = tIdMap->find(s);    \r
+   TagIdMapIT it = tIdMap->find(s);\r
    return (TagType) ((it != tIdMap->end()) ? it->second : -1);\r
-    \r
+\r
  }\r
 \r
 \r
@@ -886,7 +886,7 @@ unsigned char *XMLTree::GetTagName(TagType tagid)
     if ( tagid < 0 || tagid >= TagName->size())\r
       return (unsigned char *) "<INVALID TAG>";\r
     strcpy((char *)s, (*TagName)[tagid].c_str());\r
-    \r
+\r
     return (s == NULL ? (unsigned char*) "<INVALID TAG>" : s);\r
  }\r
 \r
@@ -897,27 +897,27 @@ const unsigned char *XMLTree::GetTagNameByRef(TagType tagid)
    unsigned char *s;\r
    if ( tagid < 0 || tagid >= TagName->size())\r
      return (unsigned char *) "<INVALID TAG>";\r
-   \r
+\r
    return (const unsigned char *) (*TagName)[tagid].c_str();\r
-   \r
+\r
  }\r
 \r
 \r
 \r
 TagType XMLTree::RegisterTag(unsigned char *tagname)\r
- {  \r
+ {\r
     TagType id = XMLTree::GetTagId(tagname);\r
     if (id == NULLT) {\r
-      string s = (char *) tagname;      \r
-      REGISTER_TAG(TagName,tIdMap,s);      \r
+      string s = (char *) tagname;\r
+      REGISTER_TAG(TagName,tIdMap,s);\r
     };\r
-    \r
+\r
     return id;\r
  }\r
 \r
 \r
 treeNode XMLTree::Closing(treeNode x) {\r
-  return fast_find_close(Par,x); \r
+  return fast_find_close(Par,x);\r
 }\r
 bool XMLTree::IsOpen(treeNode x) { return fast_inspect(Par,x); }\r
 \r
@@ -925,8 +925,8 @@ bool XMLTree::IsOpen(treeNode x) { return fast_inspect(Par,x); }
 \r
 \r
 void XMLTree::Print(int fd,treeNode x, bool no_text){\r
-  \r
-  if (buffer == 0) { \r
+\r
+  if (buffer == 0) {\r
     buffer = new string(BUFFER_ALLOC, 0);\r
     buffer->clear();\r
     print_stack = new std::vector<string *>();\r
@@ -936,30 +936,30 @@ void XMLTree::Print(int fd,treeNode x, bool no_text){
   treeNode fin = fast_find_close(Par,x);\r
   treeNode n = x;\r
   TagType tag = Tag(n);\r
-  \r
+\r
   range r = DocIds(x);\r
   treeNode first_idx;\r
   treeNode first_text = (tag == PCDATA_TAG_ID ?  x : ParentNode(r.min-1));\r
   treeNode first_att =  NULLT;\r
-  \r
-  if (first_att  == NULLT) \r
+\r
+  if (first_att  == NULLT)\r
     first_idx = first_text;\r
   else if (first_text == NULLT)\r
     first_idx = first_att;\r
   else\r
     first_idx = min(first_att,first_text);\r
-   \r
+\r
    uchar * current_text=NULL;\r
 \r
    if (first_idx != NULLT)\r
      current_text = GetText(MyTextUnsafe(first_idx));\r
-  \r
+\r
    size_t read = 0;\r
 \r
    while (n <= fin){\r
      if (fast_inspect(Par,n)){\r
-       if (tag == PCDATA_TAG_ID) {       \r
-        \r
+       if (tag == PCDATA_TAG_ID) {\r
+\r
         if (no_text)\r
           _dputs("<$/>", fd);\r
         else {\r
@@ -968,10 +968,10 @@ void XMLTree::Print(int fd,treeNode x, bool no_text){
         };\r
         n+=2; // skip closing $\r
         tag = Tag(n);\r
-        \r
+\r
        } else {\r
 \r
-        _dputc('<',fd);         \r
+        _dputc('<',fd);\r
         _dput_str((*TagName)[tag], fd);\r
         n++;\r
         if (fast_inspect(Par,n)) {\r
@@ -1005,15 +1005,15 @@ void XMLTree::Print(int fd,treeNode x, bool no_text){
             else _dputc('>', fd);\r
             n++;\r
             tag=Tag(n);\r
-          \r
-          } else \r
+\r
+          } else\r
             _dputc('>', fd);\r
-          \r
+\r
         } else {// <foo /> tag\r
           _dputs("/>", fd);\r
           n++;\r
-          tag=Tag(n);   \r
-        };     \r
+          tag=Tag(n);\r
+        };\r
        };\r
      } else do {\r
         _dputs("</", fd);\r
index 4673ccc..676c214 100644 (file)
--- a/XMLTree.h
+++ b/XMLTree.h
@@ -49,8 +49,8 @@ using SXSI::TextCollectionBuilder;
 
 
 typedef int treeNode;
-typedef int TagType; 
-typedef int DocID;  
+typedef int TagType;
+typedef int DocID;
 
 typedef struct {
    int min;
@@ -98,11 +98,25 @@ typedef TagIdMap::const_iterator TagIdMapIT;
 
 #define BUFFER_ALLOC (8192 * 2)
 #define BUFFER_SIZE (BUFFER_ALLOC / 2)
+static inline int fast_find_open(bp *b,int s)
+{
+  int r;
+  r = bwd_excess(b,s,0);
+  if (r >= -1) return r+1;
+  return -1;
+}
+
 static inline int fast_find_close(bp *b,int s)
 {
   return fwd_excess(b,s,-1);
 }
 
+static inline int fast_find_parent_close(bp *b,int s)
+{
+  return fwd_excess(b,s,-2);
+}
+
+
 static inline int fast_inspect(bp* Par,treeNode i)
 {
   int j,l;
@@ -132,12 +146,12 @@ inline static bool fast_is_ancestor(bp * Par,treeNode x,treeNode y){
 }
 
 // tag position -> tree node
-static treeNode tagpos2node(int t) 
+static treeNode tagpos2node(int t)
  {
     return (treeNode) t;
  }
 // tree node -> tag position
-static int node2tagpos(treeNode x) 
+static int node2tagpos(treeNode x)
 {
   return (int)x;
 }
@@ -153,13 +167,13 @@ class XMLTree {
  private:
    /** Balanced parentheses representation of the tree */
    bp *Par;
-   /** Mapping from tag identifer to tag name */  
+
+   /** Mapping from tag identifer to tag name */
    std::vector<std::string> *TagName;
    TagIdMap * tIdMap;
-  
+
    /** Bit vector indicating with a 1 the positions of the non-empty texts. */
-   static_bitsequence *EBVector;  
+   static_bitsequence *EBVector;
 
    /** Tag sequence represented with a data structure for rank and select */
    static_sequence *Tags;
@@ -172,21 +186,28 @@ class XMLTree {
    // Allows to disable the TextCollection for benchmarkin purposes
    bool disable_tc;
    SXSI::TextCollectionBuilder::index_type_t text_index_type;
-   
+
    std::string *buffer;
    std::vector<std::string *> *print_stack;
 
+
+   void _real_flush(int fd, size_t size) {
+     if (size == 0) return;
+     size_t written;
+     while (1) {
+       written = write(fd, buffer->data(), size);
+                  if ((written < 0) && (errno == EAGAIN || errno == EINTR))
+                    continue;
+                  break;
+     };
+     buffer->clear();
+
+   }
+
    void _flush(int fd){
           size_t size = buffer->size();
           if (size < BUFFER_SIZE) return;
-          size_t written;
-          while (1) {
-                  written = write(fd, buffer->data(), size);
-                  if ((written < 0) && (errno == EAGAIN || errno == EINTR))
-                          continue;
-                  break;
-          };      
-          buffer->clear();
+          _real_flush(fd, size);
    }
 
    void _dput_str(std::string s, int fd){
@@ -196,7 +217,7 @@ class XMLTree {
 
    void _dputs(const char* s, int fd){
      buffer->append(s);
-     _flush(fd);     
+     _flush(fd);
    }
 
    void _dputc(const char c, int fd){
@@ -245,10 +266,10 @@ class XMLTree {
            TextCollection * const TC, bool dis_tc,
            TextCollectionBuilder::index_type_t _index_type );
 
-public: 
+public:
    /** Data structure destructor */
    ~XMLTree();
-   
+
    /** root(): returns the tree root. */
    treeNode Root() { return 0; }
 
@@ -268,16 +289,16 @@ public:
    unsigned int * TagStruct() { return tags_fix; };
 
 
-   /** SubtreeSize(x): the number of nodes (and attributes) in the subtree of 
+   /** SubtreeSize(x): the number of nodes (and attributes) in the subtree of
     * node x. */
    int SubtreeSize(treeNode x) { return subtree_size(Par, x); }
-  
-   /** SubtreeTags(x,tag): the number of occurrences of tag within the subtree 
+
+   /** SubtreeTags(x,tag): the number of occurrences of tag within the subtree
     * of node x. */
    int SubtreeTags(treeNode x, TagType tag){
           //int s = x + 2*subtree_size(Par, x) - 1;
           treeNode y = fast_find_close(Par, x);
-          
+
           if (y - x < 10) {
                   int count = 0;
                   for(int i = x; i < y; i++)
@@ -287,12 +308,12 @@ public:
           else
                   return (Tags->rank(tag, y) - Tags->rank(tag, x));
    };
-   
+
    /** SubtreeElements(x) of element nodes in the subtree of x
     */
    int SubtreeElements(treeNode x);
 
-   /** IsLeaf(x): returns whether node x is leaf or not. In the succinct 
+   /** IsLeaf(x): returns whether node x is leaf or not. In the succinct
     * representation this is just a bit inspection. */
 
    bool IsLeaf(treeNode x);
@@ -300,43 +321,54 @@ public:
    /** IsAncestor(x,y): returns whether node x is ancestor of node y. */
 
    bool IsAncestor(treeNode x, treeNode y);
-  
+
+
+   /** IsRigthDescendant returns true if y is a descendant of x and y is
+       not a descendant of the first child of x */
+   bool IsRightDescendant(treeNode x, treeNode y) {
+     if (x <= Root()) return false;
+     treeNode z = fast_find_parent_close(Par, x);
+     treeNode c = fast_find_close(Par, x);
+     return (y > c && y < z );
+   }
+
+
    /** IsChild(x,y): returns whether node x is parent of node y. */
    bool IsChild(treeNode x, treeNode y);
 
    /** IsFirstChild(x): returns whether node x is the first child of its parent. */
    /* OCAML */
-   bool IsFirstChild(treeNode x) { 
+   bool IsFirstChild(treeNode x) {
           return ((x != NULLT)&&(x==Root() || prev_sibling(Par,x) == (treeNode)-1));
    };
-     
-   /** NumChildren(x): number of children of node x. Constant time with the 
+
+   /** NumChildren(x): number of children of node x. Constant time with the
     * data structure of Sadakane. */
    int NumChildren(treeNode x);
 
-   /** ChildNumber(x): returns i if node x is the i-th children of its 
+   /** ChildNumber(x): returns i if node x is the i-th children of its
     * parent. */
    int ChildNumber(treeNode x);
 
-   /** Depth(x): depth of node x, a simple binary rank on the parentheses 
+   /** Depth(x): depth of node x, a simple binary rank on the parentheses
     * sequence. */
    int Depth(treeNode x);
-   
-   /** Preorder(x): returns the preorder number of node x, just regarding tree 
-    * nodes (and not texts). */ 
+
+   /** Preorder(x): returns the preorder number of node x, just regarding tree
+    * nodes (and not texts). */
    int Preorder(treeNode x);
-   
-   /** Postorder(x): returns the postorder number of node x, just regarding 
+
+   /** Postorder(x): returns the postorder number of node x, just regarding
     * tree nodes (and not texts). */
    int Postorder(treeNode x);
-      
 
-   /** DocIds(x): returns the range (i.e., a pair of integers) of document 
+
+   /** DocIds(x): returns the range (i.e., a pair of integers) of document
     * identifiers that descend from node x. */
    range DocIds(treeNode x);
 
    /** Parent(x): returns the parent node of node x. */
-   treeNode Parent(treeNode x) {          
+   treeNode Parent(treeNode x) {
     if (x == Root())
       return NULLT;
     else
@@ -344,17 +376,17 @@ public:
    };
 
    treeNode BinaryParent(treeNode x){
-          if (x <= Root())
-                  return NULLT;
-          else {
-                  treeNode prev = x - 1;
-                  return (fast_inspect(Par, prev) == OP) ? prev : find_open(Par, prev);
-          };
+     if (x <= Root())
+       return NULLT;
+     else {
+       treeNode prev = x - 1;
+       return (fast_inspect(Par, prev) == OP) ? prev : fast_find_open(Par, prev);
+     };
    };
 
    /* Assumes x is neither 0 nor -1 */
-   
-   /** Child(x,i): returns the i-th child of node x, assuming it exists. */   
+
+   /** Child(x,i): returns the i-th child of node x, assuming it exists. */
    treeNode Child(treeNode x, int i);
 
 
@@ -362,10 +394,10 @@ public:
    /** LastChild(x): returns the last child of node x.  */
    treeNode LastChild(treeNode x) {
           NULLT_IF(x == NULLT || fast_isleaf(Par,x));
-          return find_open(Par, fast_find_close(Par, x)-1);
+          return fast_find_open(Par, fast_find_close(Par, x)-1);
    }
-   
-   /** PrevSibling(x): returns the previous sibling of node x, assuming it 
+
+   /** PrevSibling(x): returns the previous sibling of node x, assuming it
     * exists. */
 
    treeNode PrevSibling(treeNode x)
@@ -374,18 +406,18 @@ public:
           return prev_sibling(Par, x);
    }
 
-   
-   /** TaggedChild(x,tag): returns the first child of node x tagged tag, or 
-    * NULLT if there is none. Because of the balanced-parentheses representation 
-    * of the tree, this operation is not supported efficiently, just iterating 
+
+   /** TaggedChild(x,tag): returns the first child of node x tagged tag, or
+    * NULLT if there is none. Because of the balanced-parentheses representation
+    * of the tree, this operation is not supported efficiently, just iterating
     * among the children of node x until finding the desired child. */
 
-   
+
    treeNode SelectChild(treeNode x, TagIdSet * tags);
 
-   /** TaggedFollowingSibling(x,tag): returns the first sibling of node x tagged tag, or 
+   /** TaggedFollowingSibling(x,tag): returns the first sibling of node x tagged tag, or
     *  NULLT if there is none. */
-   
+
    treeNode SelectFollowingSibling(treeNode x, TagIdSet * tags);
 
 
@@ -400,19 +432,19 @@ public:
 
    TagIdSet::const_iterator tagit;
    for (tagit = tags->begin(); tagit != tags->end(); ++tagit) {
-          aux = TaggedDescendant(x, (TagType) *tagit);    
+          aux = TaggedDescendant(x, (TagType) *tagit);
           if (((unsigned int) aux) < ((unsigned int) min)) min = aux;
    };
    return min;
  }
 
-   /** TaggedPrec(x,tag): returns the first node tagged tag with smaller 
-    * preorder than x and not an ancestor of x. Returns NULLT if there 
+   /** TaggedPrec(x,tag): returns the first node tagged tag with smaller
+    * preorder than x and not an ancestor of x. Returns NULLT if there
     * is none. */
    treeNode TaggedPreceding(treeNode x, TagType tag);
-  
-   /** TaggedFoll(x,tag): returns the first node tagged tag with larger 
-    * preorder than x and not in the subtree of x. Returns NULLT if there 
+
+   /** TaggedFoll(x,tag): returns the first node tagged tag with larger
+    * preorder than x and not in the subtree of x. Returns NULLT if there
     * is none. */
    treeNode TaggedFollowing(treeNode x, TagType tag);
 
@@ -429,63 +461,63 @@ public:
 
    treeNode close = fast_find_close(Par,x);
 
-  
+
    treeNode min = NULLT;
    treeNode aux;
-  
+
 
    TagIdSet::const_iterator tagit;
    for (tagit = tags->begin(); tagit != tags->end(); ++tagit) {
 
-          aux = tagpos2node(Tags->select_next(*tagit, close));    
+          aux = tagpos2node(Tags->select_next(*tagit, close));
 
           if (((unsigned int) aux) < ((unsigned int) min)) min = aux;
    };
-     
+
 
    return (min < ancestor_closing) ? min : NULLT;
-   
+
  }
 
-   /** TaggedAncestor(x, tag): returns the closest ancestor of x tagged 
+   /** TaggedAncestor(x, tag): returns the closest ancestor of x tagged
      * tag. Return NULLT is there is none. */
    treeNode TaggedAncestor(treeNode x, TagType tag);
-   /** PrevText(x): returns the document identifier of the text to the left of 
+
+   /** PrevText(x): returns the document identifier of the text to the left of
     * node x, or NULLT if x is the root node. */
    DocID PrevText(treeNode x);
-   
-   /** NextText(x): returns the document identifier of the text to the right of 
+
+   /** NextText(x): returns the document identifier of the text to the right of
     * node x, or NULLT if x is the root node. */
    DocID NextText(treeNode x);
-   
-   /** MyText(x): returns the document identifier of the text below node x, or 
+
+   /** MyText(x): returns the document identifier of the text below node x, or
     * NULLT if x is not a leaf node. */
    DocID MyText(treeNode x);
    DocID MyTextUnsafe(treeNode x);
 
-   /** TextXMLId(d): returns the preorder of document with identifier d in the 
+   /** TextXMLId(d): returns the preorder of document with identifier d in the
     * tree consisting of all tree nodes and all text nodes. */
    int TextXMLId(DocID d);
-   
-   /** NodeXMLId(x): returns the preorder of node x in the tree consisting of 
+
+   /** NodeXMLId(x): returns the preorder of node x in the tree consisting of
     * all tree nodes and all text nodes. */
    int NodeXMLId(treeNode x);
-   
+
    /** ParentNode(d): returns the parent node of document identifier d. */
    treeNode ParentNode(DocID d);
-   
+
    treeNode PrevNode(DocID d);
 
-   /** GetTagId(tagname): returns the tag identifier corresponding to a given 
+   /** GetTagId(tagname): returns the tag identifier corresponding to a given
     * tag name. Returns NULLT in case that the tag name does not exists. */
    TagType GetTagId(unsigned char *tagname);
 
-   /** GetTagName(tagid): returns the tag name of a given tag identifier. 
+   /** GetTagName(tagid): returns the tag name of a given tag identifier.
     * Returns NULL in case that the tag identifier is not valid.*/
    unsigned char *GetTagName(TagType tagid);
 
-   /** GetTagName(tagid): returns the tag name of a given tag identifier.     
+   /** GetTagName(tagid): returns the tag name of a given tag identifier.
     *  The result is just a reference and should not be freed by the caller.
     */
    const unsigned char *GetTagNameByRef(TagType tagid);
@@ -527,35 +559,35 @@ public:
    TextCollection::document_result LessThan(uchar const *s) {
       return Text->LessThan(s);
    }
-   
+
    /** IsPrefix(x): returns true if there is a text prefixed by string s. */
    bool IsPrefix(uchar const *s) {
       return Text->IsPrefix(s);
-   }          
-   
-   /** IsSuffix(s): returns true if there is a text having string s as a 
+   }
+
+   /** IsSuffix(s): returns true if there is a text having string s as a
     * suffix.*/
    bool IsSuffix(uchar const *s) {
       return Text->IsSuffix(s);
    }
-   
-   /** IsEqual(s): returns true if there is a text that equals given 
+
+   /** IsEqual(s): returns true if there is a text that equals given
     * string s. */
    bool IsEqual(uchar const *s) {
       return Text->IsEqual(s);
    }
-   
+
    /** IsContains(s): returns true if there is a text containing string s. */
    bool IsContains(uchar const *s) {
       return Text->IsContains(s);
    }
-   
-   /** IsLessThan(s): returns true if there is at least a text that is 
+
+   /** IsLessThan(s): returns true if there is at least a text that is
     * lexicographically smaller than string s. */
    bool IsLessThan(uchar const *s) {
       return Text->IsLessThan(s);
    }
-   
+
    /** Count(s): Global counting  */
    unsigned Count(uchar const *s) {
       return Text->Count(s);
@@ -565,31 +597,31 @@ public:
    unsigned CountPrefix(uchar const *s) {
       return Text->CountPrefix(s);
    }
-   
+
    /** CountSuffix(s): counting version of Suffix(s). */
    unsigned CountSuffix(uchar const *s) {
       return Text->CountSuffix(s);
    }
-   
+
    /** CountEqual(s): counting version of Equal(s). */
    unsigned CountEqual(uchar const *s) {
       return Text->CountEqual(s);
    }
-   
+
    /** CountContains(s): counting version of Contains(s). */
    unsigned CountContains(uchar const *s) {
       return Text->CountContains(s);
    }
-   
+
    /** CountLessThan(s): counting version of LessThan(s). */
    unsigned CountLessThan(uchar const *s) {
       return Text->CountLessThan(s);
    }
-   
+
    /** GetText(d): returns the text corresponding to document with
     * id d. */
    uchar* GetText(DocID d) {
-     
+
        uchar * s = Text->GetText(d);
        return (s[0] == 1 ? (s+1) : s);
    }
@@ -604,19 +636,19 @@ public:
    TextCollection *getTextCollection() {
       return Text;
    }
-   
+
    /** Save: saves XML tree data structure to file. */
-   void Save(int fd );
-      
-   /** Load: loads XML tree data structure from file. sample_rate_text 
+   void Save(int fd, char* name );
+
+   /** Load: loads XML tree data structure from file. sample_rate_text
     * indicates the sample rate for the text search data structure. */
-   static XMLTree *Load(int fd, bool load_tc, int sample_factor);   
+   static XMLTree *Load(int fd, bool load_tc, int sample_factor, char * name);
 
    void insertTag(TagType tag, uint position);
-   
+
    void print_stats();
 
-   
+
    /** Parenthesis functions */
    treeNode Closing(treeNode x);
 
@@ -626,7 +658,7 @@ public:
    /** Print procedure */
    void Print(int fd,treeNode x, bool no_text);
    void Print(int fd,treeNode x) { Print(fd,x,false); }
-   void Flush(int fd){ _flush(fd); }
+   void Flush(int fd){ if (buffer) _real_flush(fd, buffer->size()); }
 
   // The following are inlined here for speed
   /** Tag(x): returns the tag identifier of node x. */
@@ -634,13 +666,13 @@ public:
    inline TagType Tag(treeNode x) const throw () {
          if (tags_blen == 8)
                  return  (TagType) (((uchar*)tags_fix)[(int) x]);
-         else 
+         else
                  return get_field(tags_fix, tags_blen, x);
                  /*
-                 { 
+                 {
          size_t idxlen = x * tags_blen;
          size_t j = idxlen % W;
-         size_t i = idxlen / W; 
+         size_t i = idxlen / W;
          size_t offset = W - tags_blen;
          size_t offset2 = offset - j;
          size_t w = tags_fix[i];
@@ -669,28 +701,28 @@ public:
        NULLT_IF(x == NULLT);
        switch (Tag(x)){
 
-       case ATTRIBUTE_TAG_ID:  
+       case ATTRIBUTE_TAG_ID:
               x = fast_next_sibling(Par,x);
               if (x == NULLT || Tag(x) != PCDATA_TAG_ID) return x;
-        
+
        case PCDATA_TAG_ID:
               x = x+2;
               return (fast_inspect(Par,x)==OP)? x : NULLT;
-              
+
        default:
               return x;
        }
      }
    };
 
-  /** NextSibling(x): returns the next sibling of node x, or NULLT if none 
+  /** NextSibling(x): returns the next sibling of node x, or NULLT if none
    * exists. */
-  
+
   treeNode NextSibling(treeNode x) {
     NULLT_IF (x <= 0);
     return fast_next_sibling(Par, x);
   };
-  
+
    /** NextElement(x): returns the first non text, non attribute sibling of node x, or NULLT
     *    if none.
     */
@@ -698,32 +730,32 @@ public:
   {
     NULLT_IF(x <= 0);
     x = fast_next_sibling(Par, x);
-    NULLT_IF(x == NULLT);   
+    NULLT_IF(x == NULLT);
     if (Tag(x) == PCDATA_TAG_ID){
       int y = x+2;
       return (fast_inspect(Par, y) == OP) ? y : NULLT;
     }
-    else return x;  
+    else return x;
   };
-     /** TaggedDesc(x,tag): returns the first node tagged tag with larger 
-    * preorder than x and within the subtree of x. Returns NULT if there 
+     /** TaggedDesc(x,tag): returns the first node tagged tag with larger
+    * preorder than x and within the subtree of x. Returns NULT if there
     * is none. */
   inline treeNode TaggedDescendant(treeNode x, TagType tag)
   {
-    
+
          int s = (int) Tags->select_next(tag,node2tagpos(x));
          NULLT_IF (s == -1);
-         
+
          treeNode y = tagpos2node(s); // transforms the tag position into a node position
-         
+
          return (fast_is_ancestor(Par,x,y) ? y : NULLT);
   };
-  
+
   inline treeNode TaggedFollowingBelow(treeNode x, TagType tag, treeNode ancestor)
   {
          treeNode close = fast_find_close(Par, x);
          treeNode s = tagpos2node(Tags->select_next(tag, close));
-         
+
          if (ancestor == Root() || s == NULLT || s < fast_find_close(Par,ancestor)) return s;
          else return NULLT;
   };
@@ -732,11 +764,11 @@ public:
   {
          treeNode close = fast_find_close(Par, x);
          treeNode s = tagpos2node(Tags->select_next(tag, close));
-         
+
          if (ancestor_closing == Root() || s == NULLT || s < ancestor_closing) return s;
          else return NULLT;
   };
-    
+
 // TaggedSibling(x,tag): returns the first sibling of node x tagged tag, or NULLT if there is none.
 treeNode TaggedFollowingSibling(treeNode x, TagType tag)
 {
@@ -745,18 +777,18 @@ treeNode TaggedFollowingSibling(treeNode x, TagType tag)
   TagType ctag;
   while ((sibling = fast_next_sibling(Par, sibling)) != NULLT) {
     ctag = Tag(sibling);
-    if (ctag == tag) return sibling; 
+    if (ctag == tag) return sibling;
   }
-  return NULLT; 
+  return NULLT;
 };
 
-treeNode TaggedChild(treeNode x, TagType tag) 
+treeNode TaggedChild(treeNode x, TagType tag)
 {
-   
+
    NULLT_IF(x==NULLT || fast_isleaf(Par,x));
-   treeNode child; 
+   treeNode child;
    child = fast_first_child(Par, x);
-   
+
 if (Tag(child) == tag)
      return child;
    else
index df39e2c..5c8ec4a 100644 (file)
--- a/bpcore.c
+++ b/bpcore.c
@@ -1,6 +1,8 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include "bp.h"
+#include "utils.h"
+
 
 #ifndef min
 #define min(x,y) ((x)<(y)?(x):(y))
@@ -130,7 +132,7 @@ int search_SB_r(bp *b, int i, int rel)
        }
       }
       r = min(j,ETW);
-      rel -= 2*popCount[w]-r;
+      rel -= 2*popcount(w)-r;
       x <<= r;
       i += r;
       j -= r;
@@ -329,7 +331,7 @@ int degree_SB(bp *b, int i, int t, int rel, int *ans, int ith)
       }
 
       r = min(j,ETW);
-      d += 2*popCount[w]-r;
+      d += 2*popcount(w)-r;
       x <<= r;
       i += r;
       j -= r;
@@ -615,7 +617,7 @@ int search_SB_l(bp *b, int i, int rel)
        }
       }
       r = min(j,ETW);
-      rel += 2*popCount[w]-r;
+      rel += 2*popcount(w)-r;
       x >>= r;
       i -= r;
       j -= r;
@@ -764,7 +766,7 @@ int rmq_SB(bp *b, int s, int t, int opt, int *dm)
       }
 
       r = min(j,ETW);
-      d += 2*popCount[w]-r;
+      d += 2*popcount(w)-r;
       x <<= r;
       i += r;
       j -= r;
index 780b967..890de45 100644 (file)
--- a/darray.c
+++ b/darray.c
@@ -1,6 +1,7 @@
 #include <stdio.h>\r
 #include <stdlib.h>\r
 #include "darray.h"\r
+#include "utils.h"\r
 \r
 //typedef unsigned char byte;\r
 //typedef unsigned short word;\r
@@ -134,61 +135,6 @@ static const unsigned int popCount[] = {
 \r
 static int selecttbl[8*256];\r
 \r
-unsigned int popcount_old(pb x)\r
-{\r
-  pb r;\r
-#if 0\r
-  r = x;\r
-  r = r - ((r>>1) & 0x77777777) - ((r>>2) & 0x33333333) - ((r>>3) & 0x11111111);\r
-  r = ((r + (r>>4)) & 0x0f0f0f0f) % 0xff;\r
-#elif 1\r
-  r = x;\r
-  r = ((r & 0xaaaaaaaa)>>1) + (r & 0x55555555);\r
-  r = ((r & 0xcccccccc)>>2) + (r & 0x33333333);\r
-  //r = ((r & 0xf0f0f0f0)>>4) + (r & 0x0f0f0f0f);\r
-  r = ((r>>4) + r) & 0x0f0f0f0f;\r
-  //r = ((r & 0xff00ff00)>>8) + (r & 0x00ff00ff);\r
-  r = (r>>8) + r;\r
-  //r = ((r & 0xffff0000)>>16) + (r & 0x0000ffff);\r
-  r = ((r>>16) + r) & 63;\r
-#else\r
-  r = popCount[x & 0xff];\r
-  x >>= 8;\r
-  r += popCount[x & 0xff];\r
-  x >>= 8;\r
-  r += popCount[x & 0xff];\r
-  x >>= 8;\r
-  r += popCount[x & 0xff];\r
-#endif\r
-  return r;\r
-}\r
-\r
-inline unsigned int\r
-popcount(pb x) \r
-{\r
-  uint m1 = 0x55555555;\r
-  uint m2 = 0xc30c30c3;\r
-  x -= (x >> 1) & m1;\r
-  x = (x & m2) + ((x >> 2) & m2) + ((x >> 4) & m2);\r
-  x += x >> 6;\r
-  return  (x + (x >> 12) + (x >> 24)) & 0x3f;\r
-}\r
-\r
-\r
-unsigned int popcount8(pb x)\r
-{\r
-  dword r;\r
-#if 1\r
-  r = x;\r
-  r = ((r & 0xaa)>>1) + (r & 0x55);\r
-  r = ((r & 0xcc)>>2) + (r & 0x33);\r
-  r = ((r>>4) + r) & 0x0f;\r
-#else\r
-  r = popCount[x & 0xff];\r
-#endif\r
-  return r;\r
-}\r
-\r
 void make_selecttbl(void)\r
 {\r
   int i,x,r;\r
@@ -416,17 +362,19 @@ int darray_rank0(darray *da, int i)
 \r
 int darray_rank(darray *da, int i)\r
 {\r
-  int r,j;\r
+  int r,j,i_rr, i_rrr;\r
   pb *p;\r
+  i_rr = i >> logRR;\r
+  i_rrr = i >> logRRR;\r
+  r = da->rl[i>>logR] + da->rm[i_rr];\r
 \r
-  r = da->rl[i>>logR] + da->rm[i>>logRR];\r
-  j = (i>>logRRR) & (RR/RRR-1);\r
+  j = (i_rrr) & (RR/RRR-1);\r
   while (j > 0) {\r
-    r += da->rs[((i>>logRR)<<(logRR-logRRR))+j-1];\r
+    r += da->rs[((i_rr)<<(logRR-logRRR))+j-1];\r
     j--;\r
   }\r
 \r
-  p = da->buf + ((i>>logRRR)<<(logRRR-logD));\r
+  p = da->buf + ((i_rrr)<<(logRRR-logD));\r
   j = i & (RRR-1);\r
   while (j >= D) {\r
     r += popcount(*p++);\r
@@ -618,8 +566,8 @@ int darray_select(darray *da, int i,int f)
       x = *q;\r
       while (1) {\r
        //rr = popcount(x >> (D-8));\r
-       rr = popCount[x >> (D-8)];\r
-       //rr = popcount8(x >> (D-8));\r
+       //rr = popcount(x >> (D-8));\r
+       rr = popcount8(x >> (D-8));\r
        if (r + rr >= i) break;\r
        r += rr;\r
        p += 8;\r
@@ -643,8 +591,8 @@ int darray_select(darray *da, int i,int f)
 \r
       while (1) {\r
        //rr = popcount(x >> (D-8));\r
-       rr = popCount[x >> (D-8)];\r
-       //rr = popcount8(x >> (D-8));\r
+       //rr = popCount[x >> (D-8)];\r
+       rr = popcount8(x >> (D-8));\r
        if (r + rr >= i) break;\r
        r += rr;\r
        p += 8;\r
@@ -700,8 +648,8 @@ int darray_pat_select(darray *da, int i, pb (*getpat)(pb *))
     x = getpat(q);\r
     while (1) {\r
       //rr = popcount(x >> (D-8));\r
-      rr = popCount[x >> (D-8)];\r
-      //rr = popcount8(x >> (D-8));\r
+      //rr = popCount[x >> (D-8)];\r
+      rr = popcount8(x >> (D-8));\r
       if (r + rr >= i) break;\r
       r += rr;\r
       p += 8;\r
index f5603c6..a88150b 100644 (file)
--- a/darray.h
+++ b/darray.h
@@ -41,7 +41,7 @@ int setbit(pb *B, int i,int x);
 int setbits(pb *B, int i, int d, int x);
 int getbit(pb *B, int i);
 dword getbits(pb *B, int i, int d);
-unsigned int popcount(pb x);
+//unsigned int popcount(pb x);
 
 int darray_construct(darray *da, int n, pb *buf,int opt);
 int darray_select(darray *da, int i,int f);