Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/*
* tree.hpp
*
* Created on: Jul 2, 2016
* Author: Jan Travnicek
*/
#ifndef __TREE_HPP_
#define __TREE_HPP_
namespace std {
template < class Data >
class BaseNode {
Data * parent;
template < class D, class CD, class C >
friend class NularyNode;
template < class D, class CD, class C >
friend class UnaryNode;
template < class D, class CD, class C >
friend class BinaryNode;
template < class D, class CD, class C >
friend class TernaryNode;
template < class D, int I, class CD, class C >
friend class AnyaryNode;
template < class D, class CD, class C >
friend class FixedaryNode;
template < class D, class CD, class C >
friend class VararyNode;
Data * operator ->( ) {
return static_cast < Data * > ( this );
}
const Data * operator ->( ) const {
return static_cast < Data * > ( this );
}
public:
BaseNode ( ) : parent ( nullptr ) {
}
virtual ~BaseNode ( ) noexcept {
}
BaseNode ( const BaseNode & ) : parent ( nullptr ) {
}
BaseNode ( BaseNode && ) noexcept : parent ( nullptr ) {
}
BaseNode & operator =( const BaseNode & ) {
return * this;
}
BaseNode & operator =( BaseNode && ) noexcept {
return * this;
}
Data * getParent ( ) {
return parent;
}
const Data * getParent ( ) const {
return parent;
}
};
template < class Data, int arity, class ConstData = Data, class Cast = Data >
class AnyaryNode {
typename TupleBuilder < Data, arity >::type children;
template < std::size_t ... Indices >
void setParent ( std::index_sequence < Indices ... > ) {
( void ) std::initializer_list < void * > { std::get < Indices > ( children )->parent = static_cast < Cast * > ( this ) ... };
}
AnyaryNode ( typename TupleBuilder < Data, arity >::type children ) : children ( std::move ( children ) ) {
setParent ( std::make_index_sequence < arity > ( ) );
AnyaryNode ( const AnyaryNode & other ) : AnyaryNode ( other.children ) {
AnyaryNode ( AnyaryNode && other ) noexcept : AnyaryNode ( std::move ( other.children ) ) {
AnyaryNode & operator =( const AnyaryNode & other ) {
return * this = AnyaryNode ( other );
AnyaryNode & operator =( AnyaryNode && other ) noexcept {
std::swap ( this->children, other.children );
setParent ( std::make_index_sequence < arity > ( ) );
const typename TupleBuilder < Data, arity >::type & getElements ( ) {
return children;
const typename TupleBuilder < ConstData, arity >::type & getElements ( ) const {
return reinterpret_cast < const typename TupleBuilder < ConstData, arity >::type & > ( children );
template < int N >
const ConstData & getElement ( ) const {
return reinterpret_cast < const ConstData & > ( std::get < N > ( children ) );
}
template < int N >
Data & getElement ( ) {
return std::get < N > ( children );
}
void setElements ( typename TupleBuilder < Data, arity >::type children ) {
children = std::move ( children );
setParent ( std::make_index_sequence < arity > ( ) );
}
template < int N >
void setElement ( Data d ) {
std::get < N > ( children ) = std::move ( d );
std::get < N > ( children )->parent = static_cast < Cast * > ( this );
}
};
template < class Data, class ConstData = Data, class Cast = Data >
class NularyNode : public AnyaryNode < Data, 0, ConstData, Cast > {
NularyNode ( ) : AnyaryNode < Data, 0, ConstData, Cast > ( std::tuple < > ( ) ) {
template < class Data, class ConstData = Data, class Cast = Data >
class UnaryNode : public AnyaryNode < Data, 1, ConstData, Cast > {
public:
UnaryNode ( Data c ) : AnyaryNode < Data, 1, ConstData, Cast > ( std::make_tuple ( std::move ( c ) ) ) {
Data & getChild ( ) {
return this->template getElement < 0 > ( );
const ConstData & getChild ( ) const {
return this->template getElement < 0 > ( );
void setChild ( Data c ) {
this->template setElement < 0 > ( std::move ( c ) );
}
template < class Data, class ConstData = Data, class Cast = Data >
class BinaryNode : public AnyaryNode < Data, 2, ConstData, Cast > {
public:
BinaryNode ( Data l, Data r ) : AnyaryNode < Data, 2, ConstData, Cast > ( std::make_tuple ( std::move ( l ), std::move ( r ) ) ) {
}
Data & getLeft ( ) {
}
const ConstData & getLeft ( ) const {
}
void setLeft ( Data l ) {
this->template setElement < 0 > ( std::move ( l ) );
}
Data & getRight ( ) {
}
const ConstData & getRight ( ) const {
}
void setRight ( Data r ) {
this->template setElement < 1 > ( std::move ( r ) );
}
};
template < class Data, class ConstData = Data, class Cast = Data >
class TernaryNode : public AnyaryNode < Data, 3, ConstData, Cast > {
TernaryNode ( Data f, Data s, Data t ) : AnyaryNode < Data, 3, ConstData, Cast > ( std::make_tuple ( std::move ( f ), std::move ( s ), std::move ( t ) ) ) {
}
Data & getFirst ( ) {
}
const ConstData & getFirst ( ) const {
}
void setFirst ( Data f ) {
this->template setElement < 0 > ( std::move ( f ) );
}
Data & getSecond ( ) {
}
const ConstData & getSecond ( ) const {
}
void setSecond ( Data s ) {
this->template setElement < 1 > ( std::move ( s ) );
}
Data & getThird ( ) {
}
const ConstData & getThird ( ) const {
}
void setThird ( Data t ) {
this->template setElement < 2 > ( std::move ( t ) );
237
238
239
240
241
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
}
};
template < class Data, class ConstData = Data, class Cast = Data >
class FixedaryNode {
std::vector < Data > children;
public:
FixedaryNode ( std::vector < Data > c ) : children ( std::move ( c ) ) {
for ( Data & child : children )
child->parent = static_cast < Cast * > ( this );
}
virtual ~FixedaryNode ( ) noexcept {
}
FixedaryNode ( const FixedaryNode & other ) : FixedaryNode ( other.children ) {
}
FixedaryNode ( FixedaryNode && other ) noexcept : FixedaryNode ( std::move ( other.children ) ) {
}
FixedaryNode & operator =( const FixedaryNode & other ) {
return * this = FixedaryNode ( other );
}
FixedaryNode & operator =( FixedaryNode && other ) noexcept {
std::swap ( this->children, other.children );
children->parent = static_cast < Cast * > ( this );
return * this;
}
const std::vector < Data > & getChildren ( ) {
return children;
}
const std::vector < ConstData > & getChildren ( ) const {
return reinterpret_cast < const std::vector < ConstData > & > ( children );
}
void setChildren ( std::vector < Data > c ) {
if ( c.size ( ) != children.size ( ) )
throw "Arity != size";
children = std::move ( c );
for ( Data & child : children )
child->parent = static_cast < Cast * > ( this );
}
void setChild ( Data d, int index ) {
if ( children.size ( ) >= index )
throw "Index out of bounds";
children[index] = std::move ( d );
children[index]->parent = static_cast < Cast * > ( this );
}
};
template < class Data, class ConstData = Data, class Cast = Data >
class VararyNode {
std::vector < Data > children;
public:
VararyNode ( std::vector < Data > c ) : children ( std::move ( c ) ) {
for ( Data & child : children )
child->parent = static_cast < Cast * > ( this );
}
virtual ~VararyNode ( ) noexcept {
}
VararyNode ( const VararyNode & other ) : VararyNode ( other.children ) {
}
VararyNode ( VararyNode && other ) noexcept : VararyNode ( std::move ( other.children ) ) {
}
VararyNode & operator =( const VararyNode & other ) {
return * this = VararyNode ( other );
}
VararyNode & operator =( VararyNode && other ) noexcept {
std::swap ( this->children, other.children );
children->parent = static_cast < Cast * > ( this );
return * this;
}
const std::vector < Data > & getChildren ( ) {
return children;
}
const std::vector < ConstData > & getChildren ( ) const {
return reinterpret_cast < const std::vector < ConstData > & > ( children );
}
void setChildren ( std::vector < Data > c ) {
children = std::move ( c );
for ( Data & child : children )
child->parent = static_cast < Cast * > ( this );
}
void setChild ( Data d, int index ) {
children[index] = std::move ( d );
children[index]->parent = static_cast < Cast * > ( this );
}
void pushBackChild ( Data d ) {
children.push_back ( std::move ( d ) );
children[children.size ( ) - 1]->parent = static_cast < Cast * > ( this );
}
};
template < class Data, class ConstData, class Cast >
class Tree {
public:
static void setChild ( const UnaryNode < Data, ConstData, Cast > & node, Data child ) {
const_cast < UnaryNode < Data, ConstData, Cast > & > ( node ).setChild ( std::move ( child ) );
}
static void setLeft ( const BinaryNode < Data, ConstData, Cast > & node, Data child ) {
const_cast < BinaryNode < Data, ConstData, Cast > & > ( node ).setLeft ( std::move ( child ) );
}
static void setRight ( const BinaryNode < Data, ConstData, Cast > & node, Data child ) {
const_cast < BinaryNode < Data, ConstData, Cast > & > ( node ).setRight ( std::move ( child ) );
}
static void setFirst ( const TernaryNode < Data, ConstData, Cast > & node, Data child ) {
const_cast < TernaryNode < Data, ConstData, Cast > & > ( node ).setFirst ( std::move ( child ) );
}
static void setSecond ( const TernaryNode < Data, ConstData, Cast > & node, Data child ) {
const_cast < TernaryNode < Data, ConstData, Cast > & > ( node ).setSecond ( std::move ( child ) );
}
static void setThird ( const TernaryNode < Data, ConstData, Cast > & node, Data child ) {
const_cast < TernaryNode < Data, ConstData, Cast > & > ( node ).setThird ( std::move ( child ) );
}
template < int arity >
static void setChildren ( const AnyaryNode < Data, arity, ConstData, Cast > & node, typename TupleBuilder < Data, arity >::type children ) {
const_cast < AnyaryNode < Data, arity, ConstData, Cast > & > ( node ).setChildren ( std::move ( children ) );
}
template < class N, int arity >
static void setChild ( const AnyaryNode < Data, arity, ConstData, Cast > & node, Data child ) {
const_cast < AnyaryNode < Data, arity, ConstData, Cast > & > ( node ).template setChild < N > ( std::move ( child ) );
}
static void setChildren ( const FixedaryNode < Data, ConstData, Cast > & node, std::vector < Data > child ) {
const_cast < FixedaryNode < Data, ConstData, Cast > & > ( node ).setChildren ( std::move ( child ) );
}
static void setChild ( const FixedaryNode < Data, ConstData, Cast > & node, Data child, int index ) {
const_cast < FixedaryNode < Data, ConstData, Cast > & > ( node ).setChild ( std::move ( child ), index );
}
static void setChildren ( const VararyNode < Data, ConstData, Cast > & node, std::vector < Data > children ) {
const_cast < VararyNode < Data, ConstData, Cast > & > ( node ).setChildren ( std::move ( children ) );
}
static void setChild ( const VararyNode < Data, ConstData, Cast > & node, Data child, int index ) {
const_cast < VararyNode < Data, ConstData, Cast > & > ( node ).setChild ( std::move ( child ), index );
}
static void pushBackChild ( const VararyNode < Data, ConstData, Cast > & node, Data child ) {
const_cast < VararyNode < Data, ConstData, Cast > & > ( node ).pushBackChild ( std::move ( child ) );
}
};
} /* namespace std */
#endif /* __TREE_HPP_ */