# HG changeset patch # User Eris Caffee # Date 1435091246 18000 # Node ID 3cdac4c2944525a4e0afaf0f014d15d47c3c7623 # Parent 028689700a4713c5971279956876c68112949f3e Refactored Queue's iterator and const_iterator into a template. The generic_iterator can be instatiated as either a const or non-const iterator as needed. It uses std::conditional<>::type create the appropriate types as needed. I learned this trick here: http://www.sj-vs.net/c-implementing-const_iterator-and-non-const-iterator-without-code-duplication/ diff -r 028689700a47 -r 3cdac4c29445 algs4-c++/src/Queue.hpp --- a/algs4-c++/src/Queue.hpp Tue Jun 23 14:52:17 2015 -0500 +++ b/algs4-c++/src/Queue.hpp Tue Jun 23 15:27:26 2015 -0500 @@ -6,6 +6,7 @@ #include #include #include +#include template class Queue { @@ -22,43 +23,54 @@ public: //////////////////////////////////// - class iterator : public std::iterator { + + template + class generic_iterator : public std::iterator { + + private: + typedef typename std::conditional::type NodePtrType; + typedef typename std::conditional::type ValueType; public: - iterator( Node *c ); + generic_iterator( NodePtrType c ) : + curr (c) + { + } - iterator& operator++(); - iterator operator++(int); + generic_iterator & operator++() { + if ( this->curr != nullptr ) { + this->curr = this->curr->next; + } + return *this; + } - T operator*(); - bool operator!=( typename Queue::iterator ); + generic_iterator operator++( int ) { + auto t = generic_iterator( *this ); + + if ( this->curr != nullptr ) { + this->curr = this->curr->next; + } + return t; + } + + ValueType operator*() { + return this->curr->item; + } + + bool operator!=( generic_iterator other ) { + return this->curr != other.curr; + } private: - Node *curr; + NodePtrType curr; }; //////////////////////////////////// - class const_iterator : public std::iterator { - - public: - - const_iterator( const Node *c ); - - const_iterator& operator++(); - const_iterator operator++(int); - - const T operator*(); - bool operator!=( typename Queue::const_iterator ); - - private: - - const Node *curr; - }; - - //////////////////////////////////// + typedef generic_iterator const_iterator; + typedef generic_iterator iterator; Queue( void ); ~Queue( void ); @@ -182,79 +194,5 @@ return Queue::const_iterator( nullptr ); } - -//////////////////////////////////////////////////////////////////////////////// - -template -Queue::iterator::iterator( Node *c ) : - curr (c) - { - } - -template -typename Queue::iterator & Queue::iterator::operator++() { - if ( this->curr != nullptr ) { - this->curr = this->curr->next; - } - return *this; - } - -template -typename Queue::iterator Queue::iterator::operator++( int ) { - auto t = Queue::iterator( *this ); - - if ( this->curr != nullptr ) { - this->curr = this->curr->next; - } - return t; - } - -template -T Queue::iterator::operator*() { - return this->curr->item; - } - -template -bool Queue::iterator::operator!=( typename Queue::iterator other ) { - return this->curr != other.curr; - } - -//////////////////////////////////////////////////////////////////////////////// - -template -Queue::const_iterator::const_iterator( const Node *c ) : - curr (c) - { - } - -template -typename Queue::const_iterator & Queue::const_iterator::operator++() { - if ( this->curr != nullptr ) { - this->curr = this->curr->next; - } - return *this; - } - -template -typename Queue::const_iterator Queue::const_iterator::operator++( int ) { - auto t = Queue::const_iterator( *this ); - - if ( this->curr != nullptr ) { - this->curr = this->curr->next; - } - return t; - } - -template -const T Queue::const_iterator::operator*() { - return this->curr->item; - } - -template -bool Queue::const_iterator::operator!=( typename Queue::const_iterator other ) { - return this->curr != other.curr; - } - - #endif