1 ////////////////////////////////////////////////////////////////////////////////
3 // Author: Andy Rushton
4 // Copyright: (c) Southampton University 1999-2004
5 // (c) Andy Rushton 2004-2009
6 // License: BSD License, see ../docs/license.html
8 ////////////////////////////////////////////////////////////////////////////////
15 ////////////////////////////////////////////////////////////////////////////////
22 master_iterator<ntree<T>, ntree_node<T> > m_master;
24 ntree_node<T>* m_parent;
25 std::vector<ntree_node<T>*> m_children;
28 ntree_node(const ntree<T>* owner, const T& data = T()) :
29 m_master(owner,this), m_data(data), m_parent(0)
33 void change_owner(const ntree<T>* owner)
35 m_master.change_owner(owner);
36 for (TYPENAME std::vector<ntree_node<T>*>::iterator i = m_children.begin(); i != m_children.end(); i++)
37 (*i)->change_owner(owner);
43 for (TYPENAME std::vector<ntree_node<T>*>::iterator i = m_children.begin(); i != m_children.end(); i++)
50 static ntree_node<T>* ntree_copy(const ntree<T>* new_owner, ntree_node<T>* root)
53 ntree_node<T>* new_tree = new ntree_node<T>(new_owner, root->m_data);
54 for (TYPENAME std::vector<ntree_node<T>*>::iterator i = root->m_children.begin(); i != root->m_children.end(); i++)
56 ntree_node<T>* new_child = ntree_copy(new_owner, *i);
57 new_tree->m_children.push_back(new_child);
58 new_child->m_parent = new_tree;
64 static unsigned ntree_size(ntree_node<T>* root)
68 for (TYPENAME std::vector<ntree_node<T>*>::iterator i = root->m_children.begin(); i != root->m_children.end(); i++)
69 result += ntree_size(*i);
74 static unsigned ntree_depth(ntree_node<T>* root)
77 for (ntree_node<T>* i = root; i; i = i->m_parent)
82 ////////////////////////////////////////////////////////////////////////////////
85 // constructor to create a null iterator - you must assign a valid value to this iterator before using it
86 template<typename T, typename TRef, typename TPtr>
87 ntree_iterator<T,TRef,TPtr>::ntree_iterator(void)
91 // used to create an alias of an iterator
92 template<typename T, typename TRef, typename TPtr>
93 ntree_iterator<T,TRef,TPtr>::ntree_iterator(const safe_iterator<ntree<T>, ntree_node<T> >& iterator) :
94 safe_iterator<ntree<T>,ntree_node<T> >(iterator)
98 // constructor used by ntree to create a non-null iterator
99 template<typename T, typename TRef, typename TPtr>
100 ntree_iterator<T,TRef,TPtr>::ntree_iterator(ntree_node<T>* node) :
101 safe_iterator<ntree<T>,ntree_node<T> >(node->m_master)
105 // constructor used by ntree to create an end iterator
106 template<typename T, typename TRef, typename TPtr>
107 ntree_iterator<T,TRef,TPtr>::ntree_iterator(const ntree<T>* owner) :
108 safe_iterator<ntree<T>,ntree_node<T> >(owner)
113 template<typename T, typename TRef, typename TPtr>
114 ntree_iterator<T,TRef,TPtr>::~ntree_iterator(void)
118 template<typename T, typename TRef, typename TPtr>
119 TYPENAME ntree_iterator<T,TRef,TPtr>::const_iterator ntree_iterator<T,TRef,TPtr>::constify(void) const
121 return ntree_iterator<T,const T&,const T*>(*this);
124 template<typename T, typename TRef, typename TPtr>
125 TYPENAME ntree_iterator<T,TRef,TPtr>::iterator ntree_iterator<T,TRef,TPtr>::deconstify(void) const
127 return ntree_iterator<T,T&,T*>(*this);
130 template<typename T, typename TRef, typename TPtr>
131 bool ntree_iterator<T,TRef,TPtr>::operator == (const TYPENAME ntree_iterator<T,TRef,TPtr>::this_iterator& r) const
136 template<typename T, typename TRef, typename TPtr>
137 bool ntree_iterator<T,TRef,TPtr>::operator != (const TYPENAME ntree_iterator<T,TRef,TPtr>::this_iterator& r) const
139 return !operator==(r);
142 template<typename T, typename TRef, typename TPtr>
143 bool ntree_iterator<T,TRef,TPtr>::operator < (const TYPENAME ntree_iterator<T,TRef,TPtr>::this_iterator& r) const
145 return compare(r) < 0;
148 template<typename T, typename TRef, typename TPtr>
149 TYPENAME ntree_iterator<T,TRef,TPtr>::reference ntree_iterator<T,TRef,TPtr>::operator*(void) const
150 throw(null_dereference,end_dereference)
152 this->assert_valid();
153 return this->node()->m_data;
156 template<typename T, typename TRef, typename TPtr>
157 TYPENAME ntree_iterator<T,TRef,TPtr>::pointer ntree_iterator<T,TRef,TPtr>::operator->(void) const
158 throw(null_dereference,end_dereference)
160 return &(operator*());
163 ////////////////////////////////////////////////////////////////////////////////
164 // ntree_prefix_iterator
166 template<typename T, typename TRef, typename TPtr>
167 ntree_prefix_iterator<T,TRef,TPtr>::ntree_prefix_iterator(void)
171 template<typename T, typename TRef, typename TPtr>
172 ntree_prefix_iterator<T,TRef,TPtr>::~ntree_prefix_iterator(void)
176 template<typename T, typename TRef, typename TPtr>
177 ntree_prefix_iterator<T,TRef,TPtr>::ntree_prefix_iterator(const ntree_iterator<T,TRef,TPtr>& i) :
180 // this is initialised with the root node
181 // which is also the first node in prefix traversal order
184 template<typename T, typename TRef, typename TPtr>
185 bool ntree_prefix_iterator<T,TRef,TPtr>::null(void) const
187 return m_iterator.null();
190 template<typename T, typename TRef, typename TPtr>
191 bool ntree_prefix_iterator<T,TRef,TPtr>::end(void) const
193 return m_iterator.end();
196 template<typename T, typename TRef, typename TPtr>
197 bool ntree_prefix_iterator<T,TRef,TPtr>::valid(void) const
199 return m_iterator.valid();
202 template<typename T, typename TRef, typename TPtr>
203 TYPENAME ntree_prefix_iterator<T,TRef,TPtr>::const_iterator ntree_prefix_iterator<T,TRef,TPtr>::constify(void) const
205 return ntree_prefix_iterator<T,const T&,const T*>(m_iterator);
208 template<typename T, typename TRef, typename TPtr>
209 TYPENAME ntree_prefix_iterator<T,TRef,TPtr>::iterator ntree_prefix_iterator<T,TRef,TPtr>::deconstify(void) const
211 return ntree_prefix_iterator<T,T&,T*>(m_iterator);
214 template<typename T, typename TRef, typename TPtr>
215 ntree_iterator<T,TRef,TPtr> ntree_prefix_iterator<T,TRef,TPtr>::simplify(void) const
220 template<typename T, typename TRef, typename TPtr>
221 bool ntree_prefix_iterator<T,TRef,TPtr>::operator == (const TYPENAME ntree_prefix_iterator<T,TRef,TPtr>::this_iterator& r) const
223 return m_iterator == r.m_iterator;
226 template<typename T, typename TRef, typename TPtr>
227 bool ntree_prefix_iterator<T,TRef,TPtr>::operator != (const TYPENAME ntree_prefix_iterator<T,TRef,TPtr>::this_iterator& r) const
229 return m_iterator != r.m_iterator;
232 template<typename T, typename TRef, typename TPtr>
233 bool ntree_prefix_iterator<T,TRef,TPtr>::operator < (const TYPENAME ntree_prefix_iterator<T,TRef,TPtr>::this_iterator& r) const
235 return m_iterator < r.m_iterator;
238 template<typename T, typename TRef, typename TPtr>
239 TYPENAME ntree_prefix_iterator<T,TRef,TPtr>::this_iterator& ntree_prefix_iterator<T,TRef,TPtr>::operator ++ (void)
240 throw(null_dereference,end_dereference)
242 // pre-increment operator
243 // algorithm: if there are any children, visit child 0, otherwise, go to
244 // parent and deduce which child the start node was of that parent - if
245 // there are further children, go into the next one. Otherwise, go up the
246 // tree and test again for further children. Return null if there are no
248 m_iterator.assert_valid();
249 ntree_node<T>* old_node = m_iterator.node();
250 if (!old_node->m_children.empty())
252 // simply take the first child of this node
253 m_iterator.set(old_node->m_children[0]->m_master);
257 // this loop walks up the parent pointers
258 // either it will walk off the top and exit or a new node will be found and the loop will exit
262 ntree_node<T>* parent = old_node->m_parent;
265 // we've walked off the top of the tree, so return end
266 m_iterator.set_end();
271 // otherwise walk down the next child - if there is one
272 // find which index the old node was relative to this node
273 TYPENAME std::vector<ntree_node<T>*>::iterator found =
274 std::find(parent->m_children.begin(), parent->m_children.end(), old_node);
275 // if this was found, then see if there is another and if so return that
277 if (found != parent->m_children.end())
279 // visit the next child
280 m_iterator.set((*found)->m_master);
294 template<typename T, typename TRef, typename TPtr>
295 TYPENAME ntree_prefix_iterator<T,TRef,TPtr>::this_iterator ntree_prefix_iterator<T,TRef,TPtr>::operator ++ (int)
296 throw(null_dereference,end_dereference)
298 // post-increment is defined in terms of the pre-increment
299 ntree_prefix_iterator<T,TRef,TPtr> result(*this);
304 template<typename T, typename TRef, typename TPtr>
305 TYPENAME ntree_prefix_iterator<T,TRef,TPtr>::reference ntree_prefix_iterator<T,TRef,TPtr>::operator*(void) const
306 throw(null_dereference,end_dereference)
308 return m_iterator.operator*();
311 template<typename T, typename TRef, typename TPtr>
312 TYPENAME ntree_prefix_iterator<T,TRef,TPtr>::pointer ntree_prefix_iterator<T,TRef,TPtr>::operator->(void) const
313 throw(null_dereference,end_dereference)
315 return m_iterator.operator->();
318 template<typename T, typename TRef, typename TPtr>
319 const ntree_iterator<T,TRef,TPtr>& ntree_prefix_iterator<T,TRef,TPtr>::get_iterator(void) const
324 template<typename T, typename TRef, typename TPtr>
325 ntree_iterator<T,TRef,TPtr>& ntree_prefix_iterator<T,TRef,TPtr>::get_iterator(void)
330 ////////////////////////////////////////////////////////////////////////////////
331 // ntree_postfix_iterator
333 template<typename T, typename TRef, typename TPtr>
334 ntree_postfix_iterator<T,TRef,TPtr>::ntree_postfix_iterator(void)
338 template<typename T, typename TRef, typename TPtr>
339 ntree_postfix_iterator<T,TRef,TPtr>::~ntree_postfix_iterator(void)
343 template<typename T, typename TRef, typename TPtr>
344 ntree_postfix_iterator<T,TRef,TPtr>::ntree_postfix_iterator(const ntree_iterator<T,TRef,TPtr>& i) :
347 // this is initialised with the root node
348 // initially traverse to the first node to be visited
349 if (m_iterator.valid())
351 ntree_node<T>* node = m_iterator.node();
352 while (!node->m_children.empty())
353 node = node->m_children[0];
354 m_iterator.set(node->m_master);
358 template<typename T, typename TRef, typename TPtr>
359 bool ntree_postfix_iterator<T,TRef,TPtr>::null(void) const
361 return m_iterator.null();
364 template<typename T, typename TRef, typename TPtr>
365 bool ntree_postfix_iterator<T,TRef,TPtr>::end(void) const
367 return m_iterator.end();
370 template<typename T, typename TRef, typename TPtr>
371 bool ntree_postfix_iterator<T,TRef,TPtr>::valid(void) const
373 return m_iterator.valid();
376 template<typename T, typename TRef, typename TPtr>
377 TYPENAME ntree_postfix_iterator<T,TRef,TPtr>::const_iterator ntree_postfix_iterator<T,TRef,TPtr>::constify(void) const
379 return ntree_postfix_iterator<T,const T&,const T*>(m_iterator);
382 template<typename T, typename TRef, typename TPtr>
383 TYPENAME ntree_postfix_iterator<T,TRef,TPtr>::iterator ntree_postfix_iterator<T,TRef,TPtr>::deconstify(void) const
385 return ntree_postfix_iterator<T,T&,T*>(m_iterator);
388 template<typename T, typename TRef, typename TPtr>
389 ntree_iterator<T,TRef,TPtr> ntree_postfix_iterator<T,TRef,TPtr>::simplify(void) const
394 template<typename T, typename TRef, typename TPtr>
395 bool ntree_postfix_iterator<T,TRef,TPtr>::operator == (const TYPENAME ntree_postfix_iterator<T,TRef,TPtr>::this_iterator& r) const
397 return m_iterator == r.m_iterator;
400 template<typename T, typename TRef, typename TPtr>
401 bool ntree_postfix_iterator<T,TRef,TPtr>::operator != (const TYPENAME ntree_postfix_iterator<T,TRef,TPtr>::this_iterator& r) const
403 return m_iterator != r.m_iterator;
406 template<typename T, typename TRef, typename TPtr>
407 bool ntree_postfix_iterator<T,TRef,TPtr>::operator < (const TYPENAME ntree_postfix_iterator<T,TRef,TPtr>::this_iterator& r) const
409 return m_iterator < r.m_iterator;
412 template<typename T, typename TRef, typename TPtr>
413 TYPENAME ntree_postfix_iterator<T,TRef,TPtr>::this_iterator& ntree_postfix_iterator<T,TRef,TPtr>::operator ++ (void)
414 throw(null_dereference,end_dereference)
416 // pre-increment operator
417 // algorithm: this node has been visited, therefore all children must have
418 // already been visited. So go to parent. Return null if the parent is null.
419 // Otherwise deduce which child the start node was of that parent - if there
420 // are further children, go into the next one and then walk down any
421 // subsequent first-child pointers to the bottom. Otherwise, if there are no
422 // children then the parent node is the next in the traversal.
423 m_iterator.assert_valid();
425 ntree_node<T>* old_node = m_iterator.node();
426 ntree_node<T>* parent = old_node->m_parent;
429 // we've walked off the top of the tree, so return end
430 m_iterator.set_end();
434 // otherwise find which index the old node was relative to this node
435 TYPENAME std::vector<ntree_node<T>*>::iterator found =
436 std::find(parent->m_children.begin(), parent->m_children.end(), old_node);
437 // if this was found, then see if there is another
439 if (found != parent->m_children.end())
441 // if so traverse to it and walk down the leftmost child pointers to the bottom of the new sub-tree
442 ntree_node<T>* new_node = *found;
443 while (!new_node->m_children.empty())
444 new_node = new_node->m_children[0];
445 m_iterator.set(new_node->m_master);
449 // the parent's children have all been visited - so the parent is visited
450 m_iterator.set(parent->m_master);
456 template<typename T, typename TRef, typename TPtr>
457 TYPENAME ntree_postfix_iterator<T,TRef,TPtr>::this_iterator ntree_postfix_iterator<T,TRef,TPtr>::operator ++ (int)
458 throw(null_dereference,end_dereference)
460 // post-increment is defined in terms of the pre-increment
461 ntree_postfix_iterator<T,TRef,TPtr> result(*this);
466 template<typename T, typename TRef, typename TPtr>
467 TYPENAME ntree_postfix_iterator<T,TRef,TPtr>::reference ntree_postfix_iterator<T,TRef,TPtr>::operator*(void) const
468 throw(null_dereference,end_dereference)
470 return m_iterator.operator*();
473 template<typename T, typename TRef, typename TPtr>
474 TYPENAME ntree_postfix_iterator<T,TRef,TPtr>::pointer ntree_postfix_iterator<T,TRef,TPtr>::operator->(void) const
475 throw(null_dereference,end_dereference)
477 return m_iterator.operator->();
480 template<typename T, typename TRef, typename TPtr>
481 const ntree_iterator<T,TRef,TPtr>& ntree_postfix_iterator<T,TRef,TPtr>::get_iterator(void) const
486 template<typename T, typename TRef, typename TPtr>
487 ntree_iterator<T,TRef,TPtr>& ntree_postfix_iterator<T,TRef,TPtr>::get_iterator(void)
492 ////////////////////////////////////////////////////////////////////////////////
493 ////////////////////////////////////////////////////////////////////////////////
495 ////////////////////////////////////////////////////////////////////////////////
496 ////////////////////////////////////////////////////////////////////////////////
499 ntree<T>::ntree(void) : m_root(0)
504 ntree<T>::~ntree(void)
506 if (m_root) delete m_root;
510 ntree<T>::ntree(const ntree<T>& r) : m_root(0)
516 ntree<T>& ntree<T>::operator=(const ntree<T>& r)
518 if (m_root) delete m_root;
519 m_root = ntree_copy(this, r.m_root);
524 bool ntree<T>::empty(void) const
530 unsigned ntree<T>::size(void) const
532 return ntree_size(m_root);
536 unsigned ntree<T>::size(const TYPENAME ntree<T>::const_iterator& i) const
537 throw(wrong_object,null_dereference,end_dereference)
539 i.assert_valid(this);
540 return ntree_size(i.node());
544 unsigned ntree<T>::size(const TYPENAME ntree<T>::iterator& i)
545 throw(wrong_object,null_dereference,end_dereference)
547 i.assert_valid(this);
548 return ntree_size(i.node());
552 unsigned ntree<T>::depth(const TYPENAME ntree<T>::const_iterator& i) const
553 throw(wrong_object,null_dereference,end_dereference)
555 i.assert_valid(this);
556 return ntree_depth(i.node());
560 unsigned ntree<T>::depth(const TYPENAME ntree<T>::iterator& i)
561 throw(wrong_object,null_dereference,end_dereference)
563 i.assert_valid(this);
564 return ntree_depth(i.node());
568 TYPENAME ntree<T>::const_iterator ntree<T>::root(void) const
570 if (!m_root) return ntree_iterator<T,const T&,const T*>(this);
571 return ntree_iterator<T,const T&,const T*>(m_root);
575 TYPENAME ntree<T>::iterator ntree<T>::root(void)
577 if (!m_root) return ntree_iterator<T,T&,T*>(this);
578 return ntree_iterator<T,T&,T*>(m_root);
582 unsigned ntree<T>::children(const TYPENAME ntree<T>::const_iterator& i) const
583 throw(wrong_object,null_dereference,end_dereference)
585 i.assert_valid(this);
586 return i.node()->m_children.size();
590 unsigned ntree<T>::children(const ntree_iterator<T,T&,T*>& i)
591 throw(wrong_object,null_dereference,end_dereference)
593 i.assert_valid(this);
594 return i.node()->m_children.size();
598 TYPENAME ntree<T>::const_iterator ntree<T>::child(const TYPENAME ntree<T>::const_iterator& i, unsigned child) const
599 throw(wrong_object,null_dereference,end_dereference,std::out_of_range)
601 i.assert_valid(this);
602 if (child >= children(i)) throw std::out_of_range("stlplus::ntree");
603 return ntree_iterator<T,const T&,const T*>(i.node()->m_children[child]);
607 TYPENAME ntree<T>::iterator ntree<T>::child(const TYPENAME ntree<T>::iterator& i, unsigned child)
608 throw(wrong_object,null_dereference,end_dereference,std::out_of_range)
610 i.assert_valid(this);
611 if (child >= children(i)) throw std::out_of_range("stlplus::ntree");
612 return ntree_iterator<T,T&,T*>(i.node()->m_children[child]);
616 TYPENAME ntree<T>::const_iterator ntree<T>::parent(const TYPENAME ntree<T>::const_iterator& i) const
617 throw(wrong_object,null_dereference,end_dereference)
619 i.assert_valid(this);
620 ntree_node<T>* parent = i.node()->m_parent;
621 if (!parent) return ntree_iterator<T,const T&,const T*>(this);
622 return ntree_iterator<T,const T&,const T*>(parent);
626 TYPENAME ntree<T>::iterator ntree<T>::parent(const TYPENAME ntree<T>::iterator& i)
627 throw(wrong_object,null_dereference,end_dereference)
629 i.assert_valid(this);
630 ntree_node<T>* parent = i.node()->m_parent;
631 if (!parent) return ntree_iterator<T,T&,T*>(this);
632 return ntree_iterator<T,T&,T*>(parent);
636 TYPENAME ntree<T>::const_prefix_iterator ntree<T>::prefix_begin(void) const
638 return ntree_prefix_iterator<T,const T&,const T*>(root());
642 TYPENAME ntree<T>::prefix_iterator ntree<T>::prefix_begin(void)
644 return ntree_prefix_iterator<T,T&,T*>(root());
648 TYPENAME ntree<T>::const_prefix_iterator ntree<T>::prefix_end(void) const
650 return ntree_prefix_iterator<T,const T&,const T*>(ntree_iterator<T,const T&,const T*>(this));
654 TYPENAME ntree<T>::prefix_iterator ntree<T>::prefix_end(void)
656 return ntree_prefix_iterator<T,T&,T*>(ntree_iterator<T,T&,T*>(this));
660 TYPENAME ntree<T>::const_postfix_iterator ntree<T>::postfix_begin(void) const
662 return ntree_postfix_iterator<T,const T&,const T*>(root());
666 TYPENAME ntree<T>::postfix_iterator ntree<T>::postfix_begin(void)
668 return ntree_postfix_iterator<T,T&,T*>(root());
672 TYPENAME ntree<T>::const_postfix_iterator ntree<T>::postfix_end(void) const
674 return ntree_postfix_iterator<T,const T&,const T*>(ntree_iterator<T,const T&,const T*>(this));
678 TYPENAME ntree<T>::postfix_iterator ntree<T>::postfix_end(void)
680 return ntree_postfix_iterator<T,T&,T*>(ntree_iterator<T,T&,T*>(this));
684 TYPENAME ntree<T>::iterator ntree<T>::insert(const T& data)
686 // insert a new node as the root
687 return insert(ntree_iterator<T,T&,T*>(this), 0, data);
691 TYPENAME ntree<T>::iterator ntree<T>::insert(const TYPENAME ntree<T>::iterator& i, unsigned offset, const T& data)
692 throw(wrong_object,null_dereference,end_dereference,std::out_of_range)
694 // if i is the end iterator, this means insert a new root
699 i.assert_valid(this);
700 if (offset > children(i)) throw std::out_of_range("stlplus::ntree");
702 ntree_node<T>* new_node = new ntree_node<T>(this,data);
709 i.node()->m_children.insert(i.node()->m_children.begin()+offset,new_node);
710 new_node->m_parent = i.node();
712 return ntree_iterator<T,T&,T*>(new_node);
716 TYPENAME ntree<T>::iterator ntree<T>::append(const TYPENAME ntree<T>::iterator& i, const T& data)
717 throw(wrong_object,null_dereference,end_dereference)
719 return insert(i, i.node()->m_children.size(), data);
723 TYPENAME ntree<T>::iterator ntree<T>::insert(const TYPENAME ntree<T>::iterator& i, unsigned offset, const ntree<T>& tree)
724 throw(wrong_object,null_dereference,end_dereference,std::out_of_range)
726 // insert a whole tree as a child of i
727 i.assert_valid(this);
728 if (offset > children(i)) throw std::out_of_range("stlplus::ntree");
729 ntree_node<T>* new_node = ntree_copy(this, tree.m_root);
730 i.node()->m_children.insert(i.node()->m_children.begin()+offset,new_node);
731 new_node->m_parent = i.node();
732 return ntree_iterator<T,T&,T*>(new_node);
736 TYPENAME ntree<T>::iterator ntree<T>::append(const TYPENAME ntree<T>::iterator& i, const ntree<T>& tree)
737 throw(wrong_object,null_dereference,end_dereference)
739 return insert(i, children(i), tree);
743 TYPENAME ntree<T>::iterator ntree<T>::push(const TYPENAME ntree<T>::iterator& node, const T& data)
744 throw(wrong_object,null_dereference,end_dereference)
746 // insert a new node to replace the existing node in the tree
747 // making the original node the child of the new node
748 // i.e. (node) becomes (new)->(node)
749 // afterwards, the iterator still points to the old node, now the child
750 // returns the iterator to the new node
751 node.assert_valid(this);
752 ntree_node<T>* new_node = new ntree_node<T>(this,data);
753 if (node.node() == m_root)
755 // pushing the root node
757 new_node->m_parent = 0;
761 // pushing a sub-node
762 *(std::find(node.node()->m_parent->m_children.begin(), node.node()->m_parent->m_children.end(), node.node())) = new_node;
763 new_node->m_parent = node.node()->m_parent;
765 // link up the old node as the child of the new node
766 new_node->m_children.insert(new_node->m_children.begin(),node.node());
767 node.node()->m_parent = new_node;
768 return ntree_iterator<T,T&,T*>(new_node);
772 void ntree<T>::pop(const TYPENAME ntree<T>::iterator& parent, unsigned offset)
773 throw(wrong_object,null_dereference,end_dereference)
776 // removes the specified child of the parent node, adding its children to the parent node at the same offset
777 parent.assert_valid(this);
778 ntree_node<T>* node = parent.node();
779 if (offset >= node->m_children.size()) throw std::out_of_range("stlplus::ntree");
780 // move the grandchildren first
781 ntree_node<T>* child = parent.node()->m_children[offset];
782 while (!child->m_children.empty())
784 // remove the last grandchild and insert into node just after the child to be removed
785 ntree_node<T>* grandchild = child->m_children[child->m_children.size()-1];
786 child->m_children.pop_back();
787 node->m_children.insert(node->m_children.begin()+offset+1, grandchild);
788 grandchild->m_parent = node;
790 // now remove the child
791 node->m_children.erase(node->m_children.begin()+offset);
796 void ntree<T>::erase(void)
798 // erase the whole tree
803 void ntree<T>::erase(const TYPENAME ntree<T>::iterator& i)
804 throw(wrong_object,null_dereference,end_dereference)
808 // erase this node and its subtree
809 // do this by erasing this child of its parent
810 // handle the case of erasing the root
811 i.assert_valid(this);
812 ntree_node<T>* node = i.node();
820 ntree_node<T>* parent = node->m_parent;
821 // impossible for parent to be null - should assert this
822 TYPENAME std::vector<ntree_node<T>*>::iterator found =
823 std::find(parent->m_children.begin(), parent->m_children.end(), node);
824 // impossible for find to fail - should assert this
825 parent->m_children.erase(found);
832 void ntree<T>::erase(const TYPENAME ntree<T>::iterator& i, unsigned offset)
833 throw(wrong_object,null_dereference,end_dereference,std::out_of_range)
835 erase(child(i, offset));
839 ntree<T> ntree<T>::subtree(void)
841 return subtree(root());
845 ntree<T> ntree<T>::subtree(const TYPENAME ntree<T>::iterator& i)
846 throw(wrong_object,null_dereference,end_dereference)
851 i.assert_valid(this);
852 result.m_root = ntree_copy(&result, i.node());
858 ntree<T> ntree<T>::subtree(const TYPENAME ntree<T>::iterator& i, unsigned offset)
859 throw(wrong_object,null_dereference,end_dereference,std::out_of_range)
861 return subtree(child(i, offset));
865 ntree<T> ntree<T>::cut(void)
871 ntree<T> ntree<T>::cut(const TYPENAME ntree<T>::iterator& i)
872 throw(wrong_object,null_dereference,end_dereference)
877 i.assert_valid(this);
878 ntree_node<T>* node = i.node();
881 result.m_root = m_root;
886 ntree_node<T>* parent = node->m_parent;
887 // impossible for parent to be null - should assert this
888 TYPENAME std::vector<ntree_node<T>*>::iterator found =
889 std::find(parent->m_children.begin(), parent->m_children.end(), node);
890 // impossible for find to fail - should assert this
891 result.m_root = *found;
892 parent->m_children.erase(found);
896 result.m_root->m_parent = 0;
897 result.m_root->set_new_owner(&result);
904 ntree<T> ntree<T>::cut(const TYPENAME ntree<T>::iterator& i, unsigned offset)
905 throw(wrong_object,null_dereference,end_dereference,std::out_of_range)
907 return cut(child(i, offset));
910 ////////////////////////////////////////////////////////////////////////////////
912 } // end namespace stlplus