Fix bug in xml_tree::print (which would pop the printing stack one time
[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 <bp/bp.h>
9 #include <bp/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   inline uint32_t size() const;
51   inline uint32_t num_tags() const;
52   inline uint32_t subtree_size(node_t) const;
53   inline uint32_t subtree_tags(node_t, tag_t) const;
54   uint32_t subtree_elements(node_t) const;
55   uint32_t num_children(node_t) const;
56   uint32_t child_pos(node_t) const;
57
58
59   //Node_T tests
60   inline bool is_leaf(node_t) const;
61   inline bool is_ancestor(node_t, node_t) const;
62   inline bool is_right_descendant(node_t, node_t) const;
63   bool is_child(node_t, node_t) const;
64   inline bool is_first_child(node_t) const;
65   inline bool is_nil(node_t) const;
66
67   uint32_t depth(node_t) const;
68   uint32_t preorder(node_t) const;
69   uint32_t postorder(node_t) const;
70
71   //Tag functions
72   inline tag_t tag(node_t) const;
73   const char* get_tag_name_by_ref(tag_t) const;
74   tag_t register_tag(char *s);
75
76   //Navigation functions
77   inline node_t root () const;
78   node_t child(node_t, uint32_t) const;
79   inline node_t parent(node_t) const;
80   inline node_t first_child(node_t) const;
81   inline node_t last_child(node_t) const;
82   inline node_t next_sibling(node_t) const;
83   inline node_t prev_sibling(node_t) const;
84   inline node_t first_element(node_t) const;
85   inline node_t next_element(node_t) const;
86   inline node_t tagged_next(node_t, tag_t) const;
87   inline node_t tagged_descendant(node_t, tag_t) const;
88   inline node_t tagged_following_before(node_t, tag_t, node_t) const;
89   inline node_t tagged_child(node_t, tag_t) const;
90   inline node_t tagged_sibling(node_t, tag_t) const;
91   node_t select_child(node_t, std::unordered_set<tag_t>*) const;
92   node_t select_descendant(node_t, std::unordered_set<tag_t>*) const;
93   node_t select_sibling(node_t, std::unordered_set<tag_t>*) const;
94   node_t select_following_before (node_t,
95                                   std::unordered_set<tag_t>*, node_t) const;
96   inline node_t closing(node_t) const;
97
98   //Text functions
99   inline node_t parent_node(int32_t) const;
100   inline SXSI::TextCollection *get_text_collection() const;
101   std::pair<int32_t, int32_t> text_id_range(node_t) const;
102   int32_t text_id(node_t) const;
103   unsigned char* get_text(int32_t) const;
104
105   SXSI::TextCollection::document_result prefix(uchar const *s) const;
106   SXSI::TextCollection::document_result suffix(uchar const *s) const;
107   SXSI::TextCollection::document_result equals(uchar const *s) const;
108   SXSI::TextCollection::document_result contains(uchar const *s) const;
109   SXSI::TextCollection::document_result less_than(uchar const *s) const;
110
111   //I/O functions
112   void save(int, char*);
113   static xml_tree* load(int, char*, bool, int);
114   void print(node_t, int, bool no_text=false);
115   void flush(int);
116
117 private:
118   friend class xml_tree_builder;
119   xml_tree();
120   xml_tree(std::vector<int32_t>*,
121            std::unordered_map<std::string, int32_t>*,
122            bit_vector*,
123            bool,
124            SXSI::TextCollectionBuilder *,
125            SXSI::TextCollectionBuilder::index_type_t,
126            bit_vector*);
127
128   //Parenthesis sequence
129   bp *par;
130   //tag sequence
131   static_sequence *tags;
132   uint32_t *tag_seq;
133   uint32_t tag_seq_len;
134   uint32_t bits_per_tag;
135   //Mapping from tag_t identifiers to/from tagnames
136   std::vector<std::string> *tag_names;
137   std::unordered_map<std::string, tag_t> *tag_ids;
138   //Text index
139   SXSI::TextCollection *text_collection;
140   static_bitsequence *text_positions;
141   SXSI::TextCollectionBuilder::index_type_t text_index_type;
142   //auxiliary data structures for efficient printing.
143   std::vector<std::string> *print_stack;
144   std::string *print_buffer;
145
146 #define BUFFER_SIZE (8192*2)
147
148   void uflush(int);
149   void uflush_r(int, size_t);
150   void uput_str(std::string s, int fd);
151   void uputs(const char* s, int fd);
152   void uputc(const char c, int fd);
153   size_t uprintf(const char*s, int fd);
154 };
155
156 #define XML_TREE_INTERNAL__ 1
157 #include "xml-tree-inc.hpp"
158
159 #endif //XML_TREE_HPP_