1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
|
#include <algorithm> //pour mismatch
#include <iterator> //pour begin, end
#include <utility> //pour pair
template<class InputIterator1, class InputIterator2>
bool is_less_or_equal(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2 )
{
while ( (first1!=last1) && (*first1<=*first2) )
{ ++first1; ++first2; }
return first1==last1;
}
template<class Sequence>
int compareListe(Sequence const& L, Sequence const& R)
{
auto res = std::mismatch(std::begin(L), std::end(L), std::begin(R));
if(res.first == std::end(L))
return 0; //égalité "parfaite" entre L et R
if(*res.first < *res.second)
{
//à priori L domine R.
//on vérifie notre à priori sur le reste de la collection :
if(is_less_or_equal(res.first, std::end(L), res.second))
return 1; //notre à priori s'est vérifié
return 0;
}
//à priori R domine L.
//on vérifie notre à priori sur le reste de la collection :
else if(is_less_or_equal(res.second, std::end(R), res.first))
return 2; //notre à priori s'est vérifié
return 0;
}
#define BOOST_TEST_MODULE route test
#include <boost/test/unit_test.hpp>
#include <vector>
#include <boost/assign/list_of.hpp>
BOOST_AUTO_TEST_CASE( test_dominance )
{
using namespace boost::assign;
BOOST_CHECK_EQUAL( compareListe(list_of(0)(1)(2)(3), list_of(1)(2)(3)(4)), 1 );
BOOST_CHECK_EQUAL( compareListe(list_of(1)(2)(3)(4), list_of(0)(1)(2)(3)), 2 );
BOOST_CHECK_EQUAL( compareListe(list_of(1)(2)(3)(4), list_of(1)(2)(3)(4)), 0 );
BOOST_CHECK_EQUAL( compareListe(list_of(0)(1)(2)(3), list_of(1)(2)(0)(4)), 0 );
BOOST_CHECK_EQUAL( compareListe(list_of(2)(1)(2)(3), list_of(1)(2)(3)(4)), 0 );
} |
Partager