c2b86cabde2ced6ec04382730ffe3e13850f17c5
[SXSI/XMLTree.git] / xml-tree.hpp
1 #ifndef XML_TREE_HPP_
2 #define XML_TREE_HPP_
3
4
5 #include <cstdint>
6 #include <unordered_map>
7 #include <bp/bp.h>
8 #include <bp/bp-darray.h>
9 #include <libcds/includes/basics.h>
10 #include <libcds/includes/static_bitsequence.h>
11 #include <libcds/includes/alphabet_mapper.h>
12 #include <libcds/includes/static_sequence.h>
13 #include "bit-vector.hpp"
14
15 #undef W
16 #undef Wminusone
17 #undef WW
18
19 #include <TextCollection/TextCollection.h>
20 #include <TextCollection/TextCollectionBuilder.h>
21
22 class xml_tree_builder;
23
24 class xml_tree {
25 public:
26   typedef int32_t node_t;
27   typedef int32_t tag_t;
28
29   static const node_t NIL = -1;
30   static const node_t ROOT = 0;
31
32   static const char* NIL_TAG;
33   static const tag_t NIL_TAG_ID = -1;
34   static const char* DOCUMENT_OPEN_TAG;
35   static const tag_t DOCUMENT_OPEN_TAG_ID = 0;
36   static const char* ATTRIBUTE_OPEN_TAG;
37   static const tag_t ATTRIBUTE_OPEN_TAG_ID = 1;
38   static const char* PCDATA_OPEN_TAG;
39   static const tag_t PCDATA_OPEN_TAG_ID = 2;
40   static const char* ATTRIBUTE_DATA_OPEN_TAG;
41   static const tag_t ATTRIBUTE_DATA_OPEN_TAG_ID = 3;
42   static const char* CLOSE_TAG;
43   static const tag_t CLOSE_TAG_ID = 4;
44
45
46   ~xml_tree();
47
48   //Counting functions
49   inline uint32_t size() const;
50   inline uint32_t num_tags() const;
51   inline uint32_t subtree_size(node_t) const;
52   inline uint32_t subtree_tags(node_t, tag_t) const;
53   uint32_t subtree_elements(node_t) const;
54   uint32_t num_children(node_t) const;
55   uint32_t child_pos(node_t) const;
56
57
58   //Node_T tests
59   inline bool is_leaf(node_t) const;
60   inline bool is_ancestor(node_t, node_t) const;
61   inline bool is_right_descendant(node_t, node_t) const;
62   bool is_child(node_t, node_t) const;
63   inline bool is_first_child(node_t) const;
64   inline bool is_nil(node_t) const;
65
66   uint32_t depth(node_t) const;
67   uint32_t preorder(node_t) const;
68   uint32_t postorder(node_t) const;
69
70   //Tag functions
71   inline tag_t tag(node_t) const;
72   const char* get_tag_name_by_ref(tag_t) const;
73   tag_t register_tag(char *s);
74
75   //Navigation functions
76   inline node_t root () const;
77   node_t child(node_t, uint32_t) const;
78   inline node_t parent(node_t) const;
79   inline node_t first_child(node_t) const;
80   inline node_t last_child(node_t) const;
81   inline node_t next_sibling(node_t) const;
82   inline node_t prev_sibling(node_t) const;
83   inline node_t first_element(node_t) const;
84   inline node_t next_element(node_t) const;
85   inline node_t tagged_next(node_t, tag_t) const;
86   inline node_t tagged_descendant(node_t, tag_t) const;
87   inline node_t tagged_following_before(node_t, tag_t, node_t) const;
88   inline node_t tagged_child(node_t, tag_t) const;
89   inline node_t tagged_sibling(node_t, tag_t) const;
90   node_t select_child(node_t, tag_t*) const;
91   node_t select_descendant(node_t, tag_t*) const;
92   node_t select_sibling(node_t, tag_t*) const;
93   node_t select_following_before (node_t, tag_t*, node_t) const;
94   inline node_t closing(node_t) const;
95
96   //Text functions
97   inline node_t parent_node(int32_t) const;
98   inline SXSI::TextCollection *get_text_collection() const;
99   std::pair<int32_t, int32_t> text_id_range(node_t) const;
100   int32_t text_id(node_t) const;
101   unsigned char* get_text(int32_t) const;
102
103   SXSI::TextCollection::document_result prefix(uchar const *s) const;
104   SXSI::TextCollection::document_result suffix(uchar const *s) const;
105   SXSI::TextCollection::document_result equals(uchar const *s) const;
106   SXSI::TextCollection::document_result contains(uchar const *s) const;
107   SXSI::TextCollection::document_result less_than(uchar const *s) const;
108
109   //I/O functions
110   void save(int, char*);
111   static xml_tree* load(int, char*, bool, int);
112   void print(node_t, int, bool no_text=false);
113   void flush(int);
114
115 private:
116   friend class xml_tree_builder;
117   xml_tree();
118   xml_tree(std::vector<int32_t>*,
119            std::unordered_map<std::string, int32_t>*,
120            bit_vector*,
121            bool,
122            SXSI::TextCollectionBuilder *,
123            SXSI::TextCollectionBuilder::index_type_t,
124            bit_vector*);
125
126   //Parenthesis sequence
127   bp *par;
128   //tag sequence
129   static_sequence *tags;
130   uint32_t *tag_seq;
131   uint32_t tag_seq_len;
132   uint32_t bits_per_tag;
133   //Mapping from tag_t identifiers to/from tagnames
134   std::vector<std::string> *tag_names;
135   std::unordered_map<std::string, tag_t> *tag_ids;
136   //Text index
137   SXSI::TextCollection *text_collection;
138   static_bitsequence *text_positions;
139   SXSI::TextCollectionBuilder::index_type_t text_index_type;
140   //auxiliary data structures for efficient printing.
141   std::vector<std::string> *print_stack;
142   std::string *print_buffer;
143
144 #define BUFFER_SIZE (8192*2)
145
146   void uflush(int);
147   void uflush_r(int, size_t);
148   void uput_str(std::string s, int fd);
149   void uputs(const char* s, int fd);
150   void uputc(const char c, int fd);
151   size_t uprintf(const char*s, int fd);
152 };
153
154 #define XML_TREE_INTERNAL__ 1
155 #include "xml-tree-inc.hpp"
156
157 #endif //XML_TREE_HPP_