Merge the last bit of Diego's code with the modifications:
authorkim <kim@3cdefd35-fc62-479d-8e8d-bae585ffb9ca>
Thu, 30 Apr 2009 09:32:20 +0000 (09:32 +0000)
committerkim <kim@3cdefd35-fc62-479d-8e8d-bae585ffb9ca>
Thu, 30 Apr 2009 09:32:20 +0000 (09:32 +0000)
- Remove all deprecated function
- Add description of the XML encoding
- Use vector<T> when possible instead of T*
- lot more stuff...

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

XMLTree.cpp
XMLTree.h
XMLTreeBuilder.cpp
XMLTreeBuilder.h
basics.h

index ffdf5d0..61f6a4d 100644 (file)
@@ -1,6 +1,7 @@
-#include "XMLTree.h"\r
+\r
 #include "basics.h"\r
 #include <cstring>\r
+#include "XMLTree.h"\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
 // tag position -> tree node\r
 inline treeNode tagpos2node(int t) \r
  {\r
-    return (treeNode)t;\r
+    return (treeNode) t;\r
  }\r
 \r
 // tree node -> tag position\r
 inline int node2tagpos(treeNode x) \r
- {\r
-    return (int)x;\r
- }\r
+{\r
+  return (int)x;\r
+}\r
+\r
+// returns NULLT if the test is true\r
+#define NULLT_IF(x)  do { if (x) return NULLT; } while (0)\r
 \r
 \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
+XMLTree::XMLTree( pb * const par, uint npar,  vector<string> * const TN,  TagIdMap * const tim, uint *empty_texts_bmp, TagType *tags,\r
+            TextCollection * const TC, vector<string> * const CT, bool dis_tc)\r
+\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
-\r
+    bp_construct(Par, npar, (pb*) par, OPT_DEGREE|0);    \r
+   \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
-    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
+    TagName = (vector<string>*)TN;\r
+    tIdMap = (TagIdMap *) tim;\r
+    \r
+    uint max_tag = TN->size() - 1;\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
@@ -56,24 +56,14 @@ XMLTree::XMLTree(pb *par, uint npar, unsigned char **TN, uint ntagnm, uint *empt
     free(tags);\r
     tags = NULL;\r
     \r
-    Text = TC;\r
-\r
-    CachedText = CT;\r
+    Text = (TextCollection*) TC;\r
 \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
+    CachedText = (vector<string>*) CT;\r
+    EBVector = new static_bitsequence_rrr02(empty_texts_bmp,npar,32);\r
+    free(empty_texts_bmp);\r
+    empty_texts_bmp = NULL;\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
+    \r
     disable_tc = dis_tc;\r
  }\r
 \r
@@ -86,23 +76,22 @@ XMLTree::~XMLTree()
     destroyTree(Par);\r
     free(Par); // frees the memory of struct Par\r
    \r
-    for (i=0; i<ntagnames;i++) \r
-       free(TagName[i]);\r
+    delete tIdMap;\r
+    tIdMap = NULL;\r
+    \r
+    delete TagName;\r
+    TagName = NULL;\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
+    delete EBVector;\r
+    EBVector = NULL;\r
+\r
+\r
  }\r
 \r
 \r
@@ -118,177 +107,153 @@ void XMLTree::print_stats()
  }\r
 \r
 // Save: saves XML tree data structure to file. \r
-void XMLTree::Save(unsigned char *filename\r
+void XMLTree::Save(int fd\r
  {\r
     FILE *fp;\r
     char filenameaux[1024];\r
     int i;\r
-   \r
-    sprintf(filenameaux, "%s.srx", filename);\r
-    fp = fopen(filenameaux, "w");\r
-    if (fp == NULL) {\r
-       printf("Error: cannot create file %s to store the tree structure of XML collection\n", filenameaux);\r
-       exit(1);\r
-    } \r
-    \r
+\r
+    fp = fdopen(fd, "wa");\r
     // first stores the tree topology\r
     saveTree(Par, fp);\r
\r
+\r
     // stores the table with tag names\r
-    ufwrite(&ntagnames, sizeof(int), 1, fp);\r
-    for (i=0; i<ntagnames;i++)\r
-      fprintf(fp, "%s\n",TagName[i]);\r
-    \r
-    \r
-    // stores the flags\r
-    ufwrite(&indexing_empty_texts, 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
+    int ntags = TagName->size();\r
+\r
+    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
     // 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
 \r
+    // flags \r
+    ufwrite(&disable_tc, sizeof(bool),1,fp);\r
+    \r
+    //text positions\r
+    EBVector->save(fp);\r
+    \r
     // stores the texts   \r
     if (!disable_tc) {\r
-      Text->Save(fp);\r
-      int st = CachedText.size();\r
+      int st = CachedText->size();\r
+\r
       ufwrite(&st, sizeof(int),1,fp);\r
-      for (int i = 0; i< CachedText.size(); i++){\r
-       st = CachedText.at(i).size();\r
+      for (int i = 0; i< CachedText->size(); i++){\r
+       st = CachedText->at(i).size();\r
+\r
        ufwrite(&st, sizeof(int),1,fp);\r
-       ufwrite(CachedText.at(i).c_str(),sizeof(char),1+CachedText.at(i).size(),fp);\r
+\r
+       ufwrite(CachedText->at(i).c_str(),sizeof(char),1+CachedText->at(i).size(),fp);\r
+\r
       };\r
+      \r
+      Text->Save(fp);\r
     };\r
-    fclose(fp);\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(unsigned char *filename, int sample_rate_text\r
+XMLTree *XMLTree::Load(int fd\r
  {\r
     FILE *fp;\r
     char buffer[1024];\r
     XMLTree *XML_Tree;\r
     int i;\r
-    size_t s_tree = 0;\r
-    long s_text = 0;\r
-    size_t s_tags = 0;\r
-\r
-    // first load the tree topology\r
-    sprintf(buffer, "%s.srx", filename);\r
-    fp = fopen(buffer, "r");\r
-    if (fp == NULL) {\r
-       printf("Error: cannot open file %s to load the tree structure of XML collection\n", buffer);\r
-       exit(1);\r
-    } \r
-\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
+    fp = fdopen(fd, "r");\r
 \r
-    // stores the table with tag names\r
-    ufread(&XML_Tree->ntagnames, sizeof(int), 1, fp);\r
-\r
-    s_tree += sizeof(int);\r
+    XML_Tree = new XMLTree();\r
 \r
-    XML_Tree->TagName = (unsigned char **)umalloc(XML_Tree->ntagnames*sizeof(unsigned char *));\r
+    // Load the tree structure\r
+    XML_Tree->Par = (bp *)umalloc(sizeof(bp));\r
 \r
-    s_tags += sizeof(unsigned char*)*XML_Tree->ntagnames;\r
+    loadTree(XML_Tree->Par, fp); \r
 \r
+    XML_Tree->TagName = new vector<string>();\r
+    XML_Tree->tIdMap = new std::unordered_map<string,int>();\r
+    \r
+    string s;\r
+    int ntags;\r
+    \r
+    // Load the tag names\r
+    ufread(&ntags, sizeof(int), 1, fp);\r
 \r
-    for (i=0; i<XML_Tree->ntagnames;i++) {\r
-       char * r = fgets(buffer,1023,fp);\r
+    for (i=0; i<ntags;i++) {\r
+      char * r = fgets(buffer,1023,fp);\r
        if (r==NULL)\r
         throw "Cannot read tag list";\r
+       s = (const char*) buffer;\r
+       // remove the trailing \n\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
-       // strlen is actually the right size, since there is a trailing '\n'\r
-       int len = strlen((const char*)buffer);\r
-       XML_Tree->TagName[i] = (unsigned char *)ucalloc(len,sizeof(char));\r
-       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
-    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
-    s_tree+=sizeof(bool)*4;\r
-\r
-    if (!(XML_Tree->indexing_empty_texts)) XML_Tree->EBVector = static_bitsequence_rrr02::load(fp);\r
-\r
-    s_tree+= XML_Tree->EBVector->size();\r
 \r
-    // loads the tags\r
+    // loads the tag structure\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
-    s_tree+= XML_Tree->Tags->size();\r
-\r
 \r
+    // TODO ask francisco about this\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
+      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
+    XML_Tree->Tags->test(seq,XML_Tree->tags_len);\r
     delete [] seq;\r
     /// End ugly tests\r
+    \r
 \r
-    s_text = ftell(fp);\r
+    // loads the flags\r
+    \r
+    ufread(&(XML_Tree->disable_tc), sizeof(bool), 1, fp);\r
+\r
+    XML_Tree->EBVector = static_bitsequence_rrr02::load(fp);\r
 \r
+    // Not used  \r
+    int sample_rate_text = 64;\r
     // loads the texts\r
     if (!XML_Tree->disable_tc){\r
-      XML_Tree->Text = TextCollection::Load(fp,sample_rate_text);\r
+      XML_Tree->CachedText = new vector<string>;\r
       int sst;\r
       int st;\r
       ufread(&sst, sizeof(int),1,fp);\r
+      \r
       for (int i=0;i<sst;i++){\r
        ufread(&st, sizeof(int),1,fp);\r
        char* str = (char*) malloc(sizeof(char)*st+1);\r
        ufread(str,sizeof(char),st+1,fp);\r
        string cppstr = str;\r
-       XML_Tree->CachedText.push_back(cppstr);\r
+       XML_Tree->CachedText->push_back(cppstr);\r
        free(str);\r
-      };\r
 \r
+      };\r
+      XML_Tree->Text = TextCollection::Load(fp,sample_rate_text);\r
     }\r
     else XML_Tree->Text = NULL;\r
-\r
-    s_text = ftell(fp) - s_text;\r
-\r
-    fclose(fp);\r
-\r
-    XML_Tree->print_stats();\r
+    \r
     return XML_Tree;\r
  }\r
 \r
 \r
 // root(): returns the tree root.\r
-treeNode XMLTree::Root() \r
+inline treeNode XMLTree::Root() \r
  {\r
-    return root_node(Par);\r
+   return 0; //root_node(Par);\r
  }\r
 \r
 // SubtreeSize(x): the number of nodes (and attributes) in the subtree of node x.\r
@@ -372,7 +337,7 @@ int XMLTree::Postorder(treeNode x)
 // Tag(x): returns the tag identifier of node x.\r
 TagType XMLTree::Tag(treeNode x) \r
  {\r
-    return get_field(tags_fix,tags_blen,node2tagpos(x)); //Tags->access(node2tagpos(x));\r
+    return 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
@@ -386,23 +351,19 @@ range XMLTree::DocIds(treeNode x)
        return 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
+    \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
     }\r
-    else { // we are not indexing empty texts, we need rank\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
-       }\r
-       else { // the range is non-empty, there are texts within the subtree of x\r
-          r.min = min+1;\r
-          r.max = max;\r
-       }\r
+    else { // the range is non-empty, there are texts within the subtree of x\r
+      r.min = min+1;\r
+      r.max = max;\r
     }\r
     return r;\r
+\r
  }\r
 \r
 // Parent(x): returns the parent node of node x.\r
@@ -419,38 +380,51 @@ treeNode XMLTree::Child(treeNode x, int i)
 {\r
     if (i <= OPTD) return naive_child(Par, x, i);\r
     else return child(Par, x, i);\r
- }\r
+}\r
 \r
 // FirstChild(x): returns the first child of node x, assuming it exists. Very fast in BP.\r
 treeNode XMLTree::FirstChild(treeNode x) \r
  {\r
-    return first_child(Par, x);\r
+   NULLT_IF(x==NULLT);\r
+   return first_child(Par, x);\r
+ }\r
+\r
+treeNode XMLTree::FirstElement(treeNode x) \r
+ {\r
+   NULLT_IF(x==NULLT);\r
+   treeNode fc = first_child(Par, x);\r
+   //<$> is 2\r
+   return ((fc == NULLT || Tag(fc) != PCDATA_TAG_ID) ? fc : next_sibling(Par,fc));\r
+\r
  }\r
 \r
+treeNode XMLTree::NextElement(treeNode x) \r
+{\r
+   NULLT_IF(x==NULLT);\r
+   treeNode ns = next_sibling(Par, x);\r
+   return ((ns == NULLT || Tag(ns) != PCDATA_TAG_ID) ? ns : next_sibling(Par,ns));\r
+}\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
+   NULLT_IF(x==NULLT || x == Root() || isleaf(Par,x));\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 (x == Root() || x==NULLT)\r
-      return NULLT;\r
-    \r
-    return next_sibling(Par, x);\r
+   NULLT_IF(x==NULLT || x == Root() );\r
+   return next_sibling(Par, x);\r
  }\r
 \r
 // PrevSibling(x): returns the previous sibling of node x, assuming it exists.\r
 treeNode XMLTree::PrevSibling(treeNode x) \r
  {\r
-    return prev_sibling(Par, x);\r
+   NULLT_IF(x==NULLT || x == Root());\r
+   return prev_sibling(Par, x);\r
  }\r
 \r
 // TaggedChild(x,tag): returns the first child of node x tagged tag, or NULLT if there is none.\r
@@ -458,60 +432,58 @@ treeNode XMLTree::PrevSibling(treeNode x)
 // efficiently, just iterating among the children of node x until finding the desired child.\r
 treeNode XMLTree::TaggedChild(treeNode x, TagType tag) \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)) == 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
+   NULLT_IF(x==NULLT || isleaf(Par,x));\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
+   treeNode child;   \r
+   child = first_child(Par, x); // starts at first child of node x\r
+   if (get_field(tags_fix,tags_blen,node2tagpos(child)) == tag)\r
+     return child;\r
+   else\r
+     return TaggedFollSibling(child,tag);\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
+treeNode XMLTree::TaggedFollSibling(treeNode x, TagType tag)\r
+{\r
+  NULLT_IF(x==NULLT);\r
+  treeNode sibling = next_sibling(Par, x); \r
+  while (sibling != NULLT) {\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
+treeNode XMLTree::SelectChild(treeNode x, std::unordered_set<int> *tags)\r
+{\r
+  \r
+  NULLT_IF(x==NULLT || isleaf(Par,x));\r
+  int i;\r
+  treeNode child = first_child(Par, x);  \r
+  TagType t = get_field(tags_fix, tags_blen, node2tagpos(child));\r
+  std::unordered_set<int>::const_iterator tagit = tags->find(t);\r
+  if (tagit != tags->end()) return child;  \r
+  return SelectFollSibling(child,tags);\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
+treeNode XMLTree::SelectFollSibling(treeNode x, std::unordered_set<int> *tags)\r
+{\r
+\r
+   NULLT_IF(x==NULLT);\r
+   int i;\r
+   TagType t;\r
+   treeNode sibling = next_sibling(Par, x);\r
+   std::unordered_set<int>::const_iterator tagit;\r
+   while (sibling != NULLT) {\r
+     t = get_field(tags_fix, tags_blen, node2tagpos(sibling));\r
+     tagit = tags->find(t);\r
+     if (tagit != tags->end()) return sibling;\r
+     sibling = next_sibling(Par, sibling);\r
+   }\r
+   return NULLT;    \r
  }\r
 \r
 \r
@@ -519,171 +491,36 @@ treeNode XMLTree::SelectSibling(treeNode x, TagType *tags, int ntags)
 // the subtree of x. Returns NULLT if there is none.\r
 treeNode XMLTree::TaggedDesc(treeNode x, TagType tag) \r
  {\r
-    treeNode y;\r
-    if (isleaf(Par,x))\r
-      return NULLT;\r
-\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
-\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
-    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
-    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
-\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
-        \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
-    \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
-    \r
-    return res;\r
-}\r
+   NULLT_IF(x==NULLT || isleaf(Par,x));\r
 \r
+   int s = (int) Tags->select_next(tag,node2tagpos(x));\r
+   NULLT_IF (s == -1);\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
-    return res < lim ? res : NULLT;\r
+   treeNode y = tagpos2node(s); // transforms the tag position into a node position\r
+   \r
+   return (is_ancestor(Par,x,y) ? y : NULLT);\r
  }\r
 \r
 \r
-treeNode XMLTree::TaggedDescOrFollOnly(treeNode x,TagType *folltags, unsigned int ftlen,treeNode root)\r
+treeNode XMLTree::SelectDesc(treeNode x, std::unordered_set<int> *tags)\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
-// TaggedNext(x,tag): returns the first node tagged tag with larger preorder than x \r
-// Returns NULLT if there is none.\r
-treeNode XMLTree::TaggedNext(treeNode x, TagType *childtags, unsigned int ctlen,\r
-                            TagType *folltags, unsigned int flen,treeNode root)\r
- {\r
-   treeNode y,old_y,lim,res;\r
-   TagType tag;\r
-   if (x == NULLT || x == Root())\r
-     return NULLT;\r
-\r
-   lim = find_close(Par,root);   \r
-\r
-   res = NULLT;\r
-  \r
-   y = next_sibling(Par,x);\r
-   while (y != NULLT) {\r
-     tag = get_field(tags_fix,tags_blen,node2tagpos(y));\r
-     for(unsigned int i = 0; i < ctlen;i++)\r
-       if (childtags[i] == tag)\r
-        return y;\r
-     \r
-     for(unsigned int i = 0; i < flen;i++)\r
-       if (folltags[i] == tag)\r
-        return y;\r
-\r
-     res = TaggedBelow(y,NULL,0,folltags,flen);\r
-     if (res != NULLT)\r
-       return res;\r
-     \r
-     y = next_sibling(Par,y);\r
-   };\r
-   //Found nothing in the following sibling of x.\r
-   res = NULLT;\r
-   for(unsigned int i = 0; i < flen;i++){\r
-     y = TaggedFoll(x,folltags[i]);\r
-     res = (y!= x && (res == NULLT || (y != NULLT && y < res)))? y : res;\r
+   NULLT_IF (x ==NULLT || isleaf(Par,x));\r
+   int i;\r
+   treeNode min = NULLT;\r
+   treeNode fc = first_child(Par,x);\r
+   treeNode aux;\r
+   std::unordered_set<int>::const_iterator tagit;\r
+   for (tagit = tags->begin(); tagit != tags->end(); tagit++) {\r
+     aux = TaggedDesc(x, (TagType) *tagit);\r
+     if (aux == fc) return fc;\r
+     if (aux == NULLT) continue;\r
+     if ((min == NULLT) || (aux < min)) min = aux;\r
    };\r
-  \r
-   return res < lim ? res : NULLT;   \r
+   return min;\r
  }\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
@@ -709,60 +546,61 @@ 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 (x ==NULLT || x == Root())\r
-       return NULLT;\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
+   NULLT_IF (x ==NULLT || x == Root());\r
+   \r
+   return tagpos2node(Tags->select_next(tag,find_close(Par, x)));\r
+\r
  } \r
 \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
-    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
+  NULLT_IF (x == NULLT || x == Root());\r
+  \r
+  treeNode s = tagpos2node(Tags->select_next(tag, find_close(Par, x)));\r
+  \r
+  if (root == Root()) return s;\r
+  NULLT_IF (s == NULLT || s >= find_close(Par, root));\r
+  \r
+  return s;\r
+} \r
 \r
-treeNode XMLTree::SelectFollBelow(treeNode x, TagType *tags, int ntags, treeNode ctx)\r
+/* Here we inline TaggedFoll to find the min globally, and only at the end\r
+   we check if the min is below the context node */\r
+treeNode XMLTree::SelectFollBelow(treeNode x, std::unordered_set<int> *tags, treeNode root)\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
+   NULLT_IF(x==NULLT || x==Root());\r
+   int i;\r
+   treeNode min = NULLT;\r
+   treeNode ns = next_sibling(Par, x);\r
+   treeNode aux;\r
+   std::unordered_set<int>::const_iterator tagit;\r
+   for (tagit = tags->begin(); tagit != tags->end(); tagit++) {\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
-    treeNode ns = next_sibling(Par,x);\r
+     aux = tagpos2node(Tags->select_next(*tagit, find_close(Par, x)));\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
+     if (aux == ns ) return ns;\r
+     if (aux == NULLT) continue;\r
+     if ((min == NULLT) || (aux < min)) min = aux;\r
+   };\r
+     \r
+   // found the smallest node in preorder which is after x.\r
+   // if ctx is the root node, just return what we found.\r
 \r
-    if (x == NULLT || x == Root() || ns == -1)\r
-      return NULLT;\r
+   if (root == Root()) return min;\r
+   // else check whether if is in below the ctx node\r
 \r
-    int s = (int) Tags->select_next(tag, node2tagpos(ns)-1);\r
-    if (s==-1) return NULLT;\r
-    else return tagpos2node(s);\r
+   NULLT_IF (min == NULLT || min >= find_close(Par, root));\r
+   \r
+   return min;\r
+   \r
  }\r
 \r
 \r
@@ -782,64 +620,29 @@ treeNode XMLTree::TaggedAncestor(treeNode x, TagType tag)
  }\r
 \r
 \r
-// PrevText(x): returns the document identifier of the text to the left \r
-// of node x, or NULLT if x is the root node or the text is empty.\r
-// Assumes Doc ids start from 0.\r
-DocID XMLTree::PrevText(treeNode x) \r
- {\r
-    if (x == Root()) return NULLT;\r
-    if (indexing_empty_texts)  // faster, no rank needed\r
-       return (DocID)x-1;\r
-    else { // we are not indexing empty texts, rank is needed\r
-       if (EBVector->access(x-1) == 0) \r
-          return (DocID)NULLT;  // there is no text to the left of node (text is empty)\r
-       else\r
-          return (DocID)EBVector->rank1(x-1)-1;  //-1 because document ids start from 0\r
-    }\r
- }\r
-\r
-// NextText(x): returns the document identifier of the text to the right\r
-// 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 (x == Root()) return NULLT;\r
-    if (indexing_empty_texts)  // faster, no rank needed\r
-       return (DocID)x+2*subtree_size(Par, x)-1;\r
-    else { // we are not indexing empty texts, rank is needed\r
-       int p = x+2*subtree_size(Par, x)-1;\r
-       if (EBVector->access(p) == 0) // there is no text to the right of node\r
-          return (DocID)NULLT;\r
-       else\r
-          return (DocID)EBVector->rank1(p)-1; //-1 because document ids start from 0\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
 // ids start from 0.\r
 DocID XMLTree::MyText(treeNode x) \r
- {\r
-    if (!IsLeaf(x)) return NULLT;\r
-    if (indexing_empty_texts) // faster, no rank needed\r
-       return (DocID)x;\r
-    else { // we are not indexing empty texts, rank is needed\r
-       if (EBVector->access(x) == 0)  // there is no text below node x\r
-          return (DocID)NULLT;\r
-       else\r
-          return (DocID)EBVector->rank1(x)-1; //-1 because document ids start from 0\r
-    } \r
- }\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); //-1 because document ids start from 0\r
+  else \r
+    return (DocID) NULLT;\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
  {\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
-       int s = EBVector->select1(d+1);\r
-       return rank_open(Par, s) + d + 1; // +1 because root has preorder 1\r
-    }\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
 // NodeXMLId(x): returns the preorder of node x in the tree consisting \r
@@ -847,64 +650,26 @@ int XMLTree::TextXMLId(DocID d)
 // preorder 0;\r
 int XMLTree::NodeXMLId(treeNode x) \r
  {\r
-    if (indexing_empty_texts)\r
-       return x - 1 + rank_open(Par, x);\r
-    else {\r
-       if (x == Root()) return 1; // root node has preorder 1\r
-       else\r
-          return rank_open(Par, x) + EBVector->rank1(x-1);\r
-    }\r
+   if (x == Root()) return 1; // root node has preorder 1\r
+   return rank_open(Par, x) + EBVector->rank1(x-1);\r
  }\r
 \r
 // ParentNode(d): returns the parent node of document identifier d.\r
 treeNode XMLTree::ParentNode(DocID d) \r
  {    \r
-    if (d == NULLT)\r
-      return NULLT;\r
-    \r
-    int s;\r
-    // OJO : Kim : I added the d+1. before that, else branch was \r
-    // EBVector->select1(d)\r
-    // and gave wrong results (I'm really poking a bear with a stick here).\r
-    if (indexing_empty_texts) s = d;\r
-    else s = EBVector->select1(d+1);\r
-    \r
-    if (inspect(Par,s) == CP) // is a closing parenthesis\r
-       return parent(Par, find_open(Par, s));\r
-    else // is an opening parenthesis\r
-       return (treeNode)s;\r
-     \r
- }\r
-treeNode XMLTree::PrevNode(DocID d) \r
- {    \r
-    if (d == NULLT)\r
-      return NULLT;\r
-    \r
-    int s;\r
-    \r
-    if (indexing_empty_texts) s = d;\r
-    else s = EBVector->select1(d+1);\r
-    if (s == -1)\r
-      return NULLT;\r
-    \r
-    if (inspect(Par,s) == CP) // is a closing parenthesis\r
-      return find_open(Par, s);\r
-    else // is an opening parenthesis\r
-      return NULLT;\r
-    \r
+   NULLT_IF (d == NULLT);   \r
+   return (treeNode) EBVector->select1(d+1);     \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
  {\r
-    int i;\r
-    // this should be changed for more efficient processing\r
-    for (i=0; i<ntagnames; i++)\r
-       if (strcmp((const char *)tagname,(const char *)TagName[i])==0) break; \r
-    if (i==ntagnames) return (TagType)-1; //ntagnames; //(TagType)NULLT; // tagname does not exists in the table\r
-    else return i;\r
+  \r
+   string s = (char *) tagname;\r
+   TagIdMapIT it = tIdMap->find(s);    \r
+   return (TagType) ((it != tIdMap->end()) ? it->second : -1);\r
+    \r
  }\r
 \r
 \r
@@ -913,19 +678,23 @@ TagType XMLTree::GetTagId(unsigned char *tagname)
 unsigned char *XMLTree::GetTagName(TagType tagid)\r
  {\r
     unsigned char *s;\r
-               if(tagid==(uint)-1) return NULL;\r
-    if (tagid >= ntagnames) return NULL; // invalid tag identifier\r
-    s = (unsigned char *)umalloc((strlen((const char *)TagName[tagid])+1)*sizeof(unsigned char));\r
-    strcpy((char *)s, (const char *)TagName[tagid]);\r
-    return s;\r
+    if ( tagid < 0 || tagid >= TagName->size())\r
+      return (unsigned char *) "<INVALID TAG>";\r
+    strcpy((char *)s, TagName->at(tagid).c_str());\r
+    \r
+    return (s == NULL ? (unsigned char*) "<INVALID TAG>" : s);\r
  }\r
 \r
 \r
 const unsigned char *XMLTree::GetTagNameByRef(TagType tagid)\r
  {\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
+   unsigned char *s;\r
+   if ( tagid < 0 || tagid >= TagName->size())\r
+     return (unsigned char *) "<INVALID TAG>";\r
+   \r
+   return (const unsigned char *) TagName->at(tagid).c_str();\r
+   \r
  }\r
 \r
 \r
@@ -934,13 +703,11 @@ TagType XMLTree::RegisterTag(unsigned char *tagname)
  {  \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
+      string s = (char *) tagname;      \r
+      REGISTER_TAG(TagName,tIdMap,s);\r
+      \r
+    };\r
+    \r
     return id;\r
  }\r
 \r
index ea9b938..8057773 100644 (file)
--- a/XMLTree.h
+++ b/XMLTree.h
@@ -21,6 +21,8 @@
 \r
 #ifndef XMLTREE_H_\r
 #define XMLTREE_H_\r
+#include <unordered_set>\r
+#include <unordered_map>\r
 #include "TextCollection/TextCollectionBuilder.h"\r
 #include <stdio.h>\r
 #include <stdlib.h>\r
@@ -32,7 +34,7 @@
 #undef Wminusone\r
 \r
 #include "bp.h"\r
-//#include "basics.h"\r
+\r
 #include <static_bitsequence.h>\r
 #include <alphabet_mapper.h>\r
 #include <static_sequence.h>\r
@@ -57,27 +59,54 @@ typedef struct {
    int max;\r
 } range;\r
 \r
-typedef struct nd {\r
-   uint position;\r
-   struct nd *next;\r
-} ListNode;\r
+// Encoding of the XML Document :\r
+// The following TAGs and IDs are fixed, "" is the tag of the root.\r
+// a TextNode is represented by a leaf <<$>></<$>> The DocId in the TextCollection\r
+// of that leaf is kept in a bit sequence.\r
+// a TextNode below an attribute is likewise represented by a leaf <<@$>><</@$>>\r
+// An element <e a1="v1" a2="v2" ... an="vn" > ...</e> the representation is:\r
+// <e><<@>> <<@>a1> <<$@>>DocID(v1)</<$@>></<@>a1> ... </<@>> .... </e>\r
+// Hence the attributes (if any) are always below the first child of their element,\r
+// as the children of a fake node <@>.\r
 \r
-typedef struct {\r
-   ListNode *first;\r
-   ListNode *last;\r
-} TagArrayEntry;\r
+\r
+#define DOCUMENT_OPEN_TAG ""\r
+#define DOCUMENT_TAG_ID 0\r
+#define ATTRIBUTE_OPEN_TAG "<@>"\r
+#define ATTRIBUTE_TAG_ID 1\r
+#define PCDATA_OPEN_TAG "<$>"\r
+#define PCDATA_TAG_ID 2\r
+#define ATTRIBUTE_DATA_OPEN_TAG "<@$>"\r
+#define ATTRIBUTE_DATA_TAG_ID 3\r
+#define DOCUMENT_CLOSE_TAG "/"\r
+#define ATTRIBUTE_CLOSE_TAG "/<@>"\r
+#define PCDATA_CLOSE_TAG "/<$>"\r
+#define ATTRIBUTE_DATA_CLOSE_TAG "/<@$>"\r
+\r
+\r
+\r
+typedef std::unordered_map<string,int> TagIdMap;\r
+typedef TagIdMap::const_iterator TagIdMapIT;\r
+\r
+#define REGISTER_TAG(v,h,t) do { (h)->insert(std::make_pair((t),(v)->size()));\\r
+    (v)->push_back(t); } while (false)\r
+\r
+\r
+class XMLTreeBuilder;\r
 \r
 class XMLTree {\r
+\r
+  // Only the builder can access the constructor\r
+  friend class XMLTreeBuilder;\r
+\r
+ private:\r
    /** Balanced parentheses representation of the tree */\r
    bp *Par;\r
  \r
    /** Mapping from tag identifer to tag name */  \r
-   unsigned char **TagName;\r
-   uint ntagnames;\r
+   vector<string> *TagName;\r
+   TagIdMap * tIdMap;\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 *EBVector;  \r
                      \r
@@ -90,20 +119,20 @@ class XMLTree {
    TextCollection *Text;\r
 \r
    /** The texts in the XML document (cached for faster display) */\r
-   vector<string> CachedText;\r
-   \r
-   TagArrayEntry *TagArray;\r
+   vector<string> *CachedText;\r
 \r
    // Allows to disable the TextCollection for benchmarkin purposes\r
    bool disable_tc;\r
    \r
-public:\r
+\r
    /** Data structure constructors */\r
-   XMLTree() {;};\r
+   XMLTree(){;};\r
 \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
+   // non const pointer are freed by this method.\r
+  XMLTree( pb * const par, uint npar,  vector<string> * const TN,  TagIdMap * const tim, uint *empty_texts_bmp, TagType *tags,\r
+          TextCollection * const TC, vector<string> * const CT, bool dis_tc);\r
+\r
+public: \r
    /** Data structure destructor */\r
    ~XMLTree();\r
    \r
@@ -167,13 +196,15 @@ public:
    /** FirstChild(x): returns the first child of node x, assuming it exists. \r
     * Very fast in BP. */\r
    treeNode FirstChild(treeNode x);\r
-   \r
+   treeNode FirstElement(treeNode x);\r
+\r
    /** LastChild(x): returns the last child of node x.  */\r
    treeNode LastChild(treeNode x);\r
    \r
    /** NextSibling(x): returns the next sibling of node x, assuming it \r
     * exists. */\r
    treeNode NextSibling(treeNode x);\r
+   treeNode NextElement(treeNode x);\r
    \r
    /** PrevSibling(x): returns the previous sibling of node x, assuming it \r
     * exists. */\r
@@ -185,35 +216,21 @@ public:
     * among the children of node x until finding the desired child. */\r
    treeNode TaggedChild(treeNode x, TagType tag);\r
    \r
-   treeNode SelectChild(treeNode x, TagType *tags, int ntags);\r
+   treeNode SelectChild(treeNode x, std::unordered_set<int> * tags);\r
 \r
-   /** TaggedSibling(x,tag): returns the first sibling of node x tagged tag, or \r
+   /** TaggedFollSibling(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
+   treeNode TaggedFollSibling(treeNode x, TagType tag);\r
    \r
-   treeNode SelectSibling(treeNode x, TagType *tags, int ntags);\r
+   treeNode SelectFollSibling(treeNode x, std::unordered_set<int> * tags);\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
+   treeNode SelectDesc(treeNode x, std::unordered_set<int> * tags);\r
 \r
-   treeNode TaggedBelow(treeNode x, TagType *childtags, unsigned int ctlen,\r
-                                TagType *desctags, unsigned int dtlen);\r
-   \r
-   treeNode TaggedNext(treeNode x, TagType *childtags, unsigned int ctlen,\r
-                               TagType *folltags, unsigned int flen,treeNode root);\r
-\r
-   treeNode TaggedDescOnly(treeNode x, TagType *desctags, unsigned int dtlen);\r
-   \r
-   treeNode TaggedDescOrFollOnly(treeNode x, TagType *folltags, unsigned int flen,\r
-                          treeNode root);\r
-\r
-   treeNode TaggedFollOnly(treeNode x, TagType *folltags, unsigned int flen,\r
-                          treeNode root);\r
-   \r
 \r
    /** TaggedPrec(x,tag): returns the first node tagged tag with smaller \r
     * preorder than x and not an ancestor of x. Returns NULLT if there \r
@@ -227,7 +244,7 @@ public:
 \r
    treeNode TaggedFollBelow(treeNode x, TagType tag,treeNode root);     \r
    \r
-   treeNode SelectFollBelow(treeNode x, TagType *tags, int ntags, treeNode ctx);\r
+   treeNode SelectFollBelow(treeNode x, std::unordered_set<int> * tags, treeNode root);\r
 \r
    /** TaggedFollowingSibling(x,tag) */\r
    treeNode TaggedFollowingSibling(treeNode x, TagType tag);\r
@@ -373,12 +390,13 @@ public:
    /** GetText(d): returns the text corresponding to document with\r
     * id d. */\r
    uchar* GetText(DocID d) {\r
-       return Text->GetText(d);\r
+     uchar * s = Text->GetText(d);\r
+     return (s[0] == 1 ? (uchar*)"" : s);\r
    }\r
 \r
    uchar* GetCachedText(DocID d) {\r
-     uchar * str = (uchar*) calloc(sizeof(char),(CachedText.at(d).size() + 1));\r
-     strcpy((char*) str,(const char*) CachedText.at(d).c_str());\r
+     uchar * str = (uchar*) calloc(sizeof(char),(CachedText->at(d).size() + 1));\r
+     strcpy((char*) str,(const char*) CachedText->at(d).c_str());\r
      return (uchar*) (str);\r
    }\r
    \r
@@ -387,11 +405,11 @@ public:
    }\r
    \r
    /** Save: saves XML tree data structure to file. */\r
-   void Save(unsigned char *filename);\r
+   void Save(int fd);\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
+   static XMLTree *Load(int fd);   \r
 \r
    void insertTag(TagType tag, uint position);\r
    \r
index 27bc862..138db9b 100644 (file)
@@ -1,43 +1,46 @@
-\r
-#include "XMLTreeBuilder.h"\r
 #include "basics.h"\r
+#include "XMLTreeBuilder.h"\r
+\r
+\r
+XMLTreeBuilder::~XMLTreeBuilder(){\r
+  \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 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
+   \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
+    TagName = new vector<string>();\r
+    tIdMap = new std::unordered_map<string,int>();\r
+\r
+    REGISTER_TAG(TagName,tIdMap,DOCUMENT_OPEN_TAG);\r
+    REGISTER_TAG(TagName,tIdMap,ATTRIBUTE_OPEN_TAG);\r
+    REGISTER_TAG(TagName,tIdMap,PCDATA_OPEN_TAG);\r
+    REGISTER_TAG(TagName,tIdMap,ATTRIBUTE_DATA_OPEN_TAG);\r
+    REGISTER_TAG(TagName,tIdMap,DOCUMENT_CLOSE_TAG);\r
+    REGISTER_TAG(TagName,tIdMap,ATTRIBUTE_CLOSE_TAG);\r
+    REGISTER_TAG(TagName,tIdMap,PCDATA_CLOSE_TAG);\r
+    REGISTER_TAG(TagName,tIdMap,ATTRIBUTE_DATA_CLOSE_TAG);\r
+\r
+\r
+    CachedText = new vector<string>;\r
     if (disable_tc)\r
         TextBuilder = 0;\r
     else \r
-        TextBuilder = new TextCollectionBuilder((unsigned)sample_rate_text);\r
+      TextBuilder = new TextCollectionBuilder((unsigned)sample_rate_text);\r
     Text = 0;\r
-    \r
+    empty_texts_aux = (unsigned int *)ucalloc(sizeof(unsigned int),1);\r
+    eta_size = sizeof(unsigned int);\r
     return 1;  // indicates success in the initialization of the data structure\r
  }\r
 \r
@@ -47,10 +50,10 @@ int XMLTreeBuilder::OpenDocument(bool empty_texts, int sample_rate_text, bool dt
 // 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
+    //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
@@ -60,9 +63,16 @@ XMLTree *XMLTreeBuilder::CloseDocument()
        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
+    \r
+    XMLTree *T = new XMLTree(par_aux,\r
+                            npar, \r
+                            TagName,\r
+                            tIdMap,\r
+                            empty_texts_aux,                // freed by the constructor\r
+                            tags_aux,                       //freed by the constructor\r
+                            Text,\r
+                            CachedText,\r
+                            disable_tc);\r
     return T; \r
  }\r
 \r
@@ -70,7 +80,7 @@ XMLTree *XMLTreeBuilder::CloseDocument()
 // 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
+int XMLTreeBuilder::NewOpenTag(string tagname)\r
  {\r
     int i;\r
     \r
@@ -81,31 +91,22 @@ int XMLTreeBuilder::NewOpenTag(unsigned char *tagname)
     }\r
     \r
     setbit(par_aux,npar,OP);  // marks a new opening parenthesis\r
+    \r
+    TagIdMapIT tag_id = tIdMap->find(tagname);\r
+   \r
+    if (tag_id == tIdMap->end()){\r
+      REGISTER_TAG(TagName,tIdMap,tagname);\r
+      i = TagName->size() - 1;\r
+    }\r
+    else\r
+      i = tag_id->second;\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
+    if (tagname.compare(PCDATA_OPEN_TAG) == 0 ||\r
+       tagname.compare(ATTRIBUTE_DATA_OPEN_TAG) == 0){\r
+    };\r
+    \r
     tags_aux = (TagType *) urealloc(tags_aux, sizeof(TagType)*(npar + 1));\r
-\r
+    \r
     tags_aux[npar] = i; // inserts the new tag id within the preorder sequence of tags\r
     \r
     npar++;\r
@@ -117,7 +118,7 @@ int XMLTreeBuilder::NewOpenTag(unsigned char *tagname)
 // 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
+int XMLTreeBuilder::NewClosingTag(string tagname)\r
  {\r
     int i;\r
     \r
@@ -128,20 +129,17 @@ int XMLTreeBuilder::NewClosingTag(unsigned char *tagname)
     }\r
     \r
     setbit(par_aux,npar,CP);  // marks a new closing parenthesis\r
+    \r
+    tagname.insert(0,"/");\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
+    TagIdMapIT tag_id = tIdMap->find(tagname);    \r
+\r
+    if (tag_id == tIdMap->end()){\r
+      REGISTER_TAG(TagName,tIdMap,tagname);\r
+      i = TagName->size() - 1;\r
+    }\r
+    else\r
+      i = tag_id->second;\r
 \r
     tags_aux = (TagType *)urealloc(tags_aux, sizeof(TagType)*(npar + 1));\r
 \r
@@ -156,41 +154,24 @@ int XMLTreeBuilder::NewClosingTag(unsigned char *tagname)
 // 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
+int XMLTreeBuilder::NewText(string text)\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
+   if (!disable_tc) {\r
+     if  (text.empty())\r
+       TextBuilder->InsertText((uchar *)"\001");\r
+     else\r
+       TextBuilder->InsertText((uchar *) text.c_str());\r
+   };\r
+\r
+   CachedText->push_back(text); \r
+   int n_eta_size = sizeof(uint)*(1+(npar-1)/(8*sizeof(uint)));\r
+   //see basics.h, recalloc resizes and sets the new area to 0.\r
+   \r
+   empty_texts_aux = (uint *)urecalloc(empty_texts_aux,eta_size,n_eta_size);\r
+   eta_size = n_eta_size;\r
+   bitset(empty_texts_aux, npar-1);  // marks the non-empty text with a 1 in the bit vector\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
index 38d81ba..336ca2d 100644 (file)
@@ -21,6 +21,7 @@
 \r
 #ifndef XMLTREEBUILDER_H_\r
 #define XMLTREEBUILDER_H_\r
+#include <unordered_map>\r
 #include "TextCollection/TextCollectionBuilder.h"\r
 #include <stdio.h>\r
 #include <stdlib.h>\r
@@ -31,8 +32,9 @@
 #undef WW\r
 #undef Wminusone\r
 \r
-#include "bp.h"\r
+\r
 #include "XMLTree.h"\r
+#include "bp.h"\r
 #include <static_bitsequence.h>\r
 #include <alphabet_mapper.h>\r
 #include <static_sequence.h>\r
@@ -47,6 +49,7 @@ using SXSI::TextCollectionBuilder;
 #define bitclean(e,p) ((e)[(p)/W] &= ~(1<<((p)%W)))\r
 \r
 \r
+\r
 class XMLTreeBuilder {\r
   \r
    /** Array containing the balanced parentheses sequence */\r
@@ -55,9 +58,8 @@ class XMLTreeBuilder {
    int npar;\r
 \r
    /** Mapping from tag identifer to tag name */  \r
-   unsigned char **TagName;\r
-   int ntagnames;\r
-\r
+   vector<string> *TagName;\r
+   TagIdMap * tIdMap;\r
    /** Array containing the sequence of tags */\r
    TagType *tags_aux;\r
    \r
@@ -66,19 +68,11 @@ class XMLTreeBuilder {
    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
+   vector<string> *CachedText;\r
 \r
+   unsigned int *empty_texts_aux;\r
+   int eta_size;\r
    // Allows to disable the TextCollection for benchmarkin purposes\r
    bool disable_tc;\r
 \r
@@ -88,9 +82,8 @@ public:
 \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
+   /** OpenDocument(sample_rate_text,dtc): initilizes the construction\r
+    * of the data structure for the XML document.  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
@@ -108,24 +101,23 @@ public:
    /** 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
+   int NewOpenTag(string 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
+   int NewClosingTag(string tagname);\r
  \r
-   /** NewText(s): indicates the event of finding a new (non-empty) text s in \r
+   /** NewText(s): indicates the event of finding a new 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
+    * Returns a non-zero value upon success, NULLT in case of error. \r
+    * If the string is empty, which is legal in attributes, then\r
+    * the string the sequence '\0x01\0x00' is inserted in the TextCollection\r
+    * It is ok to do so since a non printable character cannot occur in an XML document\r
+    */\r
+   int NewText(string text);\r
+\r
+\r
 };\r
 #endif\r
 \r
index 273aa1d..7449c11 100644 (file)
--- a/basics.h
+++ b/basics.h
@@ -1,15 +1,21 @@
 #ifndef BASICS_H\r
 #define BASICS_H\r
 \r
+#include <string>\r
 #include <stdio.h>\r
 #include <stdlib.h>\r
+#include <string.h> // for memset\r
+#include <sys/types.h>\r
+#include <unistd.h>\r
+#include <errno.h>\r
+\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
+      throw ("ufread I/O error" );\r
     return;\r
  }\r
 \r
@@ -30,6 +36,21 @@ inline void *urealloc(void *ptr, size_t size)
        throw std::bad_alloc();\r
     return dest;\r
  }\r
+// realloc and set to 0\r
+inline void *urecalloc(void *ptr, size_t o_size, size_t n_size)\r
+ {\r
+   if (o_size == n_size)\r
+     return ptr;\r
+   \r
+    void *dest = realloc(ptr,n_size);\r
+    //don't fail if we requested size 0\r
+    if (dest == NULL && n_size > 0 )\r
+      throw std::bad_alloc();\r
+    // Initialize the new area with 0\r
+    void * n_area_start = &(((char*) dest)[o_size]);\r
+    memset(n_area_start,0, n_size - o_size);\r
+    return dest;\r
+ }\r
 \r
 inline void *ucalloc(size_t nmemb, size_t size)\r
  {\r