Add RegisterTag method
[SXSI/XMLTree.git] / XMLTree.h
1 \r
2 /******************************************************************************\r
3  *   Copyright (C) 2008 by Diego Arroyuelo                                    *\r
4  *   Interface for the in-memory XQuery/XPath engine                          *\r
5  *                                                                            *\r
6  *   This program is free software; you can redistribute it and/or modify     *\r
7  *   it under the terms of the GNU Lesser General Public License as published *\r
8  *   by the Free Software Foundation; either version 2 of the License, or     *\r
9  *   (at your option) any later version.                                      *\r
10  *                                                                            *\r
11  *   This program is distributed in the hope that it will be useful,          *\r
12  *   but WITHOUT ANY WARRANTY; without even the implied warranty of           *\r
13  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            *\r
14  *   GNU Lesser General Public License for more details.                      *\r
15  *                                                                            *\r
16  *   You should have received a copy of the GNU Lesser General Public License *\r
17  *   along with this program; if not, write to the                            *\r
18  *   Free Software Foundation, Inc.,                                          *\r
19  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.                *\r
20  ******************************************************************************/ \r
21 \r
22 #ifndef XMLTREE_H_\r
23 #define XMLTREE_H_\r
24 #include "TextCollection/TextCollection.h"\r
25 #include <stdio.h>\r
26 #include <stdlib.h>\r
27 #include <cstring>\r
28 #include "bp.h"\r
29 #include <static_bitsequence.h>\r
30 #include <alphabet_mapper.h>\r
31 #include <static_sequence.h>\r
32 using SXSI::TextCollection;\r
33 \r
34 \r
35 // this constant is used to efficiently compute the child operation in the tree\r
36 #define OPTD 10\r
37 \r
38 #define NULLT -1\r
39 \r
40 #define PERM_SAMPLE 10\r
41 \r
42         // sets bit p in e\r
43 #define bitset(e,p) ((e)[(p)/W] |= (1<<((p)%W)))\r
44         // cleans bit p in e\r
45 #define bitclean(e,p) ((e)[(p)/W] &= ~(1<<((p)%W)))\r
46 \r
47 \r
48 typedef int treeNode;\r
49 typedef int TagType; \r
50 typedef int DocID;  \r
51 \r
52 typedef struct {\r
53    int min;\r
54    int max;\r
55 } range;\r
56 \r
57 \r
58 class XMLTree {\r
59    /** Balanced parentheses representation of the tree */\r
60    bp *Par;\r
61  \r
62    /** Mapping from tag identifer to tag name */  \r
63    unsigned char **TagName;\r
64   \r
65    /** boolean flag indicating whether we are indexing empty texts or not */\r
66    bool indexing_empty_texts; \r
67    \r
68    /** Bit vector indicating with a 1 the positions of the non-empty texts. */\r
69    static_bitsequence_rrr02 *EBVector;  \r
70                       \r
71    /** Tag sequence represented with a data structure for rank and select */\r
72    static_sequence *Tags;\r
73 \r
74    /** The texts in the XML document */\r
75    TextCollection *Text;\r
76    \r
77    /** Flag indicating whether the whole data structure has been constructed or \r
78     * not. If the value is true, you cannot add more texts, nodes, etc. */\r
79    bool finished;\r
80 \r
81    /** Flag indicating whether the construction of the data structure has been\r
82     * initialized or not (by calling method OpenDocument()). If this is true,\r
83     * you cannot insert new tags or texts. */\r
84    bool initialized;\r
85    \r
86    /* the following components are used for construction purposes */\r
87    pb *par_aux;\r
88    TagType *tags_aux;\r
89    int npar;\r
90    int parArraySize;\r
91    int ntagnames;\r
92    unsigned int *empty_texts_aux;\r
93    \r
94 public:\r
95 \r
96    /** Data structure constructor */\r
97    XMLTree() {finished = false; initialized = false;}; \r
98  \r
99    /** Data structure destructor */\r
100    ~XMLTree();\r
101    \r
102    /** root(): returns the tree root. */\r
103    treeNode Root();\r
104    \r
105    /** SubtreeSize(x): the number of nodes (and attributes) in the subtree of \r
106     * node x. */\r
107    int SubtreeSize(treeNode x);\r
108    \r
109    /** SubtreeTags(x,tag): the number of occurrences of tag within the subtree \r
110     * of node x. */\r
111    int SubtreeTags(treeNode x, TagType tag);\r
112    \r
113    /** IsLeaf(x): returns whether node x is leaf or not. In the succinct \r
114     * representation this is just a bit inspection. */\r
115    bool IsLeaf(treeNode x);\r
116     \r
117    /** IsAncestor(x,y): returns whether node x is ancestor of node y. */\r
118    bool IsAncestor(treeNode x, treeNode y);\r
119   \r
120    /** IsChild(x,y): returns whether node x is parent of node y. */\r
121    bool IsChild(treeNode x, treeNode y);\r
122    \r
123    /** NumChildren(x): number of children of node x. Constant time with the \r
124     * data structure of Sadakane. */\r
125    int NumChildren(treeNode x);\r
126    \r
127    /** ChildNumber(x): returns i if node x is the i-th children of its \r
128     * parent. */\r
129    inline int ChildNumber(treeNode x);\r
130 \r
131    /** Depth(x): depth of node x, a simple binary rank on the parentheses \r
132     * sequence. */\r
133    int Depth(treeNode x);\r
134    \r
135    /** Preorder(x): returns the preorder number of node x, just regarding tree \r
136     * nodes (and not texts). */ \r
137    int Preorder(treeNode x);\r
138    \r
139    /** Postorder(x): returns the postorder number of node x, just regarding \r
140     * tree nodes (and not texts). */\r
141    int Postorder(treeNode x);\r
142    \r
143    /** Tag(x): returns the tag identifier of node x. */\r
144    TagType Tag(treeNode x);\r
145    \r
146    /** DocIds(x): returns the range (i.e., a pair of integers) of document \r
147     * identifiers that descend from node x. */\r
148    range DocIds(treeNode x);\r
149    \r
150    /** Parent(x): returns the parent node of node x. */\r
151    treeNode Parent(treeNode x);\r
152    \r
153    /** Child(x,i): returns the i-th child of node x, assuming it exists. */   \r
154    treeNode Child(treeNode x, int i);\r
155    \r
156    /** FirstChild(x): returns the first child of node x, assuming it exists. \r
157     * Very fast in BP. */\r
158    treeNode FirstChild(treeNode x);\r
159    \r
160    /** NextSibling(x): returns the next sibling of node x, assuming it \r
161     * exists. */\r
162    treeNode NextSibling(treeNode x);\r
163    \r
164    /** PrevSibling(x): returns the previous sibling of node x, assuming it \r
165     * exists. */\r
166    treeNode PrevSibling(treeNode x);\r
167    \r
168    /** TaggedChild(x,i,tag): returns the i-th child of node x tagged tag, or \r
169     * NULLT if there is none. Because of the balanced-parentheses representation \r
170     * of the tree, this operation is not supported efficiently, just iterating \r
171     * among the children of node x until finding the desired child. */\r
172    treeNode TaggedChild(treeNode x, int i, TagType tag);\r
173    \r
174    /** TaggedDesc(x,tag): returns the first node tagged tag with larger \r
175     * preorder than x and within the subtree of x. Returns NULT if there \r
176     * is none. */\r
177    treeNode TaggedDesc(treeNode x, TagType tag);\r
178 \r
179    /** TaggedPrec(x,tag): returns the first node tagged tag with smaller \r
180     * preorder than x and not an ancestor of x. Returns NULLT if there \r
181     * is none. */\r
182    treeNode TaggedPrec(treeNode x, TagType tag);\r
183   \r
184    /** TaggedFoll(x,tag): returns the first node tagged tag with larger \r
185     * preorder than x and not in the subtree of x. Returns NULLT if there \r
186     * is none. */\r
187    treeNode TaggedFoll(treeNode x, TagType tag);\r
188    \r
189    /** PrevText(x): returns the document identifier of the text to the left of \r
190     * node x, or NULLT if x is the root node. */\r
191    DocID PrevText(treeNode x);\r
192    \r
193    /** NextText(x): returns the document identifier of the text to the right of \r
194     * node x, or NULLT if x is the root node. */\r
195    DocID NextText(treeNode x);\r
196    \r
197    /** MyText(x): returns the document identifier of the text below node x, or \r
198     * NULLT if x is not a leaf node. */\r
199    DocID MyText(treeNode x);\r
200    \r
201    /** TextXMLId(d): returns the preorder of document with identifier d in the \r
202     * tree consisting of all tree nodes and all text nodes. */\r
203    int TextXMLId(DocID d);\r
204    \r
205    /** NodeXMLId(x): returns the preorder of node x in the tree consisting of \r
206     * all tree nodes and all text nodes. */\r
207    int NodeXMLId(treeNode x);\r
208    \r
209    /** ParentNode(d): returns the parent node of document identifier d. */\r
210    treeNode ParentNode(DocID d);\r
211 \r
212    /** OpenDocument(empty_texts,sample_rate_text): initilizes the construction\r
213     * of the data structure for the XML document. Parameter empty_texts \r
214     * indicates whether we index empty texts in document or not. Parameter \r
215     * sample_rate_text indicates the sampling rate for the text searching data\r
216     * structures (small values get faster searching but a bigger space \r
217     * requirement). Returns a non-zero value upon success, NULLT in case of \r
218     * error. */\r
219    int OpenDocument(bool empty_texts, int sample_rate_text);\r
220 \r
221    /** CloseDocument(): finishes the construction of the data structure for \r
222     * the XML document. Tree and tags are represented in the final form, \r
223     * dynamic data structures are made static, and the flag "finished" is set \r
224     * to true. After that, the data structure can be queried. */\r
225    int CloseDocument();\r
226 \r
227    /** NewOpenTag(tagname): indicates the event of finding a new opening tag \r
228     * in the document. Tag name is given. Returns a non-zero value upon \r
229     * success, and returns NULLT in case of error. */\r
230    int NewOpenTag(unsigned char *tagname);\r
231    \r
232    /** NewClosingTag(tagname): indicates the event of finding a new closing tag\r
233     *  in the document. Tag name is given. Returns a non-zero value upon \r
234     *  success, and returns NULLT in case of error. */\r
235    int NewClosingTag(unsigned char *tagname);\r
236  \r
237    /** NewText(s): indicates the event of finding a new (non-empty) text s in \r
238     * the document. The new text is inserted within the text collection. \r
239     * Returns a non-zero value upon success, NULLT in case of error. */\r
240    int NewText(unsigned char *s);\r
241 \r
242    /** NewEmptyText(): indicates the event of finding a new empty text in the \r
243     * document. In case of indexing empty and non-empty texts, we insert the \r
244     * empty texts into the text collection. In case of indexing only non-empty\r
245     * texts, it just indicates an empty text in the bit vector of empty texts. \r
246     * Returns a non-zero value upon success, NULLT in case of error. */\r
247    int NewEmptyText();\r
248 \r
249    /** GetTagId(tagname): returns the tag identifier corresponding to a given \r
250     * tag name. Returns NULLT in case that the tag name does not exists. */\r
251    TagType GetTagId(unsigned char *tagname);\r
252 \r
253    /** GetTagName(tagid): returns the tag name of a given tag identifier. \r
254     * Returns NULL in case that the tag identifier is not valid.*/\r
255    unsigned char *GetTagName(TagType tagid);\r
256 \r
257 \r
258    TagType RegisterTag(unsigned char *tagname);\r
259 \r
260    bool EmptyText(DocID i) {\r
261       return Text->EmptyText(i);\r
262    }\r
263    /** Prefix(s): search for texts prefixed by string s. */\r
264    TextCollection::document_result Prefix(uchar const *s) {\r
265       return Text->Prefix(s);\r
266    }\r
267 \r
268    /** Suffix(s): search for texts having string s as a suffix. */\r
269    TextCollection::document_result Suffix(uchar const *s) {\r
270       return Text->Suffix(s);\r
271    }\r
272 \r
273    /** Equal(s): search for texts equal to string s. */\r
274    TextCollection::document_result Equal(uchar const *s) {\r
275       return Text->Equal(s);\r
276    }\r
277 \r
278    /** Contains(s): search for texts containing string s.  */\r
279    TextCollection::document_result Contains(uchar const *s) {\r
280       return Text->Contains(s);\r
281    }\r
282 \r
283    /** LessThan(s): returns document identifiers for the texts that\r
284     * are lexicographically smaller than string s. */\r
285    TextCollection::document_result LessThan(uchar const *s) {\r
286       return Text->LessThan(s);\r
287    }\r
288    \r
289    /** IsPrefix(x): returns true if there is a text prefixed by string s. */\r
290    bool IsPrefix(uchar const *s) {\r
291       return Text->IsPrefix(s);\r
292    }          \r
293    \r
294    /** IsSuffix(s): returns true if there is a text having string s as a \r
295     * suffix.*/\r
296    bool IsSuffix(uchar const *s) {\r
297       return Text->IsSuffix(s);\r
298    }\r
299    \r
300    /** IsEqual(s): returns true if there is a text that equals given \r
301     * string s. */\r
302    bool IsEqual(uchar const *s) {\r
303       return Text->IsEqual(s);\r
304    }\r
305    \r
306    /** IsContains(s): returns true if there is a text containing string s. */\r
307    bool IsContains(uchar const *s) {\r
308       return Text->IsContains(s);\r
309    }\r
310    \r
311    /** IsLessThan(s): returns true if there is at least a text that is \r
312     * lexicographically smaller than string s. */\r
313    bool IsLessThan(uchar const *s) {\r
314       return Text->IsLessThan(s);\r
315    }\r
316 \r
317    /** CountPrefix(s): counting version of Prefix(s). */\r
318    unsigned CountPrefix(uchar const *s) {\r
319       return Text->CountPrefix(s);\r
320    }\r
321    \r
322    /** CountSuffix(s): counting version of Suffix(s). */\r
323    unsigned CountSuffix(uchar const *s) {\r
324       return Text->CountSuffix(s);\r
325    }\r
326    \r
327    /** CountEqual(s): counting version of Equal(s). */\r
328    unsigned CountEqual(uchar const *s) {\r
329       return Text->CountEqual(s);\r
330    }\r
331    \r
332    /** CountContains(s): counting version of Contains(s). */\r
333    unsigned CountContains(uchar const *s) {\r
334       return Text->CountContains(s);\r
335    }\r
336    \r
337    /** CountLessThan(s): counting version of LessThan(s). */\r
338    unsigned CountLessThan(uchar const *s) {\r
339       return CountLessThan(s);\r
340    }\r
341    \r
342    /** GetText(d): returns the text corresponding to document with\r
343     * id d. */\r
344    uchar* GetText(DocID d) {\r
345       return Text->GetText(d);\r
346    }\r
347    \r
348    TextCollection *getTextCollection() {\r
349       return Text;\r
350    }\r
351    /** Save: saves XML tree data structure to file. */\r
352    void Save(unsigned char *filename);\r
353       \r
354    /** Load: loads XML tree data structure from file. sample_rate_text \r
355     * indicates the sample rate for the text search data structure. */\r
356    static XMLTree *Load(unsigned char *filename, int sample_rate_text);   \r
357 };\r
358 #endif\r