/* Copyright (c) 2003-2006, Troy Aaron Johnson All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of Troy Aaron Johnson nor the names of any contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ template void tree::iterator::clear(void) { /* this line necessary for g++ 3.4 onward */ tree_node*& current = this->current; assert(current != NULL); for (sibling_iterator i(beginChildren()); i != endChildren(); ) { i.clear(); ++i; (i - 1).destroy(); } current->first_child = NULL; current->last_child = NULL; } template typename tree::iterator tree::iterator::erase(void) { tree_node*& current = this->current; assert(current != NULL); /* remove all children and grandchildren */ clear(); /* make copies because there is an ordering problem updating the previous and next links */ tree_node* old_prev = current->prev_sibling; tree_node* old_next = current->next_sibling; if (old_prev != NULL) old_prev->next_sibling = old_next; if (old_next != NULL) old_next->prev_sibling = old_prev; if (current->parent->first_child == current) current->parent->first_child = old_next; if (current->parent->last_child == current) current->parent->last_child = old_prev; /* destroy has no parameters, so gcc complains that it can't find the method unless this is made explicit */ super::destroy(); return iterator(old_next); } template typename tree::iterator tree::iterator::insert(const N& data) { tree_node*& current = this->current; assert(current != NULL); tree_node* p = new tree_node(data); p->parent = current->parent; p->first_child = p->last_child = NULL; p->prev_sibling = current->prev_sibling; p->next_sibling = current; if (current->prev_sibling != NULL) current->prev_sibling->next_sibling = p; else current->parent->first_child = p; current->prev_sibling = p; return p; } template typename tree::iterator tree::iterator::insert(const tree& t) { assert(this->current != NULL); return absorb_before(new tree(t)); } template typename tree::iterator tree::iterator::insert_after(const N& data) { /* this line necessary for g++ 3.4 onward */ tree_node*& current = this->current; assert(current != NULL); tree_node* p = new tree_node(data); p->parent = current->parent; p->first_child = p->last_child = NULL; p->prev_sibling = current; p->next_sibling = current->next_sibling; if (current->next_sibling != NULL) current->next_sibling->prev_sibling = p; else current->parent->last_child = p; current->next_sibling = p; return p; } template typename tree::iterator tree::iterator::insert_after(const tree& t) { assert(this->current != NULL); return absorb_after(new tree(t)); } template void tree::iterator::pop_back(void) { /* this line necessary for g++ 3.4 onward */ tree_node*& current = this->current; assert(current != NULL); assert(current->last_child != NULL); assert(current->last_child->next_sibling == NULL); tree_node* p = current->last_child; current->last_child = p->prev_sibling; if (p->prev_sibling != NULL) p->prev_sibling->next_sibling = NULL; else current->first_child = NULL; delete p; } template void tree::iterator::pop_front(void) { /* this line necessary for g++ 3.4 onward */ tree_node*& current = this->current; assert(current != NULL); assert(current->first_child != NULL); assert(current->first_child->prev_sibling == NULL); tree_node* p = current->first_child; current->first_child = p->next_sibling; if (p->next_sibling != NULL) p->next_sibling->prev_sibling = NULL; else current->last_child = NULL; delete p; } template typename tree::iterator tree::iterator::push_back(const N& data) { /* this line necessary for g++ 3.4 onward */ tree_node*& current = this->current; assert(current != NULL); tree_node* p = new tree_node(data); p->parent = current; p->first_child = p->last_child = NULL; if (current->last_child == NULL) { current->first_child = current->last_child = p; p->prev_sibling = p->next_sibling = NULL; } else { p->prev_sibling = current->last_child; p->next_sibling = NULL; current->last_child->next_sibling = p; current->last_child = p; } return iterator(p); } template typename tree::iterator tree::iterator::push_back(const tree& t) { assert(this->current != NULL); return absorb_back(new tree(t)); } template typename tree::iterator tree::iterator::push_front(const N& data) { /* this line necessary for g++ 3.4 onward */ tree_node*& current = this->current; assert(current != NULL); tree_node* p = new tree_node(data); p->parent = current; p->first_child = p->last_child = NULL; if (current->first_child == NULL) { current->first_child = current->last_child = p; p->prev_sibling = p->next_sibling = NULL; } else { p->prev_sibling = NULL; p->next_sibling = current->first_child; current->first_child->prev_sibling = p; current->first_child = p; } return iterator(p); } template typename tree::iterator tree::iterator::push_front(const tree& t) { assert(this->current != NULL); return absorb_front(new tree(t)); } template N tree::iterator::replace(const N& data) { N ret(this->current->data); this->current->data = data; return ret; } template size_t tree::iterator::size(void) const { assert(this->current != NULL); size_t s = 1; for (typename tree::sibling_iterator i(beginChildren()); i != endChildren(); ++i) { s += i.size(); } return s; } template typename tree::iterator tree::iterator::splice_after(tree& t) { /* this line necessary for g++ 3.4 onward */ tree_node*& current = this->current; assert(current != NULL); tree_node* p = t.master->next_sibling; t.master->next_sibling = t.master; t.master->prev_sibling = t.master; p->parent = current->parent; p->prev_sibling = current; p->next_sibling = current->next_sibling; if (current->next_sibling != NULL) current->next_sibling->prev_sibling = p; else current->parent->last_child = p; current->next_sibling = p; return p; } template typename tree::iterator tree::iterator::splice_back(tree& t) { /* this line necessary for g++ 3.4 onward */ tree_node*& current = this->current; assert(current != NULL); assert(!t.empty()); tree_node* p = t.master->next_sibling; t.master->next_sibling = t.master; t.master->prev_sibling = t.master; p->parent = current; if (current->last_child == NULL) { current->first_child = current->last_child = p; p->prev_sibling = p->next_sibling = NULL; } else { p->prev_sibling = current->last_child; p->next_sibling = NULL; current->last_child->next_sibling = p; current->last_child = p; } return p; } template typename tree::iterator tree::iterator::splice_before(tree& t) { /* TODO */ assert(false); return NULL; } template typename tree::iterator tree::iterator::splice_front(tree& t) { /* TODO */ assert(false); return NULL; }