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