1 | // |
---|
2 | // Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo |
---|
3 | // |
---|
4 | // The contents of this file are covered under the licence agreement in the |
---|
5 | // file "LICENCE" distributed with Cforall. |
---|
6 | // |
---|
7 | // XXX.cc -- |
---|
8 | // |
---|
9 | // Author : Richard C. Bilson |
---|
10 | // Created On : Mon May 18 07:44:20 2015 |
---|
11 | // Last Modified By : |
---|
12 | // Last Modified On : |
---|
13 | // Update Count : 0 |
---|
14 | // |
---|
15 | #include <cassert> |
---|
16 | #include <string> |
---|
17 | #include <stack> |
---|
18 | |
---|
19 | namespace diet { |
---|
20 | /* A DIET ( Discrete Interval Encoding Tree ) range-map |
---|
21 | */ |
---|
22 | |
---|
23 | class diet_tree_exception : public std::exception { |
---|
24 | public: |
---|
25 | diet_tree_exception() {} |
---|
26 | diet_tree_exception( std::string _what ) : what( _what ) {} |
---|
27 | ~diet_tree_exception() throw () {} |
---|
28 | |
---|
29 | std::string get_what() const { return what; } |
---|
30 | void set_what( std::string newValue ) { what = newValue; } |
---|
31 | private: |
---|
32 | std::string what; |
---|
33 | }; |
---|
34 | |
---|
35 | template < typename T > class diet_tree_node; |
---|
36 | template < typename T > class diet_tree_iterator; |
---|
37 | |
---|
38 | template< typename key_type > |
---|
39 | class diet_tree { |
---|
40 | typedef key_type OrderedValue; |
---|
41 | typedef OrderedValue T; |
---|
42 | friend class diet_tree_iterator<T>; |
---|
43 | public: |
---|
44 | typedef OrderedValue value_type; |
---|
45 | typedef diet_tree_iterator<OrderedValue> iterator; |
---|
46 | typedef std::pair<value_type, value_type> pair_type; |
---|
47 | |
---|
48 | diet_tree() : root(0), left(0), right(0) {} |
---|
49 | ~diet_tree() { |
---|
50 | if ( root != 0 ) { delete root; root = 0; } |
---|
51 | if ( left != 0 ) { delete left; left = 0; } |
---|
52 | if ( right != 0 ) { delete right; right = 0; } |
---|
53 | } |
---|
54 | |
---|
55 | void insert( value_type _lo, value_type _hi ) { |
---|
56 | if ( _lo > _hi ) return; // throw exception? |
---|
57 | if ( root == 0 ) |
---|
58 | root = new diet_tree_node<value_type>(_lo, _hi); |
---|
59 | else { |
---|
60 | value_type lo = root->get_lo(), hi = root->get_hi(); |
---|
61 | if ( _lo < lo ) { |
---|
62 | if ( _hi > hi ) { |
---|
63 | /* can either minimize the work or minimize the number of nodes. |
---|
64 | Let's minimize the work. */ |
---|
65 | if ( left == 0 ) left = new diet_tree<T>(); |
---|
66 | left->insert( _lo, lo ); |
---|
67 | if ( right == 0 ) right = new diet_tree<T>(); |
---|
68 | right->insert( _hi, hi ); |
---|
69 | return; |
---|
70 | } else if ( _hi < lo ) { |
---|
71 | if ( left == 0 ) left = new diet_tree<T>(); |
---|
72 | left->insert( _lo, _hi ); |
---|
73 | } else if ( _hi <= hi ) { |
---|
74 | if ( left == 0 ) left = new diet_tree<T>(); |
---|
75 | left->insert( _lo, _hi ); |
---|
76 | root->set_range(_hi,hi); |
---|
77 | } |
---|
78 | } else if (_lo >= lo && _hi <= hi ) { |
---|
79 | root->set_range(_lo,_hi); |
---|
80 | } else if ( _hi > hi) { |
---|
81 | if ( _lo > hi ) { |
---|
82 | if ( right == 0 ) right = new diet_tree<T>(); |
---|
83 | right->insert( _lo, _hi ); |
---|
84 | } else if ( _lo < hi ) { |
---|
85 | root->set_range(lo, _lo); |
---|
86 | if ( right == 0 ) right = new diet_tree<T>(); |
---|
87 | right->insert(_lo, _hi); |
---|
88 | } |
---|
89 | } |
---|
90 | } |
---|
91 | return; |
---|
92 | } |
---|
93 | |
---|
94 | void insert( std::pair<value_type, value_type> p ) { |
---|
95 | insert(p.first, p.second); |
---|
96 | } |
---|
97 | |
---|
98 | pair_type get_range_pair() const { |
---|
99 | return pair_type(root->get_lo(),root->get_hi()); |
---|
100 | } |
---|
101 | |
---|
102 | /* |
---|
103 | void display( std::ostream &os = std::cout ) { |
---|
104 | if ( root != 0 ) { |
---|
105 | if ( left != 0 ) left->display(os); |
---|
106 | os << "(" << root->get_lo() << ", " << root->get_hi() << ")" << std::endl; |
---|
107 | if ( right != 0 ) right->display(os); |
---|
108 | } |
---|
109 | return; |
---|
110 | } |
---|
111 | */ |
---|
112 | |
---|
113 | iterator begin() { return iterator( this ); } |
---|
114 | iterator end() { return iterator( (diet_tree< value_type > *)0 ); } |
---|
115 | |
---|
116 | protected: |
---|
117 | diet_tree( diet_tree_node< OrderedValue > *_root ) : root( _root ) {} |
---|
118 | private: |
---|
119 | diet_tree_node< value_type > *root; |
---|
120 | diet_tree< value_type > *left, *right; |
---|
121 | }; |
---|
122 | |
---|
123 | template< typename OrderedValue > |
---|
124 | class diet_tree_node { |
---|
125 | public: |
---|
126 | typedef OrderedValue value_type; |
---|
127 | |
---|
128 | diet_tree_node( const OrderedValue &_lo, const OrderedValue &_hi ) |
---|
129 | : lo( _lo ), hi( _hi ) { |
---|
130 | if ( lo >= hi ) throw diet_tree_exception( "Invalid range" ); |
---|
131 | } |
---|
132 | |
---|
133 | void set_range(const OrderedValue &newLo, const OrderedValue &newHi) |
---|
134 | { lo = newLo; hi = newHi; } |
---|
135 | OrderedValue get_lo() const { return lo; } |
---|
136 | OrderedValue get_hi() const { return hi; } |
---|
137 | |
---|
138 | private: |
---|
139 | OrderedValue lo, hi; |
---|
140 | }; |
---|
141 | |
---|
142 | /* forward iterator */ |
---|
143 | template < typename T > |
---|
144 | class diet_tree_iterator { |
---|
145 | typedef diet_tree_iterator<T> self; |
---|
146 | typedef typename diet_tree<T>::pair_type pair_type; |
---|
147 | |
---|
148 | public: |
---|
149 | // typedef forward_iterator_tag iterator_category; |
---|
150 | |
---|
151 | diet_tree_iterator( diet_tree<T> *_tree ) : current( _tree ) { |
---|
152 | // current is allowed to be 0 only for `end' |
---|
153 | if (_tree != 0) go_leftmost(); |
---|
154 | } |
---|
155 | |
---|
156 | ~diet_tree_iterator() {} |
---|
157 | pair_type operator*() { |
---|
158 | if ( current == 0 ) throw diet_tree_exception( "Invalid dereference" ); |
---|
159 | return current->get_range_pair(); |
---|
160 | } |
---|
161 | |
---|
162 | bool operator==( const diet_tree_iterator<T> &other ) { return current == other.current; } |
---|
163 | bool operator!=( const diet_tree_iterator<T> &other ) { return current != other.current; } |
---|
164 | |
---|
165 | diet_tree_iterator<T> operator++() { |
---|
166 | assert(current != 0); |
---|
167 | if ( current->right == 0 ) |
---|
168 | if ( ! st.empty() ) |
---|
169 | { current = st.top(); st.pop(); } |
---|
170 | else |
---|
171 | current = 0; |
---|
172 | else { |
---|
173 | current = current->right; |
---|
174 | go_leftmost(); |
---|
175 | } |
---|
176 | return *this; |
---|
177 | } |
---|
178 | |
---|
179 | diet_tree_iterator<T> operator++(int) { |
---|
180 | self temp = *this; |
---|
181 | this->operator++(); |
---|
182 | return temp; |
---|
183 | } |
---|
184 | |
---|
185 | private: |
---|
186 | void go_leftmost() { |
---|
187 | assert(current != 0); |
---|
188 | diet_tree<T> *next = 0; |
---|
189 | while ( current->left != 0 ) { |
---|
190 | next = current->left; st.push( current ); current = next; |
---|
191 | } |
---|
192 | return; |
---|
193 | } |
---|
194 | |
---|
195 | void defrag() { |
---|
196 | /* join adjacent trees */ |
---|
197 | return; |
---|
198 | } |
---|
199 | |
---|
200 | diet_tree<T> *current; |
---|
201 | std::stack< diet_tree<T> * > st; |
---|
202 | }; |
---|
203 | |
---|
204 | template < typename Key, typename Value > |
---|
205 | class diet_tree_assoc_node : public diet_tree_node<Key> { |
---|
206 | public: |
---|
207 | typedef Key key_type; |
---|
208 | typedef Value data_type; |
---|
209 | typedef std::pair<Key,Value> value_type; |
---|
210 | private: |
---|
211 | Value data; |
---|
212 | }; |
---|
213 | |
---|
214 | } // namespace diet |
---|
215 | |
---|
216 | |
---|
217 | /* |
---|
218 | Local Variables: |
---|
219 | mode: c++ |
---|
220 | End: |
---|
221 | */ |
---|
222 | // Local Variables: // |
---|
223 | // tab-width: 4 // |
---|
224 | // mode: c++ // |
---|
225 | // compile-command: "make install" // |
---|
226 | // End: // |
---|