iterator_sequence.hh
Go to the documentation of this file.
00001 #ifndef UTILMM_ITERATOR_SEQUENCE_HPP
00002 #define UTILMM_ITERATOR_SEQUENCE_HPP
00003 
00004 #include <boost/iterator/iterator_facade.hpp>
00005 #include <boost/iterator/iterator_traits.hpp>
00006 
00007 namespace utilmm {
00013     template<typename It1, typename It2>
00014     class iterator_sequence : 
00015         public boost::iterator_facade<
00016             iterator_sequence<It1, It2>,
00017             typename It1::value_type,
00018             typename boost::iterator_traversal<It1>::type,
00019             typename It1::reference >
00020     {
00021         typedef iterator_sequence<It1, It2> Self;
00022 
00023     public:
00036         iterator_sequence(
00037                 It1 first, It1 first_end,
00038                 It2 second_begin, It2 second)
00039             : m_first(first), m_first_end(first_end)
00040             , m_second_begin(second_begin), m_second(second) {}
00041         iterator_sequence() {}
00042 
00043     private:
00044         friend class boost::iterator_core_access;
00045 
00046         It1 m_first, m_first_end;
00047         It2 m_second_begin, m_second;
00048 
00049         typename Self::reference dereference() const
00050         {
00051             if (m_first == m_first_end)
00052                 return *m_second;
00053             else
00054                 return *m_first;
00055         }
00056         bool equal(Self x) const
00057         { return (x.m_first == m_first && x.m_second == m_second); }
00058         void increment()
00059         {
00060             if (m_first == m_first_end)
00061                 ++m_second;
00062             else
00063                 ++m_first;
00064         }
00065         void decrement()
00066         {
00067             if (m_second == m_second_begin)
00068                 --m_first;
00069             else
00070                 --m_second;
00071         }
00072         void advance(typename Self::difference_type i)
00073         {
00074             if (i > 0)
00075             {
00076                 typename Self::difference_type remains = m_first_end - m_first;
00077                 if (remains > i)
00078                     m_first += i;
00079                 else
00080                 {
00081                     m_first = m_first_end;
00082                     m_second += i - remains;
00083                 }
00084             }
00085             else
00086             {
00087                 typename Self::difference_type remains = m_second_begin - m_second;
00088                 if (remains < i)
00089                     m_second += i;
00090                 else
00091                 {
00092                     m_second = m_second_begin;
00093                     m_first += i - remains;
00094                 }
00095             }
00096         }
00097 
00098         typename Self::difference_type distance_to(Self x) const
00099         { return (x.m_first - m_first) + (x.m_second - m_second); }
00100     };
00101 }
00102 #endif


utilmm
Author(s): Sylvain Joyeux/sylvain.joyeux@m4x.org
autogenerated on Thu Jan 2 2014 11:38:31