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