more fixes
[SXSI/XMLTree.git] / XMLTree.cpp
index a696d7f..6fee8f4 100644 (file)
@@ -1,7 +1,7 @@
 #include "XMLTree.h"\r
 #include <cstring>\r
 \r
- // functions to convert tag positions to the corresponding tree node and viceversa. \r
+// functions to convert tag positions to the corresponding tree node and viceversa. \r
 // These are implemented in order to be able to change the tree and Tags representations, \r
 // without affecting the code so much.\r
 // Current implementation corresponds to balanced-parentheses representation for\r
@@ -17,7 +17,8 @@ inline int node2tagpos(treeNode x) {
    return (int)x;\r
 }\r
 \r
-// to prevent suprious "unused result" warnings\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
@@ -27,6 +28,7 @@ inline void ufread(void *ptr, size_t size, size_t nmemb, FILE *stream){
 \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
@@ -35,6 +37,46 @@ inline void ufwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream){
   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
+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
+\r
 // Save: saves XML tree data structure to file. \r
 void XMLTree::Save(unsigned char *filename) \r
  {\r
@@ -69,11 +111,21 @@ void XMLTree::Save(unsigned char *filename)
     \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
     // stores the texts   \r
-    if (!disable_tc)\r
+    if (!disable_tc) {\r
       Text->Save(fp);\r
-\r
+      int st = CachedText.size();\r
+      ufwrite(&st, sizeof(int),1,fp);\r
+      for (int i = 0; i< CachedText.size(); i++){\r
+       st = CachedText.at(i).size();\r
+       ufwrite(&st, sizeof(int),1,fp);\r
+       ufwrite(CachedText.at(i).c_str(),sizeof(char),1+CachedText.at(i).size(),fp);\r
+      };\r
+    };\r
     fclose(fp);\r
 \r
  }\r
@@ -85,66 +137,117 @@ 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
+    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(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
+\r
+    s_tree += sizeof(bp);\r
+\r
     // stores the table with tag names\r
     ufread(&XML_Tree->ntagnames, sizeof(int), 1, fp);\r
-    XML_Tree->TagName = (unsigned char **)malloc(XML_Tree->ntagnames*sizeof(unsigned char *));\r
+    \r
+    s_tree += sizeof(int);\r
+\r
+    XML_Tree->TagName = (unsigned char **)umalloc(XML_Tree->ntagnames*sizeof(unsigned char *));\r
+    \r
+    s_tags += sizeof(unsigned char*)*XML_Tree->ntagnames;\r
+\r
 \r
     for (i=0; i<XML_Tree->ntagnames;i++) {\r
       \r
-      // Kim is it needed ?\r
+      // OJO Kim is it needed ?\r
       int k = feof(fp);\r
-       // fscanf chokes ont "\n" which is the case for the root element\r
-       char * r = fgets(filenameaux,1023,fp);\r
-       //       int r = fscanf(fp, "<%s>\n",filenameaux);\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
-\r
-       int len = strlen((const char*)filenameaux);\r
-       XML_Tree->TagName[i] = (unsigned char *)calloc(len,sizeof(char));\r
-\r
-       //XML_Tree->TagName[i] = (unsigned char *)malloc(sizeof(unsigned char)*(strlen((const char *)filenameaux)+1));\r
-       //the - 1 removes the trailing \n\r
-       strncpy((char *)XML_Tree->TagName[i], (const char *)filenameaux,len - 1);\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
     // 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
     ufread(&(XML_Tree->disable_tc), sizeof(bool), 1, fp);\r
-    if (!(XML_Tree->indexing_empty_texts)) XML_Tree->EBVector = static_bitsequence_rrr02::load(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
     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
+    s_text = ftell(fp);\r
 \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
+      int sst;\r
+      int st;\r
+      ufread(&sst, sizeof(int),1,fp);\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
+       free(str);\r
+      };\r
+\r
     }\r
-    else\r
+    else {\r
       XML_Tree->Text = NULL;\r
+    }\r
+    s_text = ftell(fp) - s_text;\r
+\r
+    \r
+\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
     return XML_Tree;\r
  }\r
 \r
@@ -207,6 +310,9 @@ int XMLTree::SubtreeTags(treeNode x, TagType tag)
        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
 \r
     int s = x + 2*subtree_size(Par, x) - 1;\r
  \r
@@ -297,7 +403,6 @@ int XMLTree::Postorder(treeNode x)
        fprintf(stderr, "Error: data structure has not been constructed properly\n");\r
        exit(1);\r
     }\r
-\r
     return postorder_rank(Par, x);\r
  }\r
 \r
@@ -308,8 +413,8 @@ TagType XMLTree::Tag(treeNode x)
        fprintf(stderr, "Error: data structure has not been constructed properly\n");\r
        exit(1);\r
     }\r
-\r
-    return Tags->access(node2tagpos(x));\r
+    \r
+    return get_field(tags_fix,tags_blen,node2tagpos(x)); //Tags->access(node2tagpos(x));\r
  }\r
 \r
 // DocIds(x): returns the range of text identifiers that descend from node x.\r
@@ -416,7 +521,7 @@ treeNode XMLTree::TaggedChild(treeNode x, int i, TagType tag)
     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 (Tags->access(node2tagpos(child)) == tag) { // current child is labeled with tag of interest\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
@@ -436,6 +541,9 @@ treeNode XMLTree::TaggedDesc(treeNode x, TagType tag)
 \r
     int r, s;\r
     treeNode y;\r
+    if (isleaf(Par,x))\r
+      return NULLT;\r
+\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
@@ -444,6 +552,28 @@ treeNode XMLTree::TaggedDesc(treeNode x, TagType tag)
     else return y;\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 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 (x==NULLT)\r
+      return NULLT;\r
+\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
+    y = tagpos2node(s); // transforms the tag position into a node position  \r
+    return (y<=x ? NULLT : y);\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
@@ -469,9 +599,30 @@ treeNode XMLTree::TaggedPrec(treeNode x, TagType tag)
     return NULLT; // there is no such node \r
  }\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
-treeNode XMLTree::TaggedFoll(treeNode x, TagType tag) \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
+    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
+    if (s==-1) return NULLT;\r
+    else return tagpos2node(s);\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
@@ -479,12 +630,39 @@ treeNode XMLTree::TaggedFoll(treeNode x, TagType tag)
     }\r
 \r
     int r, s;\r
-    r = (int) Tags->rank(tag, node2tagpos(next_sibling(Par, x))-1);\r
+    treeNode ns = next_sibling(Par,x);\r
+\r
+    if (x == NULLT || x == Root() || ns == -1)\r
+      return NULLT;\r
+\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
     if (s==-1) return NULLT;\r
     else return tagpos2node(s);\r
  }\r
 \r
+\r
+// TaggedAncestor(x, tag): returns the closest ancestor of x tagged tag. Return\r
+// NULLT is there is none.\r
+treeNode XMLTree::TaggedAncestor(treeNode x, TagType tag)\r
+ {\r
+    if (!finished) {\r
+       fprintf(stderr, "Error: data structure has not been constructed properly\n");\r
+       exit(1);\r
+    }\r
+    \r
+    if (x == NULLT || x == Root())\r
+       return NULLT;\r
+    \r
+    treeNode s = parent(Par, x), r = Root();\r
+    while (s != r) {\r
+       if (get_field(tags_fix,tags_blen,node2tagpos(s)) /*Tags->access(node2tagpos(s))*/ == tag) return s;\r
+       s = parent(Par, s);\r
+    }\r
+    return NULLT;\r
+ }\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
@@ -591,10 +769,16 @@ treeNode XMLTree::ParentNode(DocID d)
        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
     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);\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
@@ -614,53 +798,39 @@ int XMLTree::OpenDocument(bool empty_texts, int sample_rate_text,bool dtc)
     found_attributes = false;\r
     npar = 0;\r
     parArraySize = 1;\r
-    ntagnames = 2;    \r
+    ntagnames = 4;    \r
     disable_tc = dtc;\r
     \r
     indexing_empty_texts = empty_texts;\r
     \r
-    par_aux = (pb *)malloc(sizeof(pb)*parArraySize);\r
-    if (!par_aux) {\r
-       fprintf(stderr, "Error: not enough memory\n");\r
-       return NULLT;\r
-    }\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 **) malloc(2*sizeof(unsigned char*));\r
-    if (!TagName){\r
-       fprintf(stderr, "Error: not enough memory\n");\r
-       return NULLT;\r
-    }\r
+    TagName = (unsigned char **) umalloc(4*sizeof(unsigned char*));\r
 \r
-    TagName[0] = (unsigned char *) malloc(4*sizeof(unsigned char));\r
-    strcpy((char *) TagName[0], "<@>");\r
+    TagName[0] = (unsigned char *) umalloc(4*sizeof(unsigned char));\r
 \r
-    if (!TagName[0]){\r
-      fprintf(stderr, "Error: not enough memory\n");\r
-      return NULLT;\r
-    }\r
+    strcpy((char *) TagName[0], "<@>");\r
 \r
-    TagName[1] = (unsigned char *) malloc(4*sizeof(unsigned char));\r
-    if (!TagName[1]){\r
-      fprintf(stderr, "Error: not enough memory\n");\r
-      return NULLT;\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
-    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
+    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
+    \r
     \r
     Text = TextCollection::InitTextCollection((unsigned)sample_rate_text);\r
     \r
@@ -679,30 +849,38 @@ 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
+    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
-    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_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
+               static_bitsequence_builder * bmb = new static_bitsequence_builder_brw32(20);\r
+               alphabet_mapper *am = new alphabet_mapper_none();\r
+               wt_coder * wc = new wt_coder_huff((uint*)tags_aux,npar-1,am);\r
+               Tags = new static_sequence_wvtree((uint*)tags_aux,npar-1,wc ,bmb, am);\r
+    //Tags = new static_sequence_gmr((uint *) tags_aux, (uint) npar-1,ntagsize, bmb, ssb);\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
+               tags_blen = bits(max_tag);\r
+               tags_len = (uint)npar-1;\r
+               tags_fix = new uint[uint_len(tags_blen,tags_len)];\r
+               for(uint i=0;i<(uint)npar-1;i++)\r
+                       set_field(tags_fix,tags_blen,i,tags_aux[i]);\r
     \r
     delete bmb;\r
-    delete pmb;\r
-    delete ssb;\r
+    //delete pmb;\r
+    //delete ssb;\r
     // makes the text collection static\r
     if (!disable_tc)\r
       Text->MakeStatic();\r
@@ -714,10 +892,13 @@ int XMLTree::CloseDocument()
        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
@@ -737,15 +918,10 @@ int XMLTree::NewOpenTag(unsigned char *tagname)
     \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 *)realloc(par_aux, sizeof(pb)*2*parArraySize);\r
+       par_aux = (pb *)urealloc(par_aux, sizeof(pb)*2*parArraySize);\r
        parArraySize *= 2;\r
     }\r
     \r
-    if (!par_aux) {\r
-       fprintf(stderr, "Error: not enough memory\n");\r
-       return NULLT;    \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
@@ -759,7 +935,7 @@ int XMLTree::NewOpenTag(unsigned char *tagname)
       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
@@ -767,19 +943,15 @@ 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
-    tags_aux = (TagType *) realloc(tags_aux, sizeof(TagType)*(npar + 1));\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
     npar++;\r
-\r
+    \r
     return 1;\r
     \r
  }\r
@@ -799,41 +971,27 @@ int XMLTree::NewClosingTag(unsigned char *tagname)
     \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 *)realloc(par_aux, sizeof(pb)*2*parArraySize);\r
+       par_aux = (pb *)urealloc(par_aux, sizeof(pb)*2*parArraySize);\r
        parArraySize *= 2;\r
     }\r
     \r
-    if (!par_aux) {\r
-       fprintf(stderr, "Error: not enough memory\n");\r
-       return NULLT;    \r
-    }\r
-    setbit(par_aux,npar,CP);  // marks a new closing opening parenthesis\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])==0) break;\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 **)realloc(TagName, sizeof(char *)*(ntagnames+1));\r
-       \r
-       if (!TagName) {\r
-          fprintf(stderr, "Error: not enough memory\n");\r
-          return NULLT;\r
-       }\r
+       TagName = (unsigned char **)urealloc(TagName, sizeof(char *)*(ntagnames+1));\r
        \r
        ntagnames++;\r
-       TagName[i] = (unsigned char *)malloc(sizeof(char)*(strlen((const char *)tagname)+2));\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
@@ -860,16 +1018,13 @@ int XMLTree::NewText(unsigned char *s)
     };\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
+    string cpps = (char*) s;\r
+    CachedText.push_back(cpps); \r
     \r
     return 1; // success\r
  }\r
@@ -888,11 +1043,7 @@ 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
@@ -922,23 +1073,33 @@ 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
-\r
   TagType id = XMLTree::GetTagId(tagname);\r
   if (id == NULLT){\r
     id = ntagnames;\r
     ntagnames = ntagnames + 1;    \r
-    TagName = (unsigned char **) realloc(TagName,ntagnames*(sizeof(unsigned char*)));\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