4ada586c05b4b243bd3c82a9567937ada66fae01
[SXSI/XMLTree.git] / xml-tree.hpp
1 #ifndef XML_TREE_HPP_
2 #define XML_TREE_HPP_
3
4
5 #include <cstdint>
6 #include <unordered_set>
7 #include <unordered_map>
8 #include <libbp/bp.h>
9 #include <libbp/bp-darray.h>
10 #include <libcds/includes/basics.h>
11 #include <libcds/includes/static_bitsequence.h>
12 #include <libcds/includes/alphabet_mapper.h>
13 #include <libcds/includes/static_sequence.h>
14 #include "bit-vector.hpp"
15
16 #undef W
17 #undef Wminusone
18 #undef WW
19
20 #include <TextCollection/TextCollection.h>
21 #include <TextCollection/TextCollectionBuilder.h>
22
23 class xml_tree_builder;
24
25 class xml_tree {
26 public:
27   typedef int32_t node_t;
28   typedef int32_t tag_t;
29
30   static const node_t NIL = -1;
31   static const node_t ROOT = 0;
32
33   static const char* NIL_TAG;
34   static const tag_t NIL_TAG_ID = -1;
35   static const char* DOCUMENT_OPEN_TAG;
36   static const tag_t DOCUMENT_OPEN_TAG_ID = 0;
37   static const char* ATTRIBUTE_OPEN_TAG;
38   static const tag_t ATTRIBUTE_OPEN_TAG_ID = 1;
39   static const char* PCDATA_OPEN_TAG;
40   static const tag_t PCDATA_OPEN_TAG_ID = 2;
41   static const char* ATTRIBUTE_DATA_OPEN_TAG;
42   static const tag_t ATTRIBUTE_DATA_OPEN_TAG_ID = 3;
43   static const char* CLOSE_TAG;
44   static const tag_t CLOSE_TAG_ID = 4;
45
46
47   ~xml_tree();
48
49   //Counting functions
50   /**
51    * [size()] returns the size of the tree (number of nodes)
52    * Runs in O(1)
53    */
54   inline uint32_t size() const;
55
56   /**
57    * [num_tags()] returns the number of distinct tags.
58    * Runs in O(1)
59    */
60   inline uint32_t num_tags() const;
61
62   /**
63    * [subtree_size(n)] returns the size of the subtree (number of nodes)
64    * rooted at n.
65    * Runs in O(1)
66    */
67   inline uint32_t subtree_size(node_t) const;
68
69   /**
70    * [subtree_tags(n, t)] returns the number of occurences of tag [t] in the
71    * subtree rooted at [n]
72    * Runs in O(1)
73    */
74   inline uint32_t subtree_tags(node_t, tag_t) const;
75
76   /**
77    * [subtree_elements(n)] returns the number of element nodes below [n]
78    * Runs in O(attribute_ids->size()+3)
79    */
80   inline uint32_t subtree_elements(node_t) const;
81
82   /**
83    * [num_children(n)] returns the number of child nodes of [n]
84    * (both text and elements, and including a fake <@> node if
85    * present).
86    * Runs in O(1) (?)
87    */
88   uint32_t num_children(node_t) const;
89
90   /**
91    * [child_pos(n)] returns the position of [n] amongst its siblings
92    * Runs in O(1) (?)
93    */
94   uint32_t child_pos(node_t) const;
95
96
97   //Node_T tests
98   /**
99    * [is_leaf(n)] returns true if [n] is a leaf (i.e. if [num_children(n)]
100    * returns 0
101    * Runs in O(1)
102    */
103   inline bool is_leaf(node_t) const;
104
105   /**
106    * [is_ancestor(n, m)] returns true if [n] is an ancestor of [m], false
107    * otherwise
108    * Runs in O(1)
109    */
110   inline bool is_ancestor(node_t, node_t) const;
111
112   /**
113    * [is_right_descendant(n, m)] returns true if [m] is a descendant-or-self of
114    * a following-sibling of [n], false otherwise
115    * Runs in O(1)
116    */
117   inline bool is_right_descendant(node_t, node_t) const;
118
119   /**
120    * [is_child(n, m)] returns true if [m] is a child of [n]
121    * Runs in O(1)
122    */
123   bool is_child(node_t, node_t) const;
124
125   /**
126    * [is_first_child(n, m)] returns true if [m] is the first child of [n]
127    * Runs in O(1)
128    */
129   inline bool is_first_child(node_t) const;
130
131   /**
132    * [is_nil(n)] returns true if [n] is equal to xml_tree::NIL
133    * Runs in O(1)
134    */
135   inline bool is_nil(node_t) const;
136
137   /**
138    * [is_open(n)] returns true if [n], seen as an index in the
139    * underlying BP representation corresponds to an opening parenthesis.
140    * Runs in O(1)
141    */
142   inline bool is_open(node_t) const;
143
144   //Numbering functions
145   /**
146    * [depth(n)] returns the depths of node [n]. The root has depth 1.
147    * Runs in O(1)
148    */
149   uint32_t depth(node_t) const;
150
151   /**
152    * [preorder(n)] returns the preorder of node [n]. Equivalent to calling
153    * rank on the underlying BP representation.
154    * Runs in O(1)
155    */
156   uint32_t preorder(node_t) const;
157
158   /**
159    * [preorder(n)] returns the postorder of node [n].
160    * Runs in O(1)
161    */
162   uint32_t postorder(node_t) const;
163
164   //Tag functions
165   /**
166    * [tag(n)] returns the tag of node [n] which must be a valid node identifier
167    * (in particular not NIL)
168    * Runs in O(1)
169    */
170   inline tag_t tag(node_t) const;
171
172   /**
173    * [get_tag_name_by_ref(t)] returns the string representation of tag [t]
174    * For elements, the string representation is the tag name itself
175    * Returns <!INVALID!> if [t] is not a proper tag identifier.
176    * Runs in O(1)
177    */
178   const char* get_tag_name_by_ref(tag_t) const;
179
180   /**
181    * [register_tag(s)] returns the tag identifier for the tag represented
182    * by the string [s]. If no such tag exists in the document, return a
183    * fresh tag identifier [i]. Subsequent calls with the same [s] will return
184    * the same identifier.
185    * Runs in O(1)
186    */
187   tag_t register_tag(char *s);
188
189   //Navigation functions
190   inline node_t root () const;
191   node_t child(node_t, uint32_t) const;
192   inline node_t parent(node_t) const;
193   inline node_t first_child(node_t) const;
194   inline node_t last_child(node_t) const;
195   inline node_t next_sibling(node_t) const;
196   inline node_t prev_sibling(node_t) const;
197   inline node_t first_element(node_t) const;
198   inline node_t next_element(node_t) const;
199   inline node_t tagged_next_close(node_t, tag_t) const;
200   inline node_t tagged_next(node_t, tag_t) const;
201   inline node_t tagged_descendant(node_t, tag_t) const;
202   inline node_t tagged_following_before(node_t, tag_t, node_t) const;
203   inline node_t tagged_child(node_t, tag_t) const;
204   inline node_t tagged_sibling(node_t, tag_t) const;
205   node_t select_child(node_t, tag_t*) const;
206   inline node_t select_descendant(node_t, tag_t*) const;
207   node_t select_sibling(node_t, tag_t*) const;
208   inline node_t select_following_before (node_t, tag_t*, node_t) const;
209   inline node_t closing(node_t) const;
210
211   //Text functions
212   inline node_t parent_node(int32_t) const;
213   inline SXSI::TextCollection *get_text_collection() const;
214   std::pair<int32_t, int32_t> text_id_range(node_t) const;
215   int32_t text_id(node_t) const;
216   unsigned char* get_text(int32_t) const;
217
218   SXSI::TextCollection::document_result prefix(uchar const *s) const;
219   SXSI::TextCollection::document_result suffix(uchar const *s) const;
220   SXSI::TextCollection::document_result equals(uchar const *s) const;
221   SXSI::TextCollection::document_result contains(uchar const *s) const;
222   SXSI::TextCollection::document_result less_than(uchar const *s) const;
223
224
225   bool naive_contains(node_t, uchar const *s) const;
226
227   //I/O functions
228   void save(int, char*);
229   static xml_tree* load(int, char*, bool, int);
230   void print(node_t, int, bool no_text=false);
231   void flush(int);
232
233 private:
234   friend class xml_tree_builder;
235   xml_tree();
236   xml_tree(std::vector<int32_t>*,
237            std::unordered_map<std::string, int32_t>*,
238            bit_vector*,
239            bool,
240            SXSI::TextCollectionBuilder *,
241            SXSI::TextCollectionBuilder::index_type_t,
242            bit_vector*);
243
244   //Parenthesis sequence
245   bp *par;
246   //tag sequence
247   std::vector<static_bitsequence_sdarray*> tags;
248   uint32_t *tag_seq;
249   uint32_t tag_seq_len;
250   uint32_t bits_per_tag;
251   //Mapping from tag_t identifiers to/from tagnames
252   std::vector<std::string> *tag_names;
253   std::unordered_map<std::string, tag_t> *tag_ids;
254   //Set of tag ids that map to attribute nodes
255   std::unordered_set<tag_t> *attribute_ids;
256   //Text index
257   SXSI::TextCollection *text_collection;
258   static_bitsequence *text_positions;
259   SXSI::TextCollectionBuilder::index_type_t text_index_type;
260   //auxiliary data structures for efficient printing.
261   std::vector<std::string> *print_stack;
262   std::string *print_buffer;
263
264 #define BUFFER_SIZE (8192*2)
265
266   void uflush(int);
267   void uflush_r(int, size_t);
268   void uput_str(std::string s, int fd);
269   void uputs(const char* s, int fd);
270   void uputc(const char c, int fd);
271   size_t uprintf(const char*s, int fd);
272 };
273
274 #define XML_TREE_INTERNAL__ 1
275 #include "xml-tree-inc.hpp"
276
277 #endif //XML_TREE_HPP_