Add check for Root Node to the parent function
[SXSI/XMLTree.git] / XMLTree.cpp
index ea12fad..21658e9 100644 (file)
@@ -1,6 +1,5 @@
-\r
 #include "XMLTree.h"\r
-\r
+#include <cstring>\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
@@ -102,7 +101,7 @@ XMLTree *XMLTree::Load(unsigned char *filename, int sample_rate_text)
     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
@@ -127,16 +126,16 @@ 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
+    //Text->~TextCollection();\r
     delete Text;\r
     Text = NULL;\r
 \r
@@ -312,8 +311,10 @@ treeNode XMLTree::Parent(treeNode x)
        fprintf(stderr, "Error: data structure has not been constructed properly\n");\r
        exit(1);\r
     }\r
-\r
-    return parent(Par, x);\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
@@ -346,7 +347,9 @@ treeNode XMLTree::NextSibling(treeNode x)
        fprintf(stderr, "Error: data structure has not been constructed properly\n");\r
        exit(1);\r
     }\r
-\r
+    if (x == Root())\r
+      return NULLT;\r
\r
     return next_sibling(Par, x);\r
  }\r
 \r
@@ -572,19 +575,18 @@ int XMLTree::OpenDocument(bool empty_texts, int sample_rate_text)
     initialized = true;\r
     finished = false;\r
     npar = 0;\r
-    ntagnames = 0;\r
-    \r
+    parArraySize = 1;\r
+    ntagnames = 0;    \r
\r
     indexing_empty_texts = empty_texts;\r
     \r
-    par_aux = (pb *)malloc(sizeof(pb));\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
-    setbit(par_aux,npar,OP);  // marks a new opening parenthesis for the tree root\r
-    npar++;\r
     \r
-    tags_aux = (TagType *)malloc(sizeof(TagType));\r
+    tags_aux = (TagType *) malloc(sizeof(TagType));\r
     if (!tags_aux) {\r
        fprintf(stderr, "Error: not enough memory\n");\r
        return NULLT;\r
@@ -622,18 +624,20 @@ int XMLTree::CloseDocument()
        fprintf(stderr, "Error: not enough memory\n");\r
        return NULLT;    \r
     }\r
-    setbit(par_aux,npar,CP); \r
-    npar++;\r
     \r
     // creates the data structure for the tree topology\r
-    Par = (bp *)malloc(sizeof(bp));      \r
+    Par = (bp *)malloc(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
+    Tags = new static_sequence_gmr((uint *) tags_aux, (uint) npar-1,2*ntagnames, bmb, ssb);\r
+    \r
+    delete bmb;\r
+    delete pmb;\r
+    delete ssb;\r
     // makes the text collection static\r
     Text->MakeStatic();\r
     \r
@@ -660,11 +664,16 @@ 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 (sizeof(pb)*8*parArraySize == npar) { // no space left for the new parenthesis\r
+       par_aux = (pb *)realloc(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
@@ -684,9 +693,7 @@ int XMLTree::NewOpenTag(unsigned char *tagname)
        TagName[i] = (unsigned char *)malloc(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
+    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
@@ -714,7 +721,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 (sizeof(pb)*8*parArraySize == npar) { // no space left for the new parenthesis\r
+       par_aux = (pb *)realloc(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