Files
boost_range/include/boost/range/iterator_range.hpp

289 lines
9.4 KiB
C++
Raw Normal View History

2004-06-29 02:50:07 +00:00
// Boost.Range library
//
// Copyright Thorsten Ottosen & Pavol Droba 2003-2004. Use, modification and
// distribution is subject to the Boost Software License, Version
// 1.0. (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//
// For more information, see http://www.boost.org/libs/range/
//
#ifndef BOOST_RANGE_ITERATOR_RANGE_HPP
#define BOOST_RANGE_ITERATOR_RANGE_HPP
#include <boost/range/config.hpp>
#include <boost/range/begin.hpp>
#include <boost/range/end.hpp>
#include <boost/iterator/iterator_traits.hpp>
#include <iterator>
#include <algorithm>
#include <ostream>
#include <cstddef>
/*! \file
Defines the \c iterator_class and related functions.
\c iterator_range is a simple wrapper of iterator pair idiom. It provides
a rich subset of Container interface.
*/
namespace boost {
// iterator range template class -----------------------------------------//
//! iterator_range class
/*!
An \c iterator_range delimits a range in a sequence by beginning and ending iterators.
An iterator_range can be passed to an algorithm which requires a sequence as an input.
For example, the \c toupper() function may be used most frequently on strings,
but can also be used on iterator_ranges:
\code
boost::tolower( find( s, "UPPERCASE STRING" ) );
\endcode
Many algorithms working with sequences take a pair of iterators,
delimiting a working range, as an arguments. The \c iterator_range class is an
encapsulation of a range identified by a pair of iterators.
It provides a collection interface,
so it is possible to pass an instance to an algorithm requiring a collection as an input.
*/
template<typename IteratorT>
class iterator_range
{
public:
//! this type
typedef iterator_range<IteratorT> type;
2004-07-25 17:12:17 +00:00
//BOOST_BROKEN_COMPILER_TYPE_TRAITS_SPECIALIZATION(value_type);
2004-07-24 13:16:12 +00:00
2004-06-29 02:50:07 +00:00
//! Encapsulated value type
2004-07-27 17:53:15 +00:00
typedef BOOST_DEDUCED_TYPENAME
2004-06-29 02:50:07 +00:00
iterator_value<IteratorT>::type value_type;
2004-07-24 13:16:12 +00:00
2004-06-29 02:50:07 +00:00
//! Difference type
2004-07-27 17:53:15 +00:00
typedef BOOST_DEDUCED_TYPENAME
2004-06-29 02:50:07 +00:00
iterator_difference<IteratorT>::type difference_type;
//! Size type
typedef std::size_t size_type; // note: must be unsigned
//! const_iterator type
/*!
There is no distinction between const_iterator and iterator.
These typedefs are provides to fulfill container interface
*/
typedef IteratorT const_iterator;
//! iterator type
typedef IteratorT iterator;
//! Constructor from a pair of iterators
2004-07-30 03:12:22 +00:00
template< class Iterator >
iterator_range( Iterator Begin, Iterator End ) :
2004-06-29 02:50:07 +00:00
m_Begin(Begin), m_End(End) {}
//! Constructor from a Range
template< class Range >
iterator_range( const Range& r ) :
2004-08-10 16:09:30 +00:00
m_Begin( boost::begin( r ) ), m_End( boost::end( r ) ) {}
2004-06-29 02:50:07 +00:00
//! Constructor from a Range
template< class Range >
iterator_range( Range& r ) :
2004-08-10 16:09:30 +00:00
m_Begin( boost::begin( r ) ), m_End( boost::end( r ) ) {}
2004-06-29 02:50:07 +00:00
2004-08-10 16:05:53 +00:00
template< class ForwardRange >
iterator_range& operator=( ForwardRange& r )
2004-06-29 02:50:07 +00:00
{
2004-08-10 16:09:30 +00:00
m_Begin = boost::begin( r );
m_End = boost::end( r );
2004-07-30 03:12:22 +00:00
return *this;
2004-06-29 02:50:07 +00:00
}
2004-08-10 16:05:53 +00:00
template< class ForwardRange >
iterator_range& operator=( const ForwardRange& r )
2004-06-29 02:50:07 +00:00
{
2004-08-10 16:09:30 +00:00
m_Begin = boost::begin( r );
m_End = boost::end( r );
2004-07-30 03:12:22 +00:00
return *this;
2004-06-29 02:50:07 +00:00
}
//! begin access
/*!
Retrieve the begin iterator
*/
IteratorT begin() const
{
return m_Begin;
}
//! end access
/*!
Retrieve the end iterator
*/
IteratorT end() const
{
return m_End;
}
//! Size of the range
/*!
Retrieve the size of the range
*/
size_type size() const
{
2004-06-29 09:06:52 +00:00
return std::distance( m_Begin, m_End );
}
bool empty() const
{
return m_Begin == m_End;
2004-06-29 02:50:07 +00:00
}
//! Safe bool conversion
/*!
Check whenever the range is empty.
Allows to use construction like this:
\code
iterator_range r;
if (!r)
{
...
}
\endcode
*/
typedef iterator (iterator_range::*unspecified_bool_type) () const;
operator unspecified_bool_type() const
{
2004-06-29 09:06:52 +00:00
return empty() ? 0: &iterator_range::end;
2004-06-29 02:50:07 +00:00
}
private:
// begin and end iterators
IteratorT m_Begin;
IteratorT m_End;
2004-07-30 03:12:22 +00:00
2004-06-29 02:50:07 +00:00
};
// iterator range free-standing operators ---------------------------//
//! iterator_range output operator
/*!
Output the range to an ostream. Elements are outputed
in a sequence without separators.
*/
template< typename IteratorT, typename Elem, typename Traits >
2004-07-30 03:12:22 +00:00
inline std::basic_ostream<Elem,Traits>& operator<<(
std::basic_ostream<Elem, Traits>& Os,
const iterator_range<IteratorT>& r )
2004-06-29 02:50:07 +00:00
{
std::copy( begin(r), end(r), std::ostream_iterator<Elem>(Os));
return Os;
}
2004-07-30 03:12:22 +00:00
//! Comparison operator ( equal )
/*!
Compare operands for equality
*/
2004-08-12 10:58:13 +00:00
template< class IteratorT, class IteratorT2 >
2004-07-30 03:12:22 +00:00
inline bool operator==( const iterator_range<IteratorT>& l,
2004-08-12 10:58:13 +00:00
const iterator_range<IteratorT2>& r )
2004-07-30 03:12:22 +00:00
{
return ! (l != r);
}
//! Comparison operator ( not-equal )
/*!
Compare operands for non-equality
*/
2004-08-12 10:58:13 +00:00
template< class IteratorT, class IteratorT2 >
2004-07-30 03:12:22 +00:00
inline bool operator!=( const iterator_range<IteratorT>& l,
2004-08-12 10:58:13 +00:00
const iterator_range<IteratorT2>& r )
2004-07-30 03:12:22 +00:00
{
return l.begin() != r.begin() || l.end() != r.end();
}
2004-06-29 02:50:07 +00:00
// iterator range utilities -----------------------------------------//
//! iterator_range construct helper
/*!
Construct an \c iterator_range from a pair of iterators
\param Begin A begin iterator
\param End An end iterator
\return iterator_range object
*/
template< typename IteratorT >
inline iterator_range< IteratorT >
make_iterator_range( IteratorT Begin, IteratorT End )
{
return iterator_range<IteratorT>( Begin, End );
}
2004-07-27 17:53:15 +00:00
#ifdef BOOST_NO_FUNCTION_TEMPLATE_ORDERING
template< typename Range >
2004-08-16 22:07:07 +00:00
inline iterator_range< BOOST_DEDUCED_TYPENAME range_result_iterator<Range>::type >
2004-07-27 17:53:15 +00:00
make_iterator_range( Range& r )
{
2004-08-16 22:07:07 +00:00
return iterator_range< BOOST_DEDUCED_TYPENAME range_result_iterator<Range>::type >
2004-07-27 17:53:15 +00:00
( begin( r ), end( r ) );
}
#else
2004-06-29 02:50:07 +00:00
//! iterator_range construct helper
/*!
Construct an \c iterator_range from a \c Range containing the begin
and end iterators.
*/
2004-08-10 16:05:53 +00:00
template< class ForwardRange >
2004-08-16 22:07:07 +00:00
inline iterator_range< BOOST_DEDUCED_TYPENAME range_iterator<ForwardRange>::type >
2004-08-10 16:05:53 +00:00
make_iterator_range( ForwardRange& r )
2004-06-29 02:50:07 +00:00
{
2004-08-16 22:07:07 +00:00
return iterator_range< BOOST_DEDUCED_TYPENAME range_iterator<ForwardRange>::type >
2004-06-29 02:50:07 +00:00
( r );
}
2004-08-10 16:05:53 +00:00
template< class ForwardRange >
2004-08-16 22:07:07 +00:00
inline iterator_range< BOOST_DEDUCED_TYPENAME range_const_iterator<ForwardRange>::type >
2004-08-10 16:05:53 +00:00
make_iterator_range( const ForwardRange& r )
2004-06-29 02:50:07 +00:00
{
2004-08-16 22:07:07 +00:00
return iterator_range< BOOST_DEDUCED_TYPENAME range_const_iterator<ForwardRange>::type >
2004-06-29 02:50:07 +00:00
( r );
}
2004-07-27 17:53:15 +00:00
#endif
2004-06-29 02:50:07 +00:00
//! copy a range into a sequence
/*!
Construct a new sequence of the specified type from the elements
in the given range
\param Range An input range
\return New sequence
*/
template< typename SeqT, typename Range >
inline SeqT copy_range( const Range& r )
{
return SeqT( begin( r ), end( r ) );
}
//! transform a range into a sequence
/*!
Create a new sequence from the elements in the range, transformed
by a function
\param Range An input range
\param Func Transformation function
\return New sequence
*/
template< typename SeqT, typename Range, typename FuncT >
inline SeqT transform_range( const Range& r, FuncT Func )
{
SeqT Seq;
std::transform( begin( r ), end( r ), std::back_inserter(Seq), Func );
return Seq;
}
} // namespace 'boost'
#endif