#ifndef RADIX_TREE_IT #define RADIX_TREE_IT #include // forward declaration template class radix_tree; template class radix_tree_node; template class radix_tree_it { public: using iterator_category = std::forward_iterator_tag; using value_type = std::pair; using difference_type = std::pair; using pointer = std::pair*; using reference = std::pair&; radix_tree_it() : m_pointee(0) { } radix_tree_it(const radix_tree_it& r) : m_pointee(r.m_pointee) { } radix_tree_it& operator=(const radix_tree_it& r) { m_pointee = r.m_pointee; return *this; } ~radix_tree_it() { } std::pair& operator* () const; std::pair* operator-> () const; const radix_tree_it& operator++ (); radix_tree_it operator++ (int); // const radix_tree_it& operator-- (); bool operator!= (const radix_tree_it &lhs) const; bool operator== (const radix_tree_it &lhs) const; radix_tree_node *m_pointee; radix_tree_it(radix_tree_node *p) : m_pointee(p) { } radix_tree_node* increment(radix_tree_node* node) const; radix_tree_node* descend(radix_tree_node* node) const; }; template radix_tree_node* radix_tree_it::increment(radix_tree_node* node) const { radix_tree_node* parent = node->m_parent; if (parent == NULL) return NULL; typename radix_tree_node::it_child it = parent->m_children.find(node->m_key); assert(it != parent->m_children.end()); ++it; if (it == parent->m_children.end()) return increment(parent); else return descend(it->second); } template radix_tree_node* radix_tree_it::descend(radix_tree_node* node) const { if (node->m_is_leaf) return node; typename radix_tree_node::it_child it = node->m_children.begin(); assert(it != node->m_children.end()); return descend(it->second); } template std::pair& radix_tree_it::operator* () const { return *m_pointee->m_value; } template std::pair* radix_tree_it::operator-> () const { return m_pointee->m_value; } template bool radix_tree_it::operator!= (const radix_tree_it &lhs) const { return m_pointee != lhs.m_pointee; } template bool radix_tree_it::operator== (const radix_tree_it &lhs) const { return m_pointee == lhs.m_pointee; } template const radix_tree_it& radix_tree_it::operator++ () { if (m_pointee != NULL) // it is undefined behaviour to dereference iterator that is out of bounds... m_pointee = increment(m_pointee); return *this; } template radix_tree_it radix_tree_it::operator++ (int) { radix_tree_it copy(*this); ++(*this); return copy; } #endif // RADIX_TREE_IT