Newer
Older
/*
* bidirectional_tree.hpp
*
* Created on: Jul 2, 2016
* Author: Jan Travnicek
*/
#ifndef __TRIE_HPP_
#define __TRIE_HPP_
#include <memory>
#include <iterator>
#include <sstream>
#include "compare.hpp"
#include "pair.hpp"
#include "tuple.hpp"
template < class Key, class Value >
class trie {
Value m_data;
trie * m_parent;
public:
trie * getParent ( ) {
return m_parent;
}
const trie * getParent ( ) const {
return m_parent;
}
Value & getData ( ) {
return m_data;
}
const Value & getData ( ) const {
return m_data;
}
const ext::map < Key, trie > & getChildren ( ) const {
return m_children;
}
static void nicePrint ( std::ostream & os, const std::pair < const Key, trie < Key, Value > > & data, std::string prefix, const bool last ) {
os << prefix;
if ( last ) {
os << "\\-";
prefix += " ";
} else {
os << "|-";
prefix += "| ";
}
os << data.first << ":" << data.second.getData ( ) << std::endl;
unsigned int i = 0;
for ( const std::pair < const Key, trie < Key, Value > > & subdata : data.second ) {
os << prefix << "|" << std::endl;
nicePrint ( os, subdata, prefix, i == data.second.m_children.size ( ) - 1 );
++i;
}
}
typedef typename ext::map < Key, trie >::const_iterator const_children_iterator;
typedef const trie * const_child_iterator;
// ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
private:
void insert_helper ( const_child_iterator under, const_children_iterator begin, const_children_iterator end ) {
ext::map < Key, trie > & children = const_cast < ext::map < Key, trie > & > ( under->getChildren ( ) );
for ( ; begin != end; ++begin )
children.insert ( * begin ).first->second.m_parent = const_cast < trie * > ( & * under );
}
public:
void insert ( const_child_iterator under, Key key, trie < Key, Value > && value ) {
ext::map < Key, trie > & children = const_cast < ext::map < Key, trie > & > ( under->getChildren ( ) );
std::pair < typename ext::map < Key, trie >::iterator, bool > iter = children.insert ( std::make_pair ( std::move ( key ), std::move ( value ) ) );
iter.first->second.m_parent = const_cast < trie * > ( & * under );
}
void insert ( const_children_iterator under, Key key, const trie < Key, Value > & value ) {
insert ( under, std::move ( key ), trie < Key, Value > ( value ) );
}
void insert ( const_child_iterator under, const_children_iterator begin, const_children_iterator end ) {
insert_helper ( under, begin, end );
}
// ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
public:
trie ( Value && data, ext::map < Key, trie > && children ) : m_data ( std::move ( data ) ), m_parent ( nullptr ), m_children ( std::move ( children ) ) {
for ( std::pair < const Key, trie > & child : m_children )
child.second.m_parent = this;
}
trie ( const Value & data, const ext::map < Key, trie > & subtrees ) : trie ( Value ( data ), ext::map < Key, trie > ( subtrees ) ) {
}
template < typename ... Types >
trie ( const Value & data, Types ... subtrees ) : trie ( data, ext::map < Key, trie > { subtrees ... } ) {
}
template < typename ... Types >
trie ( Value && data, Types ... subtrees ) : trie ( std::move ( data ), ext::map < Key, trie > { subtrees ... } ) {
trie ( const Value & data, const_children_iterator begin, const_children_iterator end ) : trie ( data, ext::map < Key, trie > ( begin, end ) ) {
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
}
~trie ( ) noexcept {
}
trie ( const trie & other ) : m_data ( other.m_data ), m_parent ( other.m_parent ), m_children ( other.m_children ) {
for ( std::pair < const Key, trie > & child : m_children )
child.second.m_parent = this;
}
trie ( trie && other ) noexcept : m_data ( std::move ( other.m_data ) ), m_parent ( other.m_parent ), m_children ( std::move ( other.m_children ) ) {
for ( std::pair < const Key, trie > & child : m_children )
child.second.m_parent = this;
}
trie & operator =( const trie & node ) {
return this->operator =( trie ( node ) );
}
trie & operator =( trie && node ) noexcept {
m_data = std::move ( node.m_data );
m_children = std::move ( node.m_children );
for ( std::pair < const Key, trie > & child : m_children )
child.second.m_parent = this;
return * this;
}
// ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
const_child_iterator root ( ) const {
return this;
}
const_child_iterator parent ( ) const {
return this->getParent ( );
}
const_children_iterator begin ( ) const {
return m_children.begin ( );
}
const_children_iterator end ( ) const {
return m_children.end ( );
}
// ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
const_children_iterator erase ( const_child_iterator under, const_children_iterator position ) {
ext::map < Key, trie > & children = const_cast < ext::map < Key, trie > & > ( under->getChildren ( ) );
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
return children.erase ( position );
}
// ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
template < class ... Indexes >
const Value & operator ()( Indexes ... indexes ) const {
return this->operator ()( { ( Key ) indexes ... } );
}
const Value & operator ()( std::initializer_list < Key > indexes ) const {
const trie * node = this;
for ( Key index : indexes ) {
node = & node->getChildren ( ).find ( index )->second;
}
return node->getData ( );
}
template < class ... Indexes >
Value & operator ()( Indexes ... indexes ) {
return this->operator ()( { ( Key ) indexes ... } );
}
Value & operator ()( std::initializer_list < Key > indexes ) {
trie * node = this;
for ( Key index : indexes ) {
node = & node->getChildren ( ).find ( index )->second;
}
return node->getData ( );
}
// ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
bool checkStructure ( const trie & node ) const {
bool sign = true;
for ( const std::pair < const Key, trie > & child : node.getChildren ( ) )
sign &= child.second.getParent ( ) == & node && checkStructure ( child.second );
return sign;
}
bool checkStructure ( ) const {
return m_parent == nullptr && checkStructure ( * this );
}
// ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
friend void swap ( trie & first, trie & second ) {
swap ( std::move ( first.m_data ), std::move ( second.m_data ) );
swap ( std::move ( first.m_children ), std::move ( second.m_children ) );
swap ( std::move ( first.m_parent ), std::move ( second.m_parent ) );
}
// ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
int compare ( const trie & other ) const {
auto first = ext::tie ( this->getData ( ), this->getChildren ( ) );
auto second = ext::tie ( other.getData ( ), other.getChildren ( ) );
static ext::compare < decltype ( first ) > comp;
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
return comp ( first, second );
}
bool operator ==( const trie & other ) {
return compare ( other ) == 0;
}
bool operator !=( const trie & other ) {
return compare ( other ) != 0;
}
bool operator <( const trie & other ) {
return compare ( other ) < 0;
}
bool operator <=( const trie & other ) {
return compare ( other ) <= 0;
}
bool operator >( const trie & other ) {
return compare ( other ) > 0;
}
bool operator >=( const trie & other ) {
return compare ( other ) >= 0;
}
// ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
std::ostream & nicePrint ( std::ostream & os ) const {
os << "\\-";
std::string prefix ( " " );
os << getData ( ) << std::endl;
unsigned int i = 0;
for ( const std::pair < const Key, trie < Key, Value > > & subdata : getChildren ( ) ) {
os << prefix << "|" << std::endl;
nicePrint ( os, subdata, prefix, i == m_children.size ( ) - 1 );
++i;
}
return os;
}
};
template < class Key, class Value >
std::ostream & operator <<( std::ostream & out, const trie < Key, Value > & t ) {
out << "[";
out << t.getData ( ) << ";";
for ( typename ext::map < Key, trie < Key, Value > >::const_iterator iter = t.getChildren ( ).begin ( ); iter != t.getChildren ( ).end ( ); ++ iter) {
if ( iter != t.getChildren ( ).begin ( ) ) {
out << ",";
}
out << iter->first << ":" << iter->second;
}
out << "]";
return out;
}
template < class Key, class Value >
struct compare < ext::trie < Key, Value > > {
int operator ()( const ext::trie < Key, Value > & first, const ext::trie < Key, Value > & second ) const {
return first.compare ( second );
}
};
template < class Key, class Value >
std::string to_string ( const ext::trie < Key, Value > & value ) {
std::stringstream ss;
ss << value;
return ss.str();
}