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