Some more debugging and comments.
[SXSI/XMLTree.git] / XMLTree.cpp
index a3fefea..b93e75a 100644 (file)
@@ -1,6 +1,6 @@
-\r
 #include "XMLTree.h"\r
 #include <cstring>\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
 // without affecting the code so much.\r
@@ -17,6 +17,56 @@ inline int node2tagpos(treeNode x) {
    return (int)x;\r
 }\r
 \r
+\r
+//KIM OJO to prevent suprious "unused result" warnings\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
+\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
+\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
+\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
+\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
+\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
 // Save: saves XML tree data structure to file. \r
 void XMLTree::Save(unsigned char *filename) \r
  {\r
@@ -36,14 +86,16 @@ void XMLTree::Save(unsigned char *filename)
     saveTree(Par, fp);\r
  \r
     // stores the table with tag names\r
-    fwrite(&ntagnames, sizeof(int), 1, fp);\r
+    ufwrite(&ntagnames, sizeof(int), 1, fp);\r
     for (i=0; i<ntagnames;i++)\r
-       fprintf(fp, "%s\n",TagName[i]);\r
+      fprintf(fp, "%s\n",TagName[i]);\r
+    \r
     \r
     // stores the flags\r
-    fwrite(&indexing_empty_texts, sizeof(bool), 1, fp);\r
-    fwrite(&initialized, sizeof(bool), 1, fp);\r
-    fwrite(&finished, sizeof(bool), 1, fp);\r
+    ufwrite(&indexing_empty_texts, sizeof(bool), 1, fp);\r
+    ufwrite(&initialized, sizeof(bool), 1, fp);\r
+    ufwrite(&finished, sizeof(bool), 1, fp);\r
+    ufwrite(&disable_tc, sizeof(bool),1,fp);\r
     \r
     if (!indexing_empty_texts) EBVector->save(fp);\r
     \r
@@ -51,7 +103,8 @@ void XMLTree::Save(unsigned char *filename)
     Tags->save(fp);\r
 \r
     // stores the texts   \r
-    //Text->Save(fp);\r
+    if (!disable_tc)\r
+      Text->Save(fp);\r
 \r
     fclose(fp);\r
 \r
@@ -64,51 +117,67 @@ XMLTree *XMLTree::Load(unsigned char *filename, int sample_rate_text)
  {\r
 \r
     FILE *fp;\r
-    char filenameaux[1024];\r
+    char buffer[1024];\r
     XMLTree *XML_Tree;\r
     int i;\r
     \r
     // first load the tree topology\r
-    sprintf(filenameaux, "%s.srx", filename);\r
-    fp = fopen(filenameaux, "r");\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", filenameaux);\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
-    XML_Tree->Par = (bp *)malloc(sizeof(bp));\r
+    XML_Tree->Par = (bp *)umalloc(sizeof(bp));\r
 \r
     loadTree(XML_Tree->Par, fp); \r
     \r
     // stores the table with tag names\r
-    fread(&XML_Tree->ntagnames, sizeof(int), 1, fp);\r
-\r
-    XML_Tree->TagName = (unsigned char **)malloc(XML_Tree->ntagnames*sizeof(unsigned char *));\r
+    ufread(&XML_Tree->ntagnames, sizeof(int), 1, fp);\r
+    XML_Tree->TagName = (unsigned char **)umalloc(XML_Tree->ntagnames*sizeof(unsigned char *));\r
 \r
     for (i=0; i<XML_Tree->ntagnames;i++) {\r
-       int k = feof(fp);\r
-       fscanf(fp, "%s\n",filenameaux);\r
-       XML_Tree->TagName[i] = (unsigned char *)malloc(sizeof(unsigned char)*(strlen((const char *)filenameaux)+1));\r
-       strcpy((char *)XML_Tree->TagName[i], (const char *)filenameaux);\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
+       // 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
     }\r
        \r
     // loads the flags\r
-    fread(&(XML_Tree->indexing_empty_texts), sizeof(bool), 1, fp);\r
-    fread(&(XML_Tree->initialized), sizeof(bool), 1, fp);\r
-    fread(&(XML_Tree->finished), sizeof(bool), 1, fp);\r
-    \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
+    ufread(&(XML_Tree->disable_tc), sizeof(bool), 1, fp);\r
+\r
     if (!(XML_Tree->indexing_empty_texts)) XML_Tree->EBVector = static_bitsequence_rrr02::load(fp);\r
 \r
     // loads the tags\r
-    XML_Tree->Tags = static_sequence_wvtree::load(fp);\r
+    XML_Tree->Tags = static_sequence::load(fp);\r
 \r
-    // loads the texts   \r
-    //XML_Tree->Text->Load(fp,sample_rate_text);\r
+    // loads the texts\r
+    if (!XML_Tree->disable_tc){\r
+      XML_Tree->Text = TextCollection::InitTextCollection(sample_rate_text);\r
+      XML_Tree->Text->Load(fp,sample_rate_text);\r
+    }\r
+    else\r
+      XML_Tree->Text = NULL;\r
 \r
     fclose(fp);\r
-    \r
     return XML_Tree;\r
  }\r
 \r
@@ -127,18 +196,18 @@ XMLTree::~XMLTree()
     free(TagName);\r
 \r
     if (!indexing_empty_texts) {\r
-       EBVector->~static_bitsequence_rrr02();\r
+       //EBVector->~static_bitsequence_rrr02();\r
        delete EBVector;\r
        EBVector = NULL;\r
     }\r
 \r
-    Tags->~static_sequence_wvtree();\r
+    //Tags->~static_sequence_wvtree();\r
     delete Tags;\r
     Tags = NULL;\r
 \r
     //Text->~TextCollection();\r
-   // delete Text;\r
-   // Text = NULL;\r
+    delete Text;\r
+    Text = NULL;\r
 \r
     initialized = false;\r
     finished = false;\r
@@ -147,18 +216,31 @@ XMLTree::~XMLTree()
 // 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
+\r
     int s = x + 2*subtree_size(Par, x) - 1;\r
  \r
     return Tags->rank(tag, s) - Tags->rank(tag, node2tagpos(x)-1);\r
@@ -174,12 +256,22 @@ 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
@@ -188,18 +280,33 @@ bool XMLTree::IsChild(treeNode x, treeNode y)
 // 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
@@ -207,6 +314,11 @@ 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
@@ -214,12 +326,21 @@ 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 Tags->access(node2tagpos(x));\r
  }\r
 \r
@@ -227,6 +348,11 @@ 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 (indexing_empty_texts) { // faster, no rank needed\r
        r.min = x;\r
@@ -250,12 +376,24 @@ range XMLTree::DocIds(treeNode x)
 // Parent(x): returns the parent node of node x.\r
 treeNode XMLTree::Parent(treeNode x) \r
  {\r
-    return parent(Par, x);\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
+      return parent(Par, x);\r
  }\r
 \r
 // Child(x,i): returns the i-th child of node x, assuming it exists.\r
 treeNode XMLTree::Child(treeNode x, int i) \r
- {\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
@@ -263,18 +401,35 @@ 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
 // 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())\r
+      return NULLT;\r
\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
+    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
@@ -283,6 +438,11 @@ 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, int i, 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
@@ -301,6 +461,11 @@ treeNode XMLTree::TaggedChild(treeNode x, int i, TagType tag)
 // 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
     r = (int) Tags->rank(tag, node2tagpos(x));\r
@@ -315,6 +480,11 @@ treeNode XMLTree::TaggedDesc(treeNode x, TagType tag)
 // 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
     int r, s;\r
     treeNode node_s, root;\r
     r = (int)Tags->rank(tag, node2tagpos(x)-1);\r
@@ -335,6 +505,11 @@ 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
     r = (int) Tags->rank(tag, node2tagpos(next_sibling(Par, x))-1);\r
     s = (int) Tags->select(tag, r+1);  // select returns -1 in case that there is no r+1-th tag.\r
@@ -347,6 +522,11 @@ treeNode XMLTree::TaggedFoll(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
@@ -362,6 +542,11 @@ 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
@@ -379,6 +564,11 @@ 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
@@ -394,6 +584,11 @@ 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
@@ -407,6 +602,11 @@ 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
@@ -419,9 +619,19 @@ int XMLTree::NodeXMLId(treeNode x)
 // ParentNode(d): returns the parent node of document identifier d.\r
 treeNode XMLTree::ParentNode(DocID d) \r
  {\r
-    int s;\r
+    if (!finished) {\r
+       fprintf(stderr, "Error: data structure has not been constructed properly\n");\r
+       exit(1);\r
+    }\r
+    \r
+    if (d == NULLT)\r
+      return NULLT;\r
+    \r
+    int s = d;\r
+    // Kim : I added the d+1. before that, else was select1(d)\r
+    // and gave wrong results but I'm really poking a dead bear with a stick here.\r
     if (indexing_empty_texts) s = d;\r
-    else s = EBVector->select1(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
@@ -434,38 +644,39 @@ treeNode XMLTree::ParentNode(DocID d)
 // 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)\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
-    ntagnames = 0;\r
+    parArraySize = 1;\r
+    ntagnames = 2;    \r
+    disable_tc = dtc;\r
     \r
     indexing_empty_texts = empty_texts;\r
     \r
-    par_aux = (pb *)malloc(sizeof(pb));\r
-    if (!par_aux) {\r
-       fprintf(stderr, "Error: not enough memory\n");\r
-       return NULLT;\r
-    }\r
-    setbit(par_aux,npar,OP);  // marks a new opening parenthesis for the tree root\r
-    npar++;\r
+    par_aux = (pb *)umalloc(sizeof(pb)*parArraySize);\r
     \r
-    tags_aux = (TagType *)malloc(sizeof(TagType));\r
-    if (!tags_aux) {\r
-       fprintf(stderr, "Error: not enough memory\n");\r
-       return NULLT;\r
-    }\r
+    tags_aux = (TagType *) umalloc(sizeof(TagType));\r
+    \r
+    TagName = (unsigned char **) umalloc(2*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
+\r
+    if (!indexing_empty_texts) \r
+      empty_texts_aux = (unsigned int *)umalloc(sizeof(unsigned int));\r
+       \r
     \r
-    if (!indexing_empty_texts) {\r
-       empty_texts_aux = (unsigned int *)malloc(sizeof(unsigned int));\r
-       if (!empty_texts_aux) {\r
-          fprintf(stderr, "Error: not enough memory\n");\r
-          return NULLT;\r
-       }\r
-    }\r
     \r
-    //Text = TextCollection::InitTextCollection(sample_rate_text);\r
+    Text = TextCollection::InitTextCollection((unsigned)sample_rate_text);\r
     \r
     return 1;  // indicates success in the initialization of the data structure\r
  }\r
@@ -482,29 +693,41 @@ int XMLTree::CloseDocument()
     }\r
     \r
     // closing parenthesis for the tree root\r
-    par_aux = (pb *)realloc(par_aux, sizeof(pb)*(1+npar/(8*sizeof(pb))));\r
-    if (!par_aux) {\r
-       fprintf(stderr, "Error: not enough memory\n");\r
-       return NULLT;    \r
-    }\r
-    setbit(par_aux,npar,CP); \r
-    npar++;\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 *)malloc(sizeof(bp));      \r
+    Par = (bp *)umalloc(sizeof(bp));\r
     bp_construct(Par, npar, par_aux, OPT_DEGREE|0);    \r
     // creates structure for tags\r
-    alphabet_mapper * am = new alphabet_mapper_none();\r
-    static_bitsequence_builder * bmb = new static_bitsequence_builder_rrr02(32); \r
-    wt_coder * wtc = new wt_coder_huff((uint *)tags_aux,npar-1,am);\r
-    Tags = new static_sequence_wvtree((uint *) tags_aux, (uint) npar-1, wtc, bmb, am);\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
 \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
+    int ntagsize = found_attributes ? 2*ntagnames-1 : 2*ntagnames - 2;\r
+\r
+    Tags = new static_sequence_gmr((uint *) tags_aux, (uint) npar-1,ntagsize, bmb, ssb);\r
+    \r
+    delete bmb;\r
+    delete pmb;\r
+    delete ssb;\r
     // makes the text collection static\r
-    //Text->MakeStatic();\r
+    if (!disable_tc)\r
+      Text->MakeStatic();\r
     \r
     // creates the data structure marking the non-empty texts (just in the case it is necessary)\r
-    if (!indexing_empty_texts) \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
 \r
@@ -525,20 +748,25 @@ int XMLTree::NewOpenTag(unsigned char *tagname)
     }\r
     \r
     // inserts a new opening parentheses in the bit sequence\r
-    par_aux = (pb *)realloc(par_aux, sizeof(pb)*(1+npar/(8*sizeof(pb))));\r
-    if (!par_aux) {\r
-       fprintf(stderr, "Error: not enough memory\n");\r
-       return NULLT;    \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
+      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 **)realloc(TagName, sizeof(char *)*(ntagnames+1));\r
+       TagName = (unsigned char **)urealloc(TagName, sizeof(char *)*(ntagnames+1));\r
        \r
        if (!TagName) {\r
           fprintf(stderr, "Error: not enough memory\n");\r
@@ -546,16 +774,10 @@ int XMLTree::NewOpenTag(unsigned char *tagname)
        }\r
        \r
        ntagnames++;\r
-       TagName[i] = (unsigned char *)malloc(sizeof(unsigned char)*(strlen((const char *)tagname)+1));\r
+       TagName[i] = (unsigned char *)umalloc(sizeof(unsigned char)*(strlen((const char *)tagname)+1));\r
        strcpy((char *)TagName[i], (const char *)tagname);\r
     } \r
-    \r
-    tags_aux = (TagType *)realloc(tags_aux, sizeof(TagType)*(npar + 1));\r
-\r
-    if (!tags_aux) {\r
-       fprintf(stderr, "Error: not enough memory\n");\r
-       return NULLT;\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
@@ -579,11 +801,11 @@ int XMLTree::NewClosingTag(unsigned char *tagname)
     }\r
     \r
     // inserts a new closing parentheses in the bit sequence\r
-    par_aux = (pb *)realloc(par_aux, sizeof(pb)*(1+npar/(8*sizeof(pb))));\r
-    if (!par_aux) {\r
-       fprintf(stderr, "Error: not enough memory\n");\r
-       return NULLT;    \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 opening parenthesis\r
 \r
     // transforms the tagname into a tag identifier. If the tag is new, we insert\r
@@ -592,24 +814,16 @@ int XMLTree::NewClosingTag(unsigned char *tagname)
        if (strcmp((const char *)tagname,(const char *)TagName[i])==0) break;\r
  \r
     if (i==ntagnames) { // the tag is a new one, then we insert it\r
-       TagName = (unsigned char **)realloc(TagName, sizeof(char *)*(ntagnames+1));\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 *)malloc(sizeof(char)*(strlen((const char *)tagname)+1));\r
-       strcpy((char *)TagName[i], (const char *)tagname);\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 *)realloc(tags_aux, sizeof(TagType)*(npar + 1));\r
-\r
-    if (!tags_aux) {\r
-       fprintf(stderr, "Error: not enough memory\n");\r
-       return NULLT;\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
@@ -630,17 +844,17 @@ int XMLTree::NewText(unsigned char *s)
        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 *)realloc(empty_texts_aux, sizeof(pb)*(1+(npar-1)/(8*sizeof(pb))));\r
-       if (!empty_texts_aux) {\r
-          fprintf(stderr, "Error: not enough memory\n");\r
-          return NULLT;\r
-       }\r
-       \r
-       bitset(empty_texts_aux, npar-1);  // marks the non-empty text with a 1 in the bit vector\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
-    //Text->InsertText(s);\r
+    Text->InsertText(s);\r
     \r
     return 1; // success\r
  }\r
@@ -659,15 +873,11 @@ int XMLTree::NewEmptyText()
     }\r
 \r
     if (!indexing_empty_texts) {\r
-       empty_texts_aux = (unsigned int *)realloc(empty_texts_aux, sizeof(pb)*(1+(npar-1)/(8*sizeof(pb))));\r
-       if (!empty_texts_aux) {\r
-          fprintf(stderr, "Error: not enough memory\n");\r
-          return NULLT;\r
-       }\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 Text->InsertText(&c); // we insert the empty text just in case we index all the texts\r
+    else Text->InsertText(&c); // we insert the empty text just in case we index all the texts\r
     \r
     return 1; // success    \r
  }\r
@@ -693,8 +903,35 @@ unsigned char *XMLTree::GetTagName(TagType tagid)
     unsigned char *s;\r
 \r
     if (tagid >= ntagnames) return NULL; // invalid tag identifier\r
-    s = (unsigned char *)malloc((strlen((const char *)TagName[tagid])+1)*sizeof(unsigned char));\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
  }\r
 \r
+\r
+//KIM : OJO need the two following methods\r
+\r
+const unsigned char *XMLTree::GetTagNameByRef(TagType tagid)\r
+ {\r
+    if (tagid >= ntagnames) return NULL; // invalid tag identifier\r
+    return ((const unsigned char*)  TagName[tagid]);\r
+ }\r
+\r
+\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