forked from boostorg/algorithm
Initial revision.
[SVN r59253]
This commit is contained in:
11
creasing/example/Jamfile
Normal file
11
creasing/example/Jamfile
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
# Boost.Creasing Library test Jamfile
|
||||||
|
#
|
||||||
|
# Copyright (c) 2010 Nuovation System Designs, LLC
|
||||||
|
# Grant Erickson <gerickson@nuovations.com>
|
||||||
|
#
|
||||||
|
# Distributed under 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)
|
||||||
|
|
||||||
|
exe creasing_ex : creasing_ex.cpp ;
|
||||||
|
|
69
creasing/example/creasing_ex.cpp
Normal file
69
creasing/example/creasing_ex.cpp
Normal file
@ -0,0 +1,69 @@
|
|||||||
|
// Copyright (c) 2010 Nuovation System Designs, LLC
|
||||||
|
// Grant Erickson <gerickson@nuovations.com>
|
||||||
|
//
|
||||||
|
// Distributed under 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)
|
||||||
|
//
|
||||||
|
// See http://www.boost.org/ for latest version.
|
||||||
|
|
||||||
|
#include <algorithm>
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
#include <boost/algorithm/creasing.hpp>
|
||||||
|
#include <boost/lambda/lambda.hpp>
|
||||||
|
|
||||||
|
/* Preprocessor Defines */
|
||||||
|
|
||||||
|
#define elementsof(v) (sizeof (v) / sizeof (v[0]))
|
||||||
|
#define begin(v) (v)
|
||||||
|
#define end(v) (v + elementsof (v))
|
||||||
|
|
||||||
|
static void
|
||||||
|
output_sequence_property(bool result, const char * property)
|
||||||
|
{
|
||||||
|
std::cout << " "
|
||||||
|
<< ((result) ? "Is " : "Is not ")
|
||||||
|
<< property
|
||||||
|
<< std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename InputIterator>
|
||||||
|
static void
|
||||||
|
analyze_sequence(InputIterator first, InputIterator last)
|
||||||
|
{
|
||||||
|
bool is_increasing;
|
||||||
|
bool is_decreasing;
|
||||||
|
bool is_strictly_increasing;
|
||||||
|
bool is_strictly_decreasing;
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
using namespace boost::lambda;
|
||||||
|
|
||||||
|
is_increasing = boost::is_increasing(first, last);
|
||||||
|
is_decreasing = boost::is_decreasing(first, last);
|
||||||
|
is_strictly_increasing = boost::is_strictly_increasing(first, last);
|
||||||
|
is_strictly_decreasing = boost::is_strictly_decreasing(first, last);
|
||||||
|
|
||||||
|
cout << "The sequence { ";
|
||||||
|
for_each(first, last, cout << _1 << ' ');
|
||||||
|
cout << " }..." << endl;
|
||||||
|
|
||||||
|
output_sequence_property(is_increasing, "increasing");
|
||||||
|
output_sequence_property(is_strictly_increasing, "strictly increasing");
|
||||||
|
output_sequence_property(is_decreasing, "decreasing");
|
||||||
|
output_sequence_property(is_strictly_decreasing, "strictly decreasing");
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
const int sequence1[] = { 1, 2, 3, 4, 5 };
|
||||||
|
const int sequence2[] = { 7, 7, 7, 7, 7 };
|
||||||
|
const float sequence3[] = { 7.618, 4.971, 6.126, 1.727, 6.510 };
|
||||||
|
|
||||||
|
analyze_sequence(begin(sequence1), end(sequence1));
|
||||||
|
analyze_sequence(begin(sequence2), end(sequence2));
|
||||||
|
analyze_sequence(begin(sequence3), end(sequence3));
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
18
creasing/test/Jamfile.v2
Normal file
18
creasing/test/Jamfile.v2
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
# Boost.Creasing Library test Jamfile
|
||||||
|
#
|
||||||
|
# Copyright (c) 2010 Nuovation System Designs, LLC
|
||||||
|
# Grant Erickson <gerickson@nuovations.com>
|
||||||
|
#
|
||||||
|
# Distributed under 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)
|
||||||
|
|
||||||
|
import testing ;
|
||||||
|
|
||||||
|
{
|
||||||
|
test-suite algorithm/creasing:
|
||||||
|
: [ run creasing_test.cpp
|
||||||
|
: : : : creasing ]
|
||||||
|
;
|
||||||
|
}
|
||||||
|
|
165
creasing/test/creasing_test.cpp
Normal file
165
creasing/test/creasing_test.cpp
Normal file
@ -0,0 +1,165 @@
|
|||||||
|
// Copyright (c) 2010 Nuovation System Designs, LLC
|
||||||
|
// Grant Erickson <gerickson@nuovations.com>
|
||||||
|
//
|
||||||
|
// Distributed under 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)
|
||||||
|
//
|
||||||
|
// See http://www.boost.org/ for latest version.
|
||||||
|
|
||||||
|
#include <algorithm>
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
#include <boost/algorithm/creasing.hpp>
|
||||||
|
#include <boost/lambda/lambda.hpp>
|
||||||
|
#include <boost/test/unit_test.hpp>
|
||||||
|
|
||||||
|
using namespace boost;
|
||||||
|
|
||||||
|
/* Preprocessor Defines */
|
||||||
|
|
||||||
|
#define elementsof(v) (sizeof (v) / sizeof (v[0]))
|
||||||
|
#define begin(v) (v)
|
||||||
|
#define end(v) (v + elementsof (v))
|
||||||
|
|
||||||
|
template <typename InputIterator, typename Function>
|
||||||
|
static bool
|
||||||
|
test_sequence(InputIterator inBegin,
|
||||||
|
InputIterator inEnd,
|
||||||
|
Function inFunction)
|
||||||
|
{
|
||||||
|
return (inFunction(inBegin, inEnd));
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
test_creasing(void)
|
||||||
|
{
|
||||||
|
const int strictlyIncreasingValues[] = { 1, 2, 3, 4, 5 };
|
||||||
|
const int strictlyDecreasingValues[] = { 9, 8, 7, 6, 5 };
|
||||||
|
const int increasingValues[] = { 1, 2, 2, 2, 5 };
|
||||||
|
const int decreasingValues[] = { 9, 7, 7, 7, 5 };
|
||||||
|
const int randomValues[] = { 3, 6, 1, 2, 7 };
|
||||||
|
const int constantValues[] = { 7, 7, 7, 7, 7 };
|
||||||
|
bool result;
|
||||||
|
|
||||||
|
// Test a strictly increasing sequence
|
||||||
|
|
||||||
|
result = test_sequence(begin(strictlyIncreasingValues),
|
||||||
|
end(strictlyIncreasingValues),
|
||||||
|
is_strictly_increasing<int const *>);
|
||||||
|
BOOST_CHECK_EQUAL(result, true);
|
||||||
|
result = test_sequence(begin(strictlyIncreasingValues),
|
||||||
|
end(strictlyIncreasingValues),
|
||||||
|
is_increasing<int const *>);
|
||||||
|
BOOST_CHECK_EQUAL(result, true);
|
||||||
|
result = test_sequence(begin(strictlyIncreasingValues),
|
||||||
|
end(strictlyIncreasingValues),
|
||||||
|
is_strictly_decreasing<int const *>);
|
||||||
|
BOOST_CHECK_EQUAL(result, false);
|
||||||
|
result = test_sequence(begin(strictlyIncreasingValues),
|
||||||
|
end(strictlyIncreasingValues),
|
||||||
|
is_decreasing<int const *>);
|
||||||
|
BOOST_CHECK_EQUAL(result, false);
|
||||||
|
|
||||||
|
// Test a strictly decreasing sequence
|
||||||
|
|
||||||
|
result = test_sequence(begin(strictlyDecreasingValues),
|
||||||
|
end(strictlyDecreasingValues),
|
||||||
|
is_strictly_increasing<int const *>);
|
||||||
|
BOOST_CHECK_EQUAL(result, false);
|
||||||
|
result = test_sequence(begin(strictlyDecreasingValues),
|
||||||
|
end(strictlyDecreasingValues),
|
||||||
|
is_increasing<int const *>);
|
||||||
|
BOOST_CHECK_EQUAL(result, false);
|
||||||
|
result = test_sequence(begin(strictlyDecreasingValues),
|
||||||
|
end(strictlyDecreasingValues),
|
||||||
|
is_strictly_decreasing<int const *>);
|
||||||
|
BOOST_CHECK_EQUAL(result, true);
|
||||||
|
result = test_sequence(begin(strictlyDecreasingValues),
|
||||||
|
end(strictlyDecreasingValues),
|
||||||
|
is_decreasing<int const *>);
|
||||||
|
BOOST_CHECK_EQUAL(result, true);
|
||||||
|
|
||||||
|
// Test an increasing sequence
|
||||||
|
|
||||||
|
result = test_sequence(begin(increasingValues),
|
||||||
|
end(increasingValues),
|
||||||
|
is_strictly_increasing<int const *>);
|
||||||
|
BOOST_CHECK_EQUAL(result, false);
|
||||||
|
result = test_sequence(begin(increasingValues),
|
||||||
|
end(increasingValues),
|
||||||
|
is_increasing<int const *>);
|
||||||
|
BOOST_CHECK_EQUAL(result, true);
|
||||||
|
result = test_sequence(begin(increasingValues),
|
||||||
|
end(increasingValues),
|
||||||
|
is_strictly_decreasing<int const *>);
|
||||||
|
BOOST_CHECK_EQUAL(result, false);
|
||||||
|
result = test_sequence(begin(increasingValues),
|
||||||
|
end(increasingValues),
|
||||||
|
is_decreasing<int const *>);
|
||||||
|
BOOST_CHECK_EQUAL(result, false);
|
||||||
|
|
||||||
|
// Test a decreasing sequence
|
||||||
|
|
||||||
|
result = test_sequence(begin(decreasingValues),
|
||||||
|
end(decreasingValues),
|
||||||
|
is_strictly_increasing<int const *>);
|
||||||
|
BOOST_CHECK_EQUAL(result, false);
|
||||||
|
result = test_sequence(begin(decreasingValues),
|
||||||
|
end(decreasingValues),
|
||||||
|
is_increasing<int const *>);
|
||||||
|
BOOST_CHECK_EQUAL(result, false);
|
||||||
|
result = test_sequence(begin(decreasingValues),
|
||||||
|
end(decreasingValues),
|
||||||
|
is_strictly_decreasing<int const *>);
|
||||||
|
BOOST_CHECK_EQUAL(result, false);
|
||||||
|
result = test_sequence(begin(decreasingValues),
|
||||||
|
end(decreasingValues),
|
||||||
|
is_decreasing<int const *>);
|
||||||
|
BOOST_CHECK_EQUAL(result, true);
|
||||||
|
|
||||||
|
// Test a random sequence
|
||||||
|
|
||||||
|
result = test_sequence(begin(randomValues),
|
||||||
|
end(randomValues),
|
||||||
|
is_strictly_increasing<int const *>);
|
||||||
|
BOOST_CHECK_EQUAL(result, false);
|
||||||
|
result = test_sequence(begin(randomValues),
|
||||||
|
end(randomValues),
|
||||||
|
is_increasing<int const *>);
|
||||||
|
BOOST_CHECK_EQUAL(result, false);
|
||||||
|
result = test_sequence(begin(randomValues),
|
||||||
|
end(randomValues),
|
||||||
|
is_strictly_decreasing<int const *>);
|
||||||
|
BOOST_CHECK_EQUAL(result, false);
|
||||||
|
result = test_sequence(begin(randomValues),
|
||||||
|
end(randomValues),
|
||||||
|
is_decreasing<int const *>);
|
||||||
|
BOOST_CHECK_EQUAL(result, false);
|
||||||
|
|
||||||
|
// Test a constant sequence
|
||||||
|
|
||||||
|
result = test_sequence(begin(constantValues),
|
||||||
|
end(constantValues),
|
||||||
|
is_strictly_increasing<int const *>);
|
||||||
|
BOOST_CHECK_EQUAL(result, false);
|
||||||
|
result = test_sequence(begin(constantValues),
|
||||||
|
end(constantValues),
|
||||||
|
is_increasing<int const *>);
|
||||||
|
BOOST_CHECK_EQUAL(result, true);
|
||||||
|
result = test_sequence(begin(constantValues),
|
||||||
|
end(constantValues),
|
||||||
|
is_strictly_decreasing<int const *>);
|
||||||
|
BOOST_CHECK_EQUAL(result, false);
|
||||||
|
result = test_sequence(begin(constantValues),
|
||||||
|
end(constantValues),
|
||||||
|
is_decreasing<int const *>);
|
||||||
|
BOOST_CHECK_EQUAL(result, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
int test_main( int, char * [] )
|
||||||
|
{
|
||||||
|
test_creasing();
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
94
include/boost/algorithm/creasing.hpp
Normal file
94
include/boost/algorithm/creasing.hpp
Normal file
@ -0,0 +1,94 @@
|
|||||||
|
// Boost creasing.hpp header file -----------------------------------------//
|
||||||
|
|
||||||
|
// Copyright (c) 2010 Nuovation System Designs, LLC
|
||||||
|
// Grant Erickson <gerickson@nuovations.com>
|
||||||
|
//
|
||||||
|
// Distributed under 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)
|
||||||
|
//
|
||||||
|
// See http://www.boost.org/ for latest version.
|
||||||
|
//
|
||||||
|
// Description:
|
||||||
|
// A set of four algorithms for inquiring about the properties of
|
||||||
|
// sequences, including:
|
||||||
|
//
|
||||||
|
// - Increasing
|
||||||
|
// - Decreasing
|
||||||
|
// - Strictly Increasing
|
||||||
|
// - Strictly Decreasing
|
||||||
|
|
||||||
|
#ifndef BOOST_ALGORITHM_CREASING_HPP
|
||||||
|
#define BOOST_ALGORITHM_CREASING_HPP
|
||||||
|
|
||||||
|
#include <algorithm>
|
||||||
|
#include <functional>
|
||||||
|
#include <iterator>
|
||||||
|
|
||||||
|
namespace boost {
|
||||||
|
|
||||||
|
namespace detail {
|
||||||
|
|
||||||
|
template <typename ForwardIterator, typename BinaryPredicate>
|
||||||
|
bool
|
||||||
|
is_creasing(ForwardIterator first, ForwardIterator last,
|
||||||
|
BinaryPredicate binary_pred)
|
||||||
|
{
|
||||||
|
return std::adjacent_find(first,
|
||||||
|
last,
|
||||||
|
std::not2(binary_pred)) == last;
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace detail
|
||||||
|
|
||||||
|
template <typename ForwardIterator>
|
||||||
|
bool
|
||||||
|
is_increasing(ForwardIterator first, ForwardIterator last)
|
||||||
|
{
|
||||||
|
typedef typename std::iterator_traits<ForwardIterator>::value_type
|
||||||
|
value_type;
|
||||||
|
|
||||||
|
return detail::is_creasing(first,
|
||||||
|
last,
|
||||||
|
std::less_equal<value_type>());
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename ForwardIterator>
|
||||||
|
bool
|
||||||
|
is_decreasing(ForwardIterator first, ForwardIterator last)
|
||||||
|
{
|
||||||
|
typedef typename std::iterator_traits<ForwardIterator>::value_type
|
||||||
|
value_type;
|
||||||
|
|
||||||
|
return detail::is_creasing(first,
|
||||||
|
last,
|
||||||
|
std::greater_equal<value_type>());
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename ForwardIterator>
|
||||||
|
bool
|
||||||
|
is_strictly_increasing(ForwardIterator first, ForwardIterator last)
|
||||||
|
{
|
||||||
|
typedef typename std::iterator_traits<ForwardIterator>::value_type
|
||||||
|
value_type;
|
||||||
|
|
||||||
|
return detail::is_creasing(first,
|
||||||
|
last,
|
||||||
|
std::less<value_type>());
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename ForwardIterator>
|
||||||
|
bool
|
||||||
|
is_strictly_decreasing(ForwardIterator first, ForwardIterator last)
|
||||||
|
{
|
||||||
|
typedef typename std::iterator_traits<ForwardIterator>::value_type
|
||||||
|
value_type;
|
||||||
|
|
||||||
|
return detail::is_creasing(first,
|
||||||
|
last,
|
||||||
|
std::greater<value_type>());
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace boost
|
||||||
|
|
||||||
|
#endif // BOOST_ALGORITHM_CREASING_HPP
|
Reference in New Issue
Block a user