Files
range/test/algorithm_example.cpp

93 lines
2.6 KiB
C++
Raw Permalink Normal View History

2004-06-29 02:58:13 +00:00
// Boost.Range library
//
// Copyright Thorsten Ottosen 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/
//
2004-07-29 14:45:19 +00:00
#include <boost/detail/workaround.hpp>
2004-07-29 14:20:35 +00:00
#if BOOST_WORKAROUND(BOOST_BORLANDC, BOOST_TESTED_AT(0x564))
# pragma warn -8091 // suppress warning in Boost.Test
2004-07-29 14:20:35 +00:00
# pragma warn -8057 // unused argument argc/argv in Boost.Test
#endif
2004-08-16 22:07:07 +00:00
#include <boost/range/functions.hpp>
2004-08-10 16:05:53 +00:00
#include <boost/range/metafunctions.hpp>
#include <boost/range/as_literal.hpp>
2004-08-12 10:58:13 +00:00
#include <boost/test/test_tools.hpp>
2004-06-29 02:58:13 +00:00
#include <iostream>
#include <algorithm>
#include <vector>
#include <utility>
namespace
{
2004-08-10 16:05:53 +00:00
//
// example: extracting bounds in a generic algorithm
2004-08-10 16:05:53 +00:00
//
template< typename Range, typename T >
2004-08-16 22:07:07 +00:00
inline typename boost::range_iterator<Range>::type
2004-08-10 16:05:53 +00:00
find( Range& c, const T& value )
{
2004-06-29 02:58:13 +00:00
return std::find( boost::begin( c ), boost::end( c ), value );
2004-08-10 16:05:53 +00:00
}
template< typename Range, typename T >
inline typename boost::range_iterator<Range>::type
2004-08-10 16:05:53 +00:00
find( const Range& c, const T& value )
{
2004-06-29 02:58:13 +00:00
return std::find( boost::begin( c ), boost::end( c ), value );
2004-08-10 16:05:53 +00:00
}
2004-06-29 02:58:13 +00:00
2004-08-10 16:05:53 +00:00
//
// replace first value and return its index
//
template< class Range, class T >
inline typename boost::range_difference<Range>::type
2004-08-10 16:05:53 +00:00
my_generic_replace( Range& c, const T& value, const T& replacement )
{
2004-08-16 22:07:07 +00:00
typename boost::range_iterator<Range>::type found = find( c, value );
2004-06-29 02:58:13 +00:00
if( found != boost::end( c ) )
*found = replacement;
return std::distance( boost::begin( c ), found );
2004-08-10 16:05:53 +00:00
}
2004-06-29 02:58:13 +00:00
}
2004-08-12 10:58:13 +00:00
void check_algorithm()
2004-06-29 02:58:13 +00:00
{
2004-08-10 16:05:53 +00:00
//
// usage
//
const int N = 5;
2004-06-29 02:58:13 +00:00
std::vector<int> my_vector;
2004-07-28 11:01:10 +00:00
int values[] = { 1,2,3,4,5,6,7,8,9 };
my_vector.assign( values, values + 9 );
2004-06-29 02:58:13 +00:00
typedef std::vector<int>::iterator iterator;
std::pair<iterator,iterator> my_view( boost::begin( my_vector ),
boost::begin( my_vector ) + N );
BOOST_CHECK_EQUAL( my_generic_replace( my_vector, 4, 2 ), 3 );
2004-08-12 10:58:13 +00:00
BOOST_CHECK_EQUAL( my_generic_replace( my_view, 4, 2 ), N );
}
2005-02-05 20:07:02 +00:00
#include <boost/test/unit_test.hpp>
using boost::unit_test::test_suite;
2004-08-12 10:58:13 +00:00
test_suite* init_unit_test_suite( int argc, char* argv[] )
{
test_suite* test = BOOST_TEST_SUITE( "Range Test Suite" );
test->add( BOOST_TEST_CASE( &check_algorithm ) );
return test;
2004-06-29 02:58:13 +00:00
}
2004-08-12 10:58:13 +00:00