changeset 27:80ca1973e3bd tip

Fleshed out Queue::generic_iterator a bit more to make it a more or less complete example of implmenting an iterator.
author Eris Caffee <discordia@eldalin.com>
date Tue, 23 Jun 2015 17:14:09 -0500
parents 2cbfacd2a3e9
children
files algs4-c++/src/Queue.cpp algs4-c++/src/Queue.hpp
diffstat 2 files changed, 29 insertions(+), 2 deletions(-) [+]
line diff
     1.1 --- a/algs4-c++/src/Queue.cpp	Tue Jun 23 15:46:22 2015 -0500
     1.2 +++ b/algs4-c++/src/Queue.cpp	Tue Jun 23 17:14:09 2015 -0500
     1.3 @@ -2,6 +2,7 @@
     1.4  
     1.5  
     1.6  #include <iostream>
     1.7 +#include <typeinfo>
     1.8  
     1.9  #include "Queue.hpp"
    1.10  
    1.11 @@ -31,6 +32,9 @@
    1.12          std::cout << *iter << std::endl;
    1.13          }
    1.14  
    1.15 +    // Just make sure that we can build an iterator with the copy constructor.
    1.16 +    auto iter = q2.begin();
    1.17 +    auto iter2( iter );
    1.18  
    1.19      std::cout << "Dequeuing entries..." << std::endl;
    1.20  
    1.21 @@ -45,5 +49,18 @@
    1.22      // Silently empty q2 to avoid the assertion in the destructor.
    1.23      while ( ! q2.is_empty() )
    1.24          q2.dequeue();
    1.25 +
    1.26 +    // Show the iterator traits;
    1.27 +    std::cout << std::endl;
    1.28 +    if ( typeid( Queue<long>::iterator_traits::iterator_category) == typeid( std::forward_iterator_tag ) )
    1.29 +        std::cout << "category: forward iterator" << std::endl;;
    1.30 +    if ( typeid( Queue<long>::iterator_traits::value_type) == typeid( long ) )
    1.31 +        std::cout << "value_type: long" << std::endl;;
    1.32 +    if ( typeid( Queue<long>::iterator_traits::pointer) == typeid( long * ) )
    1.33 +        std::cout << "pointer: long *" << std::endl;;
    1.34 +    if ( typeid( Queue<long>::iterator_traits::reference) == typeid( long & ) )
    1.35 +        std::cout << "reference: long &" << std::endl;;
    1.36 +    if ( typeid( Queue<long>::iterator_traits::difference_type ) == typeid( ptrdiff_t ) )
    1.37 +        std::cout << "difference_type: ptrdiff_t" << std::endl;;
    1.38      }
    1.39  
     2.1 --- a/algs4-c++/src/Queue.hpp	Tue Jun 23 15:46:22 2015 -0500
     2.2 +++ b/algs4-c++/src/Queue.hpp	Tue Jun 23 17:14:09 2015 -0500
     2.3 @@ -38,6 +38,11 @@
     2.4              {
     2.5              }
     2.6  
     2.7 +        generic_iterator( const generic_iterator<false>& other ) :
     2.8 +            curr (other.curr)
     2.9 +            {
    2.10 +            }
    2.11 +
    2.12          generic_iterator & operator++() {
    2.13              if ( this->curr != nullptr ) {
    2.14                  this->curr = this->curr->next;
    2.15 @@ -58,8 +63,12 @@
    2.16              return this->curr->item;
    2.17              }
    2.18  
    2.19 -        bool operator!=( generic_iterator other ) {
    2.20 -            return this->curr != other.curr;
    2.21 +        bool operator==( generic_iterator rhs ) {
    2.22 +            return this->curr == rhs.curr;
    2.23 +            }
    2.24 +
    2.25 +        bool operator!=( generic_iterator rhs ) {
    2.26 +            return this->curr != rhs.curr;
    2.27              }
    2.28  
    2.29      private:
    2.30 @@ -71,6 +80,7 @@
    2.31  
    2.32      typedef generic_iterator<true> const_iterator;
    2.33      typedef generic_iterator<false> iterator;
    2.34 +    typedef std::iterator_traits< generic_iterator<> > iterator_traits;
    2.35  
    2.36      Queue( void );
    2.37      ~Queue( void );