From 459f65cd6453fcb5fe0210532a8bedf1922d925e Mon Sep 17 00:00:00 2001 From: Grant Erickson Date: Sun, 24 Jan 2010 16:42:02 +0000 Subject: [PATCH] Initial revision. [SVN r59253] --- creasing/example/Jamfile | 11 ++ creasing/example/creasing_ex.cpp | 69 +++++++++++ creasing/test/Jamfile.v2 | 18 +++ creasing/test/creasing_test.cpp | 165 +++++++++++++++++++++++++++ include/boost/algorithm/creasing.hpp | 94 +++++++++++++++ 5 files changed, 357 insertions(+) create mode 100644 creasing/example/Jamfile create mode 100644 creasing/example/creasing_ex.cpp create mode 100644 creasing/test/Jamfile.v2 create mode 100644 creasing/test/creasing_test.cpp create mode 100644 include/boost/algorithm/creasing.hpp diff --git a/creasing/example/Jamfile b/creasing/example/Jamfile new file mode 100644 index 0000000..c5ea6ef --- /dev/null +++ b/creasing/example/Jamfile @@ -0,0 +1,11 @@ +# Boost.Creasing Library test Jamfile +# +# Copyright (c) 2010 Nuovation System Designs, LLC +# Grant Erickson +# +# 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 ; + diff --git a/creasing/example/creasing_ex.cpp b/creasing/example/creasing_ex.cpp new file mode 100644 index 0000000..9f9f043 --- /dev/null +++ b/creasing/example/creasing_ex.cpp @@ -0,0 +1,69 @@ +// Copyright (c) 2010 Nuovation System Designs, LLC +// Grant Erickson +// +// 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 +#include + +#include +#include + +/* 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 +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; +} diff --git a/creasing/test/Jamfile.v2 b/creasing/test/Jamfile.v2 new file mode 100644 index 0000000..f7424eb --- /dev/null +++ b/creasing/test/Jamfile.v2 @@ -0,0 +1,18 @@ +# Boost.Creasing Library test Jamfile +# +# Copyright (c) 2010 Nuovation System Designs, LLC +# Grant Erickson +# +# 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 ] + ; +} + diff --git a/creasing/test/creasing_test.cpp b/creasing/test/creasing_test.cpp new file mode 100644 index 0000000..61f62fe --- /dev/null +++ b/creasing/test/creasing_test.cpp @@ -0,0 +1,165 @@ +// Copyright (c) 2010 Nuovation System Designs, LLC +// Grant Erickson +// +// 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 +#include + +#include +#include +#include + +using namespace boost; + +/* Preprocessor Defines */ + +#define elementsof(v) (sizeof (v) / sizeof (v[0])) +#define begin(v) (v) +#define end(v) (v + elementsof (v)) + +template +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); + BOOST_CHECK_EQUAL(result, true); + result = test_sequence(begin(strictlyIncreasingValues), + end(strictlyIncreasingValues), + is_increasing); + BOOST_CHECK_EQUAL(result, true); + result = test_sequence(begin(strictlyIncreasingValues), + end(strictlyIncreasingValues), + is_strictly_decreasing); + BOOST_CHECK_EQUAL(result, false); + result = test_sequence(begin(strictlyIncreasingValues), + end(strictlyIncreasingValues), + is_decreasing); + BOOST_CHECK_EQUAL(result, false); + + // Test a strictly decreasing sequence + + result = test_sequence(begin(strictlyDecreasingValues), + end(strictlyDecreasingValues), + is_strictly_increasing); + BOOST_CHECK_EQUAL(result, false); + result = test_sequence(begin(strictlyDecreasingValues), + end(strictlyDecreasingValues), + is_increasing); + BOOST_CHECK_EQUAL(result, false); + result = test_sequence(begin(strictlyDecreasingValues), + end(strictlyDecreasingValues), + is_strictly_decreasing); + BOOST_CHECK_EQUAL(result, true); + result = test_sequence(begin(strictlyDecreasingValues), + end(strictlyDecreasingValues), + is_decreasing); + BOOST_CHECK_EQUAL(result, true); + + // Test an increasing sequence + + result = test_sequence(begin(increasingValues), + end(increasingValues), + is_strictly_increasing); + BOOST_CHECK_EQUAL(result, false); + result = test_sequence(begin(increasingValues), + end(increasingValues), + is_increasing); + BOOST_CHECK_EQUAL(result, true); + result = test_sequence(begin(increasingValues), + end(increasingValues), + is_strictly_decreasing); + BOOST_CHECK_EQUAL(result, false); + result = test_sequence(begin(increasingValues), + end(increasingValues), + is_decreasing); + BOOST_CHECK_EQUAL(result, false); + + // Test a decreasing sequence + + result = test_sequence(begin(decreasingValues), + end(decreasingValues), + is_strictly_increasing); + BOOST_CHECK_EQUAL(result, false); + result = test_sequence(begin(decreasingValues), + end(decreasingValues), + is_increasing); + BOOST_CHECK_EQUAL(result, false); + result = test_sequence(begin(decreasingValues), + end(decreasingValues), + is_strictly_decreasing); + BOOST_CHECK_EQUAL(result, false); + result = test_sequence(begin(decreasingValues), + end(decreasingValues), + is_decreasing); + BOOST_CHECK_EQUAL(result, true); + + // Test a random sequence + + result = test_sequence(begin(randomValues), + end(randomValues), + is_strictly_increasing); + BOOST_CHECK_EQUAL(result, false); + result = test_sequence(begin(randomValues), + end(randomValues), + is_increasing); + BOOST_CHECK_EQUAL(result, false); + result = test_sequence(begin(randomValues), + end(randomValues), + is_strictly_decreasing); + BOOST_CHECK_EQUAL(result, false); + result = test_sequence(begin(randomValues), + end(randomValues), + is_decreasing); + BOOST_CHECK_EQUAL(result, false); + + // Test a constant sequence + + result = test_sequence(begin(constantValues), + end(constantValues), + is_strictly_increasing); + BOOST_CHECK_EQUAL(result, false); + result = test_sequence(begin(constantValues), + end(constantValues), + is_increasing); + BOOST_CHECK_EQUAL(result, true); + result = test_sequence(begin(constantValues), + end(constantValues), + is_strictly_decreasing); + BOOST_CHECK_EQUAL(result, false); + result = test_sequence(begin(constantValues), + end(constantValues), + is_decreasing); + BOOST_CHECK_EQUAL(result, true); +} + +int test_main( int, char * [] ) +{ + test_creasing(); + + return 0; +} diff --git a/include/boost/algorithm/creasing.hpp b/include/boost/algorithm/creasing.hpp new file mode 100644 index 0000000..a7b9cd1 --- /dev/null +++ b/include/boost/algorithm/creasing.hpp @@ -0,0 +1,94 @@ +// Boost creasing.hpp header file -----------------------------------------// + +// Copyright (c) 2010 Nuovation System Designs, LLC +// Grant Erickson +// +// 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 +#include +#include + +namespace boost { + + namespace detail { + + template + bool + is_creasing(ForwardIterator first, ForwardIterator last, + BinaryPredicate binary_pred) + { + return std::adjacent_find(first, + last, + std::not2(binary_pred)) == last; + } + + } // namespace detail + + template + bool + is_increasing(ForwardIterator first, ForwardIterator last) + { + typedef typename std::iterator_traits::value_type + value_type; + + return detail::is_creasing(first, + last, + std::less_equal()); + } + + template + bool + is_decreasing(ForwardIterator first, ForwardIterator last) + { + typedef typename std::iterator_traits::value_type + value_type; + + return detail::is_creasing(first, + last, + std::greater_equal()); + } + + template + bool + is_strictly_increasing(ForwardIterator first, ForwardIterator last) + { + typedef typename std::iterator_traits::value_type + value_type; + + return detail::is_creasing(first, + last, + std::less()); + } + + template + bool + is_strictly_decreasing(ForwardIterator first, ForwardIterator last) + { + typedef typename std::iterator_traits::value_type + value_type; + + return detail::is_creasing(first, + last, + std::greater()); + } + +} // namespace boost + +#endif // BOOST_ALGORITHM_CREASING_HPP