Initial merge of Diego's cleaned up XMLTree class.
authorkim <kim@3cdefd35-fc62-479d-8e8d-bae585ffb9ca>
Tue, 28 Apr 2009 01:02:46 +0000 (01:02 +0000)
committerkim <kim@3cdefd35-fc62-479d-8e8d-bae585ffb9ca>
Tue, 28 Apr 2009 01:02:46 +0000 (01:02 +0000)
git-svn-id: svn+ssh://idea.nguyen.vg/svn/sxsi/trunk/XMLTree@363 3cdefd35-fc62-479d-8e8d-bae585ffb9ca

XMLTree.cpp
XMLTree.h
XMLTreeBuilder.cpp [new file with mode: 0644]
XMLTreeBuilder.h [new file with mode: 0644]
basics.h [new file with mode: 0644]
makefile

index bd728af..ffdf5d0 100644 (file)
@@ -1,4 +1,5 @@
 #include "XMLTree.h"\r
+#include "basics.h"\r
 #include <cstring>\r
 \r
 // functions to convert tag positions to the corresponding tree node and viceversa. \r
 // the tree, and storing 2 tags per tree node (opening and closing tags).\r
 \r
 // tag position -> tree node\r
-inline treeNode tagpos2node(int t) {\r
-   return (treeNode)t;\r
-}\r
+inline treeNode tagpos2node(int t) \r
+ {\r
+    return (treeNode)t;\r
+ }\r
 \r
 // tree node -> tag position\r
-inline int node2tagpos(treeNode x) {\r
-   return (int)x;\r
-}\r
+inline int node2tagpos(treeNode x) \r
+ {\r
+    return (int)x;\r
+ }\r
 \r
 \r
-//KIM OJO to prevent suprious "unused result" warnings\r
+XMLTree::XMLTree(pb *par, uint npar, unsigned char **TN, uint ntagnm, uint *empty_texts_bmp, TagType *tags,\r
+                 TextCollection *TC, vector<string> CT, bool indexing_empty_t, bool dis_tc)\r
+ {\r
+    // creates the data structure for the tree topology\r
+    Par = (bp *)umalloc(sizeof(bp));\r
+    bp_construct(Par, npar, par, OPT_DEGREE|0);    \r
 \r
-inline void ufread(void *ptr, size_t size, size_t nmemb, FILE *stream){\r
-  size_t res;\r
-  res = fread(ptr,size,nmemb,stream);\r
-  if (res < nmemb)\r
-    throw "ufread I/O error";\r
 \r
-  return;\r
-}\r
+    // creates structure for tags\r
 \r
-inline void ufwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream){\r
-  size_t res;\r
-  res = fwrite(ptr,size,nmemb,stream);\r
-  if (res < nmemb)\r
-    throw "ufwrite I/O error";\r
-  return;\r
-}\r
+    // If we found an attribute then "<@>" is present in the tree\r
+    // if we didn't then it is not. "<$>" is never present in the tree\r
+    TagName = TN;\r
+    ntagnames = ntagnm;\r
+    uint max_tag = 0;\r
+    for(uint i=0;i<(uint)npar-1;i++)\r
+       max_tag = max(max_tag,tags[i]);\r
+    int ntagsize = 2*ntagnames + 2;\r
 \r
-// OJO to fail cleanly while doing a realloc\r
-// if we can't realloc we are pretty much screwed anyway but\r
-// it makes the code clearer to not have a bunch of if (!ptr) { printf("..."); exit(1); };\r
-inline void * urealloc(void *ptr, size_t size){\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
+    cout << "Tags test: " << Tags->test((uint*)tags,npar) << endl;\r
 \r
-  void * dest = realloc(ptr,size);\r
-  //don't fail if we requested size 0\r
-  if (dest == NULL && size > 0 )\r
-    throw std::bad_alloc();\r
-  return dest;\r
+    tags_blen = bits(max_tag);\r
+    tags_len = (uint)npar;\r
+    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
 \r
-}\r
+    delete bmb;    \r
+    free(tags);\r
+    tags = NULL;\r
+    \r
+    Text = TC;\r
 \r
-inline void * ucalloc(size_t nmemb, size_t size){\r
+    CachedText = CT;\r
 \r
-  void * dest = calloc(nmemb,size);\r
-  //don't fail if we requested size 0\r
-  if (dest == NULL && nmemb > 0 && size > 0 )\r
-    throw std::bad_alloc();\r
-  return dest;\r
+    // creates the data structure marking the non-empty texts (just in the case it is necessary)\r
+    indexing_empty_texts = indexing_empty_t;\r
+    if (!indexing_empty_t)  {\r
+       EBVector = new static_bitsequence_rrr02((uint *)empty_texts_bmp,(ulong)npar,(uint)32);\r
+       free(empty_texts_bmp);\r
+       empty_texts_bmp = NULL;\r
+    }\r
 \r
-}\r
+    TagArray = new TagArrayEntry[ntagnames];\r
+    for (uint i=0; i<ntagnames; i++) {\r
+       TagArray[i].first = NULL;\r
+       TagArray[i].last = NULL;\r
+    }\r
\r
+    disable_tc = dis_tc;\r
+ }\r
 \r
-inline void * umalloc(size_t size){\r
-  void * dest = malloc(size);\r
-  if (dest == NULL && size > 0)\r
-    throw std::bad_alloc();\r
-  return dest;\r
-}\r
 \r
-void XMLTree::print_stats() {\r
-       uint total_space = Tags->size()+sizeof(static_sequence*);\r
-       total_space += sizeof(uint*)+sizeof(uint)*(2+uint_len(tags_blen,tags_len));\r
-       cout << "Space usage for XMLTree:" << endl\r
-               << " - tags static_sequence: " << Tags->size()+sizeof(static_sequence*) << endl\r
-               << " - tags access array:    " << sizeof(uint*)+sizeof(uint)*(2+uint_len(tags_blen,tags_len)) << endl\r
-               << " ... add Diego structures ... " << endl\r
-               << " *total* " << total_space << endl;\r
-}\r
+// ~XMLTree: frees memory of XML tree.\r
+XMLTree::~XMLTree() \r
+ { \r
+    int i;\r
+\r
+    destroyTree(Par);\r
+    free(Par); // frees the memory of struct Par\r
+   \r
+    for (i=0; i<ntagnames;i++) \r
+       free(TagName[i]);\r
+    \r
+    free(TagName);\r
+\r
+    if (!indexing_empty_texts) {\r
+       delete EBVector;\r
+       EBVector = NULL;\r
+    }\r
+\r
+    delete Tags;\r
+    Tags = NULL;\r
+\r
+    delete Text; \r
+    Text = NULL;\r
+\r
+    delete TagArray;\r
+ }\r
+\r
+\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
+    cout << "Space usage for XMLTree:" << endl\r
+         << " - tags static_sequence: " << Tags->size()+sizeof(static_sequence*) << endl\r
+         << " - tags access array:    " << sizeof(uint*)+sizeof(uint)*(2+uint_len(tags_blen,tags_len)) << endl\r
+         << " ... add Diego structures ... " << endl\r
+         << " *total* " << total_space << endl;\r
+ }\r
 \r
 // Save: saves XML tree data structure to file. \r
 void XMLTree::Save(unsigned char *filename) \r
  {\r
-\r
     FILE *fp;\r
     char filenameaux[1024];\r
     int i;\r
@@ -103,17 +142,18 @@ void XMLTree::Save(unsigned char *filename)
     \r
     // stores the flags\r
     ufwrite(&indexing_empty_texts, sizeof(bool), 1, fp);\r
-    ufwrite(&initialized, sizeof(bool), 1, fp);\r
-    ufwrite(&finished, sizeof(bool), 1, fp);\r
+    bool ignore = true;\r
+    ufwrite(&ignore, sizeof(bool),1,fp);\r
+    ufwrite(&ignore, sizeof(bool),1,fp);\r
     ufwrite(&disable_tc, sizeof(bool),1,fp);\r
     \r
     if (!indexing_empty_texts) EBVector->save(fp);\r
     \r
     // stores the tags\r
     Tags->save(fp);\r
-               ufwrite(&tags_blen,sizeof(uint),1,fp);\r
-               ufwrite(&tags_len,sizeof(uint),1,fp);\r
-               ufwrite(tags_fix,sizeof(uint),uint_len(tags_blen,tags_len),fp);\r
+    ufwrite(&tags_blen,sizeof(uint),1,fp);\r
+    ufwrite(&tags_len,sizeof(uint),1,fp);\r
+    ufwrite(tags_fix,sizeof(uint),uint_len(tags_blen,tags_len),fp);\r
 \r
     // stores the texts   \r
     if (!disable_tc) {\r
@@ -135,7 +175,6 @@ void XMLTree::Save(unsigned char *filename)
 // a pointer to the loaded data structure\r
 XMLTree *XMLTree::Load(unsigned char *filename, int sample_rate_text) \r
  {\r
-\r
     FILE *fp;\r
     char buffer[1024];\r
     XMLTree *XML_Tree;\r
@@ -154,31 +193,26 @@ XMLTree *XMLTree::Load(unsigned char *filename, int sample_rate_text)
 \r
     XML_Tree = new XMLTree();\r
 \r
+\r
     XML_Tree->Par = (bp *)umalloc(sizeof(bp));\r
 \r
     loadTree(XML_Tree->Par, fp); \r
 \r
     s_tree += sizeof(bp);\r
 \r
+\r
     // stores the table with tag names\r
     ufread(&XML_Tree->ntagnames, sizeof(int), 1, fp);\r
-    \r
+\r
     s_tree += sizeof(int);\r
 \r
     XML_Tree->TagName = (unsigned char **)umalloc(XML_Tree->ntagnames*sizeof(unsigned char *));\r
-    \r
+\r
     s_tags += sizeof(unsigned char*)*XML_Tree->ntagnames;\r
 \r
 \r
     for (i=0; i<XML_Tree->ntagnames;i++) {\r
-      \r
-      // OJO Kim is it needed ?\r
-      int k = feof(fp);\r
-\r
-      \r
-       // fscanf chokes on "\n" which is the case for the root element\r
        char * r = fgets(buffer,1023,fp);\r
-       //       int r = fscanf(fp, "%s\n",buffer);\r
        if (r==NULL)\r
         throw "Cannot read tag list";\r
 \r
@@ -188,36 +222,39 @@ XMLTree *XMLTree::Load(unsigned char *filename, int sample_rate_text)
        strncpy((char *)XML_Tree->TagName[i], (const char *)buffer,len - 1);\r
        s_tags+= len*sizeof(char);\r
     }\r
+\r
        \r
     // loads the flags\r
 \r
     ufread(&(XML_Tree->indexing_empty_texts), sizeof(bool), 1, fp);\r
-    ufread(&(XML_Tree->initialized), sizeof(bool), 1, fp);\r
-    ufread(&(XML_Tree->finished), sizeof(bool), 1, fp);\r
+    bool ignore;\r
+    ufread(&ignore, sizeof(bool), 1, fp);\r
+    ufread(&ignore, sizeof(bool), 1, fp);\r
     ufread(&(XML_Tree->disable_tc), sizeof(bool), 1, fp);\r
-    \r
+\r
     s_tree+=sizeof(bool)*4;\r
 \r
     if (!(XML_Tree->indexing_empty_texts)) XML_Tree->EBVector = static_bitsequence_rrr02::load(fp);\r
-    \r
+\r
     s_tree+= XML_Tree->EBVector->size();\r
-    \r
+\r
     // loads the tags\r
     XML_Tree->Tags = static_sequence::load(fp);\r
-               ufread(&XML_Tree->tags_blen,sizeof(uint),1,fp);\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
-               s_tree+=2*sizeof(uint)+sizeof(uint)*uint_len(XML_Tree->tags_blen,XML_Tree->tags_len);\r
+    ufread(&XML_Tree->tags_blen,sizeof(uint),1,fp);\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
+    s_tree+=2*sizeof(uint)+sizeof(uint)*uint_len(XML_Tree->tags_blen,XML_Tree->tags_len);\r
     s_tree+= XML_Tree->Tags->size();\r
 \r
-               /// FIXME:UGLY tests!\r
-               uint * seq = new uint[XML_Tree->tags_len];\r
-               for(uint i=0;i<XML_Tree->tags_len;i++)\r
-                       seq[i] = get_field(XML_Tree->tags_fix,XML_Tree->tags_blen,i);\r
-               cout << "Tags test: " << XML_Tree->Tags->test(seq,XML_Tree->tags_len) << endl;\r
-               delete [] seq;\r
-               /// End ugly tests\r
+\r
+    /// FIXME:UGLY tests!\r
+    uint * seq = new uint[XML_Tree->tags_len];\r
+    for(uint i=0;i<XML_Tree->tags_len;i++)\r
+       seq[i] = get_field(XML_Tree->tags_fix,XML_Tree->tags_blen,i);\r
+    cout << "Tags test: " << XML_Tree->Tags->test(seq,XML_Tree->tags_len) << endl;\r
+    delete [] seq;\r
+    /// End ugly tests\r
 \r
     s_text = ftell(fp);\r
 \r
@@ -237,88 +274,32 @@ XMLTree *XMLTree::Load(unsigned char *filename, int sample_rate_text)
       };\r
 \r
     }\r
-    else {\r
-      XML_Tree->Text = NULL;\r
-    }\r
-    s_text = ftell(fp) - s_text;\r
-\r
-    \r
+    else XML_Tree->Text = NULL;\r
 \r
+    s_text = ftell(fp) - s_text;\r
 \r
     fclose(fp);\r
 \r
-    /*std::cerr << "Tree part is " << s_tree/1024 << " Kbytes,\n"\r
-             << "with node->tagid part " << XML_Tree->Tags->size()/1024+(uint_len(XML_Tree->tags_blen,XML_Tree->tags_len)*sizeof(uint))/1024  << "Kbytes \n"\r
-             << "size of Tag part : " << XML_Tree->Tags->length () << " elements\n"\r
-             << "sizof(unsigned int)* " <<  XML_Tree->Tags->length () << " = " << \r
-      sizeof(unsigned int) * XML_Tree->Tags->length () / 1024 << " Kbytes\n"\r
-             << "Tag part is " << s_tags/1024 << " Kbytes,\n"\r
-             << "Text collection is " << s_text/1024 << " Kbytes \n";*/\r
-               XML_Tree->print_stats();\r
+    XML_Tree->print_stats();\r
     return XML_Tree;\r
  }\r
 \r
 \r
-// ~XMLTree: frees memory of XML tree.\r
-XMLTree::~XMLTree() \r
- { \r
-    int i;\r
-\r
-    destroyTree(Par);\r
-    free(Par); // frees the memory of struct Par\r
-   \r
-    for (i=0; i<ntagnames;i++) \r
-       free(TagName[i]);\r
-    \r
-    free(TagName);\r
-\r
-    if (!indexing_empty_texts) {\r
-       //EBVector->~static_bitsequence_rrr02();\r
-       delete EBVector;\r
-       EBVector = NULL;\r
-    }\r
-\r
-    //Tags->~static_sequence_wvtree();\r
-    delete Tags;\r
-    Tags = NULL;\r
-\r
-    //Text->~TextCollection();\r
-    delete TextBuilder; \r
-    TextBuilder = NULL;\r
-    delete Text; \r
-    Text = NULL;\r
-\r
-    initialized = false;\r
-    finished = false;\r
- }\r
-\r
 // root(): returns the tree root.\r
 treeNode XMLTree::Root() \r
  {\r
-    if (!finished) {\r
-       fprintf(stderr, "Root() : Error: data structure has not been constructed properly\n");\r
-       exit(1);\r
-    }\r
     return root_node(Par);\r
  }\r
 \r
 // SubtreeSize(x): the number of nodes (and attributes) in the subtree of node x.\r
 int XMLTree::SubtreeSize(treeNode x) \r
  {\r
-    if (!finished) {\r
-       fprintf(stderr, "Error: data structure has not been constructed properly\n");\r
-       exit(1);\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
 int XMLTree::SubtreeTags(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
     if (x == Root())\r
       x = first_child(Par,x);\r
     \r
@@ -338,59 +319,39 @@ bool XMLTree::IsLeaf(treeNode x)
 // IsAncestor(x,y): returns whether node x is ancestor of node y.\r
 bool XMLTree::IsAncestor(treeNode x, treeNode y) \r
  {\r
-    if (!finished) {\r
-       fprintf(stderr, "Error: data structure has not been constructed properly\n");\r
-       exit(1);\r
-    }\r
-\r
     return 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
  {\r
-    if (!finished) {\r
-       fprintf(stderr, "Error: data structure has not been constructed properly\n");\r
-       exit(1);\r
-    }\r
-\r
     if (!is_ancestor(Par, x, y)) return false;\r
     return depth(Par, x) == (depth(Par, y) + 1);\r
  }\r
-bool XMLTree::IsFirstChild(treeNode x){\r
-  return ((x != NULLT)&&(x==Root() || prev_sibling(Par,x) == NULLT));\r
-}\r
+\r
+// IsFirstChild(x): returns whether node x is the first child of its parent.\r
+bool XMLTree::IsFirstChild(treeNode x)\r
+ {\r
+    return ((x != NULLT)&&(x==Root() || prev_sibling(Par,x) == (treeNode)-1));\r
+ }\r
+\r
+\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
  {\r
-    if (!finished) {\r
-       fprintf(stderr, "Error: data structure has not been constructed properly\n");\r
-       exit(1);\r
-    }\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
  {\r
-    if (!finished) {\r
-       fprintf(stderr, "Error: data structure has not been constructed properly\n");\r
-       exit(1);\r
-    }\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
  {\r
-    if (!finished) {\r
-       fprintf(stderr, "Error: data structure has not been constructed properly\n");\r
-       exit(1);\r
-    }\r
-\r
     return depth(Par, x);\r
  }\r
 \r
@@ -398,11 +359,6 @@ int XMLTree::Depth(treeNode x)
 // nodes (i.e., tags, it disregards the texts in the tree).\r
 int XMLTree::Preorder(treeNode x) \r
  {\r
-    if (!finished) {\r
-       fprintf(stderr, "Error: data structure has not been constructed properly\n");\r
-       exit(1);\r
-    }\r
-\r
     return preorder_rank(Par, x);\r
  }\r
 \r
@@ -410,21 +366,12 @@ int XMLTree::Preorder(treeNode x)
 // nodes (i.e., tags, it disregards the texts in the tree).\r
 int XMLTree::Postorder(treeNode x) \r
  {\r
-    if (!finished) {\r
-       fprintf(stderr, "Error: data structure has not been constructed properly\n");\r
-       exit(1);\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
  {\r
-    if (!finished) {\r
-       fprintf(stderr, "Error: data structure has not been constructed properly\n");\r
-       exit(1);\r
-    }\r
-    \r
     return get_field(tags_fix,tags_blen,node2tagpos(x)); //Tags->access(node2tagpos(x));\r
  }\r
 \r
@@ -432,20 +379,13 @@ TagType XMLTree::Tag(treeNode x)
 // returns {NULLT, NULLT} when there are no texts descending from x.\r
 range XMLTree::DocIds(treeNode x) \r
  {\r
-    if (!finished) {\r
-       fprintf(stderr, "Error: data structure has not been constructed properly\n");\r
-       exit(1);\r
-    }\r
-\r
     range r;\r
-    if (x == NULLT)\r
-      {\r
-       r.min = NULLT;\r
-       r.max = NULLT;\r
-       return r;\r
-      };\r
+    if (x == NULLT) {\r
+       r.min = NULLT;\r
+       r.max = NULLT;\r
+       return r;\r
+    };\r
         \r
-      \r
     if (indexing_empty_texts) { // faster, no rank needed\r
        r.min = x;\r
        r.max = x+2*subtree_size(Par, x)-2;\r
@@ -468,10 +408,6 @@ range XMLTree::DocIds(treeNode x)
 // Parent(x): returns the parent node of node x.\r
 treeNode XMLTree::Parent(treeNode x) \r
  {\r
-    if (!finished) {\r
-       fprintf(stderr, "Error: data structure has not been constructed properly\n");\r
-       exit(1);\r
-    }\r
     if (x == Root())\r
       return NULLT;\r
     else\r
@@ -481,11 +417,6 @@ treeNode XMLTree::Parent(treeNode x)
 // Child(x,i): returns the i-th child of node x, assuming it exists.\r
 treeNode XMLTree::Child(treeNode x, int i) \r
 {\r
-    if (!finished) {\r
-       fprintf(stderr, "Error: data structure has not been constructed properly\n");\r
-       exit(1);\r
-    }\r
-\r
     if (i <= OPTD) return naive_child(Par, x, i);\r
     else return child(Par, x, i);\r
  }\r
@@ -493,29 +424,23 @@ treeNode XMLTree::Child(treeNode x, int i)
 // FirstChild(x): returns the first child of node x, assuming it exists. Very fast in BP.\r
 treeNode XMLTree::FirstChild(treeNode x) \r
  {\r
-    if (!finished) {\r
-       fprintf(stderr, "Error: data structure has not been constructed properly\n");\r
-       exit(1);\r
-    }\r
-\r
     return first_child(Par, x);\r
  }\r
 \r
-treeNode XMLTree::LastChild(treeNode x) \r
-{\r
-  if (x == Root() || isleaf(Par,x) || x == NULLT)\r
-    return x;\r
-  else\r
-  return find_open(Par,find_close(Par,parent(Par,x))-1);\r
-}\r
+// LastChild(x): returns the last child of node x.\r
+treeNode XMLTree::LastChild(treeNode x)\r
+ {\r
+    if (x == Root() || isleaf(Par,x) || x == NULLT)\r
+       return x;\r
+    else\r
+//       return find_open(Par,find_close(Par,parent(Par,x))-1);\r
+       return find_open(Par, find_close(Par, x)-1);\r
+ }\r
+\r
 \r
 // NextSibling(x): returns the next sibling of node x, assuming it exists.\r
 treeNode XMLTree::NextSibling(treeNode x) \r
  {\r
-    if (!finished) {\r
-       fprintf(stderr, "Error: data structure has not been constructed properly\n");\r
-       exit(1);\r
-    }\r
     if (x == Root() || x==NULLT)\r
       return NULLT;\r
     \r
@@ -525,163 +450,195 @@ treeNode XMLTree::NextSibling(treeNode x)
 // PrevSibling(x): returns the previous sibling of node x, assuming it exists.\r
 treeNode XMLTree::PrevSibling(treeNode x) \r
  {\r
-    if (!finished) {\r
-       fprintf(stderr, "Error: data structure has not been constructed properly\n");\r
-       exit(1);\r
-    }\r
-\r
     return prev_sibling(Par, x);\r
  }\r
 \r
-// TaggedChild(x,i,tag): returns the i-th child of node x tagged tag, or NULLT if there is none.\r
+// TaggedChild(x,tag): returns the first child of node x tagged tag, or NULLT if there is none.\r
 // 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
-treeNode XMLTree::TaggedChild(treeNode x, int i, TagType tag) \r
+treeNode XMLTree::TaggedChild(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
     treeNode child;\r
    \r
     child = first_child(Par, x); // starts at first child of node x\r
     if (child==(treeNode)-1) return NULLT; // node x is a leaf, there is no such child\r
     while (child!=(treeNode)-1) {\r
-       if (get_field(tags_fix,tags_blen,node2tagpos(child)) /*Tags->access(node2tagpos(child))*/ == tag) { // current child is labeled with tag of interest\r
-          i--;\r
-          if (i==0) return child; // we have seen i children of x tagged tag, this is the one we are looking for\r
-       }\r
-       child = next_sibling(Par, x); // OK, let's try with the next child\r
+       if (get_field(tags_fix,tags_blen,node2tagpos(child)) == tag) // current child is labeled with tag of interest\r
+          return child; \r
+       child = next_sibling(Par, child); // OK, let's try with the next child\r
     }\r
     return NULLT; // no such child was found  \r
  }\r
 \r
+\r
+treeNode XMLTree::SelectChild(treeNode x, TagType *tags, int ntags)\r
+ {\r
+    int i;\r
+    treeNode child = first_child(Par, x);\r
+\r
+    while (child!=(treeNode)-1) {\r
+       TagType t = get_field(tags_fix, tags_blen, node2tagpos(child));\r
+       for (i=0; i<ntags; i++)\r
+          if (t==tags[i]) return child;\r
+       child = next_sibling(Par, child);\r
+    }\r
+    return NULLT;\r
+ }\r
+\r
+\r
+// TaggedSibling(x,tag): returns the first sibling of node x tagged tag, or NULLT if there is none.\r
+treeNode XMLTree::TaggedSibling(treeNode x, TagType tag)\r
+ {\r
+    treeNode sibling = next_sibling(Par, x);\r
+   \r
+    while (sibling!=(treeNode)-1) {\r
+       if (get_field(tags_fix,tags_blen,node2tagpos(sibling)) == tag) // current sibling is labeled with tag of interest\r
+          return sibling; \r
+       sibling = next_sibling(Par, sibling); // OK, let's try with the next sibling\r
+    }\r
+    return NULLT; // no such sibling was found   \r
+ }\r
+\r
+\r
+treeNode XMLTree::SelectSibling(treeNode x, TagType *tags, int ntags)\r
+ {\r
+    int i;\r
+    treeNode sibling = next_sibling(Par, x);\r
+\r
+    while (sibling!=(treeNode)-1) {\r
+       TagType t = get_field(tags_fix, tags_blen, node2tagpos(sibling));\r
+       for (i=0; i<ntags; i++)\r
+          if (t==tags[i]) return sibling;\r
+       sibling = next_sibling(Par, sibling);\r
+    }\r
+    return NULLT;    \r
+ }\r
+\r
+\r
 // TaggedDesc(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
 treeNode XMLTree::TaggedDesc(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
     treeNode y;\r
     if (isleaf(Par,x))\r
       return NULLT;\r
 \r
-               int s = (int) Tags->select_next(tag,node2tagpos(x));\r
-    /*r = (int) Tags->rank(tag, node2tagpos(x));\r
-    s = (int) Tags->select(tag, r+1);*/\r
-    if (s == -1) return NULLT; // there is no such node\r
+    int s = (int) Tags->select_next(tag,node2tagpos(x));\r
+    if (s==-1) return NULLT; // there is no such node\r
     y = tagpos2node(s); // transforms the tag position into a node position\r
     if (!is_ancestor(Par, x, y)) return NULLT; // the next node tagged tag (in preorder) is not within the subtree of x.\r
     else return y;\r
  }\r
 \r
-treeNode XMLTree::TaggedDescOnly(treeNode x,TagType *desctags, unsigned int dtlen)\r
-{\r
 \r
-  treeNode res,y;\r
-  if (isleaf(Par,x))\r
-    return NULLT;\r
-  \r
-  res=NULLT;\r
-  for (unsigned int i = 0; i < dtlen; i ++ )\r
-    {\r
-      y = TaggedDesc(x,desctags[i]);\r
-      res = (res == NULLT) || (( res != NULLT) && (y =! NULLT) && y < res) ? y : res;\r
-      \r
-    };\r
+treeNode XMLTree::SelectDesc(treeNode x, TagType *tags, int ntags)\r
+ {\r
+    int i;\r
+    treeNode min = NULLT;\r
+    treeNode fc = first_child(Par,x);\r
+    \r
+    for (i=0; i<ntags; i++) {\r
+       treeNode aux = TaggedDesc(x, tags[i]);\r
+       if (aux == fc) \r
+          return fc;\r
+       else \r
+          if ((min == (treeNode)NULLT) || (aux < min)) min = aux;\r
+    }\r
+    return min;\r
+ }\r
+\r
+\r
+treeNode XMLTree::TaggedDescOnly(treeNode x,TagType *desctags, unsigned int dtlen)\r
+ {\r
+    treeNode res, y;\r
+    if (isleaf(Par,x))\r
+       return NULLT;\r
   \r
-  return res;\r
+    res=NULLT;\r
+    for (unsigned int i = 0; i < dtlen; i ++ ) {\r
+       y = TaggedDesc(x,desctags[i]);\r
+       res = (res == NULLT) || (( res != NULLT) && (y =! NULLT) && y < res) ? y : res;  \r
+    }\r
   \r
-}\r
+    return res;\r
+ } \r
 \r
 \r
 treeNode XMLTree::TaggedBelow(treeNode x, TagType *childtags, unsigned int ctlen,\r
-                             TagType *desctags, unsigned int dtlen)\r
-{\r
-  treeNode fs,y,res;\r
-  TagType tag;\r
+                             TagType *desctags, unsigned int dtlen) \r
+ {\r
+    treeNode fs,y,res;\r
+    TagType tag;\r
 \r
-  if (isleaf(Par,x))\r
-    return NULLT;\r
+    if (isleaf(Par,x))\r
+      return NULLT;\r
   \r
-  res = NULLT;\r
-  fs = first_child(Par,x);\r
-  while (fs != NULLT) {\r
-    tag = get_field(tags_fix,tags_blen,node2tagpos(fs));\r
+    res = NULLT;\r
+    fs = first_child(Par,x);\r
+    while (fs != NULLT) {\r
+       tag = get_field(tags_fix,tags_blen,node2tagpos(fs));\r
         \r
-    /* Check for first_child */\r
-    for (unsigned int i = 0; i < ctlen; i++) {\r
-      if (childtags[i] == tag)\r
-       return fs;\r
-    };\r
+       /* Check for first_child */\r
+       for (unsigned int i = 0; i < ctlen; i++) {\r
+          if (childtags[i] == tag)\r
+            return fs;\r
+       }\r
     \r
-    for (unsigned int i = 0; i < dtlen; i++)\r
-      if (desctags[i] == tag)\r
-       return fs;  \r
+       for (unsigned int i = 0; i < dtlen; i++)\r
+          if (desctags[i] == tag)\r
+             return fs;  \r
     \r
-    /* check in the descendants */\r
-    res = NULLT;\r
-    for (unsigned int i = 0; i < dtlen; i ++ ){\r
-      /* maybe inline by hand */\r
-      y = TaggedDesc(fs,desctags[i]);\r
-      res = (res==NULLT || (y != NULLT) &&(y < res)) ? y : res;   \r
-    };\r
-    if (res != NULLT)\r
-      return res;\r
+       /* check in the descendants */\r
+       res = NULLT;\r
+       for (unsigned int i = 0; i < dtlen; i ++ ) {\r
+          /* maybe inline by hand */\r
+          y = TaggedDesc(fs,desctags[i]);\r
+         res = (res==NULLT || (y != NULLT) &&(y < res)) ? y : res;   \r
+       } \r
+       if (res != NULLT)\r
+          return res;\r
     \r
-    fs = next_sibling(Par,fs);\r
-  };\r
-  return res;\r
+       fs = next_sibling(Par,fs);\r
+    }\r
     \r
+    return res;\r
 }\r
-treeNode XMLTree::TaggedFollOnly(treeNode x,TagType *folltags, unsigned int ftlen,treeNode root)\r
-{\r
 \r
-  treeNode res,y,lim;\r
-  lim = find_close(Par,root);   \r
-  res=NULLT;\r
-  for (unsigned int i = 0; i < ftlen; i ++ )\r
-    {\r
-      y = TaggedFoll(x,folltags[i]);\r
-      res = (res == NULLT) || (( res != NULLT) && (y =! NULLT) && y < res) ? y : res;\r
-      \r
-    };\r
-  \r
-  return res < lim ? res : NULLT;\r
+\r
+treeNode XMLTree::TaggedFollOnly(treeNode x,TagType *folltags, unsigned int ftlen,treeNode root)\r
+ {\r
+    treeNode res,y,lim;\r
+    lim = find_close(Par,root);   \r
+    res=NULLT;\r
+    \r
+    for (unsigned int i = 0; i < ftlen; i ++ ) {\r
+       y = TaggedFoll(x,folltags[i]);\r
+       res = (res == NULLT) || (( res != NULLT) && (y =! NULLT) && y < res) ? y : res;\r
+    }\r
   \r
-}\r
+    return res < lim ? res : NULLT;\r
+ }\r
 \r
-treeNode XMLTree::TaggedDescOrFollOnly(treeNode x,TagType *folltags, unsigned int ftlen,treeNode root)\r
-{\r
 \r
-  treeNode res,y,lim;\r
-  //int r,s;\r
-  lim = find_close(Par,root);   \r
-  res=NULLT;\r
-  for (unsigned int i = 0; i < ftlen; i ++ )\r
-    {\r
-\r
-                       int s = (int) Tags->select_next(folltags[i],node2tagpos(x));\r
-      /*r = (int) Tags->rank(folltags[i], node2tagpos(x));\r
-      s = (int) Tags->select(folltags[i], r+1);*/\r
-      if (s == -1) \r
-       y = NULLT; // there is no such node\r
-      else {\r
-       y = tagpos2node(s); \r
-       if (y >= lim)\r
-         y = NULLT;\r
-      };\r
-      res = (res == NULLT) || (( res != NULLT) && (y =! NULLT) && y < res) ? y : res;\r
-      \r
-    };\r
-  \r
-  return res < lim ? res : NULLT;\r
+treeNode XMLTree::TaggedDescOrFollOnly(treeNode x,TagType *folltags, unsigned int ftlen,treeNode root)\r
+ {\r
+    treeNode res,y,lim;\r
+    lim = find_close(Par,root);   \r
+    res=NULLT;\r
   \r
+    for (unsigned int i = 0; i < ftlen; i++) {\r
+       int s = (int) Tags->select_next(folltags[i],node2tagpos(x));\r
+       if (s == -1) \r
+          y = NULLT; // there is no such node\r
+       else {\r
+          y = tagpos2node(s); \r
+         if (y >= lim)\r
+            y = NULLT;\r
+       }\r
+       res = (res == NULLT) || (( res != NULLT) && (y =! NULLT) && y < res) ? y : res;\r
+    }\r
+    \r
+    return res < lim ? res : NULLT;\r
 }\r
 \r
 \r
@@ -695,7 +652,6 @@ treeNode XMLTree::TaggedNext(treeNode x, TagType *childtags, unsigned int ctlen,
    if (x == NULLT || x == Root())\r
      return NULLT;\r
 \r
-\r
    lim = find_close(Par,root);   \r
 \r
    res = NULLT;\r
@@ -724,20 +680,14 @@ treeNode XMLTree::TaggedNext(treeNode x, TagType *childtags, unsigned int ctlen,
      res = (y!= x && (res == NULLT || (y != NULLT && y < res)))? y : res;\r
    };\r
   \r
-   return res < lim ? res : NULLT;\r
-   \r
+   return res < lim ? res : NULLT;   \r
  }\r
 \r
 \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::TaggedPrec(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
+ {    \r
     int r, s;\r
     treeNode node_s, root;\r
     r = (int)Tags->rank(tag, node2tagpos(x)-1);\r
@@ -759,57 +709,58 @@ treeNode XMLTree::TaggedPrec(treeNode x, TagType tag)
 // the subtree of x. Returns NULLT if there is none.\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
-               int s = (int) Tags->select_next(tag,find_close(Par,x));\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
+    \r
+    int s = (int) Tags->select_next(tag,find_close(Par, x));               \r
     if (s==-1) return NULLT;\r
     else return tagpos2node(s);\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
+// 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
 treeNode XMLTree::TaggedFollBelow(treeNode x, TagType tag, treeNode root)\r
  {\r
-\r
-   if (x == NULLT || x == Root()) return NULLT;\r
-   treeNode s = (treeNode) Tags->select_next(tag,find_close(Par,x));\r
-   /*int r = (int) Tags->rank(tag, find_close(Par, x));\r
-     int s = (int) Tags->select(tag, r+1); */\r
-   if (root == Root())\r
-     return s;\r
-   \r
-   if (s == NULLT || s >= find_close(Par,root)) return NULLT;\r
-   return s;\r
+    if (x == NULLT || x == Root())\r
+       return NULLT;\r
+    \r
+    treeNode s = (treeNode) Tags->select_next(tag, find_close(Par, x));\r
+    \r
+    if (root == Root()) \r
+      return s;\r
+    if (s == NULLT || s >= find_close(Par, root)) return NULLT;\r
+    return s;\r
  } \r
 \r
 \r
+treeNode XMLTree::SelectFollBelow(treeNode x, TagType *tags, int ntags, treeNode ctx)\r
+ {\r
+    int i;\r
+    treeNode min = NULLT;\r
+    treeNode fc = first_child(Par, x);\r
+    \r
+    for (i=0; i<ntags; i++) {\r
+       treeNode aux = TaggedFollBelow(x, tags[i], ctx);\r
+       if (aux == fc)\r
+          return fc;\r
+       else\r
+          if ((min == NULLT) || (aux < min)) min = aux;\r
+    }\r
+    return min;    \r
+ }\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
-       exit(1);\r
-    }\r
-\r
-    //int r, s;\r
     treeNode ns = next_sibling(Par,x);\r
 \r
     if (x == NULLT || x == Root() || ns == -1)\r
       return NULLT;\r
 \r
-               int s = (int) Tags->select_next(tag,node2tagpos(ns)-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
+    int s = (int) Tags->select_next(tag, node2tagpos(ns)-1);\r
     if (s==-1) return NULLT;\r
     else return tagpos2node(s);\r
  }\r
@@ -818,12 +769,7 @@ treeNode XMLTree::TaggedFollowingSibling(treeNode x, TagType tag)
 // 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
+ {    \r
     if (x == NULLT || x == Root())\r
        return NULLT;\r
     \r
@@ -841,11 +787,6 @@ treeNode XMLTree::TaggedAncestor(treeNode x, TagType tag)
 // Assumes Doc ids start from 0.\r
 DocID XMLTree::PrevText(treeNode x) \r
  {\r
-    if (!finished) {\r
-       fprintf(stderr, "Error: data structure has not been constructed properly\n");\r
-       exit(1);\r
-    }\r
-\r
     if (x == Root()) return NULLT;\r
     if (indexing_empty_texts)  // faster, no rank needed\r
        return (DocID)x-1;\r
@@ -861,11 +802,6 @@ DocID XMLTree::PrevText(treeNode x)
 // of node x, or NULLT if x is the root node. Assumes Doc ids start from 0.\r
 DocID XMLTree::NextText(treeNode x) \r
  {\r
-    if (!finished) {\r
-       fprintf(stderr, "Error: data structure has not been constructed properly\n");\r
-       exit(1);\r
-    }\r
-\r
     if (x == Root()) return NULLT;\r
     if (indexing_empty_texts)  // faster, no rank needed\r
        return (DocID)x+2*subtree_size(Par, x)-1;\r
@@ -883,11 +819,6 @@ DocID XMLTree::NextText(treeNode x)
 // ids start from 0.\r
 DocID XMLTree::MyText(treeNode x) \r
  {\r
-    if (!finished) {\r
-       fprintf(stderr, "Error: data structure has not been constructed properly\n");\r
-       exit(1);\r
-    }\r
-\r
     if (!IsLeaf(x)) return NULLT;\r
     if (indexing_empty_texts) // faster, no rank needed\r
        return (DocID)x;\r
@@ -903,11 +834,6 @@ DocID XMLTree::MyText(treeNode x)
 // all tree nodes and all text nodes. Assumes that the tree root has preorder 1.\r
 int XMLTree::TextXMLId(DocID d) \r
  {\r
-    if (!finished) {\r
-       fprintf(stderr, "Error: data structure has not been constructed properly\n");\r
-       exit(1);\r
-    }\r
-\r
     if (indexing_empty_texts) \r
        return d + rank_open(Par, d)+1; // +1 because root has preorder 1\r
     else { // slower, needs rank and select\r
@@ -921,11 +847,6 @@ int XMLTree::TextXMLId(DocID d)
 // preorder 0;\r
 int XMLTree::NodeXMLId(treeNode x) \r
  {\r
-    if (!finished) {\r
-       fprintf(stderr, "Error: data structure has not been constructed properly\n");\r
-       exit(1);\r
-    }\r
-\r
     if (indexing_empty_texts)\r
        return x - 1 + rank_open(Par, x);\r
     else {\r
@@ -937,12 +858,7 @@ int XMLTree::NodeXMLId(treeNode x)
 \r
 // ParentNode(d): returns the parent node of document identifier d.\r
 treeNode XMLTree::ParentNode(DocID d) \r
- {\r
-    if (!finished) {\r
-       fprintf(stderr, "Error: data structure has not been constructed properly\n");\r
-       exit(1);\r
-    }\r
-    \r
+ {    \r
     if (d == NULLT)\r
       return NULLT;\r
     \r
@@ -960,12 +876,7 @@ treeNode XMLTree::ParentNode(DocID d)
      \r
  }\r
 treeNode XMLTree::PrevNode(DocID d) \r
- {\r
-    if (!finished) {\r
-       fprintf(stderr, "Error: data structure has not been constructed properly\n");\r
-       exit(1);\r
-    }\r
-    \r
+ {    \r
     if (d == NULLT)\r
       return NULLT;\r
     \r
@@ -984,288 +895,6 @@ treeNode XMLTree::PrevNode(DocID d)
  }\r
 \r
 \r
-// OpenDocument(empty_texts): it starts the construction of the data structure for\r
-// the XML document. Parameter empty_texts indicates whether we index empty texts\r
-// in document or not. Returns a non-zero value upon success, NULLT in case of error.\r
-int XMLTree::OpenDocument(bool empty_texts, int sample_rate_text,bool dtc)\r
- {\r
-    initialized = true;\r
-    finished = false;\r
-    found_attributes = false;\r
-    npar = 0;\r
-    parArraySize = 1;\r
-    ntagnames = 4;    \r
-    disable_tc = dtc;\r
-    \r
-    indexing_empty_texts = empty_texts;\r
-    \r
-    par_aux = (pb *)umalloc(sizeof(pb)*parArraySize);\r
-    \r
-    tags_aux = (TagType *) umalloc(sizeof(TagType));\r
-    \r
-    TagName = (unsigned char **) umalloc(4*sizeof(unsigned char*));\r
-\r
-    TagName[0] = (unsigned char *) umalloc(4*sizeof(unsigned char));\r
-\r
-    strcpy((char *) TagName[0], "<@>");\r
-\r
-    TagName[1] = (unsigned char *) umalloc(4*sizeof(unsigned char));\r
-\r
-    strcpy((char *) TagName[1], "<$>");\r
-    \r
-    //OJO need to put these in the table too.\r
-    TagName[2] = (unsigned char *) umalloc(5*sizeof(unsigned char));\r
-\r
-    strcpy((char *) TagName[2], "/<@>");\r
-\r
-    TagName[3] = (unsigned char *) umalloc(5*sizeof(unsigned char));\r
-\r
-    strcpy((char *) TagName[3], "/<$>");\r
-\r
-\r
-    if (!indexing_empty_texts) \r
-      empty_texts_aux = (unsigned int *)umalloc(sizeof(unsigned int));\r
-       \r
-    if (disable_tc)\r
-        TextBuilder = 0;\r
-    else \r
-        TextBuilder = new TextCollectionBuilder((unsigned)sample_rate_text);\r
-    Text = 0;\r
-    \r
-    return 1;  // indicates success in the initialization of the data structure\r
- }\r
-\r
-// CloseDocument(): it finishes the construction of the data structure for the XML\r
-// document. Tree and tags are represented in the final form, dynamic data \r
-// structures are made static, and the flag "finished" is set to true. After that, \r
-// the data structure can be queried.\r
-int XMLTree::CloseDocument()\r
- {\r
-    if (!initialized) {  // data structure has not been initialized properly\r
-       fprintf(stderr, "Error: data structure has not been initialized properly (by calling method OpenDocument)\n");\r
-       return NULLT;\r
-    }\r
-    \r
-    // closing parenthesis for the tree root\r
-    par_aux = (pb *)urealloc(par_aux, sizeof(pb)*(1+npar/(8*sizeof(pb))));\r
-    \r
-    // creates the data structure for the tree topology\r
-    Par = (bp *)umalloc(sizeof(bp));\r
-    bp_construct(Par, npar, par_aux, OPT_DEGREE|0);    \r
-    // creates structure for tags\r
-\r
-    // If we found an attribute then "<@>" is present in the tree\r
-    // if we didn't then it is not. "<$>" is never present in the tree\r
-               uint max_tag = 0;\r
-               for(uint i=0;i<(uint)npar-1;i++)\r
-                       max_tag = max(max_tag,tags_aux[i]);\r
-               //max_tag++;\r
-               //tags_aux = (TagType *) urealloc(tags_aux, sizeof(TagType)*(npar + 1));\r
-               //tags_aux[npar++] = max_tag;\r
-    //int ntagsize = found_attributes ? 2*ntagnames-1 : 2*ntagnames - 2;\r
-    int ntagsize = 2*ntagnames + 2;\r
-\r
-    //static_bitsequence_builder * bmb = new static_bitsequence_builder_brw32(20);\r
-    //static_permutation_builder * pmb = new static_permutation_builder_mrrr(PERM_SAMPLE, bmb);\r
-    //static_sequence_builder * ssb = new static_sequence_builder_gmr_chunk(bmb, pmb);\r
-               static_bitsequence_builder * bmb = new static_bitsequence_builder_sdarray();\r
-               alphabet_mapper *am = new alphabet_mapper_none();\r
-               //wt_coder * wc = new wt_coder_huff((uint*)tags_aux,npar,am);\r
-               //Tags = new static_sequence_wvtree((uint*)tags_aux,npar,wc ,bmb, am);\r
-    //Tags = new static_sequence_gmr((uint *) tags_aux, (uint) npar,ntagsize, bmb, ssb);\r
-               Tags = new static_sequence_bs((uint*)tags_aux,npar,am,bmb);\r
-               \r
-               cout << "Tags test: " << Tags->test((uint*)tags_aux,npar) << endl;\r
-\r
-               tags_blen = bits(max_tag);\r
-               tags_len = (uint)npar;\r
-               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_aux[i]);\r
-    \r
-    delete bmb;\r
-    //delete pmb;\r
-    //delete ssb;\r
-\r
-    \r
-    // makes the text collection static\r
-    if (!disable_tc)\r
-    {\r
-        assert(Text == 0);\r
-        assert(TextBuilder != 0);\r
-        Text = TextBuilder->InitTextCollection();\r
-        delete TextBuilder;\r
-        TextBuilder = 0;\r
-    }\r
-\r
-    // creates the data structure marking the non-empty texts (just in the case it is necessary)\r
-    if (!indexing_empty_texts)  {\r
-       EBVector = new static_bitsequence_rrr02((uint *)empty_texts_aux,(ulong)npar,(uint)32);\r
-       free (empty_texts_aux);\r
-       empty_texts_aux = NULL;\r
-    }\r
-   \r
-    // OJO was leaked before, found by valgrind\r
-    free(tags_aux);\r
-\r
-    tags_aux = NULL;\r
-\r
-    finished = true;\r
-               print_stats();\r
-\r
-    return 1; // indicates success in the inicialization\r
- }\r
-\r
-\r
-// NewOpenTag(tagname): indicates the event of finding a new opening tag in the document.\r
-// Tag name is given. Returns a non-zero value upon success, and returns NULLT\r
-// in case of failing when trying to insert the new tag.\r
-int XMLTree::NewOpenTag(unsigned char *tagname)\r
- {\r
-    int i;\r
-\r
-    if (!initialized) {  // data structure has not been initialized properly\r
-       fprintf(stderr, "Error: you cannot insert a new opening tag without first calling method OpenDocument first\n");\r
-       return NULLT;\r
-    }\r
-    \r
-    // inserts a new opening parentheses in the bit sequence\r
-    if (sizeof(pb)*8*parArraySize == npar) { // no space left for the new parenthesis\r
-       par_aux = (pb *)urealloc(par_aux, sizeof(pb)*2*parArraySize);\r
-       parArraySize *= 2;\r
-    }\r
-    \r
-    setbit(par_aux,npar,OP);  // marks a new opening 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
\r
-\r
-    // NewOpenTag("<@>") was called\r
-    if (i==0) \r
-      found_attributes=true;\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
-       if (!TagName) {\r
-          fprintf(stderr, "Error: not enough memory\n");\r
-          return NULLT;\r
-       }\r
-       \r
-       ntagnames++;\r
-       TagName[i] = (unsigned char *)umalloc(sizeof(unsigned char)*(strlen((const char *)tagname)+1));\r
-       strcpy((char *)TagName[i], (const char *)tagname);\r
-    } \r
-    tags_aux = (TagType *) urealloc(tags_aux, sizeof(TagType)*(npar + 1));\r
-\r
-    tags_aux[npar] = i; // inserts the new tag id within the preorder sequence of tags\r
-    \r
-    npar++;\r
-    \r
-    return 1;\r
-    \r
- }\r
-\r
-\r
-// NewClosingTag(tagname): indicates the event of finding a new closing tag in the document.\r
-// Tag name is given. Returns a non-zero value upon success, and returns NULLT\r
-// in case of failing when trying to insert the new tag.\r
-int XMLTree::NewClosingTag(unsigned char *tagname)\r
- {\r
-    int i;\r
-\r
-    if (!initialized) {  // data structure has not been initialized properly\r
-       fprintf(stderr, "Error: you cannot insert a new closing tag without first calling method OpenDocument first\n");\r
-       return NULLT;\r
-    }\r
-    \r
-    // inserts a new closing parentheses in the bit sequence\r
-    if (sizeof(pb)*8*parArraySize == npar) { // no space left for the new parenthesis\r
-       par_aux = (pb *)urealloc(par_aux, sizeof(pb)*2*parArraySize);\r
-       parArraySize *= 2;\r
-    }\r
-    \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]+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
-       ntagnames++;\r
-       TagName[i] = (unsigned char *)umalloc(sizeof(char)*(strlen((const char *)tagname)+2));\r
-       TagName[i][0] = '/';\r
-       strcpy((char *)&(TagName[i][1]), (const char *)tagname);\r
-    } \r
-\r
-    tags_aux = (TagType *)urealloc(tags_aux, sizeof(TagType)*(npar + 1));\r
-\r
-    tags_aux[npar] = i; // inserts the new tag id within the preorder sequence of tags\r
-    \r
-    npar++;\r
-\r
-    return 1; // success\r
-    \r
- }\r
-\r
-\r
-// NewText(s): indicates the event of finding a new (non-empty) text s in the document.\r
-// The new text is inserted within the text collection. Returns a non-zero value upon\r
-// success, NULLT in case of error.\r
-int XMLTree::NewText(unsigned char *s)\r
- {\r
-    if (!initialized) {  // data structure has not been initialized properly\r
-       fprintf(stderr, "Error: you cannot insert a new text without first calling method OpenDocument first\n");\r
-       return NULLT;\r
-    }\r
-\r
-    if (disable_tc) {\r
-      XMLTree::NewEmptyText();\r
-      return 1;\r
-    };\r
-\r
-    if (!indexing_empty_texts) {\r
-       empty_texts_aux = (unsigned int *)urealloc(empty_texts_aux, sizeof(pb)*(1+(npar-1)/(8*sizeof(pb))));\r
-              bitset(empty_texts_aux, npar-1);  // marks the non-empty text with a 1 in the bit vector\r
-    }\r
-    \r
-    TextBuilder->InsertText(s);\r
-    string cpps = (char*) s;\r
-    CachedText.push_back(cpps); \r
-    \r
-    return 1; // success\r
- }\r
-\r
-// NewEmptyText(): indicates the event of finding a new empty text in the document.\r
-// In case of indexing empty and non-empty texts, we insert the empty texts into the\r
-// text collection. In case of indexing only non-empty texts, it just indicates an\r
-// empty text in the bit vector of empty texts. Returns a non-zero value upon\r
-// success, NULLT in case of error.\r
-int XMLTree::NewEmptyText() \r
- {\r
-    unsigned char c = 0;\r
-    if (!initialized) {  // data structure has not been initialized properly\r
-       fprintf(stderr, "Error: you cannot insert a new empty text without first calling method OpenDocument first\n");\r
-       return NULLT;\r
-    }\r
-\r
-    if (!indexing_empty_texts) {\r
-       empty_texts_aux = (unsigned int *)urealloc(empty_texts_aux, sizeof(pb)*(1+(npar-1)/(8*sizeof(pb))));\r
-       \r
-       bitclean(empty_texts_aux, npar-1);  // marks the empty text with a 0 in the bit vector\r
-    }\r
-    else TextBuilder->InsertText(&c); // we insert the empty text just in case we index all the texts\r
-    \r
-    return 1; // success    \r
- }\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
@@ -1292,11 +921,9 @@ unsigned char *XMLTree::GetTagName(TagType tagid)
  }\r
 \r
 \r
-//KIM : OJO need the two following methods\r
-\r
 const unsigned char *XMLTree::GetTagNameByRef(TagType tagid)\r
  {\r
-               if(tagid==(uint)-1) return NULL;\r
+    if(tagid==(uint)-1) return NULL;\r
     if (tagid >= ntagnames) return NULL; // invalid tag identifier\r
     return ((const unsigned char*)  TagName[tagid]);\r
  }\r
@@ -1304,18 +931,17 @@ const unsigned char *XMLTree::GetTagNameByRef(TagType tagid)
 \r
 \r
 TagType XMLTree::RegisterTag(unsigned char *tagname)\r
-{\r
-  if (!finished)\r
-    return NULLT;\r
-  \r
-  TagType id = XMLTree::GetTagId(tagname);\r
-  if (id == NULLT){\r
-    id = ntagnames;\r
-    ntagnames = ntagnames + 1;    \r
-    TagName = (unsigned char **) urealloc(TagName,ntagnames*(sizeof(unsigned char*)));\r
-    TagName[id] = (unsigned char *) umalloc(sizeof(unsigned char)*strlen( (const char*) tagname)+1);\r
-    strcpy((char*)TagName[id], (const char *)tagname);  \r
-  };\r
-\r
-  return id;\r
-}\r
+ {  \r
+    TagType id = XMLTree::GetTagId(tagname);\r
+    if (id == NULLT) {\r
+       id = ntagnames;\r
+       ntagnames = ntagnames + 1;    \r
+       TagName = (unsigned char **) urealloc(TagName,ntagnames*(sizeof(unsigned char*)));\r
+       TagName[id] = (unsigned char *) umalloc(sizeof(unsigned char)*strlen( (const char*) tagname)+1);\r
+       strcpy((char*)TagName[id], (const char *)tagname);  \r
+    }\r
+\r
+    return id;\r
+ }\r
+\r
+\r
index 1268cb6..ea9b938 100644 (file)
--- a/XMLTree.h
+++ b/XMLTree.h
@@ -17,7 +17,7 @@
  *   along with this program; if not, write to the                            *\r
  *   Free Software Foundation, Inc.,                                          *\r
  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.                *\r
- ******************************************************************************/ \r
+ ******************************************************************************/\r
 \r
 #ifndef XMLTREE_H_\r
 #define XMLTREE_H_\r
 #include <stdlib.h>\r
 #include <cstring>\r
 \r
-//KIM : OJO\r
-//clash between TextCollection/Tools.h and libcds/includes/basics.h\r
+\r
 #undef W\r
 #undef WW\r
 #undef Wminusone\r
 \r
 #include "bp.h"\r
+//#include "basics.h"\r
 #include <static_bitsequence.h>\r
 #include <alphabet_mapper.h>\r
 #include <static_sequence.h>\r
@@ -47,11 +47,6 @@ using SXSI::TextCollectionBuilder;
 \r
 #define PERM_SAMPLE 10\r
 \r
-        // sets bit p in e\r
-#define bitset(e,p) ((e)[(p)/W] |= (1<<((p)%W)))\r
-        // cleans bit p in e\r
-#define bitclean(e,p) ((e)[(p)/W] &= ~(1<<((p)%W)))\r
-\r
 \r
 typedef int treeNode;\r
 typedef int TagType; \r
@@ -62,13 +57,15 @@ typedef struct {
    int max;\r
 } range;\r
 \r
+typedef struct nd {\r
+   uint position;\r
+   struct nd *next;\r
+} ListNode;\r
 \r
-//KIM : OJO\r
-// I know this class implements the working draft that we have but the logics seem flawed here...\r
-// We should have two classes. One XMLTreeBuilder and one XMLTree.\r
-// XMLTreeBuilder would have OpenDocument, NewOpenTag,... and CloseDocument would return an XMLTree\r
-// XMLTree would have only an initialized structure. If find it really ugly to check (!finished) or (!initialized)\r
-// in every function (FirstChild....).\r
+typedef struct {\r
+   ListNode *first;\r
+   ListNode *last;\r
+} TagArrayEntry;\r
 \r
 class XMLTree {\r
    /** Balanced parentheses representation of the tree */\r
@@ -76,59 +73,36 @@ class XMLTree {
  \r
    /** Mapping from tag identifer to tag name */  \r
    unsigned char **TagName;\r
+   uint ntagnames;\r
   \r
    /** boolean flag indicating whether we are indexing empty texts or not */\r
    bool indexing_empty_texts; \r
    \r
    /** Bit vector indicating with a 1 the positions of the non-empty texts. */\r
-   static_bitsequence_rrr02 *EBVector;  \r
+   static_bitsequence *EBVector;  \r
                      \r
    /** Tag sequence represented with a data structure for rank and select */\r
    static_sequence *Tags;\r
-        uint * tags_fix;\r
-        uint tags_blen, tags_len;\r
+   uint * tags_fix;\r
+   uint tags_blen, tags_len;\r
 \r
    /** The texts in the XML document */\r
-   TextCollectionBuilder *TextBuilder;\r
    TextCollection *Text;\r
 \r
    /** The texts in the XML document (cached for faster display) */\r
    vector<string> CachedText;\r
    \r
-   /** Flag indicating whether the whole data structure has been constructed or \r
-    * not. If the value is true, you cannot add more texts, nodes, etc. */\r
-   bool finished;\r
-\r
-   /** Flag indicating whether the construction of the data structure has been\r
-    * initialized or not (by calling method OpenDocument()). If this is true,\r
-    * you cannot insert new tags or texts. */\r
-   bool initialized;\r
-   \r
-   /* the following components are used for construction purposes */\r
-   pb *par_aux;\r
-   TagType *tags_aux;\r
-   int npar;\r
-   int parArraySize;\r
-   int ntagnames;\r
-   unsigned int *empty_texts_aux;\r
-\r
-   // KIM : OJO\r
-   // I added those two. The TagName array should always contains two special tags\r
-   // <@> for attributes and <$> for PCDATA.\r
-   // <$> can never be in a document (since we handle the text differently)\r
-   // but <@> can be returned by the parser. This boolean is needed for the construction\r
-   // of the Tag bitmap to know if <@> must be taken into account or not\r
-   bool found_attributes;\r
-\r
-   // KIM : OJO\r
+   TagArrayEntry *TagArray;\r
+\r
    // Allows to disable the TextCollection for benchmarkin purposes\r
    bool disable_tc;\r
    \r
 public:\r
-        void print_stats();\r
+   /** Data structure constructors */\r
+   XMLTree() {;};\r
 \r
-   /** Data structure constructor */\r
-   XMLTree() {finished = false; initialized = false; Text = 0; TextBuilder = 0; }; \r
+   XMLTree(pb *par, uint npar, unsigned char **TN, uint ntagnames, uint *empty_texts_bmp, TagType *tags,\r
+           TextCollection *TC, vector<string> CT, bool indexing_empty_t, bool dis_tc);\r
  \r
    /** Data structure destructor */\r
    ~XMLTree();\r
@@ -154,9 +128,9 @@ public:
    /** IsChild(x,y): returns whether node x is parent of node y. */\r
    bool IsChild(treeNode x, treeNode y);\r
 \r
-   /** IsChild(x,y): returns whether node x is the first child of its parent */\r
+   /** IsFirstChild(x): returns whether node x is the first child of its parent. */\r
    bool IsFirstChild(treeNode x);\r
-   \r
+\r
    /** NumChildren(x): number of children of node x. Constant time with the \r
     * data structure of Sadakane. */\r
    int NumChildren(treeNode x);\r
@@ -194,30 +168,37 @@ public:
     * Very fast in BP. */\r
    treeNode FirstChild(treeNode x);\r
    \r
-   /** LastChild(x): returns the last child of node x. \r
-    * Implemented by Kim naively. */\r
+   /** LastChild(x): returns the last child of node x.  */\r
    treeNode LastChild(treeNode x);\r
-\r
+   \r
    /** NextSibling(x): returns the next sibling of node x, assuming it \r
     * exists. */\r
-\r
    treeNode NextSibling(treeNode x);\r
    \r
    /** PrevSibling(x): returns the previous sibling of node x, assuming it \r
     * exists. */\r
    treeNode PrevSibling(treeNode x);\r
    \r
-   /** TaggedChild(x,i,tag): returns the i-th child of node x tagged tag, or \r
+   /** TaggedChild(x,tag): returns the first child of node x tagged tag, or \r
     * NULLT if there is none. Because of the balanced-parentheses representation \r
     * of the tree, this operation is not supported efficiently, just iterating \r
     * among the children of node x until finding the desired child. */\r
-   treeNode TaggedChild(treeNode x, int i, TagType tag);\r
+   treeNode TaggedChild(treeNode x, TagType tag);\r
    \r
+   treeNode SelectChild(treeNode x, TagType *tags, int ntags);\r
+\r
+   /** TaggedSibling(x,tag): returns the first sibling of node x tagged tag, or \r
+    *  NULLT if there is none. */\r
+   treeNode TaggedSibling(treeNode x, TagType tag);\r
+   \r
+   treeNode SelectSibling(treeNode x, TagType *tags, int ntags);\r
+\r
    /** TaggedDesc(x,tag): returns the first node tagged tag with larger \r
     * preorder than x and within the subtree of x. Returns NULT if there \r
     * is none. */\r
    treeNode TaggedDesc(treeNode x, TagType tag);\r
 \r
+   treeNode SelectDesc(treeNode x, TagType *tags, int ntags);\r
 \r
    treeNode TaggedBelow(treeNode x, TagType *childtags, unsigned int ctlen,\r
                                 TagType *desctags, unsigned int dtlen);\r
@@ -245,7 +226,9 @@ public:
    treeNode TaggedFoll(treeNode x, TagType tag);\r
 \r
    treeNode TaggedFollBelow(treeNode x, TagType tag,treeNode root);     \r
-  \r
+   \r
+   treeNode SelectFollBelow(treeNode x, TagType *tags, int ntags, treeNode ctx);\r
+\r
    /** TaggedFollowingSibling(x,tag) */\r
    treeNode TaggedFollowingSibling(treeNode x, TagType tag);\r
 \r
@@ -275,48 +258,9 @@ public:
    \r
    /** ParentNode(d): returns the parent node of document identifier d. */\r
    treeNode ParentNode(DocID d);\r
+   \r
    treeNode PrevNode(DocID d);\r
 \r
-   /** OpenDocument(empty_texts,sample_rate_text,dtc): initilizes the construction\r
-    * of the data structure for the XML document. Parameter empty_texts \r
-    * indicates whether we index empty texts in document or not. Parameter \r
-    * sample_rate_text indicates the sampling rate for the text searching data\r
-    * structures (small values get faster searching but a bigger space \r
-    * requirement). dtc disable the use of the TextCollection\r
-    * (i.e. everything is considered an empty text *)\r
-    * Returns a non-zero value upon success, NULLT in case of \r
-    * error. */\r
-\r
-   int OpenDocument(bool empty_texts, int sample_rate_text, bool dtc);\r
-\r
-   /** CloseDocument(): finishes the construction of the data structure for \r
-    * the XML document. Tree and tags are represented in the final form, \r
-    * dynamic data structures are made static, and the flag "finished" is set \r
-    * to true. After that, the data structure can be queried. */\r
-   int CloseDocument();\r
-\r
-   /** NewOpenTag(tagname): indicates the event of finding a new opening tag \r
-    * in the document. Tag name is given. Returns a non-zero value upon \r
-    * success, and returns NULLT in case of error. */\r
-   int NewOpenTag(unsigned char *tagname);\r
-   \r
-   /** NewClosingTag(tagname): indicates the event of finding a new closing tag\r
-    *  in the document. Tag name is given. Returns a non-zero value upon \r
-    *  success, and returns NULLT in case of error. */\r
-   int NewClosingTag(unsigned char *tagname);\r
\r
-   /** NewText(s): indicates the event of finding a new (non-empty) text s in \r
-    * the document. The new text is inserted within the text collection. \r
-    * Returns a non-zero value upon success, NULLT in case of error. */\r
-   int NewText(unsigned char *s);\r
-\r
-   /** NewEmptyText(): indicates the event of finding a new empty text in the \r
-    * document. In case of indexing empty and non-empty texts, we insert the \r
-    * empty texts into the text collection. In case of indexing only non-empty\r
-    * texts, it just indicates an empty text in the bit vector of empty texts. \r
-    * Returns a non-zero value upon success, NULLT in case of error. */\r
-   int NewEmptyText();\r
-\r
    /** GetTagId(tagname): returns the tag identifier corresponding to a given \r
     * tag name. Returns NULLT in case that the tag name does not exists. */\r
    TagType GetTagId(unsigned char *tagname);\r
@@ -325,14 +269,11 @@ public:
     * Returns NULL in case that the tag identifier is not valid.*/\r
    unsigned char *GetTagName(TagType tagid);\r
 \r
-\r
-   // OJO\r
    /** GetTagName(tagid): returns the tag name of a given tag identifier.     \r
     *  The result is just a reference and should not be freed by the caller.\r
     */\r
    const unsigned char *GetTagNameByRef(TagType tagid);\r
 \r
-   //OJO\r
    /** RegisterTag adds a new tag to the tag collection this is needed\r
     * if the query contains a tag which is not in the document, we need\r
     * to give this new tag a fresh id and store it somewhere. A logical\r
@@ -344,6 +285,7 @@ public:
    bool EmptyText(DocID i) {\r
        return Text->EmptyText(i);\r
    }\r
+\r
    /** Prefix(s): search for texts prefixed by string s. */\r
    TextCollection::document_result Prefix(uchar const *s) {\r
       return Text->Prefix(s);\r
@@ -425,7 +367,7 @@ public:
    \r
    /** CountLessThan(s): counting version of LessThan(s). */\r
    unsigned CountLessThan(uchar const *s) {\r
-      return CountLessThan(s);\r
+      return Text->CountLessThan(s);\r
    }\r
    \r
    /** GetText(d): returns the text corresponding to document with\r
@@ -443,11 +385,17 @@ public:
    TextCollection *getTextCollection() {\r
       return Text;\r
    }\r
+   \r
    /** Save: saves XML tree data structure to file. */\r
    void Save(unsigned char *filename);\r
       \r
    /** Load: loads XML tree data structure from file. sample_rate_text \r
     * indicates the sample rate for the text search data structure. */\r
    static XMLTree *Load(unsigned char *filename, int sample_rate_text);   \r
+\r
+   void insertTag(TagType tag, uint position);\r
+   \r
+   void print_stats();\r
 };\r
 #endif\r
+\r
diff --git a/XMLTreeBuilder.cpp b/XMLTreeBuilder.cpp
new file mode 100644 (file)
index 0000000..27bc862
--- /dev/null
@@ -0,0 +1,196 @@
+\r
+#include "XMLTreeBuilder.h"\r
+#include "basics.h"\r
+\r
+// OpenDocument(empty_texts): it starts the construction of the data structure for\r
+// the XML document. Parameter empty_texts indicates whether we index empty texts\r
+// in document or not. Returns a non-zero value upon success, NULLT in case of error.\r
+int XMLTreeBuilder::OpenDocument(bool empty_texts, int sample_rate_text, bool dtc)\r
+ {\r
+    found_attributes = false;\r
+    npar = 0;\r
+    parArraySize = 1;\r
+    ntagnames = 4;    \r
+    disable_tc = dtc;\r
+    \r
+    indexing_empty_texts = empty_texts;\r
+    \r
+    par_aux = (pb *)umalloc(sizeof(pb)*parArraySize);\r
+    \r
+    tags_aux = (TagType *) umalloc(sizeof(TagType));\r
+    \r
+    TagName = (unsigned char **) umalloc(4*sizeof(unsigned char*));\r
+    TagName[0] = (unsigned char *) umalloc(4*sizeof(unsigned char));\r
+    strcpy((char *) TagName[0], "<@>");\r
+    TagName[1] = (unsigned char *) umalloc(4*sizeof(unsigned char));\r
+    strcpy((char *) TagName[1], "<$>");\r
+    TagName[2] = (unsigned char *) umalloc(5*sizeof(unsigned char));\r
+    strcpy((char *) TagName[2], "/<@>");\r
+    TagName[3] = (unsigned char *) umalloc(5*sizeof(unsigned char));\r
+    strcpy((char *) TagName[3], "/<$>");\r
+\r
+    if (!indexing_empty_texts) \r
+      empty_texts_aux = (unsigned int *)umalloc(sizeof(unsigned int));\r
+       \r
+    if (disable_tc)\r
+        TextBuilder = 0;\r
+    else \r
+        TextBuilder = new TextCollectionBuilder((unsigned)sample_rate_text);\r
+    Text = 0;\r
+    \r
+    return 1;  // indicates success in the initialization of the data structure\r
+ }\r
+\r
+// CloseDocument(): it finishes the construction of the data structure for the XML\r
+// document. Tree and tags are represented in the final form, dynamic data \r
+// structures are made static, and the flag "finished" is set to true. After that, \r
+// the data structure can be queried.\r
+XMLTree *XMLTreeBuilder::CloseDocument()\r
+ {    \r
+    // closing parenthesis for the tree root\r
+    par_aux = (pb *)urealloc(par_aux, sizeof(pb)*(1+npar/(8*sizeof(pb))));\r
+    setbit(par_aux, npar, CP);\r
+    npar++;\r
+    \r
+    // makes the text collection static\r
+    if (!disable_tc) {\r
+       assert(Text == 0);\r
+       assert(TextBuilder != 0);\r
+       Text = TextBuilder->InitTextCollection();\r
+       delete TextBuilder;\r
+       TextBuilder = 0;\r
+    }\r
+\r
+    XMLTree *T = new XMLTree(par_aux, npar, TagName, ntagnames, empty_texts_aux, tags_aux, \r
+              Text, CachedText, indexing_empty_texts, disable_tc);\r
+    return T; \r
+ }\r
+\r
+\r
+// NewOpenTag(tagname): indicates the event of finding a new opening tag in the document.\r
+// Tag name is given. Returns a non-zero value upon success, and returns NULLT\r
+// in case of failing when trying to insert the new tag.\r
+int XMLTreeBuilder::NewOpenTag(unsigned char *tagname)\r
+ {\r
+    int i;\r
+    \r
+    // inserts a new opening parentheses in the bit sequence\r
+    if (sizeof(pb)*8*parArraySize == npar) { // no space left for the new parenthesis\r
+       par_aux = (pb *)urealloc(par_aux, sizeof(pb)*2*parArraySize);\r
+       parArraySize *= 2;\r
+    }\r
+    \r
+    setbit(par_aux,npar,OP);  // marks a new opening 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
\r
+\r
+    // NewOpenTag("<@>") was called\r
+    if (i==0) \r
+      found_attributes=true;\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
+       if (!TagName) {\r
+          fprintf(stderr, "Error: not enough memory\n");\r
+          return NULLT;\r
+       }\r
+       \r
+       ntagnames++;\r
+       TagName[i] = (unsigned char *)umalloc(sizeof(unsigned char)*(strlen((const char *)tagname)+1));\r
+       strcpy((char *)TagName[i], (const char *)tagname);\r
+    } \r
+    tags_aux = (TagType *) urealloc(tags_aux, sizeof(TagType)*(npar + 1));\r
+\r
+    tags_aux[npar] = i; // inserts the new tag id within the preorder sequence of tags\r
+    \r
+    npar++;\r
+    \r
+    return 1; // success    \r
+ }\r
+\r
+\r
+// NewClosingTag(tagname): indicates the event of finding a new closing tag in the document.\r
+// Tag name is given. Returns a non-zero value upon success, and returns NULLT\r
+// in case of failing when trying to insert the new tag.\r
+int XMLTreeBuilder::NewClosingTag(unsigned char *tagname)\r
+ {\r
+    int i;\r
+    \r
+    // inserts a new closing parentheses in the bit sequence\r
+    if (sizeof(pb)*8*parArraySize == npar) { // no space left for the new parenthesis\r
+       par_aux = (pb *)urealloc(par_aux, sizeof(pb)*2*parArraySize);\r
+       parArraySize *= 2;\r
+    }\r
+    \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]+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
+       ntagnames++;\r
+       TagName[i] = (unsigned char *)umalloc(sizeof(char)*(strlen((const char *)tagname)+2));\r
+       TagName[i][0] = '/';\r
+       strcpy((char *)&(TagName[i][1]), (const char *)tagname);\r
+    } \r
+\r
+    tags_aux = (TagType *)urealloc(tags_aux, sizeof(TagType)*(npar + 1));\r
+\r
+    tags_aux[npar] = i; // inserts the new tag id within the preorder sequence of tags\r
+    \r
+    npar++;\r
+\r
+    return 1; // success\r
+ }\r
+\r
+\r
+// NewText(s): indicates the event of finding a new (non-empty) text s in the document.\r
+// The new text is inserted within the text collection. Returns a non-zero value upon\r
+// success, NULLT in case of error.\r
+int XMLTreeBuilder::NewText(unsigned char *s)\r
+ {\r
+    if (disable_tc) {\r
+      XMLTreeBuilder::NewEmptyText();\r
+      return 1;\r
+    }\r
+\r
+    if (!indexing_empty_texts) {\r
+       empty_texts_aux = (unsigned int *)urealloc(empty_texts_aux, sizeof(pb)*(1+(npar-1)/(8*sizeof(pb))));\r
+              bitset(empty_texts_aux, npar-1);  // marks the non-empty text with a 1 in the bit vector\r
+    }\r
+    \r
+    TextBuilder->InsertText(s);\r
+    string cpps = (char*) s;\r
+    CachedText.push_back(cpps); \r
+    \r
+    return 1; // success\r
+ }\r
+\r
+// NewEmptyText(): indicates the event of finding a new empty text in the document.\r
+// In case of indexing empty and non-empty texts, we insert the empty texts into the\r
+// text collection. In case of indexing only non-empty texts, it just indicates an\r
+// empty text in the bit vector of empty texts. Returns a non-zero value upon\r
+// success, NULLT in case of error.\r
+int XMLTreeBuilder::NewEmptyText() \r
+ {\r
+    unsigned char c = 0;\r
+\r
+    if (!indexing_empty_texts) {\r
+       empty_texts_aux = (unsigned int *)urealloc(empty_texts_aux, sizeof(pb)*(1+(npar-1)/(8*sizeof(pb))));\r
+       \r
+       bitclean(empty_texts_aux, npar-1);  // marks the empty text with a 0 in the bit vector\r
+    }\r
+    else TextBuilder->InsertText(&c); // we insert the empty text just in case we index all the texts\r
+    \r
+    return 1; // success    \r
+ }\r
+\r
diff --git a/XMLTreeBuilder.h b/XMLTreeBuilder.h
new file mode 100644 (file)
index 0000000..38d81ba
--- /dev/null
@@ -0,0 +1,131 @@
+\r
+/******************************************************************************\r
+ *   Copyright (C) 2009 by Diego Arroyuelo                                    *\r
+ *   Builder class for the in-memory XQuery/XPath engine                      *\r
+ *                                                                            *\r
+ *   This program is free software; you can redistribute it and/or modify     *\r
+ *   it under the terms of the GNU Lesser General Public License as published *\r
+ *   by the Free Software Foundation; either version 2 of the License, or     *\r
+ *   (at your option) any later version.                                      *\r
+ *                                                                            *\r
+ *   This program is distributed in the hope that it will be useful,          *\r
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of           *\r
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            *\r
+ *   GNU Lesser General Public License for more details.                      *\r
+ *                                                                            *\r
+ *   You should have received a copy of the GNU Lesser General Public License *\r
+ *   along with this program; if not, write to the                            *\r
+ *   Free Software Foundation, Inc.,                                          *\r
+ *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.                *\r
+ ******************************************************************************/ \r
+\r
+#ifndef XMLTREEBUILDER_H_\r
+#define XMLTREEBUILDER_H_\r
+#include "TextCollection/TextCollectionBuilder.h"\r
+#include <stdio.h>\r
+#include <stdlib.h>\r
+#include <cstring>\r
+\r
+\r
+#undef W\r
+#undef WW\r
+#undef Wminusone\r
+\r
+#include "bp.h"\r
+#include "XMLTree.h"\r
+#include <static_bitsequence.h>\r
+#include <alphabet_mapper.h>\r
+#include <static_sequence.h>\r
+using SXSI::TextCollection;\r
+using SXSI::TextCollectionBuilder;\r
+\r
+#define NULLT -1\r
+\r
+        // sets bit p in e\r
+#define bitset(e,p) ((e)[(p)/W] |= (1<<((p)%W)))\r
+        // cleans bit p in e\r
+#define bitclean(e,p) ((e)[(p)/W] &= ~(1<<((p)%W)))\r
+\r
+\r
+class XMLTreeBuilder {\r
+  \r
+   /** Array containing the balanced parentheses sequence */\r
+   pb *par_aux;\r
+   int parArraySize;\r
+   int npar;\r
+\r
+   /** Mapping from tag identifer to tag name */  \r
+   unsigned char **TagName;\r
+   int ntagnames;\r
+\r
+   /** Array containing the sequence of tags */\r
+   TagType *tags_aux;\r
+   \r
+   /** The texts in the XML document */\r
+   TextCollectionBuilder *TextBuilder;\r
+   TextCollection *Text;\r
+   \r
+   /** The texts in the XML document (cached for faster display) */\r
+   vector<string> CachedText;\r
+\r
+   /** boolean flag indicating whether we are indexing empty texts or not */\r
+   bool indexing_empty_texts; \r
+   unsigned int *empty_texts_aux;\r
+\r
+   // The TagName array should always contains two special tags\r
+   // <@> for attributes and <$> for PCDATA.\r
+   // <$> can never be in a document (since we handle the text differently)\r
+   // but <@> can be returned by the parser. This boolean is needed for the construction\r
+   // of the Tag bitmap to know if <@> must be taken into account or not\r
+   bool found_attributes;\r
+\r
+   // Allows to disable the TextCollection for benchmarkin purposes\r
+   bool disable_tc;\r
+\r
+public:\r
+\r
+   XMLTreeBuilder() {;};\r
+\r
+   ~XMLTreeBuilder();\r
+   \r
+   /** OpenDocument(empty_texts,sample_rate_text,dtc): initilizes the construction\r
+    * of the data structure for the XML document. Parameter empty_texts \r
+    * indicates whether we index empty texts in document or not. Parameter \r
+    * sample_rate_text indicates the sampling rate for the text searching data\r
+    * structures (small values get faster searching but a bigger space \r
+    * requirement). dtc disable the use of the TextCollection\r
+    * (i.e. everything is considered an empty text *)\r
+    * Returns a non-zero value upon success, NULLT in case of \r
+    * error. */\r
+   int OpenDocument(bool empty_texts, int sample_rate_text, bool dtc);\r
+\r
+   /** CloseDocument(): finishes the construction of the data structure for \r
+    * the XML document. Tree and tags are represented in the final form, \r
+    * dynamic data structures are made static, returning the resulting\r
+    * XMLTree. After that, the XMLTree data structure can be queried. */\r
+   XMLTree *CloseDocument();\r
+\r
+   /** NewOpenTag(tagname): indicates the event of finding a new opening tag \r
+    * in the document. Tag name is given. Returns a non-zero value upon \r
+    * success, and returns NULLT in case of error. */\r
+   int NewOpenTag(unsigned char *tagname);\r
+   \r
+   /** NewClosingTag(tagname): indicates the event of finding a new closing tag\r
+    *  in the document. Tag name is given. Returns a non-zero value upon \r
+    *  success, and returns NULLT in case of error. */\r
+   int NewClosingTag(unsigned char *tagname);\r
\r
+   /** NewText(s): indicates the event of finding a new (non-empty) text s in \r
+    * the document. The new text is inserted within the text collection. \r
+    * Returns a non-zero value upon success, NULLT in case of error. */\r
+   int NewText(unsigned char *s);\r
+\r
+   /** NewEmptyText(): indicates the event of finding a new empty text in the \r
+    * document. In case of indexing empty and non-empty texts, we insert the \r
+    * empty texts into the text collection. In case of indexing only non-empty\r
+    * texts, it just indicates an empty text in the bit vector of empty texts. \r
+    * Returns a non-zero value upon success, NULLT in case of error. */\r
+   int NewEmptyText();\r
+};\r
+#endif\r
+\r
diff --git a/basics.h b/basics.h
new file mode 100644 (file)
index 0000000..273aa1d
--- /dev/null
+++ b/basics.h
@@ -0,0 +1,51 @@
+#ifndef BASICS_H\r
+#define BASICS_H\r
+\r
+#include <stdio.h>\r
+#include <stdlib.h>\r
+\r
+inline void ufread(void *ptr, size_t size, size_t nmemb, FILE *stream)\r
+ {\r
+    size_t res;\r
+    res = fread(ptr,size,nmemb,stream);\r
+    if (res < nmemb)\r
+       throw "ufread I/O error";\r
+    return;\r
+ }\r
+\r
+inline void ufwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream)\r
+ {\r
+    size_t res;\r
+    res = fwrite(ptr,size,nmemb,stream);\r
+    if (res < nmemb)\r
+       throw "ufwrite I/O error";\r
+    return;\r
+ }\r
+\r
+inline void *urealloc(void *ptr, size_t size)\r
+ {\r
+    void *dest = realloc(ptr,size);\r
+    //don't fail if we requested size 0\r
+    if (dest == NULL && size > 0 )\r
+       throw std::bad_alloc();\r
+    return dest;\r
+ }\r
+\r
+inline void *ucalloc(size_t nmemb, size_t size)\r
+ {\r
+    void * dest = calloc(nmemb,size);\r
+    //don't fail if we requested size 0\r
+    if (dest == NULL && nmemb > 0 && size > 0 )\r
+       throw std::bad_alloc();\r
+    return dest;\r
+ }\r
+\r
+inline void *umalloc(size_t size) \r
+ {\r
+    void * dest = malloc(size);\r
+    if (dest == NULL && size > 0)\r
+       throw std::bad_alloc();\r
+    return dest;\r
+ }\r
+\r
+#endif\r
index bf21063..cd4b721 100644 (file)
--- a/makefile
+++ b/makefile
@@ -7,15 +7,16 @@ OBJECTS_TCA=TextCollection/incbwt/rlcsa.a
 \r
 all: libcds text_collection XMLTree\r
 \r
-XMLTree: XMLTree.o bp.o darray.o bpcore.o $(LIBCDS_A) $(OBJECTS_TCO) $(OBJECTS_TCA)\r
+XMLTree: XMLTree.o XMLTreeBuilder.o bp.o darray.o bpcore.o\\r
+ $(LIBCDS_A) $(OBJECTS_TCO) $(OBJECTS_TCA)\r
        cp libcds/lib/libcds.a XMLTree.a\r
        rm -rf .objs\r
        mkdir .objs\r
        cd .objs; ar x ../$(OBJECTS_TCA)\r
-       ar rvcs XMLTree.a XMLTree.o bp.o darray.o bpcore.o $(OBJECTS_TCO) .objs/*.o\r
+       ar rvcs XMLTree.a XMLTree.o XMLTreeBuilder.o  bp.o darray.o bpcore.o $(OBJECTS_TCO) .objs/*.o\r
        rm -rf .objs\r
 \r
-XMLTree.o: bp.c bp.h bpcore.c XMLTree.cpp XMLTree.h\r
+XMLTree.o: bp.c bp.h bpcore.c XMLTree.cpp XMLTree.h basics.h\r
        g++ $(FLAGS) -c XMLTree.cpp\r
 \r
 bp.o: bp.c bpcore.c darray.c bp.h darray.h\r
@@ -27,6 +28,9 @@ darray.o: darray.c darray.h bpcore.c
 bpcore.o: bpcore.c\r
        g++ $(FLAGS) -c bpcore.c\r
 \r
+XMLTreeBuilder.o: XMLTree.h XMLTreeBuilder.h XMLTreeBuilder.cpp basics.h\r
+       g++ $(FLAGS) -c XMLTreeBuilder.cpp\r
+\r
 text_collection:\r
        make -C TextCollection/\r
 \r