6 #include <unordered_set>
7 #include <unordered_map>
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"
20 #include <TextCollection/TextCollection.h>
21 #include <TextCollection/TextCollectionBuilder.h>
23 class xml_tree_builder;
27 typedef int32_t node_t;
28 typedef int32_t tag_t;
30 static const node_t NIL = -1;
31 static const node_t ROOT = 0;
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;
51 * [size()] returns the size of the tree (number of nodes)
54 inline uint32_t size() const;
57 * [num_tags()] returns the number of distinct tags.
60 inline uint32_t num_tags() const;
63 * [subtree_size(n)] returns the size of the subtree (number of nodes)
67 inline uint32_t subtree_size(node_t) const;
70 * [subtree_tags(n, t)] returns the number of occurences of tag [t] in the
71 * subtree rooted at [n]
74 inline uint32_t subtree_tags(node_t, tag_t) const;
77 * [subtree_elements(n)] returns the number of element nodes below [n]
78 * Runs in O(attribute_ids->size()+3)
80 inline uint32_t subtree_elements(node_t) const;
83 * [num_children(n)] returns the number of child nodes of [n]
84 * (both text and elements, and including a fake <@> node if
88 uint32_t num_children(node_t) const;
91 * [child_pos(n)] returns the position of [n] amongst its siblings
94 uint32_t child_pos(node_t) const;
99 * [is_leaf(n)] returns true if [n] is a leaf (i.e. if [num_children(n)]
103 inline bool is_leaf(node_t) const;
106 * [is_ancestor(n, m)] returns true if [n] is an ancestor of [m], false
110 inline bool is_ancestor(node_t, node_t) const;
113 * [is_right_descendant(n, m)] returns true if [m] is a descendant-or-self of
114 * a following-sibling of [n], false otherwise
117 inline bool is_right_descendant(node_t, node_t) const;
120 * [is_child(n, m)] returns true if [m] is a child of [n]
123 bool is_child(node_t, node_t) const;
126 * [is_first_child(n, m)] returns true if [m] is the first child of [n]
129 inline bool is_first_child(node_t) const;
132 * [is_nil(n)] returns true if [n] is equal to xml_tree::NIL
135 inline bool is_nil(node_t) const;
138 * [is_open(n)] returns true if [n], seen as an index in the
139 * underlying BP representation corresponds to an opening parenthesis.
142 inline bool is_open(node_t) const;
144 //Numbering functions
146 * [depth(n)] returns the depths of node [n]. The root has depth 1.
149 uint32_t depth(node_t) const;
152 * [preorder(n)] returns the preorder of node [n]. Equivalent to calling
153 * rank on the underlying BP representation.
156 uint32_t preorder(node_t) const;
159 * [preorder(n)] returns the postorder of node [n].
162 uint32_t postorder(node_t) const;
165 inline tag_t tag(node_t) const;
166 const char* get_tag_name_by_ref(tag_t) const;
167 tag_t register_tag(char *s);
169 //Navigation functions
170 inline node_t root () const;
171 node_t child(node_t, uint32_t) const;
172 inline node_t parent(node_t) const;
173 inline node_t first_child(node_t) const;
174 inline node_t last_child(node_t) const;
175 inline node_t next_sibling(node_t) const;
176 inline node_t prev_sibling(node_t) const;
177 inline node_t first_element(node_t) const;
178 inline node_t next_element(node_t) const;
179 inline node_t tagged_next_close(node_t, tag_t) const;
180 inline node_t tagged_next(node_t, tag_t) const;
181 inline node_t tagged_descendant(node_t, tag_t) const;
182 inline node_t tagged_following_before(node_t, tag_t, node_t) const;
183 inline node_t tagged_child(node_t, tag_t) const;
184 inline node_t tagged_sibling(node_t, tag_t) const;
185 node_t select_child(node_t, tag_t*) const;
186 inline node_t select_descendant(node_t, tag_t*) const;
187 node_t select_sibling(node_t, tag_t*) const;
188 inline node_t select_following_before (node_t, tag_t*, node_t) const;
189 inline node_t closing(node_t) const;
192 inline node_t parent_node(int32_t) const;
193 inline SXSI::TextCollection *get_text_collection() const;
194 std::pair<int32_t, int32_t> text_id_range(node_t) const;
195 int32_t text_id(node_t) const;
196 unsigned char* get_text(int32_t) const;
198 SXSI::TextCollection::document_result prefix(uchar const *s) const;
199 SXSI::TextCollection::document_result suffix(uchar const *s) const;
200 SXSI::TextCollection::document_result equals(uchar const *s) const;
201 SXSI::TextCollection::document_result contains(uchar const *s) const;
202 SXSI::TextCollection::document_result less_than(uchar const *s) const;
205 bool naive_contains(node_t, uchar const *s) const;
208 void save(int, char*);
209 static xml_tree* load(int, char*, bool, int);
210 void print(node_t, int, bool no_text=false);
214 friend class xml_tree_builder;
216 xml_tree(std::vector<int32_t>*,
217 std::unordered_map<std::string, int32_t>*,
220 SXSI::TextCollectionBuilder *,
221 SXSI::TextCollectionBuilder::index_type_t,
224 //Parenthesis sequence
227 std::vector<static_bitsequence_sdarray*> tags;
229 uint32_t tag_seq_len;
230 uint32_t bits_per_tag;
231 //Mapping from tag_t identifiers to/from tagnames
232 std::vector<std::string> *tag_names;
233 std::unordered_map<std::string, tag_t> *tag_ids;
234 //Set of tag ids that map to attribute nodes
235 std::unordered_set<tag_t> *attribute_ids;
237 SXSI::TextCollection *text_collection;
238 static_bitsequence *text_positions;
239 SXSI::TextCollectionBuilder::index_type_t text_index_type;
240 //auxiliary data structures for efficient printing.
241 std::vector<std::string> *print_stack;
242 std::string *print_buffer;
244 #define BUFFER_SIZE (8192*2)
247 void uflush_r(int, size_t);
248 void uput_str(std::string s, int fd);
249 void uputs(const char* s, int fd);
250 void uputc(const char c, int fd);
251 size_t uprintf(const char*s, int fd);
254 #define XML_TREE_INTERNAL__ 1
255 #include "xml-tree-inc.hpp"
257 #endif //XML_TREE_HPP_