X-Git-Url: http://git.nguyen.vg/gitweb/?a=blobdiff_plain;f=XMLTree.cpp;h=6fee8f4673d8a825a107e310d7e34c0de1d0ab79;hb=279e273b1928bbef4c960d6ce9b77f9c3fbecef9;hp=4ddec0170a5186dc02c43e98ab59e525c89380c2;hpb=dd9992dcd5366f37820c24ed7cddf24ecbc0d549;p=SXSI%2FXMLTree.git diff --git a/XMLTree.cpp b/XMLTree.cpp index 4ddec01..6fee8f4 100644 --- a/XMLTree.cpp +++ b/XMLTree.cpp @@ -67,6 +67,16 @@ inline void * umalloc(size_t size){ return dest; } +void XMLTree::print_stats() { + uint total_space = Tags->size()+sizeof(static_sequence*); + total_space += sizeof(uint*)+sizeof(uint)*(2+uint_len(tags_blen,tags_len)); + cout << "Space usage for XMLTree:" << endl + << " - tags static_sequence: " << Tags->size()+sizeof(static_sequence*) << endl + << " - tags access array: " << sizeof(uint*)+sizeof(uint)*(2+uint_len(tags_blen,tags_len)) << endl + << " ... add Diego structures ... " << endl + << " *total* " << total_space << endl; +} + // Save: saves XML tree data structure to file. void XMLTree::Save(unsigned char *filename) { @@ -101,11 +111,21 @@ void XMLTree::Save(unsigned char *filename) // stores the tags Tags->save(fp); + ufwrite(&tags_blen,sizeof(uint),1,fp); + ufwrite(&tags_len,sizeof(uint),1,fp); + ufwrite(tags_fix,sizeof(uint),uint_len(tags_blen,tags_len),fp); // stores the texts - if (!disable_tc) + if (!disable_tc) { Text->Save(fp); - + int st = CachedText.size(); + ufwrite(&st, sizeof(int),1,fp); + for (int i = 0; i< CachedText.size(); i++){ + st = CachedText.at(i).size(); + ufwrite(&st, sizeof(int),1,fp); + ufwrite(CachedText.at(i).c_str(),sizeof(char),1+CachedText.at(i).size(),fp); + }; + }; fclose(fp); } @@ -184,6 +204,11 @@ XMLTree *XMLTree::Load(unsigned char *filename, int sample_rate_text) // loads the tags XML_Tree->Tags = static_sequence::load(fp); + ufread(&XML_Tree->tags_blen,sizeof(uint),1,fp); + ufread(&XML_Tree->tags_len,sizeof(uint),1,fp); + XML_Tree->tags_fix = new uint[uint_len(XML_Tree->tags_blen,XML_Tree->tags_len)]; + ufread(XML_Tree->tags_fix,sizeof(uint),uint_len(XML_Tree->tags_blen,XML_Tree->tags_len),fp); + s_tree+=2*sizeof(uint)+sizeof(uint)*uint_len(XML_Tree->tags_blen,XML_Tree->tags_len); s_tree+= XML_Tree->Tags->size(); s_text = ftell(fp); @@ -192,20 +217,37 @@ XMLTree *XMLTree::Load(unsigned char *filename, int sample_rate_text) if (!XML_Tree->disable_tc){ XML_Tree->Text = TextCollection::InitTextCollection(sample_rate_text); XML_Tree->Text->Load(fp,sample_rate_text); + int sst; + int st; + ufread(&sst, sizeof(int),1,fp); + for (int i=0;iCachedText.push_back(cppstr); + free(str); + }; + } - else + else { XML_Tree->Text = NULL; - + } s_text = ftell(fp) - s_text; + + + + fclose(fp); - std::cerr << "Tree part is " << s_tree/1024 << " Kbytes,\n" - << "with node->tagid part " << XML_Tree->Tags->size()/1024 << "Kbytes \n" + /*std::cerr << "Tree part is " << s_tree/1024 << " Kbytes,\n" + << "with node->tagid part " << XML_Tree->Tags->size()/1024+(uint_len(XML_Tree->tags_blen,XML_Tree->tags_len)*sizeof(uint))/1024 << "Kbytes \n" << "size of Tag part : " << XML_Tree->Tags->length () << " elements\n" << "sizof(unsigned int)* " << XML_Tree->Tags->length () << " = " << sizeof(unsigned int) * XML_Tree->Tags->length () / 1024 << " Kbytes\n" << "Tag part is " << s_tags/1024 << " Kbytes,\n" - << "Text collection is " << s_text/1024 << " Kbytes \n"; + << "Text collection is " << s_text/1024 << " Kbytes \n";*/ + XML_Tree->print_stats(); return XML_Tree; } @@ -372,7 +414,7 @@ TagType XMLTree::Tag(treeNode x) exit(1); } - return Tags->access(node2tagpos(x)); + return get_field(tags_fix,tags_blen,node2tagpos(x)); //Tags->access(node2tagpos(x)); } // DocIds(x): returns the range of text identifiers that descend from node x. @@ -479,7 +521,7 @@ treeNode XMLTree::TaggedChild(treeNode x, int i, TagType tag) child = first_child(Par, x); // starts at first child of node x if (child==(treeNode)-1) return NULLT; // node x is a leaf, there is no such child while (child!=(treeNode)-1) { - if (Tags->access(node2tagpos(child)) == tag) { // current child is labeled with tag of interest + if (get_field(tags_fix,tags_blen,node2tagpos(child)) /*Tags->access(node2tagpos(child))*/ == tag) { // current child is labeled with tag of interest i--; if (i==0) return child; // we have seen i children of x tagged tag, this is the one we are looking for } @@ -557,9 +599,30 @@ treeNode XMLTree::TaggedPrec(treeNode x, TagType tag) return NULLT; // there is no such node } + // TaggedFoll(x,tag): returns the first node tagged tag with larger preorder than x and not in // the subtree of x. Returns NULLT if there is none. -treeNode XMLTree::TaggedFoll(treeNode x, TagType tag) +treeNode XMLTree::TaggedFoll(treeNode x, TagType tag) + { + if (!finished) { + fprintf(stderr, "Error: data structure has not been constructed properly\n"); + exit(1); + } + + int r, s; + if (x ==NULLT || x == Root()) + return NULLT; + + r = (int) Tags->rank(tag, find_close(Par, x)); + s = (int) Tags->select(tag, r+1); // select returns -1 in case that there is no r+1-th tag. + if (s==-1) return NULLT; + else return tagpos2node(s); + } + + +// TaggedFollowingSibling(x,tag): returns the first node tagged tag with larger preorder than x and not in +// the subtree of x. Returns NULLT if there is none. +treeNode XMLTree::TaggedFollowingSibling(treeNode x, TagType tag) { if (!finished) { fprintf(stderr, "Error: data structure has not been constructed properly\n"); @@ -567,15 +630,39 @@ treeNode XMLTree::TaggedFoll(treeNode x, TagType tag) } int r, s; - if (x ==NULLT || x == Root()|| (next_sibling(Par,x) == -1 )) + treeNode ns = next_sibling(Par,x); + + if (x == NULLT || x == Root() || ns == -1) return NULLT; - r = (int) Tags->rank(tag, node2tagpos(next_sibling(Par, x))-1); + r = (int) Tags->rank(tag, node2tagpos(ns)-1); s = (int) Tags->select(tag, r+1); // select returns -1 in case that there is no r+1-th tag. if (s==-1) return NULLT; else return tagpos2node(s); } + +// TaggedAncestor(x, tag): returns the closest ancestor of x tagged tag. Return +// NULLT is there is none. +treeNode XMLTree::TaggedAncestor(treeNode x, TagType tag) + { + if (!finished) { + fprintf(stderr, "Error: data structure has not been constructed properly\n"); + exit(1); + } + + if (x == NULLT || x == Root()) + return NULLT; + + treeNode s = parent(Par, x), r = Root(); + while (s != r) { + if (get_field(tags_fix,tags_blen,node2tagpos(s)) /*Tags->access(node2tagpos(s))*/ == tag) return s; + s = parent(Par, s); + } + return NULLT; + } + + // PrevText(x): returns the document identifier of the text to the left // of node x, or NULLT if x is the root node or the text is empty. // Assumes Doc ids start from 0. @@ -711,7 +798,7 @@ int XMLTree::OpenDocument(bool empty_texts, int sample_rate_text,bool dtc) found_attributes = false; npar = 0; parArraySize = 1; - ntagnames = 2; + ntagnames = 4; disable_tc = dtc; indexing_empty_texts = empty_texts; @@ -720,7 +807,7 @@ int XMLTree::OpenDocument(bool empty_texts, int sample_rate_text,bool dtc) tags_aux = (TagType *) umalloc(sizeof(TagType)); - TagName = (unsigned char **) umalloc(2*sizeof(unsigned char*)); + TagName = (unsigned char **) umalloc(4*sizeof(unsigned char*)); TagName[0] = (unsigned char *) umalloc(4*sizeof(unsigned char)); @@ -729,6 +816,15 @@ int XMLTree::OpenDocument(bool empty_texts, int sample_rate_text,bool dtc) TagName[1] = (unsigned char *) umalloc(4*sizeof(unsigned char)); strcpy((char *) TagName[1], "<$>"); + + //OJO need to put these in the table too. + TagName[2] = (unsigned char *) umalloc(5*sizeof(unsigned char)); + + strcpy((char *) TagName[2], "/<@>"); + + TagName[3] = (unsigned char *) umalloc(5*sizeof(unsigned char)); + + strcpy((char *) TagName[3], "/<$>"); if (!indexing_empty_texts) @@ -759,20 +855,32 @@ int XMLTree::CloseDocument() Par = (bp *)umalloc(sizeof(bp)); bp_construct(Par, npar, par_aux, OPT_DEGREE|0); // creates structure for tags - static_bitsequence_builder * bmb = new static_bitsequence_builder_brw32(20); - static_permutation_builder * pmb = new static_permutation_builder_mrrr(PERM_SAMPLE, bmb); - static_sequence_builder * ssb = new static_sequence_builder_gmr_chunk(bmb, pmb); + //static_bitsequence_builder * bmb = new static_bitsequence_builder_brw32(20); + //static_permutation_builder * pmb = new static_permutation_builder_mrrr(PERM_SAMPLE, bmb); + //static_sequence_builder * ssb = new static_sequence_builder_gmr_chunk(bmb, pmb); // If we found an attribute then "<@>" is present in the tree // if we didn't then it is not. "<$>" is never present in the tree int ntagsize = found_attributes ? 2*ntagnames-1 : 2*ntagnames - 2; - Tags = new static_sequence_gmr((uint *) tags_aux, (uint) npar-1,ntagsize, bmb, ssb); + static_bitsequence_builder * bmb = new static_bitsequence_builder_brw32(20); + alphabet_mapper *am = new alphabet_mapper_none(); + wt_coder * wc = new wt_coder_huff((uint*)tags_aux,npar-1,am); + Tags = new static_sequence_wvtree((uint*)tags_aux,npar-1,wc ,bmb, am); + //Tags = new static_sequence_gmr((uint *) tags_aux, (uint) npar-1,ntagsize, bmb, ssb); + uint max_tag = 0; + for(uint i=0;i<(uint)npar-1;i++) + max_tag = max(max_tag,tags_aux[i]); + tags_blen = bits(max_tag); + tags_len = (uint)npar-1; + tags_fix = new uint[uint_len(tags_blen,tags_len)]; + for(uint i=0;i<(uint)npar-1;i++) + set_field(tags_fix,tags_blen,i,tags_aux[i]); delete bmb; - delete pmb; - delete ssb; + //delete pmb; + //delete ssb; // makes the text collection static if (!disable_tc) Text->MakeStatic(); @@ -790,6 +898,7 @@ int XMLTree::CloseDocument() tags_aux = NULL; finished = true; + print_stats(); return 1; // indicates success in the inicialization } @@ -842,7 +951,7 @@ int XMLTree::NewOpenTag(unsigned char *tagname) tags_aux[npar] = i; // inserts the new tag id within the preorder sequence of tags npar++; - + return 1; } @@ -866,17 +975,16 @@ int XMLTree::NewClosingTag(unsigned char *tagname) parArraySize *= 2; } - setbit(par_aux,npar,CP); // marks a new closing opening parenthesis + setbit(par_aux,npar,CP); // marks a new closing parenthesis // transforms the tagname into a tag identifier. If the tag is new, we insert // it in the table. for (i=0; iInsertText(s); + string cpps = (char*) s; + CachedText.push_back(cpps); return 1; // success }