436e2f9ed542553b166fc65483165a2bb8dcb681
[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   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   inline 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   inline bool is_open(node_t) const;
67
68   uint32_t depth(node_t) const;
69   uint32_t preorder(node_t) const;
70   uint32_t postorder(node_t) const;
71
72   //Tag functions
73   inline tag_t tag(node_t) const;
74   const char* get_tag_name_by_ref(tag_t) const;
75   tag_t register_tag(char *s);
76
77   //Navigation functions
78   inline node_t root () const;
79   node_t child(node_t, uint32_t) const;
80   inline node_t parent(node_t) const;
81   inline node_t first_child(node_t) const;
82   inline node_t last_child(node_t) const;
83   inline node_t next_sibling(node_t) const;
84   inline node_t prev_sibling(node_t) const;
85   inline node_t first_element(node_t) const;
86   inline node_t next_element(node_t) const;
87   inline node_t tagged_next_close(node_t, tag_t) const;
88   inline node_t tagged_next(node_t, tag_t) const;
89   inline node_t tagged_descendant(node_t, tag_t) const;
90   inline node_t tagged_following_before(node_t, tag_t, node_t) const;
91   inline node_t tagged_child(node_t, tag_t) const;
92   inline node_t tagged_sibling(node_t, tag_t) const;
93   node_t select_child(node_t, tag_t*) const;
94   inline node_t select_descendant(node_t, tag_t*) const;
95   node_t select_sibling(node_t, tag_t*) const;
96   inline node_t select_following_before (node_t, tag_t*, node_t) const;
97   inline node_t closing(node_t) const;
98
99   //Text functions
100   inline node_t parent_node(int32_t) const;
101   inline SXSI::TextCollection *get_text_collection() const;
102   std::pair<int32_t, int32_t> text_id_range(node_t) const;
103   int32_t text_id(node_t) const;
104   unsigned char* get_text(int32_t) const;
105
106   SXSI::TextCollection::document_result prefix(uchar const *s) const;
107   SXSI::TextCollection::document_result suffix(uchar const *s) const;
108   SXSI::TextCollection::document_result equals(uchar const *s) const;
109   SXSI::TextCollection::document_result contains(uchar const *s) const;
110   SXSI::TextCollection::document_result less_than(uchar const *s) const;
111
112
113   bool naive_contains(node_t, uchar const *s) const;
114
115   //I/O functions
116   void save(int, char*);
117   static xml_tree* load(int, char*, bool, int);
118   void print(node_t, int, bool no_text=false);
119   void flush(int);
120
121 private:
122   friend class xml_tree_builder;
123   xml_tree();
124   xml_tree(std::vector<int32_t>*,
125            std::unordered_map<std::string, int32_t>*,
126            bit_vector*,
127            bool,
128            SXSI::TextCollectionBuilder *,
129            SXSI::TextCollectionBuilder::index_type_t,
130            bit_vector*);
131
132   //Parenthesis sequence
133   bp *par;
134   //tag sequence
135   std::vector<static_bitsequence_sdarray*> tags;
136   uint32_t *tag_seq;
137   uint32_t tag_seq_len;
138   uint32_t bits_per_tag;
139   //Mapping from tag_t identifiers to/from tagnames
140   std::vector<std::string> *tag_names;
141   std::unordered_map<std::string, tag_t> *tag_ids;
142   //Set of tag ids that map to attribute nodes
143   std::unordered_set<tag_t> *attribute_ids;
144   //Text index
145   SXSI::TextCollection *text_collection;
146   static_bitsequence *text_positions;
147   SXSI::TextCollectionBuilder::index_type_t text_index_type;
148   //auxiliary data structures for efficient printing.
149   std::vector<std::string> *print_stack;
150   std::string *print_buffer;
151
152 #define BUFFER_SIZE (8192*2)
153
154   void uflush(int);
155   void uflush_r(int, size_t);
156   void uput_str(std::string s, int fd);
157   void uputs(const char* s, int fd);
158   void uputc(const char c, int fd);
159   size_t uprintf(const char*s, int fd);
160 };
161
162 #define XML_TREE_INTERNAL__ 1
163 #include "xml-tree-inc.hpp"
164
165 #endif //XML_TREE_HPP_