[range] Added <=, ==, >, >=, != operators to iterator_range

[SVN r70037]
This commit is contained in:
Neil Groves
2011-03-16 23:33:57 +00:00
parent 19a2090ef8
commit 3cd6a7277f
3 changed files with 212 additions and 4 deletions

View File

@ -72,6 +72,24 @@ namespace boost
boost::end(r) );
}
template< class Left, class Right >
inline bool greater_than( const Left& l, const Right& r )
{
return less_than(r,l);
}
template< class Left, class Right >
inline bool less_or_equal_than( const Left& l, const Right& r )
{
return !iterator_range_detail::less_than(r,l);
}
template< class Left, class Right >
inline bool greater_or_equal_than( const Left& l, const Right& r )
{
return !iterator_range_detail::less_than(l,r);
}
// This version is maintained since it is used in other boost libraries
// such as Boost.Assign
template< class Left, class Right >
@ -272,6 +290,21 @@ namespace boost
return iterator_range_detail::less_than( *this, r );
}
bool operator>( const iterator_range& r ) const
{
return iterator_range_detail::greater_than( *this, r );
}
bool operator<=( const iterator_range& r ) const
{
return iterator_range_detail::less_or_equal_than( *this, r );
}
bool operator>=( const iterator_range& r ) const
{
return iterator_range_detail::greater_or_equal_than( *this, r );
}
#endif
public: // convenience
@ -371,6 +404,27 @@ namespace boost
return iterator_range_detail::less_than( l, r );
}
template< class IteratorT, class ForwardRange >
inline bool operator<=( const ForwardRange& l,
const iterator_range<IteratorT>& r )
{
return iterator_range_detail::less_or_equal_than( l, r );
}
template< class IteratorT, class ForwardRange >
inline bool operator>( const ForwardRange& l,
const iterator_range<IteratorT>& r )
{
return iterator_range_detail::greater_than( l, r );
}
template< class IteratorT, class ForwardRange >
inline bool operator>=( const ForwardRange& l,
const iterator_range<IteratorT>& r )
{
return iterator_range_detail::greater_or_equal_than( l, r );
}
#ifdef BOOST_NO_FUNCTION_TEMPLATE_ORDERING
#else
template< class Iterator1T, class Iterator2T >
@ -417,6 +471,48 @@ namespace boost
return iterator_range_detail::less_than( l, r );
}
template< class Iterator1T, class Iterator2T >
inline bool operator<=( const iterator_range<Iterator1T>& l,
const iterator_range<Iterator2T>& r )
{
return iterator_range_detail::less_or_equal_than( l, r );
}
template< class IteratorT, class ForwardRange >
inline bool operator<=( const iterator_range<IteratorT>& l,
const ForwardRange& r )
{
return iterator_range_detail::less_or_equal_than( l, r );
}
template< class Iterator1T, class Iterator2T >
inline bool operator>( const iterator_range<Iterator1T>& l,
const iterator_range<Iterator2T>& r )
{
return iterator_range_detail::greater_than( l, r );
}
template< class IteratorT, class ForwardRange >
inline bool operator>( const iterator_range<IteratorT>& l,
const ForwardRange& r )
{
return iterator_range_detail::greater_than( l, r );
}
template< class Iterator1T, class Iterator2T >
inline bool operator>=( const iterator_range<Iterator1T>& l,
const iterator_range<Iterator2T>& r )
{
return iterator_range_detail::greater_or_equal_than( l, r );
}
template< class IteratorT, class ForwardRange >
inline bool operator>=( const iterator_range<IteratorT>& l,
const ForwardRange& r )
{
return iterator_range_detail::greater_or_equal_than( l, r );
}
#endif // BOOST_NO_FUNCTION_TEMPLATE_ORDERING
// iterator range utilities -----------------------------------------//

View File

@ -24,6 +24,7 @@
#include <boost/test/unit_test.hpp>
#include <iostream>
#include <string>
#include <vector>
void check_reference_type();
@ -102,17 +103,127 @@ void check_iterator_range()
check_reference_type();
}
namespace iterator_range_test_detail
{
struct less
{
template< class Left, class Right >
bool operator()(const Left& l, const Right& r) const
{
return l < r;
}
};
struct greater
{
template< class Left, class Right >
bool operator()(const Left& l, const Right& r) const
{
return l > r;
}
};
struct less_or_equal
{
template< class Left, class Right >
bool operator()(const Left& l, const Right& r) const
{
return l <= r;
}
};
struct greater_or_equal
{
template< class Left, class Right >
bool operator()(const Left& l, const Right& r) const
{
return l >= r;
}
};
struct equal_to
{
template< class Left, class Right >
bool operator()(const Left& l, const Right& r) const
{
return l == r;
}
};
struct not_equal_to
{
template< class Left, class Right >
bool operator()(const Left& l, const Right& r) const
{
return l != r;
}
};
template< class Pred >
void check_iterator_range_operators_impl(Pred pred)
{
std::vector<std::string> vals;
vals.push_back(std::string());
vals.push_back("a");
vals.push_back("b");
vals.push_back("z");
vals.push_back("ab");
vals.push_back("ba");
vals.push_back("abc");
vals.push_back("cba");
vals.push_back("aa");
vals.push_back("aaa");
vals.push_back("aab");
vals.push_back("bba");
typedef std::string::const_iterator citer;
typedef boost::iterator_range<citer> iter_range;
typedef std::vector<std::string>::const_iterator value_const_iterator;
value_const_iterator first_val = vals.begin();
value_const_iterator last_val = vals.end();
for (value_const_iterator left_it = first_val; left_it != last_val; ++left_it)
{
const std::string& leftValue = *left_it;
for (value_const_iterator right_it = first_val; right_it != last_val; ++right_it)
{
const std::string& rightValue = *right_it;
iter_range left = boost::make_iterator_range(leftValue);
iter_range right = boost::make_iterator_range(rightValue);
const bool reference = pred(leftValue, rightValue);
BOOST_CHECK_EQUAL( pred(left, right), reference );
BOOST_CHECK_EQUAL( pred(left, rightValue), reference );
BOOST_CHECK_EQUAL( pred(leftValue, right), reference );
}
}
}
} // namespace iterator_range_test_detail
template<typename Pred>
inline void check_iterator_range_operator()
{
iterator_range_test_detail::check_iterator_range_operators_impl(
Pred());
}
boost::unit_test::test_suite* init_unit_test_suite( int argc, char* argv[] )
{
boost::unit_test::test_suite* test = BOOST_TEST_SUITE( "Range Test Suite" );
test->add( BOOST_TEST_CASE( &check_iterator_range ) );
test->add(BOOST_TEST_CASE(&check_iterator_range));
test->add(BOOST_TEST_CASE(&check_iterator_range_operator<iterator_range_test_detail::less>));
test->add(BOOST_TEST_CASE(&check_iterator_range_operator<iterator_range_test_detail::less_or_equal>));
test->add(BOOST_TEST_CASE(&check_iterator_range_operator<iterator_range_test_detail::greater>));
test->add(BOOST_TEST_CASE(&check_iterator_range_operator<iterator_range_test_detail::greater_or_equal>));
test->add(BOOST_TEST_CASE(&check_iterator_range_operator<iterator_range_test_detail::equal_to>));
test->add(BOOST_TEST_CASE(&check_iterator_range_operator<iterator_range_test_detail::not_equal_to>));
return test;
}
//
//
// Check that constness is propgated correct from

View File

@ -391,7 +391,8 @@ namespace boost
Container reference(cont);
Container test_cont(cont);
range_return_t range_result = test_range_t()(policy, test_cont);
test_range_t test_range_fn;
range_return_t range_result = test_range_fn(policy, test_cont);
iterator_t reference_it = policy.reference(reference);
check_results<result_type>::test(test_cont, reference,