X-Git-Url: http://git.nguyen.vg/gitweb/?a=blobdiff_plain;f=XMLTree.cpp;h=bd728af5d665e695733364fc0783436b4f072ea2;hb=5db16dd3e0bf609bc0fa84ee7d067f6bbc58013e;hp=8dcdaec6904f8e017e064e912fa129ced60a8b57;hpb=c1865639eb1a804ff9363035a2a656b64fd98b9e;p=SXSI%2FXMLTree.git diff --git a/XMLTree.cpp b/XMLTree.cpp index 8dcdaec..bd728af 100644 --- a/XMLTree.cpp +++ b/XMLTree.cpp @@ -211,12 +211,19 @@ XMLTree *XMLTree::Load(unsigned char *filename, int sample_rate_text) s_tree+=2*sizeof(uint)+sizeof(uint)*uint_len(XML_Tree->tags_blen,XML_Tree->tags_len); s_tree+= XML_Tree->Tags->size(); + /// FIXME:UGLY tests! + uint * seq = new uint[XML_Tree->tags_len]; + for(uint i=0;itags_len;i++) + seq[i] = get_field(XML_Tree->tags_fix,XML_Tree->tags_blen,i); + cout << "Tags test: " << XML_Tree->Tags->test(seq,XML_Tree->tags_len) << endl; + delete [] seq; + /// End ugly tests + s_text = ftell(fp); // loads the texts if (!XML_Tree->disable_tc){ - XML_Tree->Text = TextCollection::InitTextCollection(sample_rate_text); - XML_Tree->Text->Load(fp,sample_rate_text); + XML_Tree->Text = TextCollection::Load(fp,sample_rate_text); int sst; int st; ufread(&sst, sizeof(int),1,fp); @@ -276,7 +283,9 @@ XMLTree::~XMLTree() Tags = NULL; //Text->~TextCollection(); - delete Text; + delete TextBuilder; + TextBuilder = NULL; + delete Text; Text = NULL; initialized = false; @@ -348,7 +357,9 @@ bool XMLTree::IsChild(treeNode x, treeNode y) if (!is_ancestor(Par, x, y)) return false; return depth(Par, x) == (depth(Par, y) + 1); } - +bool XMLTree::IsFirstChild(treeNode x){ + return ((x != NULLT)&&(x==Root() || prev_sibling(Par,x) == NULLT)); +} // NumChildren(x): number of children of node x. Constant time with the data structure // of Sadakane. int XMLTree::NumChildren(treeNode x) @@ -490,6 +501,14 @@ treeNode XMLTree::FirstChild(treeNode x) return first_child(Par, x); } +treeNode XMLTree::LastChild(treeNode x) +{ + if (x == Root() || isleaf(Par,x) || x == NULLT) + return x; + else + return find_open(Par,find_close(Par,parent(Par,x))-1); +} + // NextSibling(x): returns the next sibling of node x, assuming it exists. treeNode XMLTree::NextSibling(treeNode x) { @@ -547,13 +566,14 @@ treeNode XMLTree::TaggedDesc(treeNode x, TagType tag) exit(1); } - int r, s; + //int r, s; treeNode y; if (isleaf(Par,x)) return NULLT; - r = (int) Tags->rank(tag, node2tagpos(x)); - s = (int) Tags->select(tag, r+1); + int s = (int) Tags->select_next(tag,node2tagpos(x)); + /*r = (int) Tags->rank(tag, node2tagpos(x)); + s = (int) Tags->select(tag, r+1);*/ if (s == -1) return NULLT; // there is no such node y = tagpos2node(s); // transforms the tag position into a node position if (!is_ancestor(Par, x, y)) return NULLT; // the next node tagged tag (in preorder) is not within the subtree of x. @@ -640,14 +660,15 @@ treeNode XMLTree::TaggedDescOrFollOnly(treeNode x,TagType *folltags, unsigned in { treeNode res,y,lim; - int r,s; + //int r,s; lim = find_close(Par,root); res=NULLT; for (unsigned int i = 0; i < ftlen; i ++ ) { - r = (int) Tags->rank(folltags[i], node2tagpos(x)); - s = (int) Tags->select(folltags[i], r+1); + int s = (int) Tags->select_next(folltags[i],node2tagpos(x)); + /*r = (int) Tags->rank(folltags[i], node2tagpos(x)); + s = (int) Tags->select(folltags[i], r+1);*/ if (s == -1) y = NULLT; // there is no such node else { @@ -743,12 +764,13 @@ treeNode XMLTree::TaggedFoll(treeNode x, TagType tag) exit(1); } - int r, s; + //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. + + int s = (int) Tags->select_next(tag,find_close(Par,x)); + /*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); } @@ -758,17 +780,15 @@ treeNode XMLTree::TaggedFoll(treeNode x, TagType tag) treeNode XMLTree::TaggedFollBelow(treeNode x, TagType tag, treeNode root) { - int r, s; - int lim = node2tagpos(find_close(Par,root)); - 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 || s >= lim) - return NULLT; - else - return tagpos2node(s); + if (x == NULLT || x == Root()) return NULLT; + treeNode s = (treeNode) Tags->select_next(tag,find_close(Par,x)); + /*int r = (int) Tags->rank(tag, find_close(Par, x)); + int s = (int) Tags->select(tag, r+1); */ + if (root == Root()) + return s; + + if (s == NULLT || s >= find_close(Par,root)) return NULLT; + return s; } @@ -781,14 +801,15 @@ treeNode XMLTree::TaggedFollowingSibling(treeNode x, TagType tag) exit(1); } - int r, s; + //int r, s; treeNode ns = next_sibling(Par,x); if (x == NULLT || x == Root() || ns == -1) return NULLT; - 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. + int s = (int) Tags->select_next(tag,node2tagpos(ns)-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); } @@ -1005,9 +1026,11 @@ int XMLTree::OpenDocument(bool empty_texts, int sample_rate_text,bool dtc) if (!indexing_empty_texts) empty_texts_aux = (unsigned int *)umalloc(sizeof(unsigned int)); - - - Text = TextCollection::InitTextCollection((unsigned)sample_rate_text); + if (disable_tc) + TextBuilder = 0; + else + TextBuilder = new TextCollectionBuilder((unsigned)sample_rate_text); + Text = 0; return 1; // indicates success in the initialization of the data structure } @@ -1030,39 +1053,51 @@ 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); - // 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 uint max_tag = 0; for(uint i=0;i<(uint)npar-1;i++) max_tag = max(max_tag,tags_aux[i]); - max_tag++; - int ntagsize = found_attributes ? 2*ntagnames-1 : 2*ntagnames - 2; - tags_aux = (TagType *) urealloc(tags_aux, sizeof(TagType)*(npar + 1)); - tags_aux[npar++] = max_tag; + //max_tag++; + //tags_aux = (TagType *) urealloc(tags_aux, sizeof(TagType)*(npar + 1)); + //tags_aux[npar++] = max_tag; + //int ntagsize = found_attributes ? 2*ntagnames-1 : 2*ntagnames - 2; + int ntagsize = 2*ntagnames + 2; - static_bitsequence_builder * bmb = new static_bitsequence_builder_brw32(20); + //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_sdarray(); alphabet_mapper *am = new alphabet_mapper_none(); - wt_coder * wc = new wt_coder_huff((uint*)tags_aux,npar,am); - Tags = new static_sequence_wvtree((uint*)tags_aux,npar,wc ,bmb, am); - //Tags = new static_sequence_gmr((uint *) tags_aux, (uint) npar-1,ntagsize, bmb, ssb); + //wt_coder * wc = new wt_coder_huff((uint*)tags_aux,npar,am); + //Tags = new static_sequence_wvtree((uint*)tags_aux,npar,wc ,bmb, am); + //Tags = new static_sequence_gmr((uint *) tags_aux, (uint) npar,ntagsize, bmb, ssb); + Tags = new static_sequence_bs((uint*)tags_aux,npar,am,bmb); + + cout << "Tags test: " << Tags->test((uint*)tags_aux,npar) << endl; + tags_blen = bits(max_tag); tags_len = (uint)npar; tags_fix = new uint[uint_len(tags_blen,tags_len)]; - for(uint i=0;i<(uint)npar-1;i++) + for(uint i=0;i<(uint)npar;i++) set_field(tags_fix,tags_blen,i,tags_aux[i]); delete bmb; //delete pmb; //delete ssb; + + // makes the text collection static if (!disable_tc) - Text->MakeStatic(); - + { + assert(Text == 0); + assert(TextBuilder != 0); + Text = TextBuilder->InitTextCollection(); + delete TextBuilder; + TextBuilder = 0; + } + // creates the data structure marking the non-empty texts (just in the case it is necessary) if (!indexing_empty_texts) { EBVector = new static_bitsequence_rrr02((uint *)empty_texts_aux,(ulong)npar,(uint)32); @@ -1200,7 +1235,7 @@ int XMLTree::NewText(unsigned char *s) bitset(empty_texts_aux, npar-1); // marks the non-empty text with a 1 in the bit vector } - Text->InsertText(s); + TextBuilder->InsertText(s); string cpps = (char*) s; CachedText.push_back(cpps); @@ -1225,7 +1260,7 @@ int XMLTree::NewEmptyText() bitclean(empty_texts_aux, npar-1); // marks the empty text with a 0 in the bit vector } - else Text->InsertText(&c); // we insert the empty text just in case we index all the texts + else TextBuilder->InsertText(&c); // we insert the empty text just in case we index all the texts return 1; // success } @@ -1239,7 +1274,7 @@ TagType XMLTree::GetTagId(unsigned char *tagname) // this should be changed for more efficient processing for (i=0; i= ntagnames) return NULL; // invalid tag identifier s = (unsigned char *)umalloc((strlen((const char *)TagName[tagid])+1)*sizeof(unsigned char)); strcpy((char *)s, (const char *)TagName[tagid]); @@ -1261,6 +1296,7 @@ unsigned char *XMLTree::GetTagName(TagType tagid) const unsigned char *XMLTree::GetTagNameByRef(TagType tagid) { + if(tagid==(uint)-1) return NULL; if (tagid >= ntagnames) return NULL; // invalid tag identifier return ((const unsigned char*) TagName[tagid]); }