📄 tree.h
字号:
: iterator_base(0) { }template <class T, class tree_node_allocator>tree<T, tree_node_allocator>::post_order_iterator::post_order_iterator(tree_node *tn) : iterator_base(tn) { }template <class T, class tree_node_allocator>tree<T, tree_node_allocator>::post_order_iterator::post_order_iterator(const iterator_base &other) : iterator_base(other.node) { }template <class T, class tree_node_allocator>tree<T, tree_node_allocator>::post_order_iterator::post_order_iterator(const sibling_iterator& other) : iterator_base(other.node) { if(this->node==0) { if(other.range_last()!=0) this->node=other.range_last(); else this->node=other.parent_; this->skip_children(); ++(*this); } }template <class T, class tree_node_allocator>typename tree<T, tree_node_allocator>::post_order_iterator& tree<T, tree_node_allocator>::post_order_iterator::operator++() { assert(this->node!=0); if(this->node->next_sibling==0) this->node=this->node->parent; else { this->node=this->node->next_sibling; if(this->skip_current_children_) { this->skip_current_children_=false; } else { while(this->node->first_child) this->node=this->node->first_child; } } return *this; }template <class T, class tree_node_allocator>typename tree<T, tree_node_allocator>::post_order_iterator& tree<T, tree_node_allocator>::post_order_iterator::operator--() { assert(this->node!=0); if(this->skip_current_children_ || this->node->last_child==0) { this->skip_current_children_=false; while(this->node->prev_sibling==0) this->node=this->node->parent; this->node=this->node->prev_sibling; } else { this->node=this->node->last_child; } return *this;}template <class T, class tree_node_allocator>typename tree<T, tree_node_allocator>::post_order_iterator tree<T, tree_node_allocator>::post_order_iterator::operator++(int) { post_order_iterator copy = *this; ++(*this); return copy; }template <class T, class tree_node_allocator>typename tree<T, tree_node_allocator>::post_order_iterator tree<T, tree_node_allocator>::post_order_iterator::operator--(int) { post_order_iterator copy = *this; --(*this); return copy; }template <class T, class tree_node_allocator>typename tree<T, tree_node_allocator>::post_order_iterator& tree<T, tree_node_allocator>::post_order_iterator::operator+=(unsigned int num) { while(num>0) { ++(*this); --num; } return (*this); }template <class T, class tree_node_allocator>typename tree<T, tree_node_allocator>::post_order_iterator& tree<T, tree_node_allocator>::post_order_iterator::operator-=(unsigned int num) { while(num>0) { --(*this); --num; } return (*this); }template <class T, class tree_node_allocator>void tree<T, tree_node_allocator>::post_order_iterator::descend_all() { assert(this->node!=0); while(this->node->first_child) this->node=this->node->first_child; }// Fixed depth iteratortemplate <class T, class tree_node_allocator>tree<T, tree_node_allocator>::fixed_depth_iterator::fixed_depth_iterator() : iterator_base() { set_first_parent_(); }template <class T, class tree_node_allocator>tree<T, tree_node_allocator>::fixed_depth_iterator::fixed_depth_iterator(tree_node *tn) : iterator_base(tn) { set_first_parent_(); }template <class T, class tree_node_allocator>tree<T, tree_node_allocator>::fixed_depth_iterator::fixed_depth_iterator(const iterator_base& other) : iterator_base(other.node) { set_first_parent_(); }template <class T, class tree_node_allocator>tree<T, tree_node_allocator>::fixed_depth_iterator::fixed_depth_iterator(const sibling_iterator& other) : iterator_base(other.node), first_parent_(other.parent_) { find_leftmost_parent_(); }template <class T, class tree_node_allocator>tree<T, tree_node_allocator>::fixed_depth_iterator::fixed_depth_iterator(const fixed_depth_iterator& other) : iterator_base(other.node), first_parent_(other.first_parent_) { }template <class T, class tree_node_allocator>void tree<T, tree_node_allocator>::fixed_depth_iterator::set_first_parent_() { return; // FIXME: we do not use first_parent_ yet, and it actually needs some serious reworking if // it is ever to work at the 'head' level. first_parent_=0; if(this->node==0) return; if(this->node->parent!=0) first_parent_=this->node->parent; if(first_parent_) find_leftmost_parent_(); }template <class T, class tree_node_allocator>void tree<T, tree_node_allocator>::fixed_depth_iterator::find_leftmost_parent_() { return; // FIXME: see 'set_first_parent()' tree_node *tmppar=first_parent_; while(tmppar->prev_sibling) { tmppar=tmppar->prev_sibling; if(tmppar->first_child) first_parent_=tmppar; } }template <class T, class tree_node_allocator>typename tree<T, tree_node_allocator>::fixed_depth_iterator& tree<T, tree_node_allocator>::fixed_depth_iterator::operator++() { assert(this->node!=0); if(this->node->next_sibling) { this->node=this->node->next_sibling; } else { int relative_depth=0; upper: do { this->node=this->node->parent; if(this->node==0) return *this; --relative_depth; } while(this->node->next_sibling==0); lower: this->node=this->node->next_sibling; while(this->node->first_child==0) { if(this->node->next_sibling==0) goto upper; this->node=this->node->next_sibling; if(this->node==0) return *this; } while(relative_depth<0 && this->node->first_child!=0) { this->node=this->node->first_child; ++relative_depth; } if(relative_depth<0) { if(this->node->next_sibling==0) goto upper; else goto lower; } } return *this;// if(this->node->next_sibling!=0) {// this->node=this->node->next_sibling;// assert(this->node!=0);// if(this->node->parent==0 && this->node->next_sibling==0) // feet element// this->node=0;// }// else {// tree_node *par=this->node->parent;// do {// par=par->next_sibling;// if(par==0) { // FIXME: need to keep track of this!// this->node=0;// return *this;// }// } while(par->first_child==0);// this->node=par->first_child;// } return *this; }template <class T, class tree_node_allocator>typename tree<T, tree_node_allocator>::fixed_depth_iterator& tree<T, tree_node_allocator>::fixed_depth_iterator::operator--() { assert(this->node!=0); if(this->node->prev_sibling!=0) { this->node=this->node->prev_sibling; assert(this->node!=0); if(this->node->parent==0 && this->node->prev_sibling==0) // head element this->node=0; } else { tree_node *par=this->node->parent; do { par=par->prev_sibling; if(par==0) { // FIXME: need to keep track of this! this->node=0; return *this; } } while(par->last_child==0); this->node=par->last_child; } return *this;}template <class T, class tree_node_allocator>typename tree<T, tree_node_allocator>::fixed_depth_iterator tree<T, tree_node_allocator>::fixed_depth_iterator::operator++(int) { fixed_depth_iterator copy = *this; ++(*this); return copy; }template <class T, class tree_node_allocator>typename tree<T, tree_node_allocator>::fixed_depth_iterator tree<T, tree_node_allocator>::fixed_depth_iterator::operator--(int){ fixed_depth_iterator copy = *this; --(*this); return copy;}template <class T, class tree_node_allocator>typename tree<T, tree_node_allocator>::fixed_depth_iterator& tree<T, tree_node_allocator>::fixed_depth_iterator::operator-=(unsigned int num) { while(num>0) { --(*this); --(num); } return (*this); }template <class T, class tree_node_allocator>typename tree<T, tree_node_allocator>::fixed_depth_iterator& tree<T, tree_node_allocator>::fixed_depth_iterator::operator+=(unsigned int num) { while(num>0) { ++(*this); --(num); } return *this; }// FIXME: add the other members of fixed_depth_iterator.// Sibling iteratortemplate <class T, class tree_node_allocator>tree<T, tree_node_allocator>::sibling_iterator::sibling_iterator() : iterator_base() { set_parent_(); }template <class T, class tree_node_allocator>tree<T, tree_node_allocator>::sibling_iterator::sibling_iterator(tree_node *tn) : iterator_base(tn) { set_parent_(); }template <class T, class tree_node_allocator>tree<T, tree_node_allocator>::sibling_iterator::sibling_iterator(const iterator_base& other) : iterator_base(other.node) { set_parent_(); }template <class T, class tree_node_allocator>tree<T, tree_node_allocator>::sibling_iterator::sibling_iterator(const sibling_iterator& other) : iterator_base(other), parent_(other.parent_) { }template <class T, class tree_node_allocator>void tree<T, tree_node_allocator>::sibling_iterator::set_parent_() { parent_=0; if(this->node==0) return; if(this->node->parent!=0) parent_=this->node->parent; }template <class T, class tree_node_allocator>typename tree<T, tree_node_allocator>::sibling_iterator& tree<T, tree_node_allocator>::sibling_iterator::operator++() { if(this->node) this->node=this->node->next_sibling; return *this; }template <class T, class tree_node_allocator>typename tree<T, tree_node_allocator>::sibling_iterator& tree<T, tree_node_allocator>::sibling_iterator::operator--() { if(this->node) this->node=this->node->prev_sibling; else { assert(parent_); this->node=parent_->last_child; } return *this;}template <class T, class tree_node_allocator>typename tree<T, tree_node_allocator>::sibling_iterator tree<T, tree_node_allocator>::sibling_iterator::operator++(int) { sibling_iterator copy = *this; ++(*this); return copy; }template <class T, class tree_node_allocator>typename tree<T, tree_node_allocator>::sibling_iterator tree<T, tree_node_allocator>::sibling_iterator::operator--(int) { sibling_iterator copy = *this; --(*this); return copy; }template <class T, class tree_node_allocator>typename tree<T, tree_node_allocator>::sibling_iterator& tree<T, tree_node_allocator>::sibling_iterator::operator+=(unsigned int num) { while(num>0) { ++(*this); --num; } return (*this); }template <class T, class tree_node_allocator>typename tree<T, tree_node_allocator>::sibling_iterator& tree<T, tree_node_allocator>::sibling_iterator::operator-=(unsigned int num) { while(num>0) { --(*this); --num; } return (*this); }template <class T, class tree_node_allocator>typename tree<T, tree_node_allocator>::tree_node *tree<T, tree_node_allocator>::sibling_iterator::range_first() const { tree_node *tmp=parent_->first_child; return tmp; }template <class T, class tree_node_allocator>typename tree<T, tree_node_allocator>::tree_node *tree<T, tree_node_allocator>::sibling_iterator::range_last() const { return parent_->last_child; }#endif// Local variables:// default-tab-width: 3// End:
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -