From 77dff97861ec6ff6bb7951a3b9c94a6bf47ba62f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thorsten=20J=C3=B8rgen=20Ottosen?= Date: Sat, 21 Aug 2004 21:18:00 +0000 Subject: [PATCH] *** empty log message *** [SVN r24637] --- include/boost/range/iterator_range.hpp | 42 +++++++++++++++++++++----- include/boost/range/sub_range.hpp | 5 ++- test/sub_range.cpp | 8 +++++ 3 files changed, 46 insertions(+), 9 deletions(-) diff --git a/include/boost/range/iterator_range.hpp b/include/boost/range/iterator_range.hpp index 1ab488e..ee10e37 100755 --- a/include/boost/range/iterator_range.hpp +++ b/include/boost/range/iterator_range.hpp @@ -75,34 +75,54 @@ namespace boost { //! iterator type typedef IteratorT iterator; + iterator_range() : m_Begin( iterator() ), m_End( iterator() ), + singular( true ) + { } + //! Constructor from a pair of iterators template< class Iterator > iterator_range( Iterator Begin, Iterator End ) : - m_Begin(Begin), m_End(End) {} + m_Begin(Begin), m_End(End), singular(false) {} //! Constructor from a Range template< class Range > iterator_range( const Range& r ) : - m_Begin( boost::begin( r ) ), m_End( boost::end( r ) ) {} + m_Begin( boost::begin( r ) ), m_End( boost::end( r ) ), + singular(false) {} //! Constructor from a Range template< class Range > iterator_range( Range& r ) : - m_Begin( boost::begin( r ) ), m_End( boost::end( r ) ) {} + m_Begin( boost::begin( r ) ), m_End( boost::end( r ) ), + singular(false) {} + template< class Iterator > + iterator_range& operator=( const iterator_range& r ) + { + m_Begin = r.begin(); + m_End = r.end(); + // + // remark: this need not necessarily be true, but it does no harm + // + singular = r.empty(); + return *this; + } + template< class ForwardRange > iterator_range& operator=( ForwardRange& r ) { - m_Begin = boost::begin( r ); - m_End = boost::end( r ); + m_Begin = boost::begin( r ); + m_End = boost::end( r ); + singular = false; return *this; } template< class ForwardRange > iterator_range& operator=( const ForwardRange& r ) { - m_Begin = boost::begin( r ); - m_End = boost::end( r ); + m_Begin = boost::begin( r ); + m_End = boost::end( r ); + singular = false; return *this; } @@ -130,11 +150,17 @@ namespace boost { */ size_type size() const { + if( singular ) + return 0; + return std::distance( m_Begin, m_End ); } bool empty() const { + if( singular ) + return true; + return m_Begin == m_End; } @@ -160,7 +186,7 @@ namespace boost { // begin and end iterators IteratorT m_Begin; IteratorT m_End; - + bool singular; }; // iterator range free-standing operators ---------------------------// diff --git a/include/boost/range/sub_range.hpp b/include/boost/range/sub_range.hpp index 0272fbf..701d8a0 100755 --- a/include/boost/range/sub_range.hpp +++ b/include/boost/range/sub_range.hpp @@ -35,6 +35,9 @@ namespace boost typedef BOOST_DEDUCED_TYPENAME range_size::type size_type; public: + sub_range() : base() + { } + template< class ForwardRange2 > sub_range( ForwardRange2& r ) : base( r ) { } @@ -68,7 +71,7 @@ namespace boost const_iterator begin() const { return base::begin(); } iterator end() { return base::end(); } const_iterator end() const { return base::end(); } - size_type size() const { return std::distance( begin(), end() ); } + size_type size() const { return base::size(); } }; diff --git a/test/sub_range.cpp b/test/sub_range.cpp index 6948349..5fefb30 100755 --- a/test/sub_range.cpp +++ b/test/sub_range.cpp @@ -113,6 +113,14 @@ void check_iterator_range() r.size(); s.size(); + irange singular_irange; + BOOST_CHECK( singular_irange.empty() ); + BOOST_CHECK( singular_irange.size() == 0 ); + + srange singular_srange; + BOOST_CHECK( singular_srange.empty() ); + BOOST_CHECK( singular_srange.size() == 0 ); + }