-\r
-#include "XMLTree.h"\r
+#include "basics.h"\r
#include <cstring>\r
+#include <sstream>\r
+#include "XMLTree.h"\r
+#include <sys/time.h>\r
+#include <time.h>\r
+#include <sys/stat.h> \r
+#include <unistd.h>\r
+\r
+static double tLoading = 0;\r
+\r
+static unsigned int cLoading = 0;\r
+static struct timeval tmpv1;\r
+static struct timeval tmpv2;\r
+static string mem1;\r
+static string mem2;\r
+\r
+void read_procmem(string& memstr) {\r
+ std::string buf;\r
+ pid_t pid = getpid();\r
+ std::stringstream path;\r
+ path << "/proc/" << pid << "/status";\r
+ std::ifstream infile;\r
+ infile.open (path.str().c_str(), std::ifstream::in);\r
+ while (infile.good()){\r
+ getline(infile,buf);\r
+ if (infile.eof()) {\r
+ memstr = "Could not read memory";\r
+ return;\r
+ };\r
+ int idx = buf.find("VmRSS");\r
+ if (idx == 0){\r
+ memstr = buf;\r
+ return;\r
+ };\r
+ };\r
+ memstr = "Could not read memory";\r
+ return;\r
+\r
+}\r
+\r
+#define STARTTIMER() do { \\r
+ gettimeofday(&tmpv1,NULL); \\r
+ read_procmem(mem1); \\r
+ } while (0) \\r
+\r
+#define STOPTIMER(x) do { \\r
+ read_procmem(mem2); \\r
+ gettimeofday(&tmpv2,NULL); \\r
+ (t##x) = ((tmpv2.tv_sec - tmpv1.tv_sec) * 1000000.0 + \\r
+ (tmpv2.tv_usec - tmpv1.tv_usec))/1000.0; \\r
+ (c##x)= (c##x)+1; \\r
+ } while (0)\r
+\r
+#define PRINTTIME(s,x) do { \\r
+ std::cerr << (s) << " : " << (t##x) << "ms" << std::endl; \\r
+ std::cerr << "Mem use before: " << mem1 << std::endl; \\r
+ std::cerr << "Mem use after: " << mem2 << std::endl; \\r
+ std::cerr.flush(); \\r
+ } while (0) \\r
+\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
// the tree, and storing 2 tags per tree node (opening and closing tags).\r
\r
// tag position -> tree node\r
-inline treeNode tagpos2node(int t) {\r
- return (treeNode)t;\r
-}\r
+inline treeNode tagpos2node(int t) \r
+ {\r
+ return (treeNode) t;\r
+ }\r
\r
// tree node -> tag position\r
-inline int node2tagpos(treeNode x) {\r
- return (int)x;\r
+inline int node2tagpos(treeNode x) \r
+{\r
+ return (int)x;\r
}\r
\r
-// Save: saves XML tree data structure to file. \r
-void XMLTree::Save(unsigned char *filename) \r
+// returns NULLT if the test is true\r
+#define NULLT_IF(x) do { if (x) return NULLT; } while (0)\r
+\r
+\r
+XMLTree::XMLTree( pb * const par, uint npar, vector<string> * const TN, TagIdMap * const tim, uint *empty_texts_bmp, TagType *tags,\r
+ TextCollection * const TC, bool dis_tc)\r
+\r
{\r
+ // creates the data structure for the tree topology\r
+ Par = (bp *)umalloc(sizeof(bp));\r
+ bp_construct(Par, npar, (pb*) par, OPT_DEGREE|0); \r
+ \r
+ // creates structure for tags\r
\r
- FILE *fp;\r
- char filenameaux[1024];\r
+ TagName = (vector<string>*)TN;\r
+ tIdMap = (TagIdMap *) tim;\r
+ \r
+ uint max_tag = TN->size() - 1;\r
+ \r
+ static_bitsequence_builder *bmb = new static_bitsequence_builder_sdarray();\r
+ alphabet_mapper *am = new alphabet_mapper_none();\r
+ Tags = new static_sequence_bs((uint*)tags,npar,am,bmb);\r
+ \r
+ cout << "Tags test: " << Tags->test((uint*)tags,npar) << endl;\r
+\r
+ tags_blen = bits(max_tag);\r
+ tags_len = (uint)npar;\r
+ tags_fix = new uint[uint_len(tags_blen,tags_len)];\r
+ for(uint i=0;i<(uint)npar;i++)\r
+ set_field(tags_fix,tags_blen,i,tags[i]);\r
+\r
+ delete bmb; \r
+ free(tags);\r
+ tags = NULL;\r
+ \r
+ Text = (TextCollection*) TC;\r
+\r
+\r
+ EBVector = new static_bitsequence_rrr02(empty_texts_bmp,npar,32);\r
+ free(empty_texts_bmp);\r
+ empty_texts_bmp = NULL;\r
+\r
+ \r
+ disable_tc = dis_tc;\r
+ }\r
+\r
+\r
+// ~XMLTree: frees memory of XML tree.\r
+XMLTree::~XMLTree() \r
+ { \r
int i;\r
+\r
+ destroyTree(Par);\r
+ free(Par); // frees the memory of struct Par\r
\r
- sprintf(filenameaux, "%s.srx", filename);\r
- fp = fopen(filenameaux, "w");\r
- if (fp == NULL) {\r
- printf("Error: cannot create file %s to store the tree structure of XML collection\n", filenameaux);\r
- exit(1);\r
- } \r
+ delete tIdMap;\r
+ tIdMap = NULL;\r
+ \r
+ delete TagName;\r
+ TagName = NULL;\r
\r
+ delete Tags;\r
+ Tags = NULL;\r
+\r
+ delete Text; \r
+ Text = NULL;\r
+\r
+ delete EBVector;\r
+ EBVector = NULL;\r
+\r
+\r
+ }\r
+\r
+\r
+void XMLTree::print_stats() \r
+ {\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(int fd) \r
+ {\r
+ FILE *fp;\r
+ char filenameaux[1024];\r
+ int i;\r
+\r
+ fp = fdopen(fd, "wa");\r
// first stores the tree topology\r
saveTree(Par, fp);\r
- \r
+\r
// stores the table with tag names\r
- fwrite(&ntagnames, sizeof(int), 1, fp);\r
- for (i=0; i<ntagnames;i++)\r
- fprintf(fp, "%s\n",TagName[i]);\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
- \r
- if (!indexing_empty_texts) EBVector->save(fp);\r
+ int ntags = TagName->size();\r
+\r
+ ufwrite(&ntags, sizeof(int), 1, fp);\r
+ for (i = 0; i<ntags;i++)\r
+ fprintf(fp, "%s\n",TagName->at(i).c_str());\r
\r
+\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
+ // flags \r
+ ufwrite(&disable_tc, sizeof(bool),1,fp);\r
+ \r
+ //text positions\r
+ EBVector->save(fp);\r
+ \r
// stores the texts \r
- //Text->Save(fp);\r
+ if (!disable_tc) {\r
+ Text->Save(fp);\r
+ };\r
\r
- fclose(fp);\r
\r
}\r
\r
\r
// Load: loads XML tree data structure from file. Returns\r
// a pointer to the loaded data structure\r
-XMLTree *XMLTree::Load(unsigned char *filename, int sample_rate_text) \r
+XMLTree *XMLTree::Load(int fd) \r
{\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
- if (fp == NULL) {\r
- printf("Error: cannot open file %s to load the tree structure of XML collection\n", filenameaux);\r
- exit(1);\r
- } \r
\r
- XML_Tree = new XMLTree();\r
\r
- XML_Tree->Par = (bp *)malloc(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
+ fp = fdopen(fd, "r");\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
- \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
- if (!(XML_Tree->indexing_empty_texts)) XML_Tree->EBVector = static_bitsequence_rrr02::load(fp);\r
+ XML_Tree = new XMLTree();\r
+ STARTTIMER();\r
+ // Load the tree structure\r
+ XML_Tree->Par = (bp *)umalloc(sizeof(bp));\r
\r
- // loads the tags\r
- XML_Tree->Tags = static_sequence_wvtree::load(fp);\r
+ loadTree(XML_Tree->Par, fp); \r
+ STOPTIMER(Loading);\r
+ PRINTTIME("Loading parenthesis struct", Loading);\r
+ STARTTIMER();\r
\r
- // loads the texts \r
- //XML_Tree->Text->Load(fp,sample_rate_text);\r
+ XML_Tree->TagName = new vector<string>();\r
+ XML_Tree->tIdMap = new std::unordered_map<string,int>();\r
+ \r
+ string s;\r
+ int ntags;\r
+ \r
+ // Load the tag names\r
+ ufread(&ntags, sizeof(int), 1, fp);\r
+\r
+ for (i=0; i<ntags;i++) {\r
+ char * r = fgets(buffer,1023,fp);\r
+ if (r==NULL)\r
+ throw "Cannot read tag list";\r
+ s = (const char*) buffer;\r
+ // remove the trailing \n\r
+ s.erase(s.size()-1); \r
+ XML_Tree->TagName->push_back(s);\r
+ XML_Tree->tIdMap->insert(std::make_pair(s,i));\r
+ \r
+ };\r
+ STOPTIMER(Loading);\r
+ PRINTTIME("Loading tag names struct", Loading);\r
+ STARTTIMER();\r
+\r
+ // loads the tag structure\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
+\r
+ // TODO ask francisco about this\r
+ /// FIXME:UGLY tests!\r
+ uint * seq = new uint[XML_Tree->tags_len];\r
+ for(uint i=0;i<XML_Tree->tags_len;i++)\r
+ seq[i] = get_field(XML_Tree->tags_fix,XML_Tree->tags_blen,i);\r
+ //cout << "Tags test: " << XML_Tree->Tags->test(seq,XML_Tree->tags_len) << endl;\r
+ XML_Tree->Tags->test(seq,XML_Tree->tags_len);\r
+ delete [] seq;\r
+ /// End ugly tests\r
+ \r
+ STOPTIMER(Loading);\r
+ PRINTTIME("Loading tag struct", Loading);\r
+ STARTTIMER();\r
\r
- fclose(fp);\r
+ // loads the flags\r
\r
- return XML_Tree;\r
- }\r
+ ufread(&(XML_Tree->disable_tc), sizeof(bool), 1, fp);\r
\r
+ XML_Tree->EBVector = static_bitsequence_rrr02::load(fp);\r
\r
-// ~XMLTree: frees memory of XML tree.\r
-XMLTree::~XMLTree() \r
- { \r
- int i;\r
\r
- destroyTree(Par);\r
- free(Par); // frees the memory of struct Par\r
- \r
- for (i=0; i<ntagnames;i++) \r
- free(TagName[i]);\r
- \r
- free(TagName);\r
+ STOPTIMER(Loading);\r
+ PRINTTIME("Loading text bitvector struct", Loading);\r
+ STARTTIMER();\r
\r
- if (!indexing_empty_texts) {\r
- EBVector->~static_bitsequence_rrr02();\r
- delete EBVector;\r
- EBVector = NULL;\r
+ // Not used \r
+ int sample_rate_text = 64;\r
+ // loads the texts\r
+ if (!XML_Tree->disable_tc){\r
+ XML_Tree->Text = TextCollection::Load(fp,sample_rate_text);\r
}\r
+ else XML_Tree->Text = NULL;\r
+ STOPTIMER(Loading);\r
+ PRINTTIME("Loading TextCollection", Loading);\r
+ STARTTIMER();\r
\r
- Tags->~static_sequence_wvtree();\r
- delete Tags;\r
- Tags = NULL;\r
-\r
- //Text->~TextCollection();\r
- // delete Text;\r
- // Text = NULL;\r
-\r
- initialized = false;\r
- finished = false;\r
+ return XML_Tree;\r
}\r
\r
+\r
// root(): returns the tree root.\r
-treeNode XMLTree::Root() \r
+inline treeNode XMLTree::Root() \r
{\r
- return root_node(Par);\r
+ return 0; //root_node(Par);\r
}\r
\r
// SubtreeSize(x): the number of nodes (and attributes) in the subtree of node x.\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 (x == Root())\r
+ x = first_child(Par,x);\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
return depth(Par, x) == (depth(Par, y) + 1);\r
}\r
\r
+// IsFirstChild(x): returns whether node x is the first child of its parent.\r
+bool XMLTree::IsFirstChild(treeNode x)\r
+ {\r
+ return ((x != NULLT)&&(x==Root() || prev_sibling(Par,x) == (treeNode)-1));\r
+ }\r
+\r
+\r
// NumChildren(x): number of children of node x. Constant time with the data structure\r
// of Sadakane.\r
int XMLTree::NumChildren(treeNode x) \r
// Tag(x): returns the tag identifier of node x.\r
TagType XMLTree::Tag(treeNode x) \r
{\r
- return Tags->access(node2tagpos(x));\r
+ return get_field(tags_fix,tags_blen,node2tagpos(x));\r
}\r
\r
// DocIds(x): returns the range of text identifiers that descend from node x.\r
range XMLTree::DocIds(treeNode x) \r
{\r
range r;\r
- if (indexing_empty_texts) { // faster, no rank needed\r
- r.min = x;\r
- r.max = x+2*subtree_size(Par, x)-2;\r
+ if (x == NULLT) {\r
+ r.min = NULLT;\r
+ r.max = NULLT;\r
+ return r;\r
+ };\r
+ \r
+ \r
+ int min = EBVector->rank1(x-1); \r
+ int max = EBVector->rank1(x+2*subtree_size(Par, x)-2); \r
+ if (min==max) { // range is empty, no texts within the subtree of x\r
+ r.min = NULLT;\r
+ r.max = NULLT;\r
}\r
- else { // we are not indexing empty texts, we need rank\r
- int min = EBVector->rank1(x-1); \r
- int max = EBVector->rank1(x+2*subtree_size(Par, x)-2); \r
- if (min==max) { // range is empty, no texts within the subtree of x\r
- r.min = NULLT;\r
- r.max = NULLT;\r
- }\r
- else { // the range is non-empty, there are texts within the subtree of x\r
- r.min = min+1;\r
- r.max = max;\r
- }\r
+ else { // the range is non-empty, there are texts within the subtree of x\r
+ r.min = min+1;\r
+ r.max = max;\r
}\r
return r;\r
+\r
}\r
\r
// Parent(x): returns the parent node of node x.\r
treeNode XMLTree::Parent(treeNode x) \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
treeNode XMLTree::Child(treeNode x, int i) \r
- {\r
+{\r
if (i <= OPTD) return naive_child(Par, x, i);\r
else return child(Par, x, i);\r
- }\r
+}\r
\r
// FirstChild(x): returns the first child of node x, assuming it exists. Very fast in BP.\r
treeNode XMLTree::FirstChild(treeNode x) \r
{\r
- return first_child(Par, x);\r
+ NULLT_IF(x==NULLT);\r
+ return first_child(Par, x);\r
+ }\r
+\r
+treeNode XMLTree::FirstElement(treeNode x) \r
+ {\r
+ NULLT_IF(x==NULLT);\r
+ treeNode fc = first_child(Par, x);\r
+ //<$> is 2\r
+ return ((fc == NULLT || Tag(fc) != PCDATA_TAG_ID) ? fc : next_sibling(Par,fc));\r
+\r
}\r
\r
+treeNode XMLTree::NextElement(treeNode x) \r
+{\r
+ NULLT_IF(x==NULLT);\r
+ treeNode ns = next_sibling(Par, x);\r
+ return ((ns == NULLT || Tag(ns) != PCDATA_TAG_ID) ? ns : next_sibling(Par,ns));\r
+}\r
+\r
+// LastChild(x): returns the last child of node x.\r
+treeNode XMLTree::LastChild(treeNode x)\r
+ {\r
+ NULLT_IF(x==NULLT || x == Root() || isleaf(Par,x));\r
+ return find_open(Par, find_close(Par, x)-1);\r
+ }\r
+\r
+\r
// NextSibling(x): returns the next sibling of node x, assuming it exists.\r
treeNode XMLTree::NextSibling(treeNode x) \r
{\r
- return next_sibling(Par, x);\r
+ NULLT_IF(x==NULLT || x == Root() );\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
- return prev_sibling(Par, x);\r
+ NULLT_IF(x==NULLT || x == Root());\r
+ return prev_sibling(Par, x);\r
}\r
\r
-// TaggedChild(x,i,tag): returns the i-th child of node x tagged tag, or NULLT if there is none.\r
+// TaggedChild(x,tag): returns the first child of node x tagged tag, or NULLT if there is none.\r
// Because of the balanced-parentheses representation of the tree, this operation is not supported\r
// 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
+treeNode XMLTree::TaggedChild(treeNode x, TagType tag) \r
{\r
- treeNode child;\r
\r
- 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
- 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
- child = next_sibling(Par, x); // OK, let's try with the next child\r
- }\r
- return NULLT; // no such child was found \r
+ NULLT_IF(x==NULLT || isleaf(Par,x));\r
+\r
+ treeNode child; \r
+ child = first_child(Par, x); // starts at first child of node x\r
+ if (get_field(tags_fix,tags_blen,node2tagpos(child)) == tag)\r
+ return child;\r
+ else\r
+ return TaggedFollSibling(child,tag);\r
+ }\r
+\r
+// TaggedSibling(x,tag): returns the first sibling of node x tagged tag, or NULLT if there is none.\r
+treeNode XMLTree::TaggedFollSibling(treeNode x, TagType tag)\r
+{\r
+ NULLT_IF(x==NULLT);\r
+ treeNode sibling = next_sibling(Par, x); \r
+ while (sibling != NULLT) {\r
+ if (get_field(tags_fix,tags_blen,node2tagpos(sibling)) == tag) // current sibling is labeled with tag of interest\r
+ return sibling; \r
+ sibling = next_sibling(Par, sibling); // OK, let's try with the next sibling\r
+ }\r
+ return NULLT; // no such sibling was found \r
+}\r
+\r
+treeNode XMLTree::SelectChild(treeNode x, std::unordered_set<int> *tags)\r
+{\r
+ \r
+ NULLT_IF(x==NULLT || isleaf(Par,x));\r
+ int i;\r
+ treeNode child = first_child(Par, x); \r
+ TagType t = get_field(tags_fix, tags_blen, node2tagpos(child));\r
+ std::unordered_set<int>::const_iterator tagit = tags->find(t);\r
+ if (tagit != tags->end()) return child; \r
+ return SelectFollSibling(child,tags);\r
+}\r
+\r
+\r
+treeNode XMLTree::SelectFollSibling(treeNode x, std::unordered_set<int> *tags)\r
+{\r
+\r
+ NULLT_IF(x==NULLT);\r
+ int i;\r
+ TagType t;\r
+ treeNode sibling = next_sibling(Par, x);\r
+ std::unordered_set<int>::const_iterator tagit;\r
+ while (sibling != NULLT) {\r
+ t = get_field(tags_fix, tags_blen, node2tagpos(sibling));\r
+ tagit = tags->find(t);\r
+ if (tagit != tags->end()) return sibling;\r
+ sibling = next_sibling(Par, sibling);\r
+ }\r
+ return NULLT; \r
}\r
\r
+\r
// TaggedDesc(x,tag): returns the first node tagged tag with larger preorder than x and within\r
// the subtree of x. Returns NULLT if there is none.\r
treeNode XMLTree::TaggedDesc(treeNode x, TagType tag) \r
{\r
- int r, s;\r
- treeNode y;\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
- if (!is_ancestor(Par, x, y)) return NULLT; // the next node tagged tag (in preorder) is not within the subtree of x.\r
- else return y;\r
+ NULLT_IF(x==NULLT || isleaf(Par,x));\r
+\r
+ int s = (int) Tags->select_next(tag,node2tagpos(x));\r
+ NULLT_IF (s == -1);\r
+\r
+ treeNode y = tagpos2node(s); // transforms the tag position into a node position\r
+ \r
+ return (is_ancestor(Par,x,y) ? y : NULLT);\r
}\r
\r
+\r
+treeNode XMLTree::SelectDesc(treeNode x, std::unordered_set<int> *tags)\r
+ {\r
+ NULLT_IF (x ==NULLT || isleaf(Par,x));\r
+ int i;\r
+ treeNode min = NULLT;\r
+ treeNode fc = first_child(Par,x);\r
+ treeNode aux;\r
+ std::unordered_set<int>::const_iterator tagit;\r
+ for (tagit = tags->begin(); tagit != tags->end(); tagit++) {\r
+ aux = TaggedDesc(x, (TagType) *tagit);\r
+ if (aux == fc) return fc;\r
+ if (aux == NULLT) continue;\r
+ if ((min == NULLT) || (aux < min)) min = aux;\r
+ };\r
+ return min;\r
+ }\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
- {\r
+ { \r
int r, s;\r
treeNode node_s, root;\r
r = (int)Tags->rank(tag, node2tagpos(x)-1);\r
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
- 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
- if (s==-1) return NULLT;\r
- else return tagpos2node(s);\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
-DocID XMLTree::PrevText(treeNode x) \r
- {\r
- if (x == Root()) return NULLT;\r
- if (indexing_empty_texts) // faster, no rank needed\r
- return (DocID)x-1;\r
- else { // we are not indexing empty texts, rank is needed\r
- if (EBVector->access(x-1) == 0) \r
- return (DocID)NULLT; // there is no text to the left of node (text is empty)\r
- else\r
- return (DocID)EBVector->rank1(x-1)-1; //-1 because document ids start from 0\r
- }\r
+ NULLT_IF (x ==NULLT || x == Root());\r
+ \r
+ return tagpos2node(Tags->select_next(tag,find_close(Par, x)));\r
+\r
+ } \r
+\r
+// TaggedFollBelow(x,tag,root): returns the first node tagged tag with larger preorder than x \r
+// and not in the subtree of x. Returns NULLT if there is none.\r
+treeNode XMLTree::TaggedFollBelow(treeNode x, TagType tag, treeNode root)\r
+{\r
+\r
+ NULLT_IF (x == NULLT || x == Root());\r
+ \r
+ treeNode s = tagpos2node(Tags->select_next(tag, find_close(Par, x)));\r
+ \r
+ if (root == Root()) return s;\r
+ NULLT_IF (s == NULLT || s >= find_close(Par, root));\r
+ \r
+ return s;\r
+} \r
+\r
+/* Here we inline TaggedFoll to find the min globally, and only at the end\r
+ we check if the min is below the context node */\r
+treeNode XMLTree::SelectFollBelow(treeNode x, std::unordered_set<int> *tags, treeNode root)\r
+ {\r
+\r
+ NULLT_IF(x==NULLT || x==Root());\r
+ int i;\r
+ treeNode min = NULLT;\r
+ treeNode ns = next_sibling(Par, x);\r
+ treeNode aux;\r
+ std::unordered_set<int>::const_iterator tagit;\r
+ for (tagit = tags->begin(); tagit != tags->end(); tagit++) {\r
+\r
+ aux = tagpos2node(Tags->select_next(*tagit, find_close(Par, x)));\r
+ \r
+ // The next sibling of x is guaranteed to be below ctx\r
+ // and is the node with lowest preorder which is after ctx.\r
+ // if we find it, we return early;\r
+ \r
+ if (aux == ns ) return ns;\r
+ if (aux == NULLT) continue;\r
+ if ((min == NULLT) || (aux < min)) min = aux;\r
+ };\r
+ \r
+ // found the smallest node in preorder which is after x.\r
+ // if ctx is the root node, just return what we found.\r
+\r
+ if (root == Root()) return min;\r
+ // else check whether if is in below the ctx node\r
+\r
+ NULLT_IF (min == NULLT || min >= find_close(Par, root));\r
+ \r
+ return min;\r
+ \r
}\r
\r
-// NextText(x): returns the document identifier of the text to the right\r
-// 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 (x == Root()) return NULLT;\r
- if (indexing_empty_texts) // faster, no rank needed\r
- return (DocID)x+2*subtree_size(Par, x)-1;\r
- else { // we are not indexing empty texts, rank is needed\r
- int p = x+2*subtree_size(Par, x)-1;\r
- if (EBVector->access(p) == 0) // there is no text to the right of node\r
- return (DocID)NULLT;\r
- else\r
- return (DocID)EBVector->rank1(p)-1; //-1 because document ids start from 0\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 (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
+\r
// MyText(x): returns the document identifier of the text below node x, \r
// or NULLT if x is not a leaf node or the text is empty. Assumes Doc \r
// ids start from 0.\r
DocID XMLTree::MyText(treeNode x) \r
- {\r
- if (!IsLeaf(x)) return NULLT;\r
- if (indexing_empty_texts) // faster, no rank needed\r
- return (DocID)x;\r
- else { // we are not indexing empty texts, rank is needed\r
- if (EBVector->access(x) == 0) // there is no text below node x\r
- return (DocID)NULLT;\r
- else\r
- return (DocID)EBVector->rank1(x)-1; //-1 because document ids start from 0\r
- } \r
- }\r
+{\r
+ TagType tag = Tag(x);\r
+ // seems faster than testing EBVector->access(x);\r
+\r
+ if (tag == PCDATA_TAG_ID || tag == ATTRIBUTE_DATA_TAG_ID)\r
+ return (DocID) (EBVector->rank1(x)-1); //-1 because document ids start from 0\r
+ else \r
+ return (DocID) NULLT;\r
+ \r
+}\r
\r
// TextXMLId(d): returns the preorder of document with identifier d in the tree consisting of\r
// all tree nodes and all text nodes. Assumes that the tree root has preorder 1.\r
int XMLTree::TextXMLId(DocID d) \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
- int s = EBVector->select1(d+1);\r
- return rank_open(Par, s) + d + 1; // +1 because root has preorder 1\r
- }\r
+ NULLT_IF(d == NULLT);\r
+ int s = EBVector->select1(d+1);\r
+ return rank_open(Par, s) + d + 1; // +1 because root has preorder 1\r
+ \r
}\r
\r
// NodeXMLId(x): returns the preorder of node x in the tree consisting \r
// preorder 0;\r
int XMLTree::NodeXMLId(treeNode x) \r
{\r
- if (indexing_empty_texts)\r
- return x - 1 + rank_open(Par, x);\r
- else {\r
- if (x == Root()) return 1; // root node has preorder 1\r
- else\r
- return rank_open(Par, x) + EBVector->rank1(x-1);\r
- }\r
+ NULLT_IF(x == NULLT);\r
+ if (x == Root()) return 1; // root node has preorder 1\r
+ return rank_open(Par, x) + EBVector->rank1(x-1);\r
}\r
\r
// ParentNode(d): returns the parent node of document identifier d.\r
treeNode XMLTree::ParentNode(DocID d) \r
- {\r
- int s;\r
- if (indexing_empty_texts) s = d;\r
- else s = EBVector->select1(d);\r
- \r
- if (inspect(Par,s) == CP) // is a closing parenthesis\r
- return parent(Par, find_open(Par, s));\r
- else // is an opening parenthesis\r
- return (treeNode)s;\r
- \r
+ { \r
+ NULLT_IF (d == NULLT); \r
+ return (treeNode) EBVector->select1(d+1); \r
}\r
\r
-\r
-// 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
- {\r
- initialized = true;\r
- finished = false;\r
- npar = 0;\r
- ntagnames = 0;\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
- \r
- tags_aux = (TagType *)malloc(sizeof(TagType));\r
- if (!tags_aux) {\r
- fprintf(stderr, "Error: not enough memory\n");\r
- return NULLT;\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
- \r
- return 1; // indicates success in the initialization of the data structure\r
- }\r
-\r
-// CloseDocument(): it finishes the construction of the data structure for the XML\r
-// document. Tree and tags are represented in the final form, dynamic data \r
-// structures are made static, and the flag "finished" is set to true. After that, \r
-// the data structure can be queried.\r
-int XMLTree::CloseDocument()\r
+// GetTagId: returns the tag identifier corresponding to a given tag name.\r
+// Returns NULLT in case that the tag name does not exists.\r
+TagType XMLTree::GetTagId(unsigned char *tagname)\r
{\r
- if (!initialized) { // data structure has not been initialized properly\r
- fprintf(stderr, "Error: data structure has not been initialized properly (by calling method OpenDocument)\n");\r
- return NULLT;\r
- }\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
- \r
- // creates the data structure for the tree topology\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
-\r
- // makes the text collection static\r
- //Text->MakeStatic();\r
+ \r
+ string s = (char *) tagname;\r
+ TagIdMapIT it = tIdMap->find(s); \r
+ return (TagType) ((it != tIdMap->end()) ? it->second : -1);\r
\r
- // creates the data structure marking the non-empty texts (just in the case it is necessary)\r
- if (!indexing_empty_texts) \r
- EBVector = new static_bitsequence_rrr02((uint *)empty_texts_aux,(ulong)npar,(uint)32);\r
-\r
- finished = true;\r
-\r
- return 1; // indicates success in the inicialization\r
}\r
\r
\r
-// NewOpenTag(tagname): indicates the event of finding a new opening tag in the document.\r
-// Tag name is given. Returns a non-zero value upon success, and returns NULLT\r
-// in case of failing when trying to insert the new tag.\r
-int XMLTree::NewOpenTag(unsigned char *tagname)\r
+// GetTagName(tagid): returns the tag name of a given tag identifier.\r
+// Returns NULL in case that the tag identifier is not valid.\r
+unsigned char *XMLTree::GetTagName(TagType tagid)\r
{\r
- int i;\r
-\r
- if (!initialized) { // data structure has not been initialized properly\r
- fprintf(stderr, "Error: you cannot insert a new opening tag without first calling method OpenDocument first\n");\r
- return NULLT;\r
- }\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
- }\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
- \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
- \r
- ntagnames++;\r
- 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
- if (!tags_aux) {\r
- fprintf(stderr, "Error: not enough memory\n");\r
- return NULLT;\r
- }\r
-\r
- tags_aux[npar] = i; // inserts the new tag id within the preorder sequence of tags\r
- \r
- npar++;\r
-\r
- return 1;\r
+ unsigned char *s;\r
+ if ( tagid < 0 || tagid >= TagName->size())\r
+ return (unsigned char *) "<INVALID TAG>";\r
+ strcpy((char *)s, TagName->at(tagid).c_str());\r
\r
+ return (s == NULL ? (unsigned char*) "<INVALID TAG>" : s);\r
}\r
\r
\r
-// NewClosingTag(tagname): indicates the event of finding a new closing tag in the document.\r
-// Tag name is given. Returns a non-zero value upon success, and returns NULLT\r
-// in case of failing when trying to insert the new tag.\r
-int XMLTree::NewClosingTag(unsigned char *tagname)\r
+const unsigned char *XMLTree::GetTagNameByRef(TagType tagid)\r
{\r
- int i;\r
\r
- if (!initialized) { // data structure has not been initialized properly\r
- fprintf(stderr, "Error: you cannot insert a new closing tag without first calling method OpenDocument first\n");\r
- return NULLT;\r
- }\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
- }\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
- // it in the table.\r
- for (i=0; i<ntagnames; i++)\r
- 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
- \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
- } \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
-\r
- tags_aux[npar] = i; // inserts the new tag id within the preorder sequence of tags\r
- \r
- npar++;\r
-\r
- return 1; // success\r
- \r
+ unsigned char *s;\r
+ if ( tagid < 0 || tagid >= TagName->size())\r
+ return (unsigned char *) "<INVALID TAG>";\r
+ \r
+ return (const unsigned char *) TagName->at(tagid).c_str();\r
+ \r
}\r
\r
\r
-// NewText(s): indicates the event of finding a new (non-empty) text s in the document.\r
-// The new text is inserted within the text collection. Returns a non-zero value upon\r
-// success, NULLT in case of error.\r
-int XMLTree::NewText(unsigned char *s)\r
- {\r
- if (!initialized) { // data structure has not been initialized properly\r
- fprintf(stderr, "Error: you cannot insert a new text without first calling method OpenDocument first\n");\r
- return NULLT;\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
- }\r
- \r
- //Text->InsertText(s);\r
- \r
- return 1; // success\r
- }\r
-\r
-// NewEmptyText(): indicates the event of finding a new empty text in the document.\r
-// In case of indexing empty and non-empty texts, we insert the empty texts into the\r
-// text collection. In case of indexing only non-empty texts, it just indicates an\r
-// empty text in the bit vector of empty texts. Returns a non-zero value upon\r
-// success, NULLT in case of error.\r
-int XMLTree::NewEmptyText() \r
- {\r
- unsigned char c = 0;\r
- if (!initialized) { // data structure has not been initialized properly\r
- fprintf(stderr, "Error: you cannot insert a new empty text without first calling method OpenDocument first\n");\r
- return NULLT;\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
- 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
+TagType XMLTree::RegisterTag(unsigned char *tagname)\r
+ { \r
+ TagType id = XMLTree::GetTagId(tagname);\r
+ if (id == NULLT) {\r
+ string s = (char *) tagname; \r
+ REGISTER_TAG(TagName,tIdMap,s);\r
+ \r
+ };\r
\r
- return 1; // success \r
+ return id;\r
}\r
\r
\r
-// GetTagId: returns the tag identifier corresponding to a given tag name.\r
-// Returns NULLT in case that the tag name does not exists.\r
-TagType XMLTree::GetTagId(unsigned char *tagname)\r
- {\r
- int i;\r
- // this should be changed for more efficient processing\r
- for (i=0; i<ntagnames; i++)\r
- if (strcmp((const char *)tagname,(const char *)TagName[i])==0) break; \r
- if (i==ntagnames) return (TagType)NULLT; // tagname does not exists in the table\r
- else return i;\r
- }\r
-\r
-\r
-// GetTagName(tagid): returns the tag name of a given tag identifier.\r
-// Returns NULL in case that the tag identifier is not valid.\r
-unsigned char *XMLTree::GetTagName(TagType tagid)\r
- {\r
- 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
- strcpy((char *)s, (const char *)TagName[tagid]);\r
- return s;\r
- }\r
-\r