forked from Saluev/python-liborigin2
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtree.hh
2663 lines (2344 loc) · 83.3 KB
/
tree.hh
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
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
175
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
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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
$Id: tree.hh,v 1.147 2007/10/19 11:24:24 peekas Exp $
STL-like templated tree class.
Copyright (C) 2001-2006 Kasper Peeters <[email protected]>.
*/
/** \mainpage tree.hh
\author Kasper Peeters
\version 2.4
\date 18-Oct-2007
\see http://www.aei.mpg.de/~peekas/tree/
\see http://www.aei.mpg.de/~peekas/tree/ChangeLog
The tree.hh library for C++ provides an STL-like container class
for n-ary trees, templated over the data stored at the
nodes. Various types of iterators are provided (post-order,
pre-order, and others). Where possible the access methods are
compatible with the STL or alternative algorithms are
available.
*/
/*
The tree.hh code is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 or 3.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/** \todo
- New-style move members are not completely finished yet.
- It would be good to have an iterator which can iterate over all
nodes below a given node.
- Fixed depth iterators do not iterate over the entire range if there
are 'holes' in the tree.
- If a range uses const iter_base& as end iterator, things will
inevitably go wrong, because upcast from iter_base to a non-sibling_iter
is incorrect. This upcast should be removed (and then all illegal uses
as previously in 'equal' will be flagged by the compiler). This requires
new copy constructors though.
- There's a bug in replace(sibling_iterator, ...) when the ranges
sit next to each other. Turned up in append_child(iter,iter)
but has been avoided now.
- "std::operator<" does not work correctly on our iterators, and for some
reason a globally defined template operator< did not get picked up.
Using a comparison class now, but this should be investigated.
*/
#ifndef tree_hh_
#define tree_hh_
#include <cassert>
#include <memory>
#include <stdexcept>
#include <iterator>
#include <set>
#include <queue>
#include <iostream>
// HP-style construct/destroy have gone from the standard,
// so here is a copy.
namespace kp {
template <class T1, class T2>
void constructor(T1* p, T2& val)
{
new ((void *) p) T1(val);
}
template <class T1>
void constructor(T1* p)
{
new ((void *) p) T1;
}
template <class T1>
void destructor(T1* p)
{
p->~T1();
}
};
/// A node in the tree, combining links to other nodes as well as the actual data.
template<class T>
class tree_node_ { // size: 5*4=20 bytes (on 32 bit arch), can be reduced by 8.
public:
tree_node_<T> *parent;
tree_node_<T> *first_child, *last_child;
tree_node_<T> *prev_sibling, *next_sibling;
T data;
}; // __attribute__((packed));
template <class T, class tree_node_allocator = std::allocator<tree_node_<T> > >
class tree {
protected:
typedef tree_node_<T> tree_node;
public:
/// Value of the data stored at a node.
typedef T value_type;
class iterator_base;
class pre_order_iterator;
class post_order_iterator;
class sibling_iterator;
class leaf_iterator;
tree();
tree(const T&);
tree(const iterator_base&);
tree(const tree<T, tree_node_allocator>&);
~tree();
void operator=(const tree<T, tree_node_allocator>&);
/// Base class for iterators, only pointers stored, no traversal logic.
#ifdef __SGI_STL_PORT
class iterator_base : public stlport::bidirectional_iterator<T, ptrdiff_t> {
#else
class iterator_base {
#endif
public:
typedef T value_type;
typedef T* pointer;
typedef T& reference;
typedef size_t size_type;
typedef ptrdiff_t difference_type;
typedef std::bidirectional_iterator_tag iterator_category;
iterator_base();
iterator_base(tree_node *);
T& operator*() const;
T* operator->() const;
/// When called, the next increment/decrement skips children of this node.
void skip_children();
/// Number of children of the node pointed to by the iterator.
unsigned int number_of_children() const;
sibling_iterator begin() const;
sibling_iterator end() const;
tree_node *node;
protected:
bool skip_current_children_;
};
/// Depth-first iterator, first accessing the node, then its children.
class pre_order_iterator : public iterator_base {
public:
pre_order_iterator();
pre_order_iterator(tree_node *);
pre_order_iterator(const iterator_base&);
pre_order_iterator(const sibling_iterator&);
bool operator==(const pre_order_iterator&) const;
bool operator!=(const pre_order_iterator&) const;
pre_order_iterator& operator++();
pre_order_iterator& operator--();
pre_order_iterator operator++(int);
pre_order_iterator operator--(int);
pre_order_iterator& operator+=(unsigned int);
pre_order_iterator& operator-=(unsigned int);
};
/// Depth-first iterator, first accessing the children, then the node itself.
class post_order_iterator : public iterator_base {
public:
post_order_iterator();
post_order_iterator(tree_node *);
post_order_iterator(const iterator_base&);
post_order_iterator(const sibling_iterator&);
bool operator==(const post_order_iterator&) const;
bool operator!=(const post_order_iterator&) const;
post_order_iterator& operator++();
post_order_iterator& operator--();
post_order_iterator operator++(int);
post_order_iterator operator--(int);
post_order_iterator& operator+=(unsigned int);
post_order_iterator& operator-=(unsigned int);
/// Set iterator to the first child as deep as possible down the tree.
void descend_all();
};
/// Breadth-first iterator, using a queue
class breadth_first_queued_iterator : public iterator_base {
public:
breadth_first_queued_iterator();
breadth_first_queued_iterator(tree_node *);
breadth_first_queued_iterator(const iterator_base&);
bool operator==(const breadth_first_queued_iterator&) const;
bool operator!=(const breadth_first_queued_iterator&) const;
breadth_first_queued_iterator& operator++();
breadth_first_queued_iterator operator++(int);
breadth_first_queued_iterator& operator+=(unsigned int);
private:
std::queue<tree_node *> traversal_queue;
};
/// The default iterator types throughout the tree class.
typedef pre_order_iterator iterator;
typedef breadth_first_queued_iterator breadth_first_iterator;
/// Iterator which traverses only the nodes at a given depth from the root.
class fixed_depth_iterator : public iterator_base {
public:
fixed_depth_iterator();
fixed_depth_iterator(tree_node *);
fixed_depth_iterator(const iterator_base&);
fixed_depth_iterator(const sibling_iterator&);
fixed_depth_iterator(const fixed_depth_iterator&);
bool operator==(const fixed_depth_iterator&) const;
bool operator!=(const fixed_depth_iterator&) const;
fixed_depth_iterator& operator++();
fixed_depth_iterator& operator--();
fixed_depth_iterator operator++(int);
fixed_depth_iterator operator--(int);
fixed_depth_iterator& operator+=(unsigned int);
fixed_depth_iterator& operator-=(unsigned int);
tree_node *first_parent_;
private:
void set_first_parent_();
void find_leftmost_parent_();
};
/// Iterator which traverses only the nodes which are siblings of each other.
class sibling_iterator : public iterator_base {
public:
sibling_iterator();
sibling_iterator(tree_node *);
sibling_iterator(const sibling_iterator&);
sibling_iterator(const iterator_base&);
bool operator==(const sibling_iterator&) const;
bool operator!=(const sibling_iterator&) const;
sibling_iterator& operator++();
sibling_iterator& operator--();
sibling_iterator operator++(int);
sibling_iterator operator--(int);
sibling_iterator& operator+=(unsigned int);
sibling_iterator& operator-=(unsigned int);
tree_node *range_first() const;
tree_node *range_last() const;
tree_node *parent_;
private:
void set_parent_();
};
/// Iterator which traverses only the leaves.
class leaf_iterator : public iterator_base {
public:
leaf_iterator();
leaf_iterator(tree_node *);
leaf_iterator(const sibling_iterator&);
leaf_iterator(const iterator_base&);
bool operator==(const leaf_iterator&) const;
bool operator!=(const leaf_iterator&) const;
leaf_iterator& operator++();
leaf_iterator& operator--();
leaf_iterator operator++(int);
leaf_iterator operator--(int);
leaf_iterator& operator+=(unsigned int);
leaf_iterator& operator-=(unsigned int);
};
/// Return iterator to the beginning of the tree.
inline pre_order_iterator begin() const;
/// Return iterator to the end of the tree.
inline pre_order_iterator end() const;
/// Return post-order iterator to the beginning of the tree.
post_order_iterator begin_post() const;
/// Return post-order iterator to the end of the tree.
post_order_iterator end_post() const;
/// Return fixed-depth iterator to the first node at a given depth from the given iterator.
fixed_depth_iterator begin_fixed(const iterator_base&, unsigned int) const;
/// Return fixed-depth iterator to end of the nodes at given depth from the given iterator.
fixed_depth_iterator end_fixed(const iterator_base&, unsigned int) const;
/// Return breadth-first iterator to the first node at a given depth.
breadth_first_queued_iterator begin_breadth_first() const;
/// Return breadth-first iterator to end of the nodes at given depth.
breadth_first_queued_iterator end_breadth_first() const;
/// Return sibling iterator to the first child of given node.
sibling_iterator begin(const iterator_base&) const;
/// Return sibling iterator to the end of the children of a given node.
sibling_iterator end(const iterator_base&) const;
/// Return leaf iterator to the first leaf of the tree.
leaf_iterator begin_leaf() const;
/// Return leaf iterator to the last leaf of the tree.
leaf_iterator end_leaf() const;
/// Return iterator to the parent of a node.
template<typename iter> static iter parent(iter);
/// Return iterator to the previous sibling of a node.
template<typename iter> iter previous_sibling(iter) const;
/// Return iterator to the next sibling of a node.
template<typename iter> iter next_sibling(iter) const;
/// Return iterator to the next node at a given depth.
template<typename iter> iter next_at_same_depth(iter) const;
/// Erase all nodes of the tree.
void clear();
/// Erase element at position pointed to by iterator, return incremented iterator.
template<typename iter> iter erase(iter);
/// Erase all children of the node pointed to by iterator.
void erase_children(const iterator_base&);
/// Insert empty node as last/first child of node pointed to by position.
template<typename iter> iter append_child(iter position);
template<typename iter> iter prepend_child(iter position);
/// Insert node as last/first child of node pointed to by position.
template<typename iter> iter append_child(iter position, const T& x);
template<typename iter> iter prepend_child(iter position, const T& x);
/// Append the node (plus its children) at other_position as last/first child of position.
template<typename iter> iter append_child(iter position, iter other_position);
template<typename iter> iter prepend_child(iter position, iter other_position);
/// Append the nodes in the from-to range (plus their children) as last/first children of position.
template<typename iter> iter append_children(iter position, sibling_iterator from, sibling_iterator to);
template<typename iter> iter prepend_children(iter position, sibling_iterator from, sibling_iterator to);
/// Short-hand to insert topmost node in otherwise empty tree.
pre_order_iterator set_head(const T& x);
/// Insert node as previous sibling of node pointed to by position.
template<typename iter> iter insert(iter position, const T& x);
/// Specialisation of previous member.
sibling_iterator insert(sibling_iterator position, const T& x);
/// Insert node (with children) pointed to by subtree as previous sibling of node pointed to by position.
template<typename iter> iter insert_subtree(iter position, const iterator_base& subtree);
/// Insert node as next sibling of node pointed to by position.
template<typename iter> iter insert_after(iter position, const T& x);
/// Insert node (with children) pointed to by subtree as next sibling of node pointed to by position.
template<typename iter> iter insert_subtree_after(iter position, const iterator_base& subtree);
/// Replace node at 'position' with other node (keeping same children); 'position' becomes invalid.
template<typename iter> iter replace(iter position, const T& x);
/// Replace node at 'position' with subtree starting at 'from' (do not erase subtree at 'from'); see above.
template<typename iter> iter replace(iter position, const iterator_base& from);
/// Replace string of siblings (plus their children) with copy of a new string (with children); see above
sibling_iterator replace(sibling_iterator orig_begin, sibling_iterator orig_end,
sibling_iterator new_begin, sibling_iterator new_end);
/// Move all children of node at 'position' to be siblings, returns position.
template<typename iter> iter flatten(iter position);
/// Move nodes in range to be children of 'position'.
template<typename iter> iter reparent(iter position, sibling_iterator begin, sibling_iterator end);
/// Move all child nodes of 'from' to be children of 'position'.
template<typename iter> iter reparent(iter position, iter from);
/// Replace node with a new node, making the old node a child of the new node.
template<typename iter> iter wrap(iter position, const T& x);
/// Move 'source' node (plus its children) to become the next sibling of 'target'.
template<typename iter> iter move_after(iter target, iter source);
/// Move 'source' node (plus its children) to become the previous sibling of 'target'.
template<typename iter> iter move_before(iter target, iter source);
sibling_iterator move_before(sibling_iterator target, sibling_iterator source);
/// Move 'source' node (plus its children) to become the node at 'target' (erasing the node at 'target').
template<typename iter> iter move_ontop(iter target, iter source);
/// Merge with other tree, creating new branches and leaves only if they are not already present.
void merge(sibling_iterator, sibling_iterator, sibling_iterator, sibling_iterator,
bool duplicate_leaves=false);
/// Sort (std::sort only moves values of nodes, this one moves children as well).
void sort(sibling_iterator from, sibling_iterator to, bool deep=false);
template<class StrictWeakOrdering>
void sort(sibling_iterator from, sibling_iterator to, StrictWeakOrdering comp, bool deep=false);
/// Compare two ranges of nodes (compares nodes as well as tree structure).
template<typename iter>
bool equal(const iter& one, const iter& two, const iter& three) const;
template<typename iter, class BinaryPredicate>
bool equal(const iter& one, const iter& two, const iter& three, BinaryPredicate) const;
template<typename iter>
bool equal_subtree(const iter& one, const iter& two) const;
template<typename iter, class BinaryPredicate>
bool equal_subtree(const iter& one, const iter& two, BinaryPredicate) const;
/// Extract a new tree formed by the range of siblings plus all their children.
tree subtree(sibling_iterator from, sibling_iterator to) const;
void subtree(tree&, sibling_iterator from, sibling_iterator to) const;
/// Exchange the node (plus subtree) with its sibling node (do nothing if no sibling present).
void swap(sibling_iterator it);
/// Exchange two nodes (plus subtrees)
void swap(iterator, iterator);
/// Count the total number of nodes.
int size() const;
/// Count the total number of nodes below the indicated node (plus one).
int size(const iterator_base&) const;
/// Check if tree is empty.
bool empty() const;
/// Compute the depth to the root.
int depth(const iterator_base&) const;
/// Determine the maximal depth of the tree.
int max_depth() const;
/// Determine the maximal depth of the tree below a given one.
int max_depth(const iterator_base&) const;
/// Count the number of children of node at position.
static unsigned int number_of_children(const iterator_base&);
/// Count the number of 'next' siblings of node at iterator.
unsigned int number_of_siblings(const iterator_base&) const;
/// Determine whether node at position is in the subtrees with root in the range.
bool is_in_subtree(const iterator_base& position, const iterator_base& begin,
const iterator_base& end) const;
/// Determine whether the iterator is an 'end' iterator and thus not actually pointing to a node.
bool is_valid(const iterator_base&) const;
/// Determine the index of a node in the range of siblings to which it belongs.
unsigned int index(sibling_iterator it) const;
/// Inverse of 'index': return the n-th child of the node at position.
sibling_iterator child(const iterator_base& position, unsigned int) const;
/// Comparator class for iterators (compares pointer values; why doesn't this work automatically?)
class iterator_base_less {
public:
bool operator()(const typename tree<T, tree_node_allocator>::iterator_base& one,
const typename tree<T, tree_node_allocator>::iterator_base& two) const
{
return one.node < two.node;
}
};
tree_node *head, *feet; // head/feet are always dummy; if an iterator points to them it is invalid
private:
tree_node_allocator alloc_;
void head_initialise_();
void copy_(const tree<T, tree_node_allocator>& other);
/// Comparator class for two nodes of a tree (used for sorting and searching).
template<class StrictWeakOrdering>
class compare_nodes {
public:
compare_nodes(StrictWeakOrdering comp) : comp_(comp) {};
bool operator()(const tree_node *a, const tree_node *b)
{
static StrictWeakOrdering comp;
return comp(a->data, b->data);
}
private:
StrictWeakOrdering comp_;
};
};
//template <class T, class tree_node_allocator>
//class iterator_base_less {
// public:
// bool operator()(const typename tree<T, tree_node_allocator>::iterator_base& one,
// const typename tree<T, tree_node_allocator>::iterator_base& two) const
// {
// txtout << "operatorclass<" << one.node < two.node << std::endl;
// return one.node < two.node;
// }
//};
// template <class T, class tree_node_allocator>
// bool operator<(const typename tree<T, tree_node_allocator>::iterator& one,
// const typename tree<T, tree_node_allocator>::iterator& two)
// {
// txtout << "operator< " << one.node < two.node << std::endl;
// if(one.node < two.node) return true;
// return false;
// }
//
// template <class T, class tree_node_allocator>
// bool operator==(const typename tree<T, tree_node_allocator>::iterator& one,
// const typename tree<T, tree_node_allocator>::iterator& two)
// {
// txtout << "operator== " << one.node == two.node << std::endl;
// if(one.node == two.node) return true;
// return false;
// }
//
// template <class T, class tree_node_allocator>
// bool operator>(const typename tree<T, tree_node_allocator>::iterator_base& one,
// const typename tree<T, tree_node_allocator>::iterator_base& two)
// {
// txtout << "operator> " << one.node < two.node << std::endl;
// if(one.node > two.node) return true;
// return false;
// }
// Tree
template <class T, class tree_node_allocator>
tree<T, tree_node_allocator>::tree()
{
head_initialise_();
}
template <class T, class tree_node_allocator>
tree<T, tree_node_allocator>::tree(const T& x)
{
head_initialise_();
set_head(x);
}
template <class T, class tree_node_allocator>
tree<T, tree_node_allocator>::tree(const iterator_base& other)
{
head_initialise_();
set_head((*other));
replace(begin(), other);
}
template <class T, class tree_node_allocator>
tree<T, tree_node_allocator>::~tree()
{
clear();
alloc_.deallocate(head,1);
alloc_.deallocate(feet,1);
}
template <class T, class tree_node_allocator>
void tree<T, tree_node_allocator>::head_initialise_()
{
head = alloc_.allocate(1,0); // MSVC does not have default second argument
feet = alloc_.allocate(1,0);
head->parent=0;
head->first_child=0;
head->last_child=0;
head->prev_sibling=0; //head;
head->next_sibling=feet; //head;
feet->parent=0;
feet->first_child=0;
feet->last_child=0;
feet->prev_sibling=head;
feet->next_sibling=0;
}
template <class T, class tree_node_allocator>
void tree<T, tree_node_allocator>::operator=(const tree<T, tree_node_allocator>& other)
{
copy_(other);
}
template <class T, class tree_node_allocator>
tree<T, tree_node_allocator>::tree(const tree<T, tree_node_allocator>& other)
{
head_initialise_();
copy_(other);
}
template <class T, class tree_node_allocator>
void tree<T, tree_node_allocator>::copy_(const tree<T, tree_node_allocator>& other)
{
clear();
pre_order_iterator it=other.begin(), to=begin();
while(it!=other.end()) {
to=insert(to, (*it));
it.skip_children();
++it;
}
to=begin();
it=other.begin();
while(it!=other.end()) {
to=replace(to, it);
to.skip_children();
it.skip_children();
++to;
++it;
}
}
template <class T, class tree_node_allocator>
void tree<T, tree_node_allocator>::clear()
{
if(head)
while(head->next_sibling!=feet)
erase(pre_order_iterator(head->next_sibling));
}
template<class T, class tree_node_allocator>
void tree<T, tree_node_allocator>::erase_children(const iterator_base& it)
{
// std::cout << "erase_children " << it.node << std::endl;
if(it.node==0) return;
tree_node *cur=it.node->first_child;
tree_node *prev=0;
while(cur!=0) {
prev=cur;
cur=cur->next_sibling;
erase_children(pre_order_iterator(prev));
kp::destructor(&prev->data);
alloc_.deallocate(prev,1);
}
it.node->first_child=0;
it.node->last_child=0;
// std::cout << "exit" << std::endl;
}
template<class T, class tree_node_allocator>
template<class iter>
iter tree<T, tree_node_allocator>::erase(iter it)
{
tree_node *cur=it.node;
assert(cur!=head);
iter ret=it;
ret.skip_children();
++ret;
erase_children(it);
if(cur->prev_sibling==0) {
cur->parent->first_child=cur->next_sibling;
}
else {
cur->prev_sibling->next_sibling=cur->next_sibling;
}
if(cur->next_sibling==0) {
cur->parent->last_child=cur->prev_sibling;
}
else {
cur->next_sibling->prev_sibling=cur->prev_sibling;
}
kp::destructor(&cur->data);
alloc_.deallocate(cur,1);
return ret;
}
template <class T, class tree_node_allocator>
typename tree<T, tree_node_allocator>::pre_order_iterator tree<T, tree_node_allocator>::begin() const
{
return pre_order_iterator(head->next_sibling);
}
template <class T, class tree_node_allocator>
typename tree<T, tree_node_allocator>::pre_order_iterator tree<T, tree_node_allocator>::end() const
{
return pre_order_iterator(feet);
}
template <class T, class tree_node_allocator>
typename tree<T, tree_node_allocator>::breadth_first_queued_iterator tree<T, tree_node_allocator>::begin_breadth_first() const
{
return breadth_first_queued_iterator(head->next_sibling);
}
template <class T, class tree_node_allocator>
typename tree<T, tree_node_allocator>::breadth_first_queued_iterator tree<T, tree_node_allocator>::end_breadth_first() const
{
return breadth_first_queued_iterator();
}
template <class T, class tree_node_allocator>
typename tree<T, tree_node_allocator>::post_order_iterator tree<T, tree_node_allocator>::begin_post() const
{
tree_node *tmp=head->next_sibling;
if(tmp!=feet) {
while(tmp->first_child)
tmp=tmp->first_child;
}
return post_order_iterator(tmp);
}
template <class T, class tree_node_allocator>
typename tree<T, tree_node_allocator>::post_order_iterator tree<T, tree_node_allocator>::end_post() const
{
return post_order_iterator(feet);
}
template <class T, class tree_node_allocator>
typename tree<T, tree_node_allocator>::fixed_depth_iterator tree<T, tree_node_allocator>::begin_fixed(const iterator_base& pos, unsigned int dp) const
{
tree_node *tmp=pos.node;
unsigned int curdepth=0;
while(curdepth<dp) { // go down one level
while(tmp->first_child==0) {
if(tmp->next_sibling==0) {
// try to walk up and then right again
do {
tmp=tmp->parent;
if(tmp==0)
throw std::range_error("tree: begin_fixed out of range");
--curdepth;
} while(tmp->next_sibling==0);
}
tmp=tmp->next_sibling;
}
tmp=tmp->first_child;
++curdepth;
}
return tmp;
}
template <class T, class tree_node_allocator>
typename tree<T, tree_node_allocator>::fixed_depth_iterator tree<T, tree_node_allocator>::end_fixed(const iterator_base& pos, unsigned int dp) const
{
assert(1==0); // FIXME: not correct yet: use is_valid() as a temporary workaround
tree_node *tmp=pos.node;
unsigned int curdepth=1;
while(curdepth<dp) { // go down one level
while(tmp->first_child==0) {
tmp=tmp->next_sibling;
if(tmp==0)
throw std::range_error("tree: end_fixed out of range");
}
tmp=tmp->first_child;
++curdepth;
}
return tmp;
}
template <class T, class tree_node_allocator>
typename tree<T, tree_node_allocator>::sibling_iterator tree<T, tree_node_allocator>::begin(const iterator_base& pos) const
{
assert(pos.node!=0);
if(pos.node->first_child==0) {
return end(pos);
}
return pos.node->first_child;
}
template <class T, class tree_node_allocator>
typename tree<T, tree_node_allocator>::sibling_iterator tree<T, tree_node_allocator>::end(const iterator_base& pos) const
{
sibling_iterator ret(0);
ret.parent_=pos.node;
return ret;
}
template <class T, class tree_node_allocator>
typename tree<T, tree_node_allocator>::leaf_iterator tree<T, tree_node_allocator>::begin_leaf() const
{
tree_node *tmp=head->next_sibling;
if(tmp!=feet) {
while(tmp->first_child)
tmp=tmp->first_child;
}
return leaf_iterator(tmp);
}
template <class T, class tree_node_allocator>
typename tree<T, tree_node_allocator>::leaf_iterator tree<T, tree_node_allocator>::end_leaf() const
{
return leaf_iterator(feet);
}
template <class T, class tree_node_allocator>
template <typename iter>
iter tree<T, tree_node_allocator>::parent(iter position)
{
assert(position.node!=0);
return iter(position.node->parent);
}
template <class T, class tree_node_allocator>
template <typename iter>
iter tree<T, tree_node_allocator>::previous_sibling(iter position) const
{
assert(position.node!=0);
iter ret(position);
ret.node=position.node->prev_sibling;
return ret;
}
template <class T, class tree_node_allocator>
template <typename iter>
iter tree<T, tree_node_allocator>::next_sibling(iter position) const
{
assert(position.node!=0);
iter ret(position);
ret.node=position.node->next_sibling;
return ret;
}
template <class T, class tree_node_allocator>
template <typename iter>
iter tree<T, tree_node_allocator>::next_at_same_depth(iter position) const
{
assert(position.node!=0);
iter ret(position);
if(position.node->next_sibling) {
ret.node=position.node->next_sibling;
}
else {
int relative_depth=0;
upper:
do {
ret.node=ret.node->parent;
if(ret.node==0) return ret;
--relative_depth;
} while(ret.node->next_sibling==0);
lower:
ret.node=ret.node->next_sibling;
while(ret.node->first_child==0) {
if(ret.node->next_sibling==0)
goto upper;
ret.node=ret.node->next_sibling;
if(ret.node==0) return ret;
}
while(relative_depth<0 && ret.node->first_child!=0) {
ret.node=ret.node->first_child;
++relative_depth;
}
if(relative_depth<0) {
if(ret.node->next_sibling==0) goto upper;
else goto lower;
}
}
return ret;
}
template <class T, class tree_node_allocator>
template <typename iter>
iter tree<T, tree_node_allocator>::append_child(iter position)
{
assert(position.node!=head);
assert(position.node);
tree_node *tmp=alloc_.allocate(1,0);
kp::constructor(&tmp->data);
tmp->first_child=0;
tmp->last_child=0;
tmp->parent=position.node;
if(position.node->last_child!=0) {
position.node->last_child->next_sibling=tmp;
}
else {
position.node->first_child=tmp;
}
tmp->prev_sibling=position.node->last_child;
position.node->last_child=tmp;
tmp->next_sibling=0;
return tmp;
}
template <class T, class tree_node_allocator>
template <typename iter>
iter tree<T, tree_node_allocator>::prepend_child(iter position)
{
assert(position.node!=head);
assert(position.node);
tree_node *tmp=alloc_.allocate(1,0);
kp::constructor(&tmp->data);
tmp->first_child=0;
tmp->last_child=0;
tmp->parent=position.node;
if(position.node->first_child!=0) {
position.node->first_child->prev_sibling=tmp;
}
else {
position.node->last_child=tmp;
}
tmp->next_sibling=position.node->first_child;
position.node->prev_child=tmp;
tmp->prev_sibling=0;
return tmp;
}
template <class T, class tree_node_allocator>
template <class iter>
iter tree<T, tree_node_allocator>::append_child(iter position, const T& x)
{
// If your program fails here you probably used 'append_child' to add the top
// node to an empty tree. From version 1.45 the top element should be added
// using 'insert'. See the documentation for further information, and sorry about
// the API change.
assert(position.node!=head);
assert(position.node);
tree_node* tmp = alloc_.allocate(1,0);
kp::constructor(&tmp->data, x);
tmp->first_child=0;
tmp->last_child=0;
tmp->parent=position.node;
if(position.node->last_child!=0) {
position.node->last_child->next_sibling=tmp;
}
else {
position.node->first_child=tmp;
}
tmp->prev_sibling=position.node->last_child;
position.node->last_child=tmp;
tmp->next_sibling=0;
return tmp;
}
template <class T, class tree_node_allocator>
template <class iter>
iter tree<T, tree_node_allocator>::prepend_child(iter position, const T& x)
{
assert(position.node!=head);
assert(position.node);
tree_node* tmp = alloc_.allocate(1,0);
kp::constructor(&tmp->data, x);
tmp->first_child=0;
tmp->last_child=0;
tmp->parent=position.node;
if(position.node->first_child!=0) {
position.node->first_child->prev_sibling=tmp;
}
else {
position.node->last_child=tmp;
}
tmp->next_sibling=position.node->first_child;
position.node->first_child=tmp;
tmp->prev_sibling=0;
return tmp;
}
template <class T, class tree_node_allocator>
template <class iter>
iter tree<T, tree_node_allocator>::append_child(iter position, iter other)
{
assert(position.node!=head);
assert(position.node);
sibling_iterator aargh=append_child(position, value_type());
return replace(aargh, other);
}
template <class T, class tree_node_allocator>
template <class iter>
iter tree<T, tree_node_allocator>::prepend_child(iter position, iter other)
{
assert(position.node!=head);
assert(position.node);
sibling_iterator aargh=prepend_child(position, value_type());
return replace(aargh, other);
}
template <class T, class tree_node_allocator>
template <class iter>
iter tree<T, tree_node_allocator>::append_children(iter position, sibling_iterator from, sibling_iterator to)
{
assert(position.node!=head);
assert(position.node);
iter ret=from;
while(from!=to) {
insert_subtree(position.end(), from);
++from;
}
return ret;
}
template <class T, class tree_node_allocator>
template <class iter>
iter tree<T, tree_node_allocator>::prepend_children(iter position, sibling_iterator from, sibling_iterator to)
{
assert(position.node!=head);
assert(position.node);
iter ret=from;
while(from!=to) {
insert_subtree(position.begin(), from);
++from;
}
return ret;
}
template <class T, class tree_node_allocator>
typename tree<T, tree_node_allocator>::pre_order_iterator tree<T, tree_node_allocator>::set_head(const T& x)
{
assert(head->next_sibling==feet);
return insert(iterator(feet), x);
}
template <class T, class tree_node_allocator>
template <class iter>
iter tree<T, tree_node_allocator>::insert(iter position, const T& x)
{
if(position.node==0) {
position.node=feet; // Backward compatibility: when calling insert on a null node,
// insert before the feet.
}
tree_node* tmp = alloc_.allocate(1,0);
kp::constructor(&tmp->data, x);