This repository has been archived by the owner on Sep 24, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 103
/
ntree.tpp
961 lines (847 loc) · 31.5 KB
/
ntree.tpp
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
////////////////////////////////////////////////////////////////////////////////
// Author: Andy Rushton
// Copyright: (c) Southampton University 1999-2004
// (c) Andy Rushton 2004 onwards
// License: BSD License, see ../docs/license.html
////////////////////////////////////////////////////////////////////////////////
#include <vector>
#include <algorithm>
namespace stlplus
{
////////////////////////////////////////////////////////////////////////////////
// ntree_node
template<typename T>
class ntree_node
{
public:
master_iterator<ntree<T>, ntree_node<T> > m_master;
T m_data;
ntree_node<T>* m_parent;
std::vector<ntree_node<T>*> m_children;
public:
ntree_node(const ntree<T>* owner, const T& data = T()) :
m_master(owner,this), m_data(data), m_parent(0)
{
}
void change_owner(const ntree<T>* owner)
{
m_master.change_owner(owner);
for (TYPENAME std::vector<ntree_node<T>*>::iterator i = m_children.begin(); i != m_children.end(); i++)
(*i)->change_owner(owner);
}
~ntree_node(void)
{
m_parent = 0;
for (TYPENAME std::vector<ntree_node<T>*>::iterator i = m_children.begin(); i != m_children.end(); i++)
delete *i;
}
};
template<typename T>
static ntree_node<T>* ntree_copy(const ntree<T>* new_owner, ntree_node<T>* root)
{
if (!root) return 0;
ntree_node<T>* new_tree = new ntree_node<T>(new_owner, root->m_data);
for (TYPENAME std::vector<ntree_node<T>*>::iterator i = root->m_children.begin(); i != root->m_children.end(); i++)
{
ntree_node<T>* new_child = ntree_copy(new_owner, *i);
new_tree->m_children.push_back(new_child);
new_child->m_parent = new_tree;
}
return new_tree;
}
template<typename T>
static unsigned ntree_size(ntree_node<T>* root)
{
if (!root) return 0;
unsigned result = 1;
for (TYPENAME std::vector<ntree_node<T>*>::iterator i = root->m_children.begin(); i != root->m_children.end(); i++)
result += ntree_size(*i);
return result;
}
template<typename T>
static unsigned ntree_depth(ntree_node<T>* root)
{
unsigned depth = 0;
for (ntree_node<T>* i = root; i; i = i->m_parent)
depth++;
return depth;
}
////////////////////////////////////////////////////////////////////////////////
// ntree_iterator
// constructor to create a null iterator - you must assign a valid value to this iterator before using it
template<typename T, typename TRef, typename TPtr>
ntree_iterator<T,TRef,TPtr>::ntree_iterator(void)
{
}
// used to create an alias of an iterator
template<typename T, typename TRef, typename TPtr>
ntree_iterator<T,TRef,TPtr>::ntree_iterator(const safe_iterator<ntree<T>, ntree_node<T> >& iterator) :
safe_iterator<ntree<T>,ntree_node<T> >(iterator)
{
}
// constructor used by ntree to create a non-null iterator
template<typename T, typename TRef, typename TPtr>
ntree_iterator<T,TRef,TPtr>::ntree_iterator(ntree_node<T>* node) :
safe_iterator<ntree<T>,ntree_node<T> >(node->m_master)
{
}
// constructor used by ntree to create an end iterator
template<typename T, typename TRef, typename TPtr>
ntree_iterator<T,TRef,TPtr>::ntree_iterator(const ntree<T>* owner) :
safe_iterator<ntree<T>,ntree_node<T> >(owner)
{
}
// destructor
template<typename T, typename TRef, typename TPtr>
ntree_iterator<T,TRef,TPtr>::~ntree_iterator(void)
{
}
template<typename T, typename TRef, typename TPtr>
TYPENAME ntree_iterator<T,TRef,TPtr>::const_iterator ntree_iterator<T,TRef,TPtr>::constify(void) const
{
return ntree_iterator<T,const T&,const T*>(*this);
}
template<typename T, typename TRef, typename TPtr>
TYPENAME ntree_iterator<T,TRef,TPtr>::iterator ntree_iterator<T,TRef,TPtr>::deconstify(void) const
{
return ntree_iterator<T,T&,T*>(*this);
}
template<typename T, typename TRef, typename TPtr>
bool ntree_iterator<T,TRef,TPtr>::operator == (const TYPENAME ntree_iterator<T,TRef,TPtr>::this_iterator& r) const
{
return equal(r);
}
template<typename T, typename TRef, typename TPtr>
bool ntree_iterator<T,TRef,TPtr>::operator != (const TYPENAME ntree_iterator<T,TRef,TPtr>::this_iterator& r) const
{
return !operator==(r);
}
template<typename T, typename TRef, typename TPtr>
bool ntree_iterator<T,TRef,TPtr>::operator < (const TYPENAME ntree_iterator<T,TRef,TPtr>::this_iterator& r) const
{
return compare(r) < 0;
}
template<typename T, typename TRef, typename TPtr>
TYPENAME ntree_iterator<T,TRef,TPtr>::reference ntree_iterator<T,TRef,TPtr>::operator*(void) const
throw(null_dereference,end_dereference)
{
this->assert_valid();
return this->node()->m_data;
}
template<typename T, typename TRef, typename TPtr>
TYPENAME ntree_iterator<T,TRef,TPtr>::pointer ntree_iterator<T,TRef,TPtr>::operator->(void) const
throw(null_dereference,end_dereference)
{
return &(operator*());
}
////////////////////////////////////////////////////////////////////////////////
// ntree_prefix_iterator
template<typename T, typename TRef, typename TPtr>
ntree_prefix_iterator<T,TRef,TPtr>::ntree_prefix_iterator(void)
{
}
template<typename T, typename TRef, typename TPtr>
ntree_prefix_iterator<T,TRef,TPtr>::~ntree_prefix_iterator(void)
{
}
template<typename T, typename TRef, typename TPtr>
ntree_prefix_iterator<T,TRef,TPtr>::ntree_prefix_iterator(const ntree_iterator<T,TRef,TPtr>& i) :
m_iterator(i)
{
// this is initialised with the root node
// which is also the first node in prefix traversal order
}
template<typename T, typename TRef, typename TPtr>
bool ntree_prefix_iterator<T,TRef,TPtr>::null(void) const
{
return m_iterator.null();
}
template<typename T, typename TRef, typename TPtr>
bool ntree_prefix_iterator<T,TRef,TPtr>::end(void) const
{
return m_iterator.end();
}
template<typename T, typename TRef, typename TPtr>
bool ntree_prefix_iterator<T,TRef,TPtr>::valid(void) const
{
return m_iterator.valid();
}
template<typename T, typename TRef, typename TPtr>
TYPENAME ntree_prefix_iterator<T,TRef,TPtr>::const_iterator ntree_prefix_iterator<T,TRef,TPtr>::constify(void) const
{
return ntree_prefix_iterator<T,const T&,const T*>(m_iterator);
}
template<typename T, typename TRef, typename TPtr>
TYPENAME ntree_prefix_iterator<T,TRef,TPtr>::iterator ntree_prefix_iterator<T,TRef,TPtr>::deconstify(void) const
{
return ntree_prefix_iterator<T,T&,T*>(m_iterator);
}
template<typename T, typename TRef, typename TPtr>
ntree_iterator<T,TRef,TPtr> ntree_prefix_iterator<T,TRef,TPtr>::simplify(void) const
{
return m_iterator;
}
template<typename T, typename TRef, typename TPtr>
bool ntree_prefix_iterator<T,TRef,TPtr>::operator == (const TYPENAME ntree_prefix_iterator<T,TRef,TPtr>::this_iterator& r) const
{
return m_iterator == r.m_iterator;
}
template<typename T, typename TRef, typename TPtr>
bool ntree_prefix_iterator<T,TRef,TPtr>::operator != (const TYPENAME ntree_prefix_iterator<T,TRef,TPtr>::this_iterator& r) const
{
return m_iterator != r.m_iterator;
}
template<typename T, typename TRef, typename TPtr>
bool ntree_prefix_iterator<T,TRef,TPtr>::operator < (const TYPENAME ntree_prefix_iterator<T,TRef,TPtr>::this_iterator& r) const
{
return m_iterator < r.m_iterator;
}
template<typename T, typename TRef, typename TPtr>
TYPENAME ntree_prefix_iterator<T,TRef,TPtr>::this_iterator& ntree_prefix_iterator<T,TRef,TPtr>::operator ++ (void)
throw(null_dereference,end_dereference)
{
// pre-increment operator
// algorithm: if there are any children, visit child 0, otherwise, go to
// parent and deduce which child the start node was of that parent - if
// there are further children, go into the next one. Otherwise, go up the
// tree and test again for further children. Return null if there are no
// further nodes
m_iterator.assert_valid();
ntree_node<T>* old_node = m_iterator.node();
if (!old_node->m_children.empty())
{
// simply take the first child of this node
m_iterator.set(old_node->m_children[0]->m_master);
}
else
{
// this loop walks up the parent pointers
// either it will walk off the top and exit or a new node will be found and the loop will exit
for (;;)
{
// go up a level
ntree_node<T>* parent = old_node->m_parent;
if (!parent)
{
// we've walked off the top of the tree, so return end
m_iterator.set_end();
break;
}
else
{
// otherwise walk down the next child - if there is one
// find which index the old node was relative to this node
TYPENAME std::vector<ntree_node<T>*>::iterator found =
std::find(parent->m_children.begin(), parent->m_children.end(), old_node);
// if this was found, then see if there is another and if so return that
found++;
if (found != parent->m_children.end())
{
// visit the next child
m_iterator.set((*found)->m_master);
break;
}
else
{
// keep going up
old_node = parent;
}
}
}
}
return *this;
}
template<typename T, typename TRef, typename TPtr>
TYPENAME ntree_prefix_iterator<T,TRef,TPtr>::this_iterator ntree_prefix_iterator<T,TRef,TPtr>::operator ++ (int)
throw(null_dereference,end_dereference)
{
// post-increment is defined in terms of the pre-increment
ntree_prefix_iterator<T,TRef,TPtr> result(*this);
++(*this);
return result;
}
template<typename T, typename TRef, typename TPtr>
TYPENAME ntree_prefix_iterator<T,TRef,TPtr>::reference ntree_prefix_iterator<T,TRef,TPtr>::operator*(void) const
throw(null_dereference,end_dereference)
{
return m_iterator.operator*();
}
template<typename T, typename TRef, typename TPtr>
TYPENAME ntree_prefix_iterator<T,TRef,TPtr>::pointer ntree_prefix_iterator<T,TRef,TPtr>::operator->(void) const
throw(null_dereference,end_dereference)
{
return m_iterator.operator->();
}
template<typename T, typename TRef, typename TPtr>
const ntree_iterator<T,TRef,TPtr>& ntree_prefix_iterator<T,TRef,TPtr>::get_iterator(void) const
{
return m_iterator;
}
template<typename T, typename TRef, typename TPtr>
ntree_iterator<T,TRef,TPtr>& ntree_prefix_iterator<T,TRef,TPtr>::get_iterator(void)
{
return m_iterator;
}
////////////////////////////////////////////////////////////////////////////////
// ntree_postfix_iterator
template<typename T, typename TRef, typename TPtr>
ntree_postfix_iterator<T,TRef,TPtr>::ntree_postfix_iterator(void)
{
}
template<typename T, typename TRef, typename TPtr>
ntree_postfix_iterator<T,TRef,TPtr>::~ntree_postfix_iterator(void)
{
}
template<typename T, typename TRef, typename TPtr>
ntree_postfix_iterator<T,TRef,TPtr>::ntree_postfix_iterator(const ntree_iterator<T,TRef,TPtr>& i) :
m_iterator(i)
{
// this is initialised with the root node
// initially traverse to the first node to be visited
if (m_iterator.valid())
{
ntree_node<T>* node = m_iterator.node();
while (!node->m_children.empty())
node = node->m_children[0];
m_iterator.set(node->m_master);
}
}
template<typename T, typename TRef, typename TPtr>
bool ntree_postfix_iterator<T,TRef,TPtr>::null(void) const
{
return m_iterator.null();
}
template<typename T, typename TRef, typename TPtr>
bool ntree_postfix_iterator<T,TRef,TPtr>::end(void) const
{
return m_iterator.end();
}
template<typename T, typename TRef, typename TPtr>
bool ntree_postfix_iterator<T,TRef,TPtr>::valid(void) const
{
return m_iterator.valid();
}
template<typename T, typename TRef, typename TPtr>
TYPENAME ntree_postfix_iterator<T,TRef,TPtr>::const_iterator ntree_postfix_iterator<T,TRef,TPtr>::constify(void) const
{
return ntree_postfix_iterator<T,const T&,const T*>(m_iterator);
}
template<typename T, typename TRef, typename TPtr>
TYPENAME ntree_postfix_iterator<T,TRef,TPtr>::iterator ntree_postfix_iterator<T,TRef,TPtr>::deconstify(void) const
{
return ntree_postfix_iterator<T,T&,T*>(m_iterator);
}
template<typename T, typename TRef, typename TPtr>
ntree_iterator<T,TRef,TPtr> ntree_postfix_iterator<T,TRef,TPtr>::simplify(void) const
{
return m_iterator;
}
template<typename T, typename TRef, typename TPtr>
bool ntree_postfix_iterator<T,TRef,TPtr>::operator == (const TYPENAME ntree_postfix_iterator<T,TRef,TPtr>::this_iterator& r) const
{
return m_iterator == r.m_iterator;
}
template<typename T, typename TRef, typename TPtr>
bool ntree_postfix_iterator<T,TRef,TPtr>::operator != (const TYPENAME ntree_postfix_iterator<T,TRef,TPtr>::this_iterator& r) const
{
return m_iterator != r.m_iterator;
}
template<typename T, typename TRef, typename TPtr>
bool ntree_postfix_iterator<T,TRef,TPtr>::operator < (const TYPENAME ntree_postfix_iterator<T,TRef,TPtr>::this_iterator& r) const
{
return m_iterator < r.m_iterator;
}
template<typename T, typename TRef, typename TPtr>
TYPENAME ntree_postfix_iterator<T,TRef,TPtr>::this_iterator& ntree_postfix_iterator<T,TRef,TPtr>::operator ++ (void)
throw(null_dereference,end_dereference)
{
// pre-increment operator
// algorithm: this node has been visited, therefore all children must have
// already been visited. So go to parent. Return null if the parent is null.
// Otherwise deduce which child the start node was of that parent - if there
// are further children, go into the next one and then walk down any
// subsequent first-child pointers to the bottom. Otherwise, if there are no
// children then the parent node is the next in the traversal.
m_iterator.assert_valid();
// go up a level
ntree_node<T>* old_node = m_iterator.node();
ntree_node<T>* parent = old_node->m_parent;
if (!parent)
{
// we've walked off the top of the tree, so return end
m_iterator.set_end();
}
else
{
// otherwise find which index the old node was relative to this node
TYPENAME std::vector<ntree_node<T>*>::iterator found =
std::find(parent->m_children.begin(), parent->m_children.end(), old_node);
// if this was found, then see if there is another
found++;
if (found != parent->m_children.end())
{
// if so traverse to it and walk down the leftmost child pointers to the bottom of the new sub-tree
ntree_node<T>* new_node = *found;
while (!new_node->m_children.empty())
new_node = new_node->m_children[0];
m_iterator.set(new_node->m_master);
}
else
{
// the parent's children have all been visited - so the parent is visited
m_iterator.set(parent->m_master);
}
}
return *this;
}
template<typename T, typename TRef, typename TPtr>
TYPENAME ntree_postfix_iterator<T,TRef,TPtr>::this_iterator ntree_postfix_iterator<T,TRef,TPtr>::operator ++ (int)
throw(null_dereference,end_dereference)
{
// post-increment is defined in terms of the pre-increment
ntree_postfix_iterator<T,TRef,TPtr> result(*this);
++(*this);
return result;
}
template<typename T, typename TRef, typename TPtr>
TYPENAME ntree_postfix_iterator<T,TRef,TPtr>::reference ntree_postfix_iterator<T,TRef,TPtr>::operator*(void) const
throw(null_dereference,end_dereference)
{
return m_iterator.operator*();
}
template<typename T, typename TRef, typename TPtr>
TYPENAME ntree_postfix_iterator<T,TRef,TPtr>::pointer ntree_postfix_iterator<T,TRef,TPtr>::operator->(void) const
throw(null_dereference,end_dereference)
{
return m_iterator.operator->();
}
template<typename T, typename TRef, typename TPtr>
const ntree_iterator<T,TRef,TPtr>& ntree_postfix_iterator<T,TRef,TPtr>::get_iterator(void) const
{
return m_iterator;
}
template<typename T, typename TRef, typename TPtr>
ntree_iterator<T,TRef,TPtr>& ntree_postfix_iterator<T,TRef,TPtr>::get_iterator(void)
{
return m_iterator;
}
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
// ntree
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
template<typename T>
ntree<T>::ntree(void) : m_root(0)
{
}
template<typename T>
ntree<T>::~ntree(void)
{
if (m_root) delete m_root;
}
template<typename T>
ntree<T>::ntree(const ntree<T>& r) : m_root(0)
{
*this = r;
}
template<typename T>
ntree<T>& ntree<T>::operator=(const ntree<T>& r)
{
if (m_root) delete m_root;
m_root = ntree_copy(this, r.m_root);
return *this;
}
template<typename T>
bool ntree<T>::empty(void) const
{
return m_root == 0;
}
template<typename T>
unsigned ntree<T>::size(void) const
{
return ntree_size(m_root);
}
template<typename T>
unsigned ntree<T>::size(const TYPENAME ntree<T>::const_iterator& i) const
throw(wrong_object,null_dereference,end_dereference)
{
i.assert_valid(this);
return ntree_size(i.node());
}
template<typename T>
unsigned ntree<T>::size(const TYPENAME ntree<T>::iterator& i)
throw(wrong_object,null_dereference,end_dereference)
{
i.assert_valid(this);
return ntree_size(i.node());
}
template<typename T>
unsigned ntree<T>::depth(const TYPENAME ntree<T>::const_iterator& i) const
throw(wrong_object,null_dereference,end_dereference)
{
i.assert_valid(this);
return ntree_depth(i.node());
}
template<typename T>
unsigned ntree<T>::depth(const TYPENAME ntree<T>::iterator& i)
throw(wrong_object,null_dereference,end_dereference)
{
i.assert_valid(this);
return ntree_depth(i.node());
}
template<typename T>
TYPENAME ntree<T>::const_iterator ntree<T>::root(void) const
{
if (!m_root) return ntree_iterator<T,const T&,const T*>(this);
return ntree_iterator<T,const T&,const T*>(m_root);
}
template<typename T>
TYPENAME ntree<T>::iterator ntree<T>::root(void)
{
if (!m_root) return ntree_iterator<T,T&,T*>(this);
return ntree_iterator<T,T&,T*>(m_root);
}
template<typename T>
unsigned ntree<T>::children(const TYPENAME ntree<T>::const_iterator& i) const
throw(wrong_object,null_dereference,end_dereference)
{
i.assert_valid(this);
return i.node()->m_children.size();
}
template<typename T>
unsigned ntree<T>::children(const ntree_iterator<T,T&,T*>& i)
throw(wrong_object,null_dereference,end_dereference)
{
i.assert_valid(this);
return i.node()->m_children.size();
}
template<typename T>
TYPENAME ntree<T>::const_iterator ntree<T>::child(const TYPENAME ntree<T>::const_iterator& i, unsigned child) const
throw(wrong_object,null_dereference,end_dereference,std::out_of_range)
{
i.assert_valid(this);
if (child >= children(i)) throw std::out_of_range("stlplus::ntree");
return ntree_iterator<T,const T&,const T*>(i.node()->m_children[child]);
}
template<typename T>
TYPENAME ntree<T>::iterator ntree<T>::child(const TYPENAME ntree<T>::iterator& i, unsigned child)
throw(wrong_object,null_dereference,end_dereference,std::out_of_range)
{
i.assert_valid(this);
if (child >= children(i)) throw std::out_of_range("stlplus::ntree");
return ntree_iterator<T,T&,T*>(i.node()->m_children[child]);
}
template<typename T>
TYPENAME ntree<T>::const_iterator ntree<T>::parent(const TYPENAME ntree<T>::const_iterator& i) const
throw(wrong_object,null_dereference,end_dereference)
{
i.assert_valid(this);
ntree_node<T>* parent = i.node()->m_parent;
if (!parent) return ntree_iterator<T,const T&,const T*>(this);
return ntree_iterator<T,const T&,const T*>(parent);
}
template<typename T>
TYPENAME ntree<T>::iterator ntree<T>::parent(const TYPENAME ntree<T>::iterator& i)
throw(wrong_object,null_dereference,end_dereference)
{
i.assert_valid(this);
ntree_node<T>* parent = i.node()->m_parent;
if (!parent) return ntree_iterator<T,T&,T*>(this);
return ntree_iterator<T,T&,T*>(parent);
}
template<typename T>
TYPENAME ntree<T>::const_prefix_iterator ntree<T>::prefix_begin(void) const
{
return ntree_prefix_iterator<T,const T&,const T*>(root());
}
template<typename T>
TYPENAME ntree<T>::prefix_iterator ntree<T>::prefix_begin(void)
{
return ntree_prefix_iterator<T,T&,T*>(root());
}
template<typename T>
TYPENAME ntree<T>::const_prefix_iterator ntree<T>::prefix_end(void) const
{
return ntree_prefix_iterator<T,const T&,const T*>(ntree_iterator<T,const T&,const T*>(this));
}
template<typename T>
TYPENAME ntree<T>::prefix_iterator ntree<T>::prefix_end(void)
{
return ntree_prefix_iterator<T,T&,T*>(ntree_iterator<T,T&,T*>(this));
}
template<typename T>
TYPENAME ntree<T>::const_postfix_iterator ntree<T>::postfix_begin(void) const
{
return ntree_postfix_iterator<T,const T&,const T*>(root());
}
template<typename T>
TYPENAME ntree<T>::postfix_iterator ntree<T>::postfix_begin(void)
{
return ntree_postfix_iterator<T,T&,T*>(root());
}
template<typename T>
TYPENAME ntree<T>::const_postfix_iterator ntree<T>::postfix_end(void) const
{
return ntree_postfix_iterator<T,const T&,const T*>(ntree_iterator<T,const T&,const T*>(this));
}
template<typename T>
TYPENAME ntree<T>::postfix_iterator ntree<T>::postfix_end(void)
{
return ntree_postfix_iterator<T,T&,T*>(ntree_iterator<T,T&,T*>(this));
}
template<typename T>
TYPENAME ntree<T>::iterator ntree<T>::insert(const T& data)
{
// insert a new node as the root
erase();
m_root = new ntree_node<T>(this,data);
return ntree_iterator<T,T&,T*>(m_root);
}
template<typename T>
TYPENAME ntree<T>::iterator ntree<T>::insert(const TYPENAME ntree<T>::iterator& i, unsigned offset, const T& data)
throw(wrong_object,null_dereference,end_dereference,std::out_of_range)
{
// if i is the end iterator, this means insert a new root
// if (i.end())
// return insert(data);
// otherwise, insert a new child
i.assert_valid(this);
if (offset > children(i)) throw std::out_of_range("stlplus::ntree");
ntree_node<T>* new_node = new ntree_node<T>(this,data);
i.node()->m_children.insert(i.node()->m_children.begin()+offset,new_node);
new_node->m_parent = i.node();
return ntree_iterator<T,T&,T*>(new_node);
}
template<typename T>
TYPENAME ntree<T>::iterator ntree<T>::insert(const TYPENAME ntree<T>::iterator& i, const T& data)
throw(wrong_object,null_dereference,end_dereference)
{
return insert(i, children(i), data);
}
template<typename T>
TYPENAME ntree<T>::iterator ntree<T>::append(const TYPENAME ntree<T>::iterator& i, const T& data)
throw(wrong_object,null_dereference,end_dereference)
{
return insert(i, children(i), data);
}
template<typename T>
TYPENAME ntree<T>::iterator ntree<T>::insert(const ntree<T>& tree)
{
// insert a whole tree as root
erase();
m_root = ntree_copy(this, tree.m_root);
return ntree_iterator<T,T&,T*>(m_root);
}
template<typename T>
TYPENAME ntree<T>::iterator ntree<T>::insert(const TYPENAME ntree<T>::iterator& i, unsigned offset, const ntree<T>& tree)
throw(wrong_object,null_dereference,end_dereference,std::out_of_range)
{
// insert a whole tree as a child of i
i.assert_valid(this);
if (offset > children(i)) throw std::out_of_range("stlplus::ntree");
ntree_node<T>* new_node = ntree_copy(this, tree.m_root);
i.node()->m_children.insert(i.node()->m_children.begin()+offset,new_node);
new_node->m_parent = i.node();
return ntree_iterator<T,T&,T*>(new_node);
}
template<typename T>
TYPENAME ntree<T>::iterator ntree<T>::insert(const TYPENAME ntree<T>::iterator& i, const ntree<T>& tree)
throw(wrong_object,null_dereference,end_dereference)
{
return insert(i, children(i), tree);
}
template<typename T>
TYPENAME ntree<T>::iterator ntree<T>::append(const TYPENAME ntree<T>::iterator& i, const ntree<T>& tree)
throw(wrong_object,null_dereference,end_dereference)
{
return insert(i, children(i), tree);
}
template<typename T>
TYPENAME ntree<T>::iterator ntree<T>::move(ntree<T>& tree)
{
// insert a whole tree as root, removing it from source
erase();
m_root = tree.m_root;
tree.m_root = 0;
if (m_root) m_root->change_owner(this);
return ntree_iterator<T,T&,T*>(m_root);
}
template<typename T>
TYPENAME ntree<T>::iterator ntree<T>::move(const TYPENAME ntree<T>::iterator& i, unsigned offset, ntree<T>& tree)
throw(wrong_object,null_dereference,end_dereference,std::out_of_range)
{
// insert a whole tree as a child of i
i.assert_valid(this);
if (offset > children(i)) throw std::out_of_range("stlplus::ntree");
ntree_node<T>* new_node = tree.m_root;
tree.m_root = 0;
if (new_node) new_node->change_owner(this);
i.node()->m_children.insert(i.node()->m_children.begin()+offset,new_node);
new_node->m_parent = i.node();
return ntree_iterator<T,T&,T*>(new_node);
}
template<typename T>
TYPENAME ntree<T>::iterator ntree<T>::move(const TYPENAME ntree<T>::iterator& i, ntree<T>& tree)
throw(wrong_object,null_dereference,end_dereference)
{
return move(i, children(i), tree);
}
template<typename T>
TYPENAME ntree<T>::iterator ntree<T>::push(const TYPENAME ntree<T>::iterator& node, const T& data)
throw(wrong_object,null_dereference,end_dereference)
{
// insert a new node to replace the existing node in the tree
// making the original node the child of the new node
// i.e. (node) becomes (new)->(node)
// afterwards, the iterator still points to the old node, now the child
// returns the iterator to the new node
node.assert_valid(this);
ntree_node<T>* new_node = new ntree_node<T>(this,data);
if (node.node() == m_root)
{
// pushing the root node
m_root = new_node;
new_node->m_parent = 0;
}
else
{
// pushing a sub-node
*(std::find(node.node()->m_parent->m_children.begin(), node.node()->m_parent->m_children.end(), node.node())) = new_node;
new_node->m_parent = node.node()->m_parent;
}
// link up the old node as the child of the new node
new_node->m_children.insert(new_node->m_children.begin(),node.node());
node.node()->m_parent = new_node;
return ntree_iterator<T,T&,T*>(new_node);
}
template<typename T>
void ntree<T>::pop(const TYPENAME ntree<T>::iterator& parent, unsigned offset)
throw(wrong_object,null_dereference,end_dereference)
{
// inverse of push
// removes the specified child of the parent node, adding its children to the parent node at the same offset
parent.assert_valid(this);
ntree_node<T>* node = parent.node();
if (offset >= node->m_children.size()) throw std::out_of_range("stlplus::ntree");
// move the grandchildren first
ntree_node<T>* child = parent.node()->m_children[offset];
while (!child->m_children.empty())
{
// remove the last grandchild and insert into node just after the child to be removed
ntree_node<T>* grandchild = child->m_children[child->m_children.size()-1];
child->m_children.pop_back();
node->m_children.insert(node->m_children.begin()+offset+1, grandchild);
grandchild->m_parent = node;
}
// now remove the child
node->m_children.erase(node->m_children.begin()+offset);
delete child;
}
template<typename T>
void ntree<T>::erase(void)
{
// erase the whole tree
erase(root());
}
template<typename T>
void ntree<T>::erase(const TYPENAME ntree<T>::iterator& i)
throw(wrong_object,null_dereference,end_dereference)
{
if (!i.end())
{
// erase this node and its subtree
// do this by erasing this child of its parent
// handle the case of erasing the root
i.assert_valid(this);
ntree_node<T>* node = i.node();
if (node == m_root)
{
delete m_root;
m_root = 0;
}
else
{
ntree_node<T>* parent = node->m_parent;
// impossible for parent to be null - should assert this
TYPENAME std::vector<ntree_node<T>*>::iterator found =
std::find(parent->m_children.begin(), parent->m_children.end(), node);
// impossible for find to fail - should assert this
parent->m_children.erase(found);
delete node;
}
}
}
template<typename T>
void ntree<T>::erase(const TYPENAME ntree<T>::iterator& i, unsigned offset)
throw(wrong_object,null_dereference,end_dereference,std::out_of_range)
{
erase(child(i, offset));
}
template<typename T>
ntree<T> ntree<T>::subtree(void)
{
return subtree(root());
}
template<typename T>
ntree<T> ntree<T>::subtree(const TYPENAME ntree<T>::iterator& i)
throw(wrong_object,null_dereference,end_dereference)
{
ntree<T> result;
if (!i.end())
{
i.assert_valid(this);
result.m_root = ntree_copy(&result, i.node());
}
return result;
}
template<typename T>
ntree<T> ntree<T>::subtree(const TYPENAME ntree<T>::iterator& i, unsigned offset)
throw(wrong_object,null_dereference,end_dereference,std::out_of_range)
{
return subtree(child(i, offset));
}
template<typename T>
ntree<T> ntree<T>::cut(void)
{
return cut(root());
}
template<typename T>
ntree<T> ntree<T>::cut(const TYPENAME ntree<T>::iterator& i)
throw(wrong_object,null_dereference,end_dereference)
{
ntree<T> result;
if (!i.end())
{
i.assert_valid(this);
ntree_node<T>* node = i.node();
if (node == m_root)
{
result.m_root = m_root;
m_root = 0;
}
else
{
ntree_node<T>* parent = node->m_parent;
// impossible for parent to be null - should assert this
TYPENAME std::vector<ntree_node<T>*>::iterator found =
std::find(parent->m_children.begin(), parent->m_children.end(), node);
// impossible for find to fail - should assert this
result.m_root = *found;
parent->m_children.erase(found);
}
if (result.m_root)
{
result.m_root->m_parent = 0;
result.m_root->change_owner(&result);
}
}
return result;
}
template<typename T>
ntree<T> ntree<T>::cut(const TYPENAME ntree<T>::iterator& i, unsigned offset)
throw(wrong_object,null_dereference,end_dereference,std::out_of_range)
{
return cut(child(i, offset));
}
////////////////////////////////////////////////////////////////////////////////
} // end namespace stlplus