Compare commits

...

77 Commits

Author SHA1 Message Date
badf1b3f0c Merge broken links to master. NFC 2018-07-31 22:57:40 -07:00
e4dfe0885f Fix broken link to license file in header comment. NFC 2018-07-30 13:07:36 -07:00
4f9afb0682 Merge for the 1.68.0 beta 2018-07-04 11:27:27 -07:00
3659b18846 Add newlines at the end of the fuzzing files 2018-07-04 11:26:52 -07:00
6bb5fa5ee0 Remove 'wrappers' 2018-07-04 11:24:01 -07:00
ee7a0247be Merge pull request #48 from tzlaine/develop
Add Travis and Appveyor CI config files.
2018-05-31 10:05:29 -07:00
d1b0024aad Add Travis and Appveyor CI config files. 2018-05-19 13:47:53 -05:00
ecb8bfab76 Merge pull request #47 from tzlaine/develop
Add find() variants.
2018-05-18 07:37:36 -07:00
3f2a962ace East const -> west const. I regret nothing! 2018-05-14 19:19:52 -05:00
6b2246468e find_{not,*backward} docs copy editing. 2018-05-14 19:15:19 -05:00
ea7d35d949 Added some comments 2018-05-13 17:24:36 -07:00
c5c5d24ff3 Add range-based overloads of find*_backward(). 2018-05-12 16:04:12 -05:00
6c68cf8624 Add range-based overload of find_not(). 2018-05-12 16:04:01 -05:00
8c9d5e858c Add docs for find_backward.hpp. 2018-05-11 15:20:12 -05:00
86d26e7e2e Add docs for find_not.hpp. 2018-05-10 19:01:12 -05:00
94460b0ca5 Add tests for find_backward.hpp. 2018-05-10 17:56:16 -05:00
45a4d2580c Add tests for find_not.hpp. 2018-05-10 13:21:21 -05:00
d7d91d431a Add find_not() and the four find_*_backward overloads from Boost.Text. Needs
tests.
2018-05-09 12:08:46 -05:00
7ec1c5c413 Remove too-simple test, add tests for the variants of minmax_element 2018-05-02 11:41:16 -07:00
d574d1edd7 Fuzzing targets for minmax 2018-05-01 19:10:47 -07:00
6d0126df8a Catch up with the changes; apply_permutation; constexpr, etc 2018-05-01 08:17:06 -07:00
3af7acabc7 fix constexpr-ness of a couple of algorithhms - and tests. Based on Pull Request #44 by Flast - thanks! 2018-04-04 11:18:38 -07:00
509201f29f Merge pull request #46 from DanielaE/fix/missing-constexpr
functions used in constexpr context must be marked 'constexpr'
2018-03-25 13:01:44 -07:00
b0f04afcb0 functions used in constexpr context must be marked 'constexpr'
Signed-off-by: Daniela Engert <dani@ngrt.de>
2018-03-25 17:46:15 +02:00
df58b44581 Merge pull request #45 from samuela/patch-1
docstring fixes - Thanks!
2018-03-20 19:05:03 -07:00
c34713b983 Revert "Fix clang "parameter not found" warning"
This reverts commit aa8cce5bb4.
2018-03-20 19:01:56 -07:00
aa8cce5bb4 Fix clang "parameter not found" warning 2018-03-20 17:57:58 -07:00
d6c8b271b3 remove other incorrect docstring params 2018-03-20 17:53:09 -07:00
50a2115236 docstring fix for first_finder 2018-03-20 17:50:04 -07:00
739d1ed8a2 Merge pull request #43 from jeremy-murphy/C++14_constexpr
Add BOOST_CXX14_CONSTEXPR to identity_operation.
2018-02-05 07:09:53 -08:00
298310f521 Add BOOST_CXX14_CONSTEXPR to identity_operation. 2018-02-06 00:47:41 +11:00
63c85e7c5e Qualify calls to Boost.Algorithm algs that have the same name as ones in the standard; prevents ADL ambiguities. Thanks to Billy O'Neill for the heads up. 2018-01-10 10:26:21 -08:00
cb52d61054 Merge pull request #39 from ZaMaZaN4iK/feature_branch/apply_permutation
Apply_permutation
2017-11-01 12:57:31 -07:00
4ac6b84cfa Merge pull request #41 from DanielaE/fix/typo-in-test
Copy-and-paste error ...
2017-10-08 07:38:32 -07:00
0539e0d84a Copy-and-paste error ...
Signed-off-by: Daniela Engert <dani@ngrt.de>
2017-10-08 10:33:22 +02:00
7df35ffd56 Different fixes 2017-08-30 20:25:15 +03:00
554db2f07d Merge pull request #35 from very-cool-name/apolukhin/constexpr
Apolukhin/constexpr
2017-08-29 09:07:59 -07:00
f9583cfd39 Merge pull request #36 from DanielaE/feature/replace-deprecated
Conditionally replace deprecated c++98 features by c++11 ones
2017-08-29 07:41:02 -07:00
8ae18ae455 Conditionally replace deprecated c++98 features by c++11 ones
change std::random_shuffle -> std::shuffle + random_engine if indicated by the supported language level or Boost configuration macro.

Signed-off-by: Daniela Engert <dani@ngrt.de>
2017-07-20 16:33:17 +02:00
32016b3c79 Fixed missed header for std::swap 2017-07-19 22:10:29 +03:00
15c7ff3828 Fixed compatibility with old compilers 2017-07-19 02:15:06 +03:00
daef6d41c5 C++17 algorithms 2017-06-28 20:13:38 -07:00
f14719c3a9 Replace an assert in a test with BOOST_CHECK 2017-06-28 10:04:25 -07:00
6bd8bc197a use boost::algorithm::iota in the tests instead of std::iota, which requires C++11 2017-06-28 07:58:21 -07:00
334737eebe Implement the C++17 new algorithms 2017-06-27 16:43:17 -07:00
000040b3d9 binary_function is removed from equal and unary_function removed from tests. 2017-06-19 19:03:00 +03:00
055075aa61 Bug fixes and is_partititioned_XXX for the 1.65.0 release 2017-06-14 18:49:39 -07:00
e07e8e65c6 Fixed references 2017-05-19 18:50:03 -06:00
ba7655eb3a Add documentation 2017-05-19 16:59:11 -06:00
1970454ecb Added test 2017-05-18 23:33:26 -06:00
1ed284d696 Added initial for apply_permutation 2017-05-18 20:38:07 -06:00
d6b7f3da90 Use boost::begin/end instead of macros 2017-05-03 22:38:21 -07:00
0e62dfb92e Merge pull request #33 from ZaMaZaN4iK/feature_branch/is_partitioned_until
is_partitioned_until support
2017-05-03 18:15:09 -07:00
f250014931 Merge pull request #38 from DanielaE/fix/narrowing
fix narrowing conversions
2017-05-03 18:14:28 -07:00
56d88410c6 fix narrowing conversions
Signed-off-by: Daniela Engert <dani@ngrt.de>
2017-04-22 18:35:17 +02:00
968d30e9e3 Merge branch 'develop' into apolukhin/constexpr 2017-04-10 12:16:38 +03:00
dfa332e915 Updated constexpr tests for all affected modules. Added conditional constexpr to equal, which uses std::distance. 2017-04-07 22:31:23 +03:00
5d76dab9b8 Merge remote-tracking branch 'refs/remotes/origin/feature_branch/is_partitioned_until'
Conflicts:
	README.md
2017-02-22 01:26:16 +03:00
6f34145390 Removed empty README.md, fixed docs 2017-02-22 01:25:38 +03:00
a446ef0758 Cleaned README 2017-02-15 00:11:10 +03:00
383e800df9 Added examples, tests, doc 2017-02-14 15:59:07 +03:00
a838feb81a Added is_partitioned_until
* Fixed doxygen comment in is_partitioned algorithm.
2017-02-14 12:22:49 +03:00
17d4f70e97 Merge pull request #32 from jhunold/extra_semi
Remove trailing ";" - thanks!
2017-01-06 18:19:59 -08:00
d22c60c2d4 Remove trailing ";"
clang warning: extra ';' after member function definition [-Wextra-semi]
2017-01-06 09:59:12 +01:00
037fe4e33d Merge pull request #31 from danieljames/fix-algorithm-location
Build the algorithm documentation standalone.
2016-12-13 19:30:52 -08:00
25d54bd1e8 Build the algorithm documentation standalone. 2016-12-13 21:38:40 +00:00
e442420d49 Change name of (another) parameter; fixes 'shadowing' warning (Trac #12623) 2016-11-25 20:50:53 -08:00
42db8a4403 Change name of parameter; fixes 'shadowing' warning (Trac #12623) 2016-11-25 08:11:01 -08:00
9b19fc00c2 use std::shuffle instead of random_shuffle in C++11 and later 2016-11-20 17:24:19 -08:00
7977bd0cdc Remove all mentions of (unary|binary)_function; not needed and they have been removed for C++17 2016-11-20 17:02:49 -08:00
efd7a8b544 Updated the docs for the searchers to reflect the new interface, and added rationale for the change. 2016-10-26 11:22:37 -07:00
8dfebc4580 Added README.md 2016-10-25 14:31:26 +03:00
6a650ade1d Merge pull request #28 from ZaMaZaN4iK/feature_branch/is_palindromic
Delete duplicate version is_palindrome
2016-10-12 16:42:14 -07:00
303ff13b86 Delete duplicate version os is_palindrome 2016-10-12 22:22:30 +03:00
eccac19108 Add, and update, documentation build targets. 2016-10-07 23:03:40 -05:00
8627d02f20 Merge pull request #2 from boostorg/develop
Sync develop branch with master
2016-08-19 21:44:27 +03:00
8d095e9d30 Added constexpr modifiers for C++14 mode and some tests 2016-01-25 13:11:01 +03:00
83 changed files with 4395 additions and 375 deletions

171
.travis.yml Normal file
View File

@ -0,0 +1,171 @@
# Copyright 2016, 2017, 2018 Peter Dimov
# Copyright 2018 T. Zachary Laine
# Distributed under the Boost Software License, Version 1.0.
# (See accompanying file LICENSE_1_0.txt or copy at http://boost.org/LICENSE_1_0.txt)
language: cpp
sudo: false
branches:
only:
- master
- develop
- /feature\/.*/
env:
matrix:
- BOGUS_JOB=true
matrix:
exclude:
- env: BOGUS_JOB=true
include:
- os: linux
env: TOOLSET=gcc COMPILER=g++ CXXSTD=11
- os: linux
env: TOOLSET=gcc COMPILER=g++-4.7 CXXSTD=11
addons:
apt:
packages:
- g++-4.7
sources:
- ubuntu-toolchain-r-test
- os: linux
env: TOOLSET=gcc COMPILER=g++-4.8 CXXSTD=11
addons:
apt:
packages:
- g++-4.8
sources:
- ubuntu-toolchain-r-test
- os: linux
env: TOOLSET=gcc COMPILER=g++-4.9 CXXSTD=11
addons:
apt:
packages:
- g++-4.9
sources:
- ubuntu-toolchain-r-test
- os: linux
env: TOOLSET=gcc COMPILER=g++-5 CXXSTD=11,14,1z
addons:
apt:
packages:
- g++-5
sources:
- ubuntu-toolchain-r-test
- os: linux
env: TOOLSET=gcc COMPILER=g++-6 CXXSTD=11,14,1z
addons:
apt:
packages:
- g++-6
sources:
- ubuntu-toolchain-r-test
- os: linux
env: TOOLSET=gcc COMPILER=g++-6 CXXSTD=11,14,1z CXXSTD_DIALECT=cxxstd-dialect=gnu
addons:
apt:
packages:
- g++-6
sources:
- ubuntu-toolchain-r-test
- os: linux
env: TOOLSET=gcc COMPILER=g++-7 CXXSTD=11,14,17
addons:
apt:
packages:
- g++-7
sources:
- ubuntu-toolchain-r-test
- os: linux
env: TOOLSET=clang COMPILER=clang++ CXXSTD=11
- os: linux
compiler: clang++-libc++
env: TOOLSET=clang COMPILER=clang++-libc++ CXXSTD=11,14,1z
addons:
apt:
packages:
- libc++-dev
- os: osx
env: TOOLSET=clang COMPILER=clang++ CXXSTD=11,14,1z
osx_image: xcode9.1
- os: osx
env: TOOLSET=clang COMPILER=clang++ CXXSTD=11,14,1z
osx_image: xcode9
- os: osx
env: TOOLSET=clang COMPILER=clang++ CXXSTD=11,14,1z
osx_image: xcode8.3
- os: osx
env: TOOLSET=clang COMPILER=clang++ CXXSTD=11,14,1z
osx_image: xcode8
- os: osx
env: TOOLSET=clang COMPILER=clang++ CXXSTD=11,14,1z
osx_image: xcode7.3
- os: osx
env: TOOLSET=clang COMPILER=clang++ CXXSTD=11,14,1z
osx_image: xcode6.4
install:
- BOOST_BRANCH=develop && [ "$TRAVIS_BRANCH" == "master" ] && BOOST_BRANCH=master || true
- cd ..
- git clone -b $BOOST_BRANCH --depth 1 https://github.com/boostorg/boost.git boost-root
- cd boost-root
- git submodule update --init tools/build
- git submodule update --init libs/config
- git submodule update --init libs/predef
- git submodule update --init libs/core
- git submodule update --init libs/detail
- git submodule update --init libs/range
- git submodule update --init libs/assert
- git submodule update --init libs/array
- git submodule update --init libs/type_traits
- git submodule update --init libs/static_assert
- git submodule update --init libs/iterator
- git submodule update --init libs/preprocessor
- git submodule update --init libs/mpl
- git submodule update --init libs/smart_ptr
- git submodule update --init libs/callable_traits
- git submodule update --init libs/type_index
- git submodule update --init libs/exception
- git submodule update --init libs/throw_exception
- git submodule update --init libs/utility
- git submodule update --init libs/bind
- git submodule update --init libs/ratio
- git submodule update --init libs/function
- git submodule update --init libs/integer
- git submodule update --init libs/numeric
- git submodule update --init libs/move
- git submodule update --init libs/container_hash
- git submodule update --init libs/io
- git submodule update --init libs/concept_check
- git submodule update --init libs/test
- git submodule update --init libs/timer
- git submodule update --init libs/chrono
- git submodule update --init libs/system
- cp -r $TRAVIS_BUILD_DIR/* libs/algorithm
- ./bootstrap.sh
- ./b2 headers
script:
- |-
echo "using $TOOLSET : : $COMPILER ;" > ~/user-config.jam
- ./b2 -j3 libs/algorithm/test toolset=$TOOLSET cxxstd=$CXXSTD $CXXSTD_DIALECT

93
appveyor.yml Normal file
View File

@ -0,0 +1,93 @@
# Copyright 2016 Peter Dimov
# Distributed under the Boost Software License, Version 1.0.
# (See accompanying file LICENSE_1_0.txt or copy at http://boost.org/LICENSE_1_0.txt)
version: 1.0.{build}-{branch}
shallow_clone: true
branches:
only:
- master
- develop
platform:
- x64
environment:
matrix:
- APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2017
ARGS: --toolset=msvc-14.1 address-model=64
- APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2017
ARGS: --toolset=msvc-14.1 address-model=32
- APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2017
ARGS: --toolset=msvc-14.1 address-model=64 cxxflags=-std:c++latest cxxflags=-permissive-
- ARGS: --toolset=msvc-9.0 address-model=32
- ARGS: --toolset=msvc-10.0 address-model=32
- ARGS: --toolset=msvc-11.0 address-model=32
- ARGS: --toolset=msvc-12.0 address-model=32
- ARGS: --toolset=msvc-14.0 address-model=32
- ARGS: --toolset=msvc-12.0 address-model=64
- ARGS: --toolset=msvc-14.0 address-model=64
- ARGS: --toolset=msvc-14.0 address-model=64 cxxflags=-std:c++latest
- ARGS: --toolset=gcc address-model=64
CXXSTD: 03,11,14,1z
PATH: C:\mingw-w64\x86_64-6.3.0-posix-seh-rt_v5-rev1\mingw64\bin;%PATH%
- ARGS: --toolset=gcc address-model=64
CXXSTD: 03,11,14,1z
PATH: C:\mingw-w64\x86_64-7.2.0-posix-seh-rt_v5-rev1\mingw64\bin;%PATH%
- ARGS: --toolset=gcc address-model=32 linkflags=-Wl,-allow-multiple-definition
CXXSTD: 03,11,14,1z
PATH: C:\MinGW\bin;%PATH%
- ARGS: --toolset=gcc address-model=64
CXXSTD: 03,11,14,1z
PATH: C:\cygwin64\bin;%PATH%
- ARGS: --toolset=gcc address-model=32
CXXSTD: 03,11,14,1z
PATH: C:\cygwin\bin;%PATH%
install:
- cd ..
- git clone -b %APPVEYOR_REPO_BRANCH% --depth 1 https://github.com/boostorg/boost.git boost-root
- cd boost-root
- xcopy /s /e /q %APPVEYOR_BUILD_FOLDER% libs\algorithm
- git submodule update --init tools/build
- git submodule update --init libs/config
- git submodule update --init libs/predef
- git submodule update --init libs/core
- git submodule update --init libs/detail
- git submodule update --init libs/range
- git submodule update --init libs/assert
- git submodule update --init libs/array
- git submodule update --init libs/type_traits
- git submodule update --init libs/static_assert
- git submodule update --init libs/iterator
- git submodule update --init libs/preprocessor
- git submodule update --init libs/mpl
- git submodule update --init libs/smart_ptr
- git submodule update --init libs/callable_traits
- git submodule update --init libs/type_index
- git submodule update --init libs/exception
- git submodule update --init libs/throw_exception
- git submodule update --init libs/utility
- git submodule update --init libs/bind
- git submodule update --init libs/ratio
- git submodule update --init libs/function
- git submodule update --init libs/integer
- git submodule update --init libs/numeric
- git submodule update --init libs/move
- git submodule update --init libs/container_hash
- git submodule update --init libs/io
- git submodule update --init libs/concept_check
- git submodule update --init libs/test
- git submodule update --init libs/timer
- git submodule update --init libs/chrono
- git submodule update --init libs/system
- bootstrap
- b2 headers
build: off
test_script:
- cd libs\config\test
- ..\..\..\b2 -j3 %ARGS% cxxstd=%CXXSTD%

View File

@ -16,17 +16,17 @@ using quickbook ;
using doxygen ;
using boostbook ;
doxygen autodoc
:
[ glob ../../../boost/algorithm/*.hpp
doxygen autodoc
:
[ glob ../../../boost/algorithm/*.hpp
../../../boost/algorithm/searching/*.hpp
../../../boost/algorithm/cxx11/*.hpp
../../../boost/algorithm/cxx14/*.hpp
]
:
:
<doxygen:param>"PREDEFINED=\"BOOST_ALGORITHM_DOXYGEN=1\""
<doxygen:param>WARNINGS=YES # Default NO, but useful to see warnings, especially in a logfile.
;
;
xml algorithm : algorithm.qbk ;
@ -35,7 +35,7 @@ boostbook standalone
:
algorithm
:
<dependency>autodoc
<dependency>autodoc
<xsl:param>boost.root=../../../..
<xsl:param>"boost.doxygen.reftitle=Boost.Algorithms C++ Reference"
<xsl:param>chapter.autolabel=0
@ -47,10 +47,10 @@ boostbook standalone
###############################################################################
alias boostdoc
: algorithm ../string/doc/string_algo.xml
: ../string/doc/string_algo.xml
:
: <dependency>autodoc <dependency>../string/doc//autodoc
: <dependency>../string/doc//autodoc
: ;
explicit boostdoc ;
alias boostrelease ;
alias boostrelease : standalone ;
explicit boostrelease ;

View File

@ -64,9 +64,13 @@ Thanks to all the people who have reviewed this library and made suggestions for
[section:Misc Other Algorithms]
[include clamp-hpp.qbk]
[include find_not.qbk]
[include find_backward.qbk]
[include gather.qbk]
[include hex.qbk]
[include is_palindrome.qbk]
[include is_partitioned_until.qbk]
[include apply_permutation.qbk]
[endsect]

96
doc/apply_permutation.qbk Normal file
View File

@ -0,0 +1,96 @@
[/ File apply_permutation.qbk]
[section:apply_permutation apply_permutation]
[/license
Copyright (c) 2017 Alexander Zaitsev
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)
]
The header file 'apply_permutation.hpp' contains two algorithms, apply_permutation and apply_reverse_permutation. Also there are range-based versions.
The algorithms transform the item sequence according to index sequence order.
The routine `apply_permutation` takes a item sequence and a order sequence. It reshuffles item sequence according to order sequence. Every value in order sequence means where the item comes from. Order sequence needs to be exactly a permutation of the sequence [0, 1, ... , N], where N is the biggest index in the item sequence (zero-indexed).
The routine `apply_reverse_permutation` takes a item sequence and a order sequence. It will reshuffle item sequence according to order sequence. Every value in order sequence means where the item goes to. Order sequence needs to be exactly a permutation of the sequence [0, 1, ... , N], where N is the biggest index in the item sequence (zero-indexed).
Implementations are based on these articles:
https://blogs.msdn.microsoft.com/oldnewthing/20170102-00/?p=95095
https://blogs.msdn.microsoft.com/oldnewthing/20170103-00/?p=95105
https://blogs.msdn.microsoft.com/oldnewthing/20170104-00/?p=95115
https://blogs.msdn.microsoft.com/oldnewthing/20170109-00/?p=95145
https://blogs.msdn.microsoft.com/oldnewthing/20170110-00/?p=95155
https://blogs.msdn.microsoft.com/oldnewthing/20170111-00/?p=95165
The routines come in 2 forms; the first one takes two iterators to define the item range and one iterator to define the beginning of index range. The second form takes range to define the item sequence and range to define index sequence.
[heading interface]
There are two versions of algorithms:
1) takes four iterators.
2) takes two ranges.
``
template<typename RandomAccessIterator1, typename RandomAccessIterator2>
void apply_permutation(RandomAccessIterator1 item_begin, RandomAccessIterator1 item_end,
RandomAccessIterator2 ind_begin, RandomAccessIterator2 ind_end);
template<typename Range1, typename Range2>
void apply_permutation(Range1& item_range, Range2& ind_range);
template<typename RandomAccessIterator1, typename RandomAccessIterator2>
void apply_reverse_permutation(RandomAccessIterator1 item_begin, RandomAccessIterator1 item_end,
RandomAccessIterator2 ind_begin, RandomAccessIterator2 ind_end);
template<typename Range1, typename Range2>
void apply_reverse_permutation(Range1& item_range, Range2& ind_range);
``
[heading Examples]
Given the containers:
std::vector<int> emp_vec, emp_order,
std::vector<int> one{1}, one_order{0},
std::vector<int> two{1,2}, two_order{1,0},
std::vector<int> vec{1, 2, 3, 4, 5},
std::vector<int> order{4, 2, 3, 1, 0}, then
``
apply_permutation(emp_vec, emp_order)) --> no changes
apply_reverse_permutation(emp_vec, emp_order)) --> no changes
apply_permutation(one, one_order) --> no changes
apply_reverse_permutation(one, one_order) --> no changes
apply_permutation(two, two_order) --> two:{2,1}
apply_reverse_permutation(two, two_order) --> two:{2,1}
apply_permutation(vec, order) --> vec:{5, 3, 4, 2, 1}
apply_reverse_permutation(vec, order) --> vec:{5, 4, 2, 3, 1}
``
[heading Iterator Requirements]
`apply_permutation` and 'apply_reverse_permutation' work only on RandomAccess iterators. RandomAccess iterators required both for item and index sequences.
[heading Complexity]
All of the variants of `apply_permutation` and `apply_reverse_permutation` run in ['O(N)] (linear) time.
More
[heading Exception Safety]
All of the variants of `apply_permutation` and `apply_reverse_permutation` take their parameters by iterators or reference, and do not depend upon any global state. Therefore, all the routines in this file provide the strong exception guarantee.
[heading Notes]
* If ItemSequence and IndexSequence are not equal, behavior is undefined.
* `apply_permutation` and `apply_reverse_permutation` work also on empty sequences.
* Order sequence must be zero-indexed.
* Order sequence gets permuted.
[endsect]
[/ File apply_permutation.qbk
Copyright 2017 Alexander Zaitsev
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).
]

View File

@ -37,7 +37,7 @@ public:
~boyer_moore ();
template <typename corpusIter>
corpusIter operator () ( corpusIter corpus_first, corpusIter corpus_last );
pair<corpusIter, corpusIter> operator () ( corpusIter corpus_first, corpusIter corpus_last );
};
``
@ -45,14 +45,28 @@ and here is the corresponding procedural interface:
``
template <typename patIter, typename corpusIter>
corpusIter boyer_moore_search (
pair<corpusIter, corpusIter> boyer_moore_search (
corpusIter corpus_first, corpusIter corpus_last,
patIter pat_first, patIter pat_last );
``
Each of the functions is passed two pairs of iterators. The first two define the corpus and the second two define the pattern. Note that the two pairs need not be of the same type, but they do need to "point" at the same type. In other words, `patIter::value_type` and `curpusIter::value_type` need to be the same type.
The return value of the function is an iterator pointing to the start of the pattern in the corpus. If the pattern is not found, it returns the end of the corpus (`corpus_last`).
The return value of the function is a pair of iterators pointing to the position of the pattern in the corpus. If the pattern is empty, it returns at empty range at the start of the corpus (`corpus_first`, `corpus_first`). If the pattern is not found, it returns at empty range at the end of the corpus (`corpus_last`, `corpus_last`).
[heading Compatibility Note]
Earlier versions of this searcher returned only a single iterator. As explained in [@https://cplusplusmusings.wordpress.com/2016/02/01/sometimes-you-get-things-wrong/], this was a suboptimal interface choice, and has been changed, starting in the 1.62.0 release. Old code that is expecting a single iterator return value can be updated by replacing the return value of the searcher's `operator ()` with the `.first` field of the pair.
Instead of:
``
iterator foo = searcher(a, b);
``
you now write:
``
iterator foo = searcher(a, b).first;
``
[heading Performance]

View File

@ -35,7 +35,7 @@ public:
~boyer_moore_horspool ();
template <typename corpusIter>
corpusIter operator () ( corpusIter corpus_first, corpusIter corpus_last );
pair<corpusIter, corpusIter> operator () ( corpusIter corpus_first, corpusIter corpus_last );
};
``
@ -43,14 +43,28 @@ and here is the corresponding procedural interface:
``
template <typename patIter, typename corpusIter>
corpusIter boyer_moore_horspool_search (
pair<corpusIter, corpusIter> boyer_moore_horspool_search (
corpusIter corpus_first, corpusIter corpus_last,
patIter pat_first, patIter pat_last );
``
Each of the functions is passed two pairs of iterators. The first two define the corpus and the second two define the pattern. Note that the two pairs need not be of the same type, but they do need to "point" at the same type. In other words, `patIter::value_type` and `curpusIter::value_type` need to be the same type.
The return value of the function is an iterator pointing to the start of the pattern in the corpus. If the pattern is not found, it returns the end of the corpus (`corpus_last`).
The return value of the function is a pair of iterators pointing to the position of the pattern in the corpus. If the pattern is empty, it returns at empty range at the start of the corpus (`corpus_first`, `corpus_first`). If the pattern is not found, it returns at empty range at the end of the corpus (`corpus_last`, `corpus_last`).
[heading Compatibility Note]
Earlier versions of this searcher returned only a single iterator. As explained in [@https://cplusplusmusings.wordpress.com/2016/02/01/sometimes-you-get-things-wrong/], this was a suboptimal interface choice, and has been changed, starting in the 1.62.0 release. Old code that is expecting a single iterator return value can be updated by replacing the return value of the searcher's `operator ()` with the `.first` field of the pair.
Instead of:
``
iterator foo = searcher(a, b);
``
you now write:
``
iterator foo = searcher(a, b).first;
``
[heading Performance]

116
doc/find_backward.qbk Normal file
View File

@ -0,0 +1,116 @@
[/ File find_backward.qbk]
[section:find_backward find_backward ]
[/license
Copyright (c) 2018 T. Zachary Laine
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)
]
The header file 'find_backward.hpp' contains variants of the stl algorithm
`find`. These variants are like `find`, except that the evaluate the elements
of the given sequence in reverse order.
Consider how finding the last element that is equal to `x` in a range is
typically done:
// Assume a valid range if elements delimited by [first, last).
while (last-- != first) {
if (*last == x) {
// Use last here...
}
}
Raw loops are icky though. Perhaps we should do a bit of extra work to allow
the use of `std::find()`:
auto rfirst = std::make_reverse_iterator(last);
auto rlast = std::make_reverse_iterator(first);
auto it = std::find(rfirst, rlast);
// Use it here...
That seems nicer in that there is no raw loop, but it has two major drawbacks.
First, it requires an unpleasant amount of typing. Second, it is less
efficient than forward-iterator `find` , since `std::reverse_iterator` calls
its base-iterator's `operator--()` in most of its member functions before
doing the work that the member function requires.
[heading interface]
template<typename BidiIter, typename T>
BidiIter find_backward(BidiIter first, BidiIter last, const T & x);
template<typename Range, typename T>
boost::range_iterator<Range> find_backward(Range & range, const T & x);
These overloads of `find_backward` return an iterator to the last element that
is equal to `x` in `[first, last)` or `r`, respectively.
template<typename BidiIter, typename T>
BidiIter find_not_backward(BidiIter first, BidiIter last, const T & x);
template<typename Range, typename T>
boost::range_iterator<Range> find_not_backward(Range & range, const T & x);
These overloads of `find_not_backward` return an iterator to the last element
that is not equal to `x` in `[first, last)` or `r`, respectively.
template<typename BidiIter, typename Pred>
BidiIter find_if_backward(BidiIter first, BidiIter last, Pred p);
template<typename Range, typename Pred>
boost::range_iterator<Range> find_if_backward(Range & range, Pred p);
These overloads of `find_if_backward` return an iterator to the last element
for which `pred` returns `true` in `[first, last)` or `r`, respectively.
template<typename BidiIter, typename Pred>
BidiIter find_if_not_backward(BidiIter first, BidiIter last, Pred p);
template<typename Range, typename Pred>
boost::range_iterator<Range> find_if_not_backward(Range & range, Pred p);
These overloads of `find_if_not_backward` return an iterator to the last
element for which `pred` returns `false` in `[first, last)` or `r`,
respectively.
[heading Examples]
Given the container `c1` containing `{ 2, 1, 2 }`, then
find_backward ( c1.begin(), c1.end(), 2 ) --> --c1.end()
find_backward ( c1.begin(), c1.end(), 3 ) --> c1.end()
find_if_backward ( c1.begin(), c1.end(), [](int i) {return i == 2;} ) --> --c1.end()
find_if_backward ( c1.begin(), c1.end(), [](int i) {return i == 3;} ) --> c1.end()
find_not_backward ( c1.begin(), c1.end(), 2 ) --> std::prev(c1.end(), 2)
find_not_backward ( c1.begin(), c1.end(), 1 ) --> c1.end()
find_if_not_backward ( c1.begin(), c1.end(), [](int i) {return i == 2;} ) --> std::prev(c1.end(), 2)
find_if_not_backward ( c1.begin(), c1.end(), [](int i) {return i == 1;} ) --> c1.end()
[heading Iterator Requirements]
All variants work on bidirectional iterators.
[heading Complexity]
Linear.
[heading Exception Safety]
All of the variants take their parameters by value and do not depend upon any
global state. Therefore, all the routines in this file provide the strong
exception guarantee.
[heading Notes]
All variants are `constexpr` in C++14 or later.
[endsect]
[/ File equal.qbk
Copyright 2018 T. Zachary Laine
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).
]

83
doc/find_not.qbk Normal file
View File

@ -0,0 +1,83 @@
[/ File find_not.qbk]
[section:find_not find_not ]
[/license
Copyright (c) 2018 T. Zachary Laine
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)
]
The header file 'find_not.hpp' contains a variants of a the stl algorithm
`find`. The algorithm finds the first value in the given sequence that is not
equal to the given value.
Consider this use of `find()`:
auto std::vector<int> vec = { 1, 1, 2 };
auto it = std::find(vec.begin(), vec.end(), 1);
This gives us the first occurance of `1` in `vec`. What if we want to find
the first occurrance of any number besides `1` in `vec`? We have to write an
unfortunate amount of code:
auto std::vector<int> vec = { 1, 1, 2 };
auto it = std::find_if(vec.begin(), vec.end(), [](int i) { return i != 1; });
With `find_not()` the code gets much more terse:
auto std::vector<int> vec = { 1, 1, 2 };
auto it = find_not(vec.begin(), vec.end(), 1);
The existing `find` variants are: `find()`, `find_if()`, and `find_if_not()`.
It seems natural to also have `find_not()`, for the very reason that we have
`find_if_not()` -- to avoid having to write a lambda to wrap the negation of
the find condition.
[heading interface]
template<typename InputIter, typename Sentinel, typename T>
InputIter find_not(InputIter first, Sentinel last, const T & x);
template<typename Range, typename T>
boost::range_iterator<Range> find_not(Range & r, const T & x);
These overloads of `find_not` return the first value that is not equal to `x`
in the sequence `[first, last)` or `r`, respectively.
[heading Examples]
Given the container `c1` containing `{ 0, 1, 2 }`, then
find_not ( c1.begin(), c1.end(), 1 ) --> c1.begin()
find_not ( c1.begin(), c1.end(), 0 ) --> std::next(c1.begin())
[heading Iterator Requirements]
`find_not` works on all iterators except output iterators.
The template parameter `Sentinel` is allowed to be different from `InputIter`,
or they may be the same. For an `InputIter` `it` and a `Sentinel` `end`, `it
== end` and `it != end` must be well-formed expressions.
[heading Complexity]
Linear.
[heading Exception Safety]
`find_not` takes its parameters by value and do not depend upon any global
state. Therefore, it provides the strong exception guarantee.
[heading Notes]
`constexpr` in C++14 or later.
[endsect]
[/ File equal.qbk
Copyright 2018 T. Zachary Laine
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).
]

View File

@ -57,7 +57,7 @@ Both of the variants of `is_partitioned` take their parameters by value or const
* The iterator-based version of the routine `is_partitioned` is also available as part of the C++11 standard.
* `is_partitioned` returns true for empty ranges, no matter what predicate is passed to test against.
* `is_partitioned` returns true for empty and single-element ranges, no matter what predicate is passed to test against.
[endsect]

View File

@ -0,0 +1,67 @@
[/ File is_partitioned_until.qbk]
[section:is_partitioned_until is_partitioned_until ]
[/license
Copyright (c) 2017 Alexander Zaitsev
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)
]
The header file 'is_partitioned_until.hpp' contains two variants of a single algorithm, `is_partitioned_until`. The algorithm tests to see if a sequence is partitioned according to a predicate; in other words, all the items in the sequence that satisfy the predicate are at the beginning of the sequence.
The routine `is_partitioned_until` takes a sequence and a predicate. It returns the last iterator 'it' in the sequence [begin, end) for which the is_partitioned(begin, it) is true.
`is_partitioned_until` come in two forms; the first one takes two iterators to define the range. The second form takes a single range parameter, and uses Boost.Range to traverse it.
[heading interface]
The function `is_partitioned_until` returns the last iterator 'it' in the sequence [begin, end) for which the is_partitioned(begin, it) is true. There are two versions; one takes two iterators, and the other takes a range.
``
template<typename InputIterator, typename Predicate>
InputIterator is_partitioned_until ( InputIterator first, InputIterator last, Predicate p );
template<typename Range, typename Predicate>
typename boost::range_iterator<const Range>::type is_partitioned_until ( const Range &r, Predicate p );
``
[heading Examples]
Given the container `c` containing `{ 0, 1, 2, 3, 14, 15 }`, then
``
bool isOdd ( int i ) { return i % 2 == 1; }
bool lessThan10 ( int i ) { return i < 10; }
is_partitioned_until ( c, isOdd ) --> iterator to '1'
is_partitioned_until ( c, lessThan10 ) --> end
is_partitioned_until ( c.begin (), c.end (), lessThan10 ) --> end
is_partitioned_until ( c.begin (), c.begin () + 3, lessThan10 ) --> end
is_partitioned_until ( c.end (), c.end (), isOdd ) --> end // empty range
``
[heading Iterator Requirements]
`is_partitioned_until` works on all iterators except output iterators.
[heading Complexity]
Both of the variants of `is_partitioned_until` run in ['O(N)] (linear) time; that is, they compare against each element in the list once. If the sequence is found to be not partitioned at any point, the routine will terminate immediately, without examining the rest of the elements.
[heading Exception Safety]
Both of the variants of `is_partitioned_until` take their parameters by value or const reference, and do not depend upon any global state. Therefore, all the routines in this file provide the strong exception guarantee.
[heading Notes]
* `is_partitioned_until` returns iterator to the end for empty and single-element ranges, no matter what predicate is passed to test against.
[endsect]
[/ File is_partitioned_until.qbk
Copyright 2017 Alexander Zaitsev
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).
]

View File

@ -39,7 +39,7 @@ public:
~knuth_morris_pratt ();
template <typename corpusIter>
corpusIter operator () ( corpusIter corpus_first, corpusIter corpus_last );
pair<corpusIter, corpusIter> operator () ( corpusIter corpus_first, corpusIter corpus_last );
};
``
@ -47,15 +47,28 @@ and here is the corresponding procedural interface:
``
template <typename patIter, typename corpusIter>
corpusIter knuth_morris_pratt_search (
pair<corpusIter, corpusIter> knuth_morris_pratt_search (
corpusIter corpus_first, corpusIter corpus_last,
patIter pat_first, patIter pat_last );
``
Each of the functions is passed two pairs of iterators. The first two define the corpus and the second two define the pattern. Note that the two pairs need not be of the same type, but they do need to "point" at the same type. In other words, `patIter::value_type` and `curpusIter::value_type` need to be the same type.
The return value of the function is an iterator pointing to the start of the pattern in the corpus. If the pattern is not found, it returns the end of the corpus (`corpus_last`).
The return value of the function is a pair of iterators pointing to the position of the pattern in the corpus. If the pattern is empty, it returns at empty range at the start of the corpus (`corpus_first`, `corpus_first`). If the pattern is not found, it returns at empty range at the end of the corpus (`corpus_last`, `corpus_last`).
[heading Compatibility Note]
Earlier versions of this searcher returned only a single iterator. As explained in [@https://cplusplusmusings.wordpress.com/2016/02/01/sometimes-you-get-things-wrong/], this was a suboptimal interface choice, and has been changed, starting in the 1.62.0 release. Old code that is expecting a single iterator return value can be updated by replacing the return value of the searcher's `operator ()` with the `.first` field of the pair.
Instead of:
``
iterator foo = searcher(a, b);
``
you now write:
``
iterator foo = searcher(a, b).first;
``
[heading Performance]
The execution time of the Knuth-Morris-Pratt algorithm is linear in the size of the string being searched. Generally the algorithm gets faster as the pattern being searched for becomes longer. Its efficiency derives from the fact that with each unsuccessful attempt to find a match between the search string and the text it is searching, it uses the information gained from that attempt to rule out as many positions of the text as possible where the string cannot match.

View File

@ -21,4 +21,5 @@ project /boost/algorithm/example
exe clamp_example : clamp_example.cpp ;
exe search_example : search_example.cpp ;
exe is_palindrome_example : is_palindrome_example.cpp;
exe is_partitioned_until_example : is_partitioned_until_example.cpp;
exe apply_permutation_example : apply_permutation_example.cpp;

View File

@ -0,0 +1,69 @@
/*
Copyright (c) Alexander Zaitsev <zamazan4ik@gmail.com>, 2017
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 <vector>
#include <iostream>
#include <boost/algorithm/apply_permutation.hpp>
namespace ba = boost::algorithm;
int main ( int /*argc*/, char * /*argv*/ [] )
{
// WARNING: Example require C++11 or newer compiler
{
std::cout << "apply_permutation with iterators:\n";
std::vector<int> vec{1, 2, 3, 4, 5}, order{4, 2, 3, 1, 0};
ba::apply_permutation(vec.begin(), vec.end(), order.begin(), order.end());
for (const auto& x : vec)
{
std::cout << x << ", ";
}
std::cout << std::endl;
}
{
std::cout << "apply_reverse_permutation with iterators:\n";
std::vector<int> vec{1, 2, 3, 4, 5}, order{4, 2, 3, 1, 0};
ba::apply_reverse_permutation(vec.begin(), vec.end(), order.begin(), order.end());
for (const auto& x : vec)
{
std::cout << x << ", ";
}
std::cout << std::endl;
}
{
std::cout << "apply_reverse_permutation with ranges:\n";
std::vector<int> vec{1, 2, 3, 4, 5}, order{4, 2, 3, 1, 0};
ba::apply_reverse_permutation(vec, order);
for (const auto& x : vec)
{
std::cout << x << ", ";
}
std::cout << std::endl;
}
{
std::cout << "apply_permutation with ranges:\n";
std::vector<int> vec{1, 2, 3, 4, 5}, order{4, 2, 3, 1, 0};
ba::apply_permutation(vec, order);
for (const auto& x : vec)
{
std::cout << x << ", ";
}
std::cout << std::endl;
}
return 0;
}

View File

@ -0,0 +1,70 @@
/*
Copyright (c) Alexander Zaitsev <zamazan4ik@gmail.by>, 2017
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)
For more information, see http://www.boost.org
*/
#include <vector>
#include <functional>
#include <iostream>
#include <boost/algorithm/is_partitioned_until.hpp>
namespace ba = boost::algorithm;
bool isOdd(const int v1)
{
return v1 % 2 != 0;
}
struct isOddComp
{
bool operator()(const int v1) const
{
return v1 % 2 != 0;
}
};
int main ( int /*argc*/, char * /*argv*/ [] )
{
std::vector<int> good({1, 2, 4});
std::vector<int> bad({1, 2, 3});
//Use custom function
auto it1 = ba::is_partitioned_until(good.begin(), good.end(), isOdd);
if(it1 == good.end())
{
std::cout << "The sequence is partitioned\n";
}
else
{
std::cout << "is_partitioned_until check failed here: " << *it1 << std::endl;
}
//Use custom comparator
auto it2 = ba::is_partitioned_until(good.begin(), good.end(), isOddComp());
if(it2 == good.end())
{
std::cout << "The sequence is partitioned\n";
}
else
{
std::cout << "is_partitioned_until check failed here: " << *it2 << std::endl;
}
auto it3 = ba::is_partitioned_until(bad, isOdd);
if(it3 == bad.end())
{
std::cout << "The sequence is partitioned\n";
}
else
{
std::cout << "is_partitioned_until check failed here: " << *it3 << std::endl;
}
return 0;
}

View File

@ -25,10 +25,10 @@
namespace boost { namespace algorithm {
template <typename T>
T identity_operation ( std::multiplies<T> ) { return T(1); }
BOOST_CXX14_CONSTEXPR T identity_operation ( std::multiplies<T> ) { return T(1); }
template <typename T>
T identity_operation ( std::plus<T> ) { return T(0); }
BOOST_CXX14_CONSTEXPR T identity_operation ( std::plus<T> ) { return T(0); }
/// \fn power ( T x, Integer n )
@ -40,7 +40,7 @@ T identity_operation ( std::plus<T> ) { return T(0); }
// \remark Taken from Knuth, The Art of Computer Programming, Volume 2:
// Seminumerical Algorithms, Section 4.6.3
template <typename T, typename Integer>
typename boost::enable_if<boost::is_integral<Integer>, T>::type
BOOST_CXX14_CONSTEXPR typename boost::enable_if<boost::is_integral<Integer>, T>::type
power (T x, Integer n) {
T y = 1; // Should be "T y{1};"
if (n == 0) return y;
@ -67,7 +67,7 @@ power (T x, Integer n) {
// \remark Taken from Knuth, The Art of Computer Programming, Volume 2:
// Seminumerical Algorithms, Section 4.6.3
template <typename T, typename Integer, typename Operation>
typename boost::enable_if<boost::is_integral<Integer>, T>::type
BOOST_CXX14_CONSTEXPR typename boost::enable_if<boost::is_integral<Integer>, T>::type
power (T x, Integer n, Operation op) {
T y = identity_operation(op);
if (n == 0) return y;

View File

@ -0,0 +1,125 @@
/*
Copyright (c) Alexander Zaitsev <zamazan4ik@gmail.com>, 2017
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.
Based on https://blogs.msdn.microsoft.com/oldnewthing/20170104-00/?p=95115
*/
/// \file apply_permutation.hpp
/// \brief Apply permutation to a sequence.
/// \author Alexander Zaitsev
#ifndef BOOST_ALGORITHM_APPLY_PERMUTATION_HPP
#define BOOST_ALGORITHM_APPLY_PERMUTATION_HPP
#include <algorithm>
#include <type_traits>
#include <boost/range/begin.hpp>
#include <boost/range/end.hpp>
namespace boost { namespace algorithm
{
/// \fn apply_permutation ( RandomAccessIterator1 item_begin, RandomAccessIterator1 item_end, RandomAccessIterator2 ind_begin )
/// \brief Reorder item sequence with index sequence order
///
/// \param item_begin The start of the item sequence
/// \param item_end One past the end of the item sequence
/// \param ind_begin The start of the index sequence.
///
/// \note Item sequence size should be equal to index size. Otherwise behavior is undefined.
/// Complexity: O(N).
template<typename RandomAccessIterator1, typename RandomAccessIterator2>
void
apply_permutation(RandomAccessIterator1 item_begin, RandomAccessIterator1 item_end,
RandomAccessIterator2 ind_begin, RandomAccessIterator2 ind_end)
{
using Diff = typename std::iterator_traits<RandomAccessIterator1>::difference_type;
using std::swap;
Diff size = std::distance(item_begin, item_end);
for (Diff i = 0; i < size; i++)
{
auto current = i;
while (i != ind_begin[current])
{
auto next = ind_begin[current];
swap(item_begin[current], item_begin[next]);
ind_begin[current] = current;
current = next;
}
ind_begin[current] = current;
}
}
/// \fn apply_reverse_permutation ( RandomAccessIterator1 item_begin, RandomAccessIterator1 item_end, RandomAccessIterator2 ind_begin )
/// \brief Reorder item sequence with index sequence order
///
/// \param item_begin The start of the item sequence
/// \param item_end One past the end of the item sequence
/// \param ind_begin The start of the index sequence.
///
/// \note Item sequence size should be equal to index size. Otherwise behavior is undefined.
/// Complexity: O(N).
template<typename RandomAccessIterator1, typename RandomAccessIterator2>
void
apply_reverse_permutation(
RandomAccessIterator1 item_begin,
RandomAccessIterator1 item_end,
RandomAccessIterator2 ind_begin,
RandomAccessIterator2 ind_end)
{
using Diff = typename std::iterator_traits<RandomAccessIterator2>::difference_type;
using std::swap;
Diff length = std::distance(item_begin, item_end);
for (Diff i = 0; i < length; i++)
{
while (i != ind_begin[i])
{
Diff next = ind_begin[i];
swap(item_begin[i], item_begin[next]);
swap(ind_begin[i], ind_begin[next]);
}
}
}
/// \fn apply_permutation ( Range1 item_range, Range2 ind_range )
/// \brief Reorder item sequence with index sequence order
///
/// \param item_range The item sequence
/// \param ind_range The index sequence
///
/// \note Item sequence size should be equal to index size. Otherwise behavior is undefined.
/// Complexity: O(N).
template<typename Range1, typename Range2>
void
apply_permutation(Range1& item_range, Range2& ind_range)
{
apply_permutation(boost::begin(item_range), boost::end(item_range),
boost::begin(ind_range), boost::end(ind_range));
}
/// \fn apply_reverse_permutation ( Range1 item_range, Range2 ind_range )
/// \brief Reorder item sequence with index sequence order
///
/// \param item_range The item sequence
/// \param ind_range The index sequence
///
/// \note Item sequence size should be equal to index size. Otherwise behavior is undefined.
/// Complexity: O(N).
template<typename Range1, typename Range2>
void
apply_reverse_permutation(Range1& item_range, Range2& ind_range)
{
apply_reverse_permutation(boost::begin(item_range), boost::end(item_range),
boost::begin(ind_range), boost::end(ind_range));
}
}}
#endif //BOOST_ALGORITHM_APPLY_PERMUTATION_HPP

View File

@ -46,7 +46,7 @@ namespace boost { namespace algorithm {
/// p ( a, b ) returns a boolean.
///
template<typename T, typename Pred>
T const & clamp ( T const& val,
BOOST_CXX14_CONSTEXPR T const & clamp ( T const& val,
typename boost::mpl::identity<T>::type const & lo,
typename boost::mpl::identity<T>::type const & hi, Pred p )
{
@ -68,11 +68,11 @@ namespace boost { namespace algorithm {
/// \param hi The upper bound of the range to be clamped to
///
template<typename T>
T const& clamp ( const T& val,
BOOST_CXX14_CONSTEXPR T const& clamp ( const T& val,
typename boost::mpl::identity<T>::type const & lo,
typename boost::mpl::identity<T>::type const & hi )
{
return (clamp) ( val, lo, hi, std::less<T>());
return boost::algorithm::clamp ( val, lo, hi, std::less<T>());
}
/// \fn clamp_range ( InputIterator first, InputIterator last, OutputIterator out,
@ -87,13 +87,13 @@ namespace boost { namespace algorithm {
/// \param hi The upper bound of the range to be clamped to
///
template<typename InputIterator, typename OutputIterator>
OutputIterator clamp_range ( InputIterator first, InputIterator last, OutputIterator out,
BOOST_CXX14_CONSTEXPR OutputIterator clamp_range ( InputIterator first, InputIterator last, OutputIterator out,
typename std::iterator_traits<InputIterator>::value_type const & lo,
typename std::iterator_traits<InputIterator>::value_type const & hi )
{
// this could also be written with bind and std::transform
while ( first != last )
*out++ = clamp ( *first++, lo, hi );
*out++ = boost::algorithm::clamp ( *first++, lo, hi );
return out;
}
@ -108,12 +108,12 @@ namespace boost { namespace algorithm {
/// \param hi The upper bound of the range to be clamped to
///
template<typename Range, typename OutputIterator>
typename boost::disable_if_c<boost::is_same<Range, OutputIterator>::value, OutputIterator>::type
BOOST_CXX14_CONSTEXPR typename boost::disable_if_c<boost::is_same<Range, OutputIterator>::value, OutputIterator>::type
clamp_range ( const Range &r, OutputIterator out,
typename std::iterator_traits<typename boost::range_iterator<const Range>::type>::value_type const & lo,
typename std::iterator_traits<typename boost::range_iterator<const Range>::type>::value_type const & hi )
{
return clamp_range ( boost::begin ( r ), boost::end ( r ), out, lo, hi );
return boost::algorithm::clamp_range ( boost::begin ( r ), boost::end ( r ), out, lo, hi );
}
@ -133,13 +133,13 @@ namespace boost { namespace algorithm {
///
template<typename InputIterator, typename OutputIterator, typename Pred>
OutputIterator clamp_range ( InputIterator first, InputIterator last, OutputIterator out,
BOOST_CXX14_CONSTEXPR OutputIterator clamp_range ( InputIterator first, InputIterator last, OutputIterator out,
typename std::iterator_traits<InputIterator>::value_type const & lo,
typename std::iterator_traits<InputIterator>::value_type const & hi, Pred p )
{
// this could also be written with bind and std::transform
while ( first != last )
*out++ = clamp ( *first++, lo, hi, p );
*out++ = boost::algorithm::clamp ( *first++, lo, hi, p );
return out;
}
@ -160,13 +160,13 @@ namespace boost { namespace algorithm {
// Disable this template if the first two parameters are the same type;
// In that case, the user will get the two iterator version.
template<typename Range, typename OutputIterator, typename Pred>
typename boost::disable_if_c<boost::is_same<Range, OutputIterator>::value, OutputIterator>::type
BOOST_CXX14_CONSTEXPR typename boost::disable_if_c<boost::is_same<Range, OutputIterator>::value, OutputIterator>::type
clamp_range ( const Range &r, OutputIterator out,
typename std::iterator_traits<typename boost::range_iterator<const Range>::type>::value_type const & lo,
typename std::iterator_traits<typename boost::range_iterator<const Range>::type>::value_type const & hi,
Pred p )
{
return clamp_range ( boost::begin ( r ), boost::end ( r ), out, lo, hi, p );
return boost::algorithm::clamp_range ( boost::begin ( r ), boost::end ( r ), out, lo, hi, p );
}

View File

@ -27,7 +27,7 @@ namespace boost { namespace algorithm {
///
/// \note This function is part of the C++2011 standard library.
template<typename InputIterator, typename Predicate>
bool all_of ( InputIterator first, InputIterator last, Predicate p )
BOOST_CXX14_CONSTEXPR bool all_of ( InputIterator first, InputIterator last, Predicate p )
{
for ( ; first != last; ++first )
if ( !p(*first))
@ -43,7 +43,7 @@ bool all_of ( InputIterator first, InputIterator last, Predicate p )
/// \param p A predicate for testing the elements of the range
///
template<typename Range, typename Predicate>
bool all_of ( const Range &r, Predicate p )
BOOST_CXX14_CONSTEXPR bool all_of ( const Range &r, Predicate p )
{
return boost::algorithm::all_of ( boost::begin (r), boost::end (r), p );
}
@ -57,7 +57,7 @@ bool all_of ( const Range &r, Predicate p )
/// \param val A value to compare against
///
template<typename InputIterator, typename T>
bool all_of_equal ( InputIterator first, InputIterator last, const T &val )
BOOST_CXX14_CONSTEXPR bool all_of_equal ( InputIterator first, InputIterator last, const T &val )
{
for ( ; first != last; ++first )
if ( val != *first )
@ -73,7 +73,7 @@ bool all_of_equal ( InputIterator first, InputIterator last, const T &val )
/// \param val A value to compare against
///
template<typename Range, typename T>
bool all_of_equal ( const Range &r, const T &val )
BOOST_CXX14_CONSTEXPR bool all_of_equal ( const Range &r, const T &val )
{
return boost::algorithm::all_of_equal ( boost::begin (r), boost::end (r), val );
}

View File

@ -28,7 +28,7 @@ namespace boost { namespace algorithm {
/// \param p A predicate for testing the elements of the sequence
///
template<typename InputIterator, typename Predicate>
bool any_of ( InputIterator first, InputIterator last, Predicate p )
BOOST_CXX14_CONSTEXPR bool any_of ( InputIterator first, InputIterator last, Predicate p )
{
for ( ; first != last; ++first )
if ( p(*first))
@ -44,7 +44,7 @@ bool any_of ( InputIterator first, InputIterator last, Predicate p )
/// \param p A predicate for testing the elements of the range
///
template<typename Range, typename Predicate>
bool any_of ( const Range &r, Predicate p )
BOOST_CXX14_CONSTEXPR bool any_of ( const Range &r, Predicate p )
{
return boost::algorithm::any_of (boost::begin (r), boost::end (r), p);
}
@ -58,7 +58,7 @@ bool any_of ( const Range &r, Predicate p )
/// \param val A value to compare against
///
template<typename InputIterator, typename V>
bool any_of_equal ( InputIterator first, InputIterator last, const V &val )
BOOST_CXX14_CONSTEXPR bool any_of_equal ( InputIterator first, InputIterator last, const V &val )
{
for ( ; first != last; ++first )
if ( val == *first )
@ -74,7 +74,7 @@ bool any_of_equal ( InputIterator first, InputIterator last, const V &val )
/// \param val A value to compare against
///
template<typename Range, typename V>
bool any_of_equal ( const Range &r, const V &val )
BOOST_CXX14_CONSTEXPR bool any_of_equal ( const Range &r, const V &val )
{
return boost::algorithm::any_of_equal (boost::begin (r), boost::end (r), val);
}

View File

@ -29,7 +29,7 @@ namespace boost { namespace algorithm {
/// \param p A predicate for testing the elements of the range
/// \note This function is part of the C++2011 standard library.
template<typename InputIterator, typename OutputIterator, typename Predicate>
OutputIterator copy_if ( InputIterator first, InputIterator last, OutputIterator result, Predicate p )
BOOST_CXX14_CONSTEXPR OutputIterator copy_if ( InputIterator first, InputIterator last, OutputIterator result, Predicate p )
{
for ( ; first != last; ++first )
if (p(*first))
@ -47,7 +47,7 @@ OutputIterator copy_if ( InputIterator first, InputIterator last, OutputIterator
/// \param p A predicate for testing the elements of the range
///
template<typename Range, typename OutputIterator, typename Predicate>
OutputIterator copy_if ( const Range &r, OutputIterator result, Predicate p )
BOOST_CXX14_CONSTEXPR OutputIterator copy_if ( const Range &r, OutputIterator result, Predicate p )
{
return boost::algorithm::copy_if (boost::begin (r), boost::end(r), result, p);
}
@ -64,7 +64,7 @@ OutputIterator copy_if ( const Range &r, OutputIterator result, Predicate p )
/// \param p A predicate for testing the elements of the range
///
template<typename InputIterator, typename OutputIterator, typename Predicate>
std::pair<InputIterator, OutputIterator>
BOOST_CXX14_CONSTEXPR std::pair<InputIterator, OutputIterator>
copy_while ( InputIterator first, InputIterator last, OutputIterator result, Predicate p )
{
for ( ; first != last && p(*first); ++first )
@ -82,7 +82,7 @@ copy_while ( InputIterator first, InputIterator last, OutputIterator result, Pre
/// \param p A predicate for testing the elements of the range
///
template<typename Range, typename OutputIterator, typename Predicate>
std::pair<typename boost::range_iterator<const Range>::type, OutputIterator>
BOOST_CXX14_CONSTEXPR std::pair<typename boost::range_iterator<const Range>::type, OutputIterator>
copy_while ( const Range &r, OutputIterator result, Predicate p )
{
return boost::algorithm::copy_while (boost::begin (r), boost::end(r), result, p);
@ -100,7 +100,7 @@ copy_while ( const Range &r, OutputIterator result, Predicate p )
/// \param p A predicate for testing the elements of the range
///
template<typename InputIterator, typename OutputIterator, typename Predicate>
std::pair<InputIterator, OutputIterator>
BOOST_CXX14_CONSTEXPR std::pair<InputIterator, OutputIterator>
copy_until ( InputIterator first, InputIterator last, OutputIterator result, Predicate p )
{
for ( ; first != last && !p(*first); ++first )
@ -118,7 +118,7 @@ copy_until ( InputIterator first, InputIterator last, OutputIterator result, Pre
/// \param p A predicate for testing the elements of the range
///
template<typename Range, typename OutputIterator, typename Predicate>
std::pair<typename boost::range_iterator<const Range>::type, OutputIterator>
BOOST_CXX14_CONSTEXPR std::pair<typename boost::range_iterator<const Range>::type, OutputIterator>
copy_until ( const Range &r, OutputIterator result, Predicate p )
{
return boost::algorithm::copy_until (boost::begin (r), boost::end(r), result, p);

View File

@ -24,7 +24,7 @@ namespace boost { namespace algorithm {
/// \param result An output iterator to write the results into
/// \note This function is part of the C++2011 standard library.
template <typename InputIterator, typename Size, typename OutputIterator>
OutputIterator copy_n ( InputIterator first, Size n, OutputIterator result )
BOOST_CXX14_CONSTEXPR OutputIterator copy_n ( InputIterator first, Size n, OutputIterator result )
{
for ( ; n > 0; --n, ++first, ++result )
*result = *first;

View File

@ -26,7 +26,7 @@ namespace boost { namespace algorithm {
/// \param p A predicate for testing the elements of the range
/// \note This function is part of the C++2011 standard library.
template<typename InputIterator, typename Predicate>
InputIterator find_if_not ( InputIterator first, InputIterator last, Predicate p )
BOOST_CXX14_CONSTEXPR InputIterator find_if_not ( InputIterator first, InputIterator last, Predicate p )
{
for ( ; first != last; ++first )
if ( !p(*first))
@ -42,7 +42,7 @@ InputIterator find_if_not ( InputIterator first, InputIterator last, Predicate p
/// \param p A predicate for testing the elements of the range
///
template<typename Range, typename Predicate>
typename boost::range_iterator<const Range>::type find_if_not ( const Range &r, Predicate p )
BOOST_CXX14_CONSTEXPR typename boost::range_iterator<const Range>::type find_if_not ( const Range &r, Predicate p )
{
return boost::algorithm::find_if_not (boost::begin (r), boost::end(r), p);
}

View File

@ -25,7 +25,7 @@ namespace boost { namespace algorithm {
/// \param value The initial value of the sequence to be generated
/// \note This function is part of the C++2011 standard library.
template <typename ForwardIterator, typename T>
void iota ( ForwardIterator first, ForwardIterator last, T value )
BOOST_CXX14_CONSTEXPR void iota ( ForwardIterator first, ForwardIterator last, T value )
{
for ( ; first != last; ++first, ++value )
*first = value;
@ -38,7 +38,7 @@ void iota ( ForwardIterator first, ForwardIterator last, T value )
/// \param value The initial value of the sequence to be generated
///
template <typename Range, typename T>
void iota ( Range &r, T value )
BOOST_CXX14_CONSTEXPR void iota ( Range &r, T value )
{
boost::algorithm::iota (boost::begin(r), boost::end(r), value);
}
@ -52,7 +52,7 @@ void iota ( Range &r, T value )
/// \param n The number of items to write
///
template <typename OutputIterator, typename T>
OutputIterator iota_n ( OutputIterator out, T value, std::size_t n )
BOOST_CXX14_CONSTEXPR OutputIterator iota_n ( OutputIterator out, T value, std::size_t n )
{
for ( ; n > 0; --n, ++value )
*out++ = value;

View File

@ -18,14 +18,15 @@
namespace boost { namespace algorithm {
/// \fn is_partitioned ( InputIterator first, InputIterator last, UnaryPredicate p )
/// \brief Tests to see if a sequence is partitioned according to a predicate
/// \brief Tests to see if a sequence is partitioned according to a predicate.
/// In other words, all the items in the sequence that satisfy the predicate are at the beginning of the sequence.
///
/// \param first The start of the input sequence
/// \param last One past the end of the input sequence
/// \param p The predicate to test the values with
/// \note This function is part of the C++2011 standard library.
template <typename InputIterator, typename UnaryPredicate>
bool is_partitioned ( InputIterator first, InputIterator last, UnaryPredicate p )
BOOST_CXX14_CONSTEXPR bool is_partitioned ( InputIterator first, InputIterator last, UnaryPredicate p )
{
// Run through the part that satisfy the predicate
for ( ; first != last; ++first )
@ -39,13 +40,14 @@ bool is_partitioned ( InputIterator first, InputIterator last, UnaryPredicate p
}
/// \fn is_partitioned ( const Range &r, UnaryPredicate p )
/// \brief Generates an increasing sequence of values, and stores them in the input Range.
/// \brief Tests to see if a sequence is partitioned according to a predicate.
/// In other words, all the items in the sequence that satisfy the predicate are at the beginning of the sequence.
///
/// \param r The input range
/// \param p The predicate to test the values with
///
template <typename Range, typename UnaryPredicate>
bool is_partitioned ( const Range &r, UnaryPredicate p )
BOOST_CXX14_CONSTEXPR bool is_partitioned ( const Range &r, UnaryPredicate p )
{
return boost::algorithm::is_partitioned (boost::begin(r), boost::end(r), p);
}

View File

@ -34,7 +34,7 @@ namespace boost { namespace algorithm {
/// \param p A binary predicate that returns true if two elements are ordered.
///
template <typename ForwardIterator, typename Pred>
ForwardIterator is_sorted_until ( ForwardIterator first, ForwardIterator last, Pred p )
BOOST_CXX14_CONSTEXPR ForwardIterator is_sorted_until ( ForwardIterator first, ForwardIterator last, Pred p )
{
if ( first == last ) return last; // the empty sequence is ordered
ForwardIterator next = first;
@ -54,7 +54,7 @@ namespace boost { namespace algorithm {
/// \param last One past the end of the sequence
///
template <typename ForwardIterator>
ForwardIterator is_sorted_until ( ForwardIterator first, ForwardIterator last )
BOOST_CXX14_CONSTEXPR ForwardIterator is_sorted_until ( ForwardIterator first, ForwardIterator last )
{
typedef typename std::iterator_traits<ForwardIterator>::value_type value_type;
return boost::algorithm::is_sorted_until ( first, last, std::less<value_type>());
@ -69,7 +69,7 @@ namespace boost { namespace algorithm {
/// \param p A binary predicate that returns true if two elements are ordered.
///
template <typename ForwardIterator, typename Pred>
bool is_sorted ( ForwardIterator first, ForwardIterator last, Pred p )
BOOST_CXX14_CONSTEXPR bool is_sorted ( ForwardIterator first, ForwardIterator last, Pred p )
{
return boost::algorithm::is_sorted_until (first, last, p) == last;
}
@ -81,7 +81,7 @@ namespace boost { namespace algorithm {
/// \param last One past the end of the sequence
///
template <typename ForwardIterator>
bool is_sorted ( ForwardIterator first, ForwardIterator last )
BOOST_CXX14_CONSTEXPR bool is_sorted ( ForwardIterator first, ForwardIterator last )
{
return boost::algorithm::is_sorted_until (first, last) == last;
}
@ -98,7 +98,7 @@ namespace boost { namespace algorithm {
/// \param p A binary predicate that returns true if two elements are ordered.
///
template <typename R, typename Pred>
typename boost::lazy_disable_if_c<
BOOST_CXX14_CONSTEXPR typename boost::lazy_disable_if_c<
boost::is_same<R, Pred>::value,
typename boost::range_iterator<const R>
>::type is_sorted_until ( const R &range, Pred p )
@ -113,7 +113,7 @@ namespace boost { namespace algorithm {
/// \param range The range to be tested.
///
template <typename R>
typename boost::range_iterator<const R>::type is_sorted_until ( const R &range )
BOOST_CXX14_CONSTEXPR typename boost::range_iterator<const R>::type is_sorted_until ( const R &range )
{
return boost::algorithm::is_sorted_until ( boost::begin ( range ), boost::end ( range ));
}
@ -126,7 +126,7 @@ namespace boost { namespace algorithm {
/// \param p A binary predicate that returns true if two elements are ordered.
///
template <typename R, typename Pred>
typename boost::lazy_disable_if_c< boost::is_same<R, Pred>::value, boost::mpl::identity<bool> >::type
BOOST_CXX14_CONSTEXPR typename boost::lazy_disable_if_c< boost::is_same<R, Pred>::value, boost::mpl::identity<bool> >::type
is_sorted ( const R &range, Pred p )
{
return boost::algorithm::is_sorted ( boost::begin ( range ), boost::end ( range ), p );
@ -139,7 +139,7 @@ namespace boost { namespace algorithm {
/// \param range The range to be tested.
///
template <typename R>
bool is_sorted ( const R &range )
BOOST_CXX14_CONSTEXPR bool is_sorted ( const R &range )
{
return boost::algorithm::is_sorted ( boost::begin ( range ), boost::end ( range ));
}
@ -159,7 +159,7 @@ namespace boost { namespace algorithm {
/// \note This function will return true for sequences that contain items that compare
/// equal. If that is not what you intended, you should use is_strictly_increasing instead.
template <typename ForwardIterator>
bool is_increasing ( ForwardIterator first, ForwardIterator last )
BOOST_CXX14_CONSTEXPR bool is_increasing ( ForwardIterator first, ForwardIterator last )
{
typedef typename std::iterator_traits<ForwardIterator>::value_type value_type;
return boost::algorithm::is_sorted (first, last, std::less<value_type>());
@ -175,7 +175,7 @@ namespace boost { namespace algorithm {
/// \note This function will return true for sequences that contain items that compare
/// equal. If that is not what you intended, you should use is_strictly_increasing instead.
template <typename R>
bool is_increasing ( const R &range )
BOOST_CXX14_CONSTEXPR bool is_increasing ( const R &range )
{
return is_increasing ( boost::begin ( range ), boost::end ( range ));
}
@ -192,7 +192,7 @@ namespace boost { namespace algorithm {
/// \note This function will return true for sequences that contain items that compare
/// equal. If that is not what you intended, you should use is_strictly_decreasing instead.
template <typename ForwardIterator>
bool is_decreasing ( ForwardIterator first, ForwardIterator last )
BOOST_CXX14_CONSTEXPR bool is_decreasing ( ForwardIterator first, ForwardIterator last )
{
typedef typename std::iterator_traits<ForwardIterator>::value_type value_type;
return boost::algorithm::is_sorted (first, last, std::greater<value_type>());
@ -207,7 +207,7 @@ namespace boost { namespace algorithm {
/// \note This function will return true for sequences that contain items that compare
/// equal. If that is not what you intended, you should use is_strictly_decreasing instead.
template <typename R>
bool is_decreasing ( const R &range )
BOOST_CXX14_CONSTEXPR bool is_decreasing ( const R &range )
{
return is_decreasing ( boost::begin ( range ), boost::end ( range ));
}
@ -224,7 +224,7 @@ namespace boost { namespace algorithm {
/// \note This function will return false for sequences that contain items that compare
/// equal. If that is not what you intended, you should use is_increasing instead.
template <typename ForwardIterator>
bool is_strictly_increasing ( ForwardIterator first, ForwardIterator last )
BOOST_CXX14_CONSTEXPR bool is_strictly_increasing ( ForwardIterator first, ForwardIterator last )
{
typedef typename std::iterator_traits<ForwardIterator>::value_type value_type;
return boost::algorithm::is_sorted (first, last, std::less_equal<value_type>());
@ -239,7 +239,7 @@ namespace boost { namespace algorithm {
/// \note This function will return false for sequences that contain items that compare
/// equal. If that is not what you intended, you should use is_increasing instead.
template <typename R>
bool is_strictly_increasing ( const R &range )
BOOST_CXX14_CONSTEXPR bool is_strictly_increasing ( const R &range )
{
return is_strictly_increasing ( boost::begin ( range ), boost::end ( range ));
}
@ -255,7 +255,7 @@ namespace boost { namespace algorithm {
/// \note This function will return false for sequences that contain items that compare
/// equal. If that is not what you intended, you should use is_decreasing instead.
template <typename ForwardIterator>
bool is_strictly_decreasing ( ForwardIterator first, ForwardIterator last )
BOOST_CXX14_CONSTEXPR bool is_strictly_decreasing ( ForwardIterator first, ForwardIterator last )
{
typedef typename std::iterator_traits<ForwardIterator>::value_type value_type;
return boost::algorithm::is_sorted (first, last, std::greater_equal<value_type>());
@ -270,7 +270,7 @@ namespace boost { namespace algorithm {
/// \note This function will return false for sequences that contain items that compare
/// equal. If that is not what you intended, you should use is_decreasing instead.
template <typename R>
bool is_strictly_decreasing ( const R &range )
BOOST_CXX14_CONSTEXPR bool is_strictly_decreasing ( const R &range )
{
return is_strictly_decreasing ( boost::begin ( range ), boost::end ( range ));
}

View File

@ -26,7 +26,7 @@ namespace boost { namespace algorithm {
/// \param p A predicate for testing the elements of the sequence
///
template<typename InputIterator, typename Predicate>
bool none_of ( InputIterator first, InputIterator last, Predicate p )
BOOST_CXX14_CONSTEXPR bool none_of ( InputIterator first, InputIterator last, Predicate p )
{
for ( ; first != last; ++first )
if ( p(*first))
@ -42,7 +42,7 @@ bool none_of ( InputIterator first, InputIterator last, Predicate p )
/// \param p A predicate for testing the elements of the range
///
template<typename Range, typename Predicate>
bool none_of ( const Range &r, Predicate p )
BOOST_CXX14_CONSTEXPR bool none_of ( const Range &r, Predicate p )
{
return boost::algorithm::none_of (boost::begin (r), boost::end (r), p );
}
@ -56,7 +56,7 @@ bool none_of ( const Range &r, Predicate p )
/// \param val A value to compare against
///
template<typename InputIterator, typename V>
bool none_of_equal ( InputIterator first, InputIterator last, const V &val )
BOOST_CXX14_CONSTEXPR bool none_of_equal ( InputIterator first, InputIterator last, const V &val )
{
for ( ; first != last; ++first )
if ( val == *first )
@ -72,7 +72,7 @@ bool none_of_equal ( InputIterator first, InputIterator last, const V &val )
/// \param val A value to compare against
///
template<typename Range, typename V>
bool none_of_equal ( const Range &r, const V & val )
BOOST_CXX14_CONSTEXPR bool none_of_equal ( const Range &r, const V & val )
{
return boost::algorithm::none_of_equal (boost::begin (r), boost::end (r), val);
}

View File

@ -12,7 +12,6 @@
#ifndef BOOST_ALGORITHM_ONE_OF_HPP
#define BOOST_ALGORITHM_ONE_OF_HPP
#include <algorithm> // for std::find and std::find_if
#include <boost/algorithm/cxx11/none_of.hpp>
#include <boost/range/begin.hpp>
@ -28,12 +27,16 @@ namespace boost { namespace algorithm {
/// \param p A predicate for testing the elements of the sequence
///
template<typename InputIterator, typename Predicate>
bool one_of ( InputIterator first, InputIterator last, Predicate p )
BOOST_CXX14_CONSTEXPR bool one_of ( InputIterator first, InputIterator last, Predicate p )
{
InputIterator i = std::find_if (first, last, p);
if (i == last)
// find_if
for (; first != last; ++first)
if (p(*first))
break;
if (first == last)
return false; // Didn't occur at all
return boost::algorithm::none_of (++i, last, p);
return boost::algorithm::none_of (++first, last, p);
}
/// \fn one_of ( const Range &r, Predicate p )
@ -43,7 +46,7 @@ bool one_of ( InputIterator first, InputIterator last, Predicate p )
/// \param p A predicate for testing the elements of the range
///
template<typename Range, typename Predicate>
bool one_of ( const Range &r, Predicate p )
BOOST_CXX14_CONSTEXPR bool one_of ( const Range &r, Predicate p )
{
return boost::algorithm::one_of ( boost::begin (r), boost::end (r), p );
}
@ -57,12 +60,16 @@ bool one_of ( const Range &r, Predicate p )
/// \param val A value to compare against
///
template<typename InputIterator, typename V>
bool one_of_equal ( InputIterator first, InputIterator last, const V &val )
BOOST_CXX14_CONSTEXPR bool one_of_equal ( InputIterator first, InputIterator last, const V &val )
{
InputIterator i = std::find (first, last, val); // find first occurrence of 'val'
if (i == last)
// find
for (; first != last; ++first)
if (*first == val)
break;
if (first == last)
return false; // Didn't occur at all
return boost::algorithm::none_of_equal (++i, last, val);
return boost::algorithm::none_of_equal (++first, last, val);
}
/// \fn one_of_equal ( const Range &r, const V &val )
@ -72,7 +79,7 @@ bool one_of_equal ( InputIterator first, InputIterator last, const V &val )
/// \param val A value to compare against
///
template<typename Range, typename V>
bool one_of_equal ( const Range &r, const V &val )
BOOST_CXX14_CONSTEXPR bool one_of_equal ( const Range &r, const V &val )
{
return boost::algorithm::one_of_equal ( boost::begin (r), boost::end (r), val );
}

View File

@ -14,6 +14,7 @@
#include <utility> // for std::pair
#include <boost/config.hpp>
#include <boost/range/begin.hpp>
#include <boost/range/end.hpp>
@ -35,7 +36,7 @@ namespace boost { namespace algorithm {
/// \note This function is part of the C++2011 standard library.
template <typename InputIterator,
typename OutputIterator1, typename OutputIterator2, typename UnaryPredicate>
std::pair<OutputIterator1, OutputIterator2>
BOOST_CXX14_CONSTEXPR std::pair<OutputIterator1, OutputIterator2>
partition_copy ( InputIterator first, InputIterator last,
OutputIterator1 out_true, OutputIterator2 out_false, UnaryPredicate p )
{
@ -57,7 +58,7 @@ partition_copy ( InputIterator first, InputIterator last,
///
template <typename Range, typename OutputIterator1, typename OutputIterator2,
typename UnaryPredicate>
std::pair<OutputIterator1, OutputIterator2>
BOOST_CXX14_CONSTEXPR std::pair<OutputIterator1, OutputIterator2>
partition_copy ( const Range &r, OutputIterator1 out_true, OutputIterator2 out_false,
UnaryPredicate p )
{

View File

@ -12,8 +12,6 @@
#ifndef BOOST_ALGORITHM_EQUAL_HPP
#define BOOST_ALGORITHM_EQUAL_HPP
#include <algorithm> // for std::equal
#include <functional> // for std::binary_function
#include <iterator>
namespace boost { namespace algorithm {
@ -21,11 +19,12 @@ namespace boost { namespace algorithm {
namespace detail {
template <class T1, class T2>
struct eq : public std::binary_function<T1, T2, bool> {
bool operator () ( const T1& v1, const T2& v2 ) const { return v1 == v2 ;}
struct eq {
BOOST_CONSTEXPR bool operator () ( const T1& v1, const T2& v2 ) const { return v1 == v2 ;}
};
template <class RandomAccessIterator1, class RandomAccessIterator2, class BinaryPredicate>
BOOST_CXX14_CONSTEXPR
bool equal ( RandomAccessIterator1 first1, RandomAccessIterator1 last1,
RandomAccessIterator2 first2, RandomAccessIterator2 last2, BinaryPredicate pred,
std::random_access_iterator_tag, std::random_access_iterator_tag )
@ -33,11 +32,16 @@ namespace detail {
// Random-access iterators let is check the sizes in constant time
if ( std::distance ( first1, last1 ) != std::distance ( first2, last2 ))
return false;
// If we know that the sequences are the same size, the original version is fine
return std::equal ( first1, last1, first2, pred );
// std::equal
for (; first1 != last1; ++first1, ++first2)
if (!pred(*first1, *first2))
return false;
return true;
}
template <class InputIterator1, class InputIterator2, class BinaryPredicate>
BOOST_CXX14_CONSTEXPR
bool equal ( InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, InputIterator2 last2, BinaryPredicate pred,
std::input_iterator_tag, std::input_iterator_tag )
@ -61,6 +65,7 @@ namespace detail {
/// \param last2 One past the end of the second range.
/// \param pred A predicate for comparing the elements of the ranges
template <class InputIterator1, class InputIterator2, class BinaryPredicate>
BOOST_CXX14_CONSTEXPR
bool equal ( InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, InputIterator2 last2, BinaryPredicate pred )
{
@ -79,6 +84,7 @@ bool equal ( InputIterator1 first1, InputIterator1 last1,
/// \param first2 The start of the second range.
/// \param last2 One past the end of the second range.
template <class InputIterator1, class InputIterator2>
BOOST_CXX14_CONSTEXPR
bool equal ( InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, InputIterator2 last2 )
{

View File

@ -2,7 +2,7 @@
Copyright (c) Marshall Clow 2008-2012.
Distributed under the Boost Software License, Version 1.0. (See accompanying
file LICENSE10.txt or copy at http://www.boost.org/LICENSE10.txt)
file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
*/
/// \file mismatch.hpp
@ -13,6 +13,7 @@
#define BOOST_ALGORITHM_MISMATCH_HPP
#include <utility> // for std::pair
#include <boost/config.hpp>
namespace boost { namespace algorithm {
@ -27,7 +28,7 @@ namespace boost { namespace algorithm {
/// \param last2 One past the end of the second range.
/// \param pred A predicate for comparing the elements of the ranges
template <class InputIterator1, class InputIterator2, class BinaryPredicate>
std::pair<InputIterator1, InputIterator2> mismatch (
BOOST_CXX14_CONSTEXPR std::pair<InputIterator1, InputIterator2> mismatch (
InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, InputIterator2 last2,
BinaryPredicate pred )
@ -47,7 +48,7 @@ std::pair<InputIterator1, InputIterator2> mismatch (
/// \param first2 The start of the second range.
/// \param last2 One past the end of the second range.
template <class InputIterator1, class InputIterator2>
std::pair<InputIterator1, InputIterator2> mismatch (
BOOST_CXX14_CONSTEXPR std::pair<InputIterator1, InputIterator2> mismatch (
InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, InputIterator2 last2 )
{

View File

@ -0,0 +1,52 @@
/*
Copyright (c) Marshall Clow 2017.
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)
*/
/// \file exclusive_scan.hpp
/// \brief ???
/// \author Marshall Clow
#ifndef BOOST_ALGORITHM_EXCLUSIVE_SCAN_HPP
#define BOOST_ALGORITHM_EXCLUSIVE_SCAN_HPP
#include <functional> // for std::plus
#include <iterator> // for std::iterator_traits
#include <boost/range/begin.hpp>
#include <boost/range/end.hpp>
#include <boost/range/value_type.hpp>
namespace boost { namespace algorithm {
template<class InputIterator, class OutputIterator, class T, class BinaryOperation>
OutputIterator exclusive_scan(InputIterator first, InputIterator last,
OutputIterator result, T init, BinaryOperation bOp)
{
if (first != last)
{
T saved = init;
do
{
init = bOp(init, *first);
*result = saved;
saved = init;
++result;
} while (++first != last);
}
return result;
}
template<class InputIterator, class OutputIterator, class T>
OutputIterator exclusive_scan(InputIterator first, InputIterator last,
OutputIterator result, T init)
{
typedef typename std::iterator_traits<InputIterator>::value_type VT;
return boost::algorithm::exclusive_scan(first, last, result, init, std::plus<VT>());
}
}} // namespace boost and algorithm
#endif // BOOST_ALGORITHM_EXCLUSIVE_SCAN_HPP

View File

@ -0,0 +1,37 @@
/*
Copyright (c) Marshall Clow 2017.
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)
*/
/// \file for_each_n.hpp
/// \brief Apply a functor to the elements of a sequence
/// \author Marshall Clow
#ifndef BOOST_ALGORITHM_FOR_EACH_N_HPP
#define BOOST_ALGORITHM_FOR_EACH_N_HPP
#include <utility> // for std::pair
namespace boost { namespace algorithm {
/// \fn for_each_n(InputIterator first, Size n, Function f);
/// \return first + n
///
/// \param first The start of the first range.
/// \param n One past the end of the first range.
/// \param f A functor to apply to the elements of the sequence
/// \note If f returns a result, the result is ignored.
template<class InputIterator, class Size, class Function>
InputIterator for_each_n(InputIterator first, Size n, Function f)
{
for ( ; n > 0; --n, ++first )
f(*first);
return first;
}
}} // namespace boost and algorithm
#endif // BOOST_ALGORITHM_FOR_EACH_N_HPP

View File

@ -0,0 +1,60 @@
/*
Copyright (c) Marshall Clow 2017.
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)
*/
/// \file transform_reduce.hpp
/// \brief Combine the (transformed) elements of a sequence (or two) into a single value.
/// \author Marshall Clow
#ifndef BOOST_ALGORITHM_TRANSFORM_REDUCE_HPP
#define BOOST_ALGORITHM_TRANSFORM_REDUCE_HPP
#include <functional> // for std::plus
#include <iterator> // for std::iterator_traits
#include <boost/range/begin.hpp>
#include <boost/range/end.hpp>
#include <boost/range/value_type.hpp>
namespace boost { namespace algorithm {
template<class InputIterator, class OutputIterator, class T, class BinaryOperation>
OutputIterator inclusive_scan(InputIterator first, InputIterator last,
OutputIterator result, BinaryOperation bOp, T init)
{
for (; first != last; ++first, (void) ++result) {
init = bOp(init, *first);
*result = init;
}
return result;
}
template<class InputIterator, class OutputIterator, class BinaryOperation>
OutputIterator inclusive_scan(InputIterator first, InputIterator last,
OutputIterator result, BinaryOperation bOp)
{
if (first != last) {
typename std::iterator_traits<InputIterator>::value_type init = *first;
*result++ = init;
if (++first != last)
return boost::algorithm::inclusive_scan(first, last, result, bOp, init);
}
return result;
}
template<class InputIterator, class OutputIterator>
OutputIterator inclusive_scan(InputIterator first, InputIterator last,
OutputIterator result)
{
typedef typename std::iterator_traits<InputIterator>::value_type VT;
return boost::algorithm::inclusive_scan(first, last, result, std::plus<VT>());
}
}} // namespace boost and algorithm
#endif // BOOST_ALGORITHM_TRANSFORM_REDUCE_HPP

View File

@ -0,0 +1,72 @@
/*
Copyright (c) Marshall Clow 2017.
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)
*/
/// \file reduce.hpp
/// \brief Combine the elements of a sequence into a single value
/// \author Marshall Clow
#ifndef BOOST_ALGORITHM_REDUCE_HPP
#define BOOST_ALGORITHM_REDUCE_HPP
#include <functional> // for std::plus
#include <iterator> // for std::iterator_traits
#include <boost/range/begin.hpp>
#include <boost/range/end.hpp>
#include <boost/range/value_type.hpp>
namespace boost { namespace algorithm {
template<class InputIterator, class T, class BinaryOperation>
T reduce(InputIterator first, InputIterator last, T init, BinaryOperation bOp)
{
;
for (; first != last; ++first)
init = bOp(init, *first);
return init;
}
template<class InputIterator, class T>
T reduce(InputIterator first, InputIterator last, T init)
{
typedef typename std::iterator_traits<InputIterator>::value_type VT;
return boost::algorithm::reduce(first, last, init, std::plus<VT>());
}
template<class InputIterator>
typename std::iterator_traits<InputIterator>::value_type
reduce(InputIterator first, InputIterator last)
{
return boost::algorithm::reduce(first, last,
typename std::iterator_traits<InputIterator>::value_type());
}
template<class Range>
typename boost::range_value<Range>::type
reduce(const Range &r)
{
return boost::algorithm::reduce(boost::begin(r), boost::end(r));
}
// Not sure that this won't be ambiguous (1)
template<class Range, class T>
T reduce(const Range &r, T init)
{
return boost::algorithm::reduce(boost::begin (r), boost::end (r), init);
}
// Not sure that this won't be ambiguous (2)
template<class Range, class T, class BinaryOperation>
T reduce(const Range &r, T init, BinaryOperation bOp)
{
return boost::algorithm::reduce(boost::begin(r), boost::end(r), init, bOp);
}
}} // namespace boost and algorithm
#endif // BOOST_ALGORITHM_REDUCE_HPP

View File

@ -0,0 +1,46 @@
/*
Copyright (c) Marshall Clow 2017.
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)
*/
/// \file transform_exclusive_scan.hpp
/// \brief ????
/// \author Marshall Clow
#ifndef BOOST_ALGORITHM_TRANSFORM_EXCLUSIVE_SCAN_HPP
#define BOOST_ALGORITHM_TRANSFORM_EXCLUSIVE_SCAN_HPP
#include <functional> // for std::plus
#include <iterator> // for std::iterator_traits
#include <boost/range/begin.hpp>
#include <boost/range/end.hpp>
#include <boost/range/value_type.hpp>
namespace boost { namespace algorithm {
template<class InputIterator, class OutputIterator, class T,
class BinaryOperation, class UnaryOperation>
OutputIterator transform_exclusive_scan(InputIterator first, InputIterator last,
OutputIterator result, T init,
BinaryOperation bOp, UnaryOperation uOp)
{
if (first != last)
{
T saved = init;
do
{
init = bOp(init, uOp(*first));
*result = saved;
saved = init;
++result;
} while (++first != last);
}
return result;
}
}} // namespace boost and algorithm
#endif // BOOST_ALGORITHM_TRANSFORM_EXCLUSIVE_SCAN_HPP

View File

@ -0,0 +1,59 @@
/*
Copyright (c) Marshall Clow 2017.
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)
*/
/// \file transform_reduce.hpp
/// \brief Combine the (transformed) elements of a sequence (or two) into a single value.
/// \author Marshall Clow
#ifndef BOOST_ALGORITHM_TRANSFORM_REDUCE_HPP
#define BOOST_ALGORITHM_TRANSFORM_REDUCE_HPP
#include <functional> // for std::plus
#include <iterator> // for std::iterator_traits
#include <boost/range/begin.hpp>
#include <boost/range/end.hpp>
#include <boost/range/value_type.hpp>
namespace boost { namespace algorithm {
template<class InputIterator, class OutputIterator,
class BinaryOperation, class UnaryOperation, class T>
OutputIterator transform_inclusive_scan(InputIterator first, InputIterator last,
OutputIterator result,
BinaryOperation bOp, UnaryOperation uOp,
T init)
{
for (; first != last; ++first, (void) ++result) {
init = bOp(init, uOp(*first));
*result = init;
}
return result;
}
template<class InputIterator, class OutputIterator,
class BinaryOperation, class UnaryOperation>
OutputIterator transform_inclusive_scan(InputIterator first, InputIterator last,
OutputIterator result,
BinaryOperation bOp, UnaryOperation uOp)
{
if (first != last) {
typename std::iterator_traits<InputIterator>::value_type init = uOp(*first);
*result++ = init;
if (++first != last)
return boost::algorithm::transform_inclusive_scan
(first, last, result, bOp, uOp, init);
}
return result;
}
}} // namespace boost and algorithm
#endif // BOOST_ALGORITHM_TRANSFORM_REDUCE_HPP

View File

@ -0,0 +1,55 @@
/*
Copyright (c) Marshall Clow 2017.
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)
*/
/// \file transform_reduce.hpp
/// \brief Combine the (transformed) elements of a sequence (or two) into a single value.
/// \author Marshall Clow
#ifndef BOOST_ALGORITHM_TRANSFORM_REDUCE_HPP
#define BOOST_ALGORITHM_TRANSFORM_REDUCE_HPP
#include <functional> // for std::plus
#include <iterator> // for std::iterator_traits
#include <boost/range/begin.hpp>
#include <boost/range/end.hpp>
#include <boost/range/value_type.hpp>
namespace boost { namespace algorithm {
template<class InputIterator1, class InputIterator2, class T,
class BinaryOperation1, class BinaryOperation2>
T transform_reduce(InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, T init,
BinaryOperation1 bOp1, BinaryOperation2 bOp2)
{
for (; first1 != last1; ++first1, (void) ++first2)
init = bOp1(init, bOp2(*first1, *first2));
return init;
}
template<class InputIterator, class T,
class BinaryOperation, class UnaryOperation>
T transform_reduce(InputIterator first, InputIterator last,
T init, BinaryOperation bOp, UnaryOperation uOp)
{
for (; first != last; ++first)
init = bOp(init, uOp(*first));
return init;
}
template<class InputIterator1, class InputIterator2, class T>
T transform_reduce(InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, T init)
{
return boost::algorithm::transform_reduce(first1, last1, first2, init,
std::plus<T>(), std::multiplies<T>());
}
}} // namespace boost and algorithm
#endif // BOOST_ALGORITHM_TRANSFORM_REDUCE_HPP

View File

@ -0,0 +1,97 @@
/*
Copyright (c) T. Zachary Laine 2018.
Distributed under the Boost Software License, Version 1.0. (See accompanying
file LICENSE10.txt or copy at http://www.boost.org/LICENSE10.txt)
*/
#ifndef BOOST_ALGORITHM_FIND_BACKWARD_HPP
#define BOOST_ALGORITHM_FIND_BACKWARD_HPP
#include <boost/config.hpp>
#include <boost/range/begin.hpp>
#include <boost/range/end.hpp>
#include <utility>
namespace boost { namespace algorithm {
template<typename BidiIter, typename T>
BOOST_CXX14_CONSTEXPR
BidiIter find_backward(BidiIter first, BidiIter last, const T & x)
{
BidiIter it = last;
while (it != first) {
if (*--it == x)
return it;
}
return last;
}
template<typename Range, typename T>
BOOST_CXX14_CONSTEXPR
typename boost::range_iterator<Range>::type find_backward(Range & range, const T & x)
{
return ::boost::algorithm::find_backward(boost::begin(range), boost::end(range), x);
}
template<typename BidiIter, typename T>
BOOST_CXX14_CONSTEXPR
BidiIter find_not_backward(BidiIter first, BidiIter last, const T & x)
{
BidiIter it = last;
while (it != first) {
if (*--it != x)
return it;
}
return last;
}
template<typename Range, typename T>
BOOST_CXX14_CONSTEXPR
typename boost::range_iterator<Range>::type find_not_backward(Range & range, const T & x)
{
return ::boost::algorithm::find_not_backward(boost::begin(range), boost::end(range), x);
}
template<typename BidiIter, typename Pred>
BOOST_CXX14_CONSTEXPR
BidiIter find_if_backward(BidiIter first, BidiIter last, Pred p)
{
BidiIter it = last;
while (it != first) {
if (p(*--it))
return it;
}
return last;
}
template<typename Range, typename Pred>
BOOST_CXX14_CONSTEXPR
typename boost::range_iterator<Range>::type find_if_backward(Range & range, Pred p)
{
return ::boost::algorithm::find_if_backward(boost::begin(range), boost::end(range), p);
}
template<typename BidiIter, typename Pred>
BOOST_CXX14_CONSTEXPR
BidiIter find_if_not_backward(BidiIter first, BidiIter last, Pred p)
{
BidiIter it = last;
while (it != first) {
if (!p(*--it))
return it;
}
return last;
}
template<typename Range, typename Pred>
BOOST_CXX14_CONSTEXPR
typename boost::range_iterator<Range>::type find_if_not_backward(Range & range, Pred p)
{
return ::boost::algorithm::find_if_not_backward(boost::begin(range), boost::end(range), p);
}
}} // namespace boost and algorithm
#endif // BOOST_ALGORITHM_FIND_BACKWARD_HPP

View File

@ -0,0 +1,39 @@
/*
Copyright (c) T. Zachary Laine 2018.
Distributed under the Boost Software License, Version 1.0. (See accompanying
file LICENSE10.txt or copy at http://www.boost.org/LICENSE10.txt)
*/
#ifndef BOOST_ALGORITHM_FIND_NOT_HPP
#define BOOST_ALGORITHM_FIND_NOT_HPP
#include <boost/config.hpp>
#include <boost/range/begin.hpp>
#include <boost/range/end.hpp>
#include <utility>
namespace boost { namespace algorithm {
template<typename InputIter, typename Sentinel, typename T>
BOOST_CXX14_CONSTEXPR
InputIter find_not(InputIter first, Sentinel last, const T & x)
{
for (; first != last; ++first) {
if (*first != x)
break;
}
return first;
}
template<typename Range, typename T>
BOOST_CXX14_CONSTEXPR
typename boost::range_iterator<Range>::type find_not(Range & r, const T & x)
{
return ::boost::algorithm::find_not(boost::begin(r), boost::end(r), x);
}
}} // namespace boost and algorithm
#endif // BOOST_ALGORITHM_FIND_NOT_HPP

View File

@ -73,7 +73,7 @@ namespace detail {
else if ( c >= 'A' && c <= 'F' ) retval = c - 'A' + 10;
else if ( c >= 'a' && c <= 'f' ) retval = c - 'a' + 10;
else BOOST_THROW_EXCEPTION (non_hex_input() << bad_char (c));
return retval;
return static_cast<char>(retval);
}
// My own iterator_traits class.

View File

@ -35,7 +35,7 @@ namespace boost { namespace algorithm {
/// For other sequences function will return false.
/// Complexity: O(N).
template <typename BidirectionalIterator, typename Predicate>
bool is_palindrome(BidirectionalIterator begin, BidirectionalIterator end, Predicate p )
bool is_palindrome(BidirectionalIterator begin, BidirectionalIterator end, Predicate p)
{
if(begin == end)
{
@ -63,7 +63,7 @@ bool is_palindrome(BidirectionalIterator begin, BidirectionalIterator end, Predi
/// \return true if the entire sequence is palindrome
///
/// \param begin The start of the input sequence
/// \param end One past the end of the input sequence
/// \param end One past the end of the input sequence
///
/// \note This function will return true for empty sequences and for palindromes.
/// For other sequences function will return false.
@ -71,26 +71,8 @@ bool is_palindrome(BidirectionalIterator begin, BidirectionalIterator end, Predi
template <typename BidirectionalIterator>
bool is_palindrome(BidirectionalIterator begin, BidirectionalIterator end)
{
if(begin == end)
{
return true;
}
--end;
while(begin != end)
{
if(!(*begin == *end))
{
return false;
}
++begin;
if(begin == end)
{
break;
}
--end;
}
return true;
return is_palindrome(begin, end,
std::equal_to<typename std::iterator_traits<BidirectionalIterator>::value_type> ());
}
/// \fn is_palindrome ( const R& range )
@ -122,7 +104,6 @@ bool is_palindrome(const R& range, Predicate p)
return is_palindrome(boost::begin(range), boost::end(range), p);
}
/// \fn is_palindrome ( const char* str )
/// \return true if the entire sequence is palindrome
///
@ -138,7 +119,6 @@ bool is_palindrome(const char* str)
return is_palindrome(str, str + strlen(str));
}
/// \fn is_palindrome ( const char* str, Predicate p )
/// \return true if the entire sequence is palindrome
///
@ -155,7 +135,6 @@ bool is_palindrome(const char* str, Predicate p)
return true;
return is_palindrome(str, str + strlen(str), p);
}
}}
#endif // BOOST_ALGORITHM_IS_PALINDROME_HPP

View File

@ -0,0 +1,63 @@
/*
Copyright (c) Alexander Zaitsev <zamazan4ik@gmail.by>, 2017.
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)
*/
/// \file is_partitioned_until.hpp
/// \brief Tell if a sequence is partitioned
/// \author Alexander Zaitsev
#ifndef BOOST_ALGORITHM_IS_PARTITIONED_UNTIL_HPP
#define BOOST_ALGORITHM_IS_PARTITIONED_UNTIL_HPP
#include <boost/range/begin.hpp>
#include <boost/range/end.hpp>
namespace boost { namespace algorithm {
/// \fn is_partitioned_until ( InputIterator first, InputIterator last, UnaryPredicate p )
/// \brief Tests to see if a sequence is partitioned according to a predicate.
/// In other words, all the items in the sequence that satisfy the predicate are at the beginning of the sequence.
///
/// \param first The start of the input sequence
/// \param last One past the end of the input sequence
/// \param p The predicate to test the values with
///
/// \note Returns the first iterator 'it' in the sequence [first, last) for which is_partitioned(first, it, p) is false.
/// Returns last if the entire sequence is partitioned.
/// Complexity: O(N).
template <typename InputIterator, typename UnaryPredicate>
InputIterator is_partitioned_until ( InputIterator first, InputIterator last, UnaryPredicate p )
{
// Run through the part that satisfy the predicate
for ( ; first != last; ++first )
if ( !p (*first))
break;
// Now the part that does not satisfy the predicate
for ( ; first != last; ++first )
if ( p (*first))
return first;
return last;
}
/// \fn is_partitioned_until ( const Range &r, UnaryPredicate p )
/// \brief Tests to see if a sequence is partitioned according to a predicate.
/// In other words, all the items in the sequence that satisfy the predicate are at the beginning of the sequence.
///
/// \param r The input range
/// \param p The predicate to test the values with
///
/// \note Returns the first iterator 'it' in the sequence [first, last) for which is_partitioned(first, it, p) is false.
/// Returns last if the entire sequence is partitioned.
/// Complexity: O(N).
template <typename Range, typename UnaryPredicate>
typename boost::range_iterator<const Range>::type is_partitioned_until ( const Range &r, UnaryPredicate p )
{
return boost::algorithm::is_partitioned_until (boost::begin(r), boost::end(r), p);
}
}}
#endif // BOOST_ALGORITHM_IS_PARTITIONED_UNTIL_HPP

View File

@ -152,8 +152,8 @@ Requirements:
template<typename Iter, typename Container>
void compute_bm_prefix ( Iter pat_first, Iter pat_last, Container &prefix ) {
const std::size_t count = std::distance ( pat_first, pat_last );
void compute_bm_prefix ( Iter first, Iter last, Container &prefix ) {
const std::size_t count = std::distance ( first, last );
BOOST_ASSERT ( count > 0 );
BOOST_ASSERT ( prefix.size () == count );
@ -161,26 +161,26 @@ Requirements:
std::size_t k = 0;
for ( std::size_t i = 1; i < count; ++i ) {
BOOST_ASSERT ( k < count );
while ( k > 0 && ( pat_first[k] != pat_first[i] )) {
while ( k > 0 && ( first[k] != first[i] )) {
BOOST_ASSERT ( k < count );
k = prefix [ k - 1 ];
}
if ( pat_first[k] == pat_first[i] )
if ( first[k] == first[i] )
k++;
prefix [ i ] = k;
}
}
void build_suffix_table ( patIter pat_first, patIter pat_last ) {
const std::size_t count = (std::size_t) std::distance ( pat_first, pat_last );
void build_suffix_table ( patIter first, patIter last ) {
const std::size_t count = (std::size_t) std::distance ( first, last );
if ( count > 0 ) { // empty pattern
std::vector<typename std::iterator_traits<patIter>::value_type> reversed(count);
(void) std::reverse_copy ( pat_first, pat_last, reversed.begin ());
(void) std::reverse_copy ( first, last, reversed.begin ());
std::vector<difference_type> prefix (count);
compute_bm_prefix ( pat_first, pat_last, prefix );
compute_bm_prefix ( first, last, prefix );
std::vector<difference_type> prefix_reversed (count);
compute_bm_prefix ( reversed.begin (), reversed.end (), prefix_reversed );

View File

@ -155,9 +155,9 @@ namespace boost { namespace algorithm {
void preKmp ( patIter first, patIter last ) {
const /*std::size_t*/ int count = std::distance ( first, last );
const difference_type count = std::distance ( first, last );
int i, j;
difference_type i, j;
i = 0;
j = skip_[0] = -1;
@ -177,7 +177,7 @@ namespace boost { namespace algorithm {
void init_skip_table ( patIter first, patIter last ) {
const difference_type count = std::distance ( first, last );
int j;
difference_type j;
skip_ [ 0 ] = -1;
for ( int i = 1; i <= count; ++i ) {
j = skip_ [ i - 1 ];

View File

@ -30,8 +30,10 @@ namespace boost {
// a tolower functor
template<typename CharT>
struct to_lowerF : public std::unary_function<CharT, CharT>
struct to_lowerF
{
typedef CharT argument_type;
typedef CharT result_type;
// Constructor
to_lowerF( const std::locale& Loc ) : m_Loc( &Loc ) {}
@ -50,8 +52,10 @@ namespace boost {
// a toupper functor
template<typename CharT>
struct to_upperF : public std::unary_function<CharT, CharT>
struct to_upperF
{
typedef CharT argument_type;
typedef CharT result_type;
// Constructor
to_upperF( const std::locale& Loc ) : m_Loc( &Loc ) {}

View File

@ -40,7 +40,7 @@ namespace boost {
// Protected construction/destruction
// Default constructor
find_iterator_base() {};
find_iterator_base() {}
// Copy construction
find_iterator_base( const find_iterator_base& Other ) :
m_Finder(Other.m_Finder) {}

View File

@ -89,9 +89,10 @@ namespace boost {
template<
typename SeqT,
typename IteratorT=BOOST_STRING_TYPENAME SeqT::const_iterator >
struct copy_iterator_rangeF :
public std::unary_function< iterator_range<IteratorT>, SeqT >
struct copy_iterator_rangeF
{
typedef iterator_range<IteratorT> argument_type;
typedef SeqT result_type;
SeqT operator()( const iterator_range<IteratorT>& Range ) const
{
return copy_range<SeqT>(Range);

View File

@ -43,7 +43,6 @@ namespace boost {
The result is given as an \c iterator_range delimiting the match.
\param Search A substring to be searched for.
\param Comp An element comparison predicate
\return An instance of the \c first_finder object
*/
template<typename RangeT>
@ -84,7 +83,6 @@ namespace boost {
The result is given as an \c iterator_range delimiting the match.
\param Search A substring to be searched for.
\param Comp An element comparison predicate
\return An instance of the \c last_finder object
*/
template<typename RangeT>
@ -124,7 +122,6 @@ namespace boost {
\param Search A substring to be searched for.
\param Nth An index of the match to be find
\param Comp An element comparison predicate
\return An instance of the \c nth_finder object
*/
template<typename RangeT>
@ -230,7 +227,6 @@ namespace boost {
\param Begin Beginning of the range
\param End End of the range
\param Range The range.
\return An instance of the \c range_finger object
*/
template< typename ForwardIteratorT >

View File

@ -0,0 +1,81 @@
// (C) Copyright Marshall Clow 2018
// Use, modification and distribution are 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)
#include <iterator> // for std::distance
#include <cassert> // for assert
#include <boost/algorithm/minmax_element.hpp>
#include <boost/algorithm/cxx11/none_of.hpp>
// Fuzzing tests for:
//
// template <class ForwardIterator>
// std::pair<ForwardIterator,ForwardIterator>
// minmax_element(ForwardIterator first, ForwardIterator last);
//
// template <class ForwardIterator, class BinaryPredicate>
// std::pair<ForwardIterator,ForwardIterator>
// minmax_element(ForwardIterator first, ForwardIterator last,
// BinaryPredicate comp);
bool greater(uint8_t lhs, uint8_t rhs) { return lhs > rhs; }
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t sz) {
typedef std::pair<const uint8_t *, const uint8_t *> result_t;
if (sz == 0) return 0; // we need at least one element
{
// Find the min and max
result_t result = boost::minmax_element(data, data + sz);
// The iterators have to be in the sequence - and not at the end!
assert(std::distance(data, result.first) < sz);
assert(std::distance(data, result.second) < sz);
// the minimum element can't be bigger than the max element
uint8_t min_value = *result.first;
uint8_t max_value = *result.second;
assert(min_value <= max_value);
// None of the elements in the sequence can be less than the min, nor greater than the max
for (size_t i = 0; i < sz; ++i) {
assert(min_value <= data[i]);
assert(data[i] <= max_value);
}
// We returned the first min element, and the first max element
assert(boost::algorithm::none_of_equal(data, result.first, min_value));
assert(boost::algorithm::none_of_equal(data, result.second, max_value));
}
{
// Find the min and max
result_t result = boost::minmax_element(data, data + sz, greater);
// The iterators have to be in the sequence - and not at the end!
assert(std::distance(data, result.first) < sz);
assert(std::distance(data, result.second) < sz);
// the minimum element can't be bigger than the max element
uint8_t min_value = *result.first;
uint8_t max_value = *result.second;
assert (!greater(max_value, min_value));
// None of the elements in the sequence can be less than the min, nor greater than the max
for (size_t i = 0; i < sz; ++i) {
assert(!greater(data[i], min_value));
assert(!greater(max_value, data[i]));
}
// We returned the first min element, and the first max element
assert(boost::algorithm::none_of_equal(data, result.first, min_value));
assert(boost::algorithm::none_of_equal(data, result.second, max_value));
}
return 0;
}

View File

@ -0,0 +1,141 @@
// (C) Copyright Marshall Clow 2018
// Use, modification and distribution are 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)
#include <iterator> // for std::distance
#include <cassert> // for assert
#include <boost/algorithm/minmax_element.hpp>
#include <boost/algorithm/cxx11/none_of.hpp>
// Fuzzing tests for:
//
// template <class ForwardIterator>
// std::pair<ForwardIterator,ForwardIterator>
// first_min_first_max_element(ForwardIterator first, ForwardIterator last);
//
// template <class ForwardIterator, class BinaryPredicate>
// std::pair<ForwardIterator,ForwardIterator>
// first_min_first_max_element(ForwardIterator first, ForwardIterator last,
// BinaryPredicate comp);
//
// identical signatures for:
// first_min_last_max_element
// last_min_first_max_element
// last_min_last_max_element
bool greater(uint8_t lhs, uint8_t rhs) { return lhs > rhs; }
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t sz) {
typedef std::pair<const uint8_t *, const uint8_t *> result_t;
const uint8_t * const dend = data + sz;
if (sz == 0) return 0; // we need at least one element
{
// Find the min and max
result_t resultff = boost::first_min_first_max_element(data, dend);
result_t resultfl = boost::first_min_last_max_element (data, dend);
result_t resultlf = boost::last_min_first_max_element (data, dend);
result_t resultll = boost::last_min_last_max_element (data, dend);
// The iterators have to be in the sequence - and not at the end!
assert(std::distance(data, resultff.first) < sz);
assert(std::distance(data, resultff.second) < sz);
assert(std::distance(data, resultfl.first) < sz);
assert(std::distance(data, resultfl.second) < sz);
assert(std::distance(data, resultlf.first) < sz);
assert(std::distance(data, resultlf.second) < sz);
assert(std::distance(data, resultll.first) < sz);
assert(std::distance(data, resultll.second) < sz);
// the minimum element can't be bigger than the max element
// Did we find the same min value and max value?
uint8_t min_value = *resultff.first;
uint8_t max_value = *resultff.second;
assert(min_value <= max_value);
// Each variant should have found the same min/max values
assert(*resultff.first == min_value);
assert(*resultfl.first == min_value);
assert(*resultlf.first == min_value);
assert(*resultll.first == min_value);
assert(*resultff.second == max_value);
assert(*resultfl.second == max_value);
assert(*resultlf.second == max_value);
assert(*resultll.second == max_value);
// None of the elements in the sequence can be less than the min, nor greater than the max
for (size_t i = 0; i < sz; ++i) {
assert(min_value <= data[i]);
assert(data[i] <= max_value);
}
// Make sure we returned the "right" first and last element
assert(boost::algorithm::none_of_equal(data, resultff.first, min_value));
assert(boost::algorithm::none_of_equal(data, resultfl.first, min_value));
assert(boost::algorithm::none_of_equal(resultlf.first + 1, dend, min_value));
assert(boost::algorithm::none_of_equal(resultll.first + 1, dend, min_value));
assert(boost::algorithm::none_of_equal(data, resultff.second, max_value));
assert(boost::algorithm::none_of_equal(resultfl.second + 1, dend, max_value));
assert(boost::algorithm::none_of_equal(data, resultlf.second, max_value));
assert(boost::algorithm::none_of_equal(resultll.second + 1, dend, max_value));
}
{
// Find the min and max
result_t resultff = boost::first_min_first_max_element(data, dend, greater);
result_t resultfl = boost::first_min_last_max_element (data, dend, greater);
result_t resultlf = boost::last_min_first_max_element (data, dend, greater);
result_t resultll = boost::last_min_last_max_element (data, dend, greater);
// The iterators have to be in the sequence - and not at the end!
assert(std::distance(data, resultff.first) < sz);
assert(std::distance(data, resultff.second) < sz);
assert(std::distance(data, resultfl.first) < sz);
assert(std::distance(data, resultfl.second) < sz);
assert(std::distance(data, resultlf.first) < sz);
assert(std::distance(data, resultlf.second) < sz);
assert(std::distance(data, resultll.first) < sz);
assert(std::distance(data, resultll.second) < sz);
// the minimum element can't be bigger than the max element
uint8_t min_value = *resultff.first;
uint8_t max_value = *resultff.second;
assert (!greater(max_value, min_value));
// Each variant should have found the same min/max values
assert(*resultff.first == min_value);
assert(*resultfl.first == min_value);
assert(*resultlf.first == min_value);
assert(*resultll.first == min_value);
assert(*resultff.second == max_value);
assert(*resultfl.second == max_value);
assert(*resultlf.second == max_value);
assert(*resultll.second == max_value);
// None of the elements in the sequence can be less than the min, nor greater than the max
for (size_t i = 0; i < sz; ++i) {
assert(!greater(data[i], min_value));
assert(!greater(max_value, data[i]));
}
// We returned the first min element, and the first max element
assert(boost::algorithm::none_of_equal(data, resultff.first, min_value));
assert(boost::algorithm::none_of_equal(data, resultfl.first, min_value));
assert(boost::algorithm::none_of_equal(resultlf.first + 1, dend, min_value));
assert(boost::algorithm::none_of_equal(resultll.first + 1, dend, min_value));
assert(boost::algorithm::none_of_equal(data, resultff.second, max_value));
assert(boost::algorithm::none_of_equal(resultfl.second + 1, dend, max_value));
assert(boost::algorithm::none_of_equal(data, resultlf.second, max_value));
assert(boost::algorithm::none_of_equal(resultll.second + 1, dend, max_value));
}
return 0;
}

View File

@ -20,6 +20,19 @@
#define BOOST_TEST_MAIN
#include <boost/test/unit_test.hpp>
#if (__cplusplus >= 201103L) || defined(BOOST_NO_CXX98_RANDOM_SHUFFLE)
#include <random>
std::default_random_engine gen;
template<typename RandomIt>
void do_shuffle(RandomIt first, RandomIt last)
{ std::shuffle(first, last, gen); }
#else
template<typename RandomIt>
void do_shuffle(RandomIt first, RandomIt last)
{ std::random_shuffle(first, last); }
#endif
class custom {
int m_x;
friend bool operator<(custom const& x, custom const& y);
@ -117,7 +130,7 @@ void test_minmax(CIterator first, CIterator last, int n)
CHECK_EQUAL_ITERATORS( min, std::min_element(first, last), first );
CHECK_EQUAL_ITERATORS( max, std::max_element(first, last), first );
// second version, comp function object (keeps a counter!)
lc.reset();
tie( boost::minmax_element(first, last, lc), min, max );
@ -183,7 +196,7 @@ void test_minmax(CIterator first, CIterator last, int n)
template <class Container, class Iterator, class Value>
void test_container(Iterator first, Iterator last, int n,
Container* dummy = 0
Container* /* dummy */ = 0
BOOST_APPEND_EXPLICIT_TEMPLATE_TYPE(Value) )
{
Container c(first, last);
@ -223,7 +236,7 @@ void test(int n BOOST_APPEND_EXPLICIT_TEMPLATE_TYPE(Value))
test_range(first, last, n);
// Populate test vector with random values
std::random_shuffle(first, last);
do_shuffle(first, last);
test_range(first, last, n);
}

View File

@ -12,7 +12,7 @@ import testing ;
alias unit_test_framework
: # sources
/boost//unit_test_framework
;
;
{
@ -31,13 +31,13 @@ alias unit_test_framework
[ run clamp_test.cpp unit_test_framework : : : : clamp_test ]
[ run power_test.cpp unit_test_framework : : : : power_test ]
[ compile-fail power_fail1.cpp : : : : ]
# Cxx11 tests
[ run all_of_test.cpp unit_test_framework : : : : all_of_test ]
[ run any_of_test.cpp unit_test_framework : : : : any_of_test ]
[ run none_of_test.cpp unit_test_framework : : : : none_of_test ]
[ run one_of_test.cpp unit_test_framework : : : : one_of_test ]
[ run ordered_test.cpp unit_test_framework : : : : ordered_test ]
[ run find_if_not_test1.cpp unit_test_framework : : : : find_if_not_test1 ]
[ run copy_if_test1.cpp unit_test_framework : : : : copy_if_test1 ]
@ -53,6 +53,16 @@ alias unit_test_framework
[ run equal_test.cpp unit_test_framework : : : : equal_test ]
[ run mismatch_test.cpp unit_test_framework : : : : mismatch_test ]
# Cxx17 tests
[ run for_each_n_test.cpp unit_test_framework : : : : for_each_n_test ]
[ run reduce_test.cpp unit_test_framework : : : : reduce_test ]
[ run transform_reduce_test.cpp unit_test_framework : : : : transform_reduce_test ]
[ run inclusive_scan_test.cpp unit_test_framework : : : : inclusive_scan_test ]
[ run exclusive_scan_test.cpp unit_test_framework : : : : exclusive_scan_test ]
[ run transform_inclusive_scan_test.cpp unit_test_framework : : : : transform_inclusive_scan_test ]
[ run transform_exclusive_scan_test.cpp unit_test_framework : : : : transform_exclusive_scan_test ]
# Maybe GCD and LCM as well
# Hex tests
[ run hex_test1.cpp unit_test_framework : : : : hex_test1 ]
[ run hex_test2.cpp unit_test_framework : : : : hex_test2 ]
@ -70,6 +80,15 @@ alias unit_test_framework
# Is_palindrome tests
[ run is_palindrome_test.cpp unit_test_framework : : : : is_palindrome_test ]
# Is_partitioned_until tests
[ run is_partitioned_until_test.cpp unit_test_framework : : : : is_partitioned_until_test ]
# Apply_permutation tests
[ run apply_permutation_test.cpp unit_test_framework : : : : apply_permutation_test ]
# Find tests
[ run find_not_test.cpp unit_test_framework : : : : find_not_test ]
[ run find_backward_test.cpp unit_test_framework : : : : find_backward_test ]
;
}

View File

@ -18,10 +18,9 @@
#include <list>
template<typename T>
struct is_ : public std::unary_function<T, bool> {
is_ ( T v ) : val_ ( v ) {}
~is_ () {}
bool operator () ( T comp ) const { return val_ == comp; }
struct is_ {
BOOST_CXX14_CONSTEXPR is_ ( T v ) : val_ ( v ) {}
BOOST_CXX14_CONSTEXPR bool operator () ( T comp ) const { return val_ == comp; }
private:
is_ (); // need a value
@ -33,7 +32,7 @@ namespace ba = boost::algorithm;
void test_all ()
{
// Note: The literal values here are tested against directly, careful if you change them:
int some_numbers[] = { 1, 1, 1, 18, 10 };
BOOST_CXX14_CONSTEXPR int some_numbers[] = { 1, 1, 1, 18, 10 };
std::vector<int> vi(some_numbers, some_numbers + 5);
std::list<int> li(vi.begin(), vi.end ());
@ -77,7 +76,15 @@ void test_all ()
l_iter++; l_iter++; l_iter++;
BOOST_CHECK ( ba::all_of_equal ( li.begin(), l_iter, 1 ));
BOOST_CHECK ( ba::all_of ( li.begin(), l_iter, is_<int> ( 1 )));
BOOST_CXX14_CONSTEXPR bool constexpr_res =
!ba::all_of_equal ( some_numbers, 1 ) &&
!ba::all_of ( some_numbers, is_<int> ( 1 )) &&
ba::all_of_equal ( some_numbers, some_numbers + 3, 1 ) &&
ba::all_of ( some_numbers, some_numbers + 3, is_<int> ( 1 )) &&
true;
BOOST_CHECK ( constexpr_res );
}

View File

@ -18,10 +18,9 @@
#include <list>
template<typename T>
struct is_ : public std::unary_function<T, bool> {
is_ ( T v ) : val_ ( v ) {}
~is_ () {}
bool operator () ( T comp ) const { return val_ == comp; }
struct is_ {
BOOST_CXX14_CONSTEXPR is_ ( T v ) : val_ ( v ) {}
BOOST_CXX14_CONSTEXPR bool operator () ( T comp ) const { return val_ == comp; }
private:
is_ (); // need a value
@ -33,7 +32,7 @@ namespace ba = boost::algorithm;
void test_any ()
{
// Note: The literal values here are tested against directly, careful if you change them:
int some_numbers[] = { 1, 5, 0, 18, 10 };
BOOST_CXX14_CONSTEXPR int some_numbers[] = { 1, 5, 0, 18, 10 };
std::vector<int> vi(some_numbers, some_numbers + 5);
std::list<int> li(vi.begin(), vi.end ());
@ -97,6 +96,15 @@ void test_any ()
BOOST_CHECK ( ba::any_of ( li.begin(), l_iter, is_<int> ( 5 )));
BOOST_CHECK (!ba::any_of_equal ( li.begin(), l_iter, 18 ));
BOOST_CHECK (!ba::any_of ( li.begin(), l_iter, is_<int> ( 18 )));
BOOST_CXX14_CONSTEXPR bool constexpr_res =
ba::any_of_equal ( some_numbers, 1 ) &&
ba::any_of ( some_numbers, is_<int> ( 1 )) &&
!ba::any_of_equal ( some_numbers, some_numbers + 3, 777 ) &&
!ba::any_of ( some_numbers, some_numbers + 3, is_<int> ( 777 )) &&
true;
BOOST_CHECK ( constexpr_res );
}

View File

@ -0,0 +1,169 @@
/*
Copyright (c) Alexander Zaitsev <zamazan4ik@gmail.com>, 2017
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 <vector>
#include <boost/algorithm/apply_permutation.hpp>
#define BOOST_TEST_DYN_LINK
#define BOOST_TEST_MAIN
#include <boost/test/unit_test.hpp>
namespace ba = boost::algorithm;
void test_apply_permutation()
{
//Empty
{
std::vector<int> vec, order, result;
ba::apply_permutation(vec.begin(), vec.end(), order.begin(), order.end());
BOOST_CHECK(vec == result);
}
//1 element
{
std::vector<int> vec, order, result;
vec.push_back(1);
order.push_back(0);
result = vec;
ba::apply_permutation(vec.begin(), vec.end(), order.begin(), order.end());
BOOST_CHECK(vec == result);
}
//2 elements, no changes
{
std::vector<int> vec, order, result;
vec.push_back(1); vec.push_back(2);
order.push_back(0); order.push_back(1);
result = vec;
ba::apply_permutation(vec.begin(), vec.end(), order.begin(), order.end());
BOOST_CHECK(vec == result);
}
//2 elements, changed
{
std::vector<int> vec, order, result;
vec.push_back(1); vec.push_back(2);
order.push_back(1); order.push_back(0);
result.push_back(2); result.push_back(1);
ba::apply_permutation(vec.begin(), vec.end(), order.begin(), order.end());
BOOST_CHECK(vec == result);
}
//Multiple elements, no changes
{
std::vector<int> vec, order, result;
vec.push_back(1); vec.push_back(2); vec.push_back(3); vec.push_back(4); vec.push_back(5);
order.push_back(0); order.push_back(1); order.push_back(2); order.push_back(3); order.push_back(4);
result = vec;
ba::apply_permutation(vec.begin(), vec.end(), order.begin(), order.end());
BOOST_CHECK(vec == result);
}
//Multiple elements, changed
{
std::vector<int> vec, order, result;
vec.push_back(1); vec.push_back(2); vec.push_back(3); vec.push_back(4); vec.push_back(5);
order.push_back(4); order.push_back(3); order.push_back(2); order.push_back(1); order.push_back(0);
result.push_back(5); result.push_back(4); result.push_back(3); result.push_back(2); result.push_back(1);
ba::apply_permutation(vec.begin(), vec.end(), order.begin(), order.end());
BOOST_CHECK(vec == result);
}
//Just test range interface
{
std::vector<int> vec, order, result;
vec.push_back(1); vec.push_back(2); vec.push_back(3); vec.push_back(4); vec.push_back(5);
order.push_back(0); order.push_back(1); order.push_back(2); order.push_back(3); order.push_back(4);
result = vec;
ba::apply_permutation(vec, order);
BOOST_CHECK(vec == result);
}
}
void test_apply_reverse_permutation()
{
//Empty
{
std::vector<int> vec, order, result;
ba::apply_reverse_permutation(vec.begin(), vec.end(), order.begin(), order.end());
BOOST_CHECK(vec == result);
}
//1 element
{
std::vector<int> vec, order, result;
vec.push_back(1);
order.push_back(0);
result = vec;
ba::apply_reverse_permutation(vec.begin(), vec.end(), order.begin(), order.end());
BOOST_CHECK(vec == result);
}
//2 elements, no changes
{
std::vector<int> vec, order, result;
vec.push_back(1); vec.push_back(2);
order.push_back(0); order.push_back(1);
result = vec;
ba::apply_reverse_permutation(vec.begin(), vec.end(), order.begin(), order.end());
BOOST_CHECK(vec == result);
}
//2 elements, changed
{
std::vector<int> vec, order, result;
vec.push_back(1); vec.push_back(2);
order.push_back(1); order.push_back(0);
result.push_back(2); result.push_back(1);
ba::apply_reverse_permutation(vec.begin(), vec.end(), order.begin(), order.end());
BOOST_CHECK(vec == result);
}
//Multiple elements, no changes
{
std::vector<int> vec, order, result;
vec.push_back(1); vec.push_back(2); vec.push_back(3); vec.push_back(4); vec.push_back(5);
order.push_back(0); order.push_back(1); order.push_back(2); order.push_back(3); order.push_back(4);
result = vec;
ba::apply_reverse_permutation(vec.begin(), vec.end(), order.begin(), order.end());
BOOST_CHECK(vec == result);
}
//Multiple elements, changed
{
std::vector<int> vec, order, result;
vec.push_back(1); vec.push_back(2); vec.push_back(3); vec.push_back(4); vec.push_back(5);
order.push_back(4); order.push_back(3); order.push_back(2); order.push_back(1); order.push_back(0);
result.push_back(5); result.push_back(4); result.push_back(3); result.push_back(2); result.push_back(1);
ba::apply_reverse_permutation(vec.begin(), vec.end(), order.begin(), order.end());
BOOST_CHECK(vec == result);
}
//Just test range interface
{
std::vector<int> vec, order, result;
vec.push_back(1); vec.push_back(2); vec.push_back(3); vec.push_back(4); vec.push_back(5);
order.push_back(0); order.push_back(1); order.push_back(2); order.push_back(3); order.push_back(4);
result = vec;
ba::apply_reverse_permutation(vec, order);
BOOST_CHECK(vec == result);
}
}
BOOST_AUTO_TEST_CASE(test_main)
{
test_apply_permutation();
test_apply_reverse_permutation();
}

View File

@ -14,8 +14,8 @@
namespace ba = boost::algorithm;
bool intGreater ( int lhs, int rhs ) { return lhs > rhs; }
bool doubleGreater ( double lhs, double rhs ) { return lhs > rhs; }
BOOST_CONSTEXPR bool intGreater ( int lhs, int rhs ) { return lhs > rhs; }
BOOST_CONSTEXPR bool doubleGreater ( double lhs, double rhs ) { return lhs > rhs; }
class custom {
public:
@ -45,6 +45,10 @@ void test_ints()
BOOST_CHECK_EQUAL ( 1, ba::clamp ( 0, 1, 10 ));
BOOST_CHECK_EQUAL ( 10, ba::clamp ( 10, 1, 10 ));
BOOST_CHECK_EQUAL ( 10, ba::clamp ( 11, 1, 10 ));
BOOST_CXX14_CONSTEXPR bool constexpr_res = (
ba::clamp ( 3, 1, 10 ) == 3
);
BOOST_CHECK( constexpr_res );
BOOST_CHECK_EQUAL ( 3, ba::clamp ( 3, 10, 1, intGreater ));
BOOST_CHECK_EQUAL ( 1, ba::clamp ( 1, 10, 1, intGreater ));
@ -206,6 +210,110 @@ void test_int_range ()
BOOST_CHECK ( std::equal ( b_e(junk), outputs ));
}
void test_constexpr()
{
// Inside the range, equal to the endpoints, and outside the endpoints.
{
BOOST_CXX14_CONSTEXPR bool check_inside = (3 == ba::clamp ( 3, 1, 10 ));
BOOST_CHECK(check_inside);
BOOST_CXX14_CONSTEXPR bool check_min = (1 == ba::clamp ( 1, 1, 10 ));
BOOST_CHECK(check_min);
BOOST_CXX14_CONSTEXPR bool check_min_out = (1 == ba::clamp ( 0, 1, 10 ));
BOOST_CHECK(check_min_out);
BOOST_CXX14_CONSTEXPR bool check_max = (10 == ba::clamp ( 10, 1, 10 ));
BOOST_CHECK(check_max);
BOOST_CXX14_CONSTEXPR bool check_max_out = (10 == ba::clamp ( 11, 1, 10 ));
BOOST_CHECK(check_max_out);
}
{
BOOST_CXX14_CONSTEXPR bool check_inside = (3 == ba::clamp ( 3, 10, 1, intGreater ));
BOOST_CHECK(check_inside);
BOOST_CXX14_CONSTEXPR bool check_min = (1 == ba::clamp ( 1, 10, 1, intGreater ));
BOOST_CHECK(check_min);
BOOST_CXX14_CONSTEXPR bool check_min_out = (1 == ba::clamp ( 0, 10, 1, intGreater ));
BOOST_CHECK(check_min_out);
BOOST_CXX14_CONSTEXPR bool check_max = (10 == ba::clamp ( 10, 10, 1, intGreater ));
BOOST_CHECK(check_max);
BOOST_CXX14_CONSTEXPR bool check_max_out = (10 == ba::clamp ( 11, 10, 1, intGreater ));
BOOST_CHECK(check_max_out);
}
// Negative numbers
{
BOOST_CXX14_CONSTEXPR bool check_inside = (-3 == ba::clamp ( -3, -10, -1 ));
BOOST_CHECK(check_inside);
BOOST_CXX14_CONSTEXPR bool check_max = (-1 == ba::clamp ( -1, -10, -1 ));
BOOST_CHECK(check_max);
BOOST_CXX14_CONSTEXPR bool check_max_out = (-1 == ba::clamp ( 0, -10, -1 ));
BOOST_CHECK(check_max_out);
BOOST_CXX14_CONSTEXPR bool check_min = (-10 == ba::clamp ( -10, -10, -1 ));
BOOST_CHECK(check_min);
BOOST_CXX14_CONSTEXPR bool check_min_out = (-10 == ba::clamp ( -11, -10, -1 ));
BOOST_CHECK(check_min_out);
}
// Mixed positive and negative numbers
{
BOOST_CXX14_CONSTEXPR bool check_inside = (5 == ba::clamp ( 5, -10, 10 ));
BOOST_CHECK(check_inside);
BOOST_CXX14_CONSTEXPR bool check_min = (-10 == ba::clamp ( -10, -10, 10 ));
BOOST_CHECK(check_min);
BOOST_CXX14_CONSTEXPR bool check_min_out = (-10 == ba::clamp ( -15, -10, 10 ));
BOOST_CHECK(check_min_out);
BOOST_CXX14_CONSTEXPR bool check_max = (10 == ba::clamp ( 10, -10, 10 ));
BOOST_CHECK(check_max);
BOOST_CXX14_CONSTEXPR bool check_max_out = (10 == ba::clamp ( 15, -10, 10 ));
BOOST_CHECK(check_max_out);
}
// Unsigned
{
BOOST_CXX14_CONSTEXPR bool check_inside = (5U == ba::clamp ( 5U, 1U, 10U ));
BOOST_CHECK(check_inside);
BOOST_CXX14_CONSTEXPR bool check_min = (1U == ba::clamp ( 1U, 1U, 10U ));
BOOST_CHECK(check_min);
BOOST_CXX14_CONSTEXPR bool check_min_out = (1U == ba::clamp ( 0U, 1U, 10U ));
BOOST_CHECK(check_min_out);
BOOST_CXX14_CONSTEXPR bool check_max = (10U == ba::clamp ( 10U, 1U, 10U ));
BOOST_CHECK(check_max);
BOOST_CXX14_CONSTEXPR bool check_max_out = (10U == ba::clamp ( 15U, 1U, 10U ));
BOOST_CHECK(check_max_out);
}
// Mixed (1)
{
BOOST_CXX14_CONSTEXPR bool check_inside = (5U == ba::clamp ( 5U, 1, 10 ));
BOOST_CHECK(check_inside);
BOOST_CXX14_CONSTEXPR bool check_min = (1U == ba::clamp ( 1U, 1, 10 ));
BOOST_CHECK(check_min);
BOOST_CXX14_CONSTEXPR bool check_min_out = (1U == ba::clamp ( 0U, 1, 10 ));
BOOST_CHECK(check_min_out);
BOOST_CXX14_CONSTEXPR bool check_max = (10U == ba::clamp ( 10U, 1, 10 ));
BOOST_CHECK(check_max);
BOOST_CXX14_CONSTEXPR bool check_max_out = (10U == ba::clamp ( 15U, 1, 10 ));
BOOST_CHECK(check_max_out);
}
// Mixed (3)
{
BOOST_CXX14_CONSTEXPR bool check_inside = (5U == ba::clamp ( 5U, 1, 10. ));
BOOST_CHECK(check_inside);
BOOST_CXX14_CONSTEXPR bool check_min = (1U == ba::clamp ( 1U, 1, 10. ));
BOOST_CHECK(check_min);
BOOST_CXX14_CONSTEXPR bool check_min_out = (1U == ba::clamp ( 0U, 1, 10. ));
BOOST_CHECK(check_min_out);
BOOST_CXX14_CONSTEXPR bool check_max = (10U == ba::clamp ( 10U, 1, 10. ));
BOOST_CHECK(check_max);
BOOST_CXX14_CONSTEXPR bool check_max_out = (10U == ba::clamp ( 15U, 1, 10. ));
BOOST_CHECK(check_max_out);
}
{
BOOST_CXX14_CONSTEXPR short foo = 50;
BOOST_CXX14_CONSTEXPR bool check_float = ( 56 == ba::clamp ( foo, 56.9, 129 ));
BOOST_CHECK(check_float);
BOOST_CXX14_CONSTEXPR bool check_over = ( 24910 == ba::clamp ( foo, 12345678, 123456999 ));
BOOST_CHECK(check_over);
}
}
BOOST_AUTO_TEST_CASE( test_main )
{
test_ints ();
@ -213,6 +321,8 @@ BOOST_AUTO_TEST_CASE( test_main )
test_custom ();
test_int_range ();
test_constexpr ();
// test_float_range ();
// test_custom_range ();
}

View File

@ -10,6 +10,8 @@
#include <boost/config.hpp>
#include <boost/algorithm/cxx11/copy_if.hpp>
#include "iterator_test.hpp"
#define BOOST_TEST_MAIN
#include <boost/test/unit_test.hpp>
@ -20,15 +22,18 @@
#include <list>
#include <boost/algorithm/cxx11/all_of.hpp>
#include <boost/algorithm/cxx14/equal.hpp>
#include <boost/algorithm/cxx11/none_of.hpp>
namespace ba = boost::algorithm;
// namespace ba = boost;
bool is_true ( int v ) { return true; }
bool is_false ( int v ) { return false; }
bool is_even ( int v ) { return v % 2 == 0; }
bool is_odd ( int v ) { return v % 2 == 1; }
BOOST_CXX14_CONSTEXPR bool is_true ( int v ) { return true; }
BOOST_CXX14_CONSTEXPR bool is_false ( int v ) { return false; }
BOOST_CXX14_CONSTEXPR bool is_even ( int v ) { return v % 2 == 0; }
BOOST_CXX14_CONSTEXPR bool is_odd ( int v ) { return v % 2 == 1; }
BOOST_CXX14_CONSTEXPR bool is_zero ( int v ) { return v == 0; }
template <typename Container>
void test_copy_if ( Container const &c ) {
@ -155,6 +160,71 @@ void test_copy_until ( Container const &c ) {
BOOST_CHECK ( ba::none_of ( v.begin (), v.end (), is_even ));
BOOST_CHECK ( std::equal ( v.begin (), v.end (), c.begin ()));
}
BOOST_CXX14_CONSTEXPR inline bool constexpr_test_copy_if() {
const int sz = 64;
int in_data[sz] = {0};
bool res = true;
const int* from = in_data;
const int* to = in_data + sz;
int out_data[sz] = {0};
int* out = out_data;
out = ba::copy_if ( from, to, out, is_false ); // copy none
res = (res && out == out_data);
out = ba::copy_if ( from, to, out, is_true ); // copy all
res = (res && out == out_data + sz
&& ba::equal( input_iterator<const int *>(out_data), input_iterator<const int *>(out_data + sz),
input_iterator<const int *>(from), input_iterator<const int *>(to)));
return res;
}
BOOST_CXX14_CONSTEXPR inline bool constexpr_test_copy_while() {
const int sz = 64;
int in_data[sz] = {0};
bool res = true;
const int* from = in_data;
const int* to = in_data + sz;
int out_data[sz] = {0};
int* out = out_data;
out = ba::copy_while ( from, to, out, is_false ).second; // copy none
res = (res && out == out_data && ba::all_of(out, out + sz, is_zero));
out = ba::copy_while ( from, to, out, is_true ).second; // copy all
res = (res && out == out_data + sz
&& ba::equal( input_iterator<const int *>(out_data), input_iterator<const int *>(out_data + sz),
input_iterator<const int *>(from), input_iterator<const int *>(to)));
return res;
}
BOOST_CXX14_CONSTEXPR inline bool constexpr_test_copy_until() {
const int sz = 64;
int in_data[sz] = {0};
bool res = true;
const int* from = in_data;
const int* to = in_data + sz;
int out_data[sz] = {0};
int* out = out_data;
out = ba::copy_until ( from, to, out, is_true ).second; // copy none
res = (res && out == out_data && ba::all_of(out, out + sz, is_zero));
out = ba::copy_until ( from, to, out, is_false ).second; // copy all
res = (res && out == out_data + sz
&& ba::equal( input_iterator<const int *>(out_data), input_iterator<const int *>(out_data + sz),
input_iterator<const int *>(from), input_iterator<const int *>(to)));
return res;
}
void test_sequence1 () {
std::vector<int> v;
@ -164,6 +234,13 @@ void test_sequence1 () {
test_copy_while ( v );
test_copy_until ( v );
BOOST_CXX14_CONSTEXPR bool constexpr_res_if = constexpr_test_copy_if();
BOOST_CHECK ( constexpr_res_if );
BOOST_CXX14_CONSTEXPR bool constexpr_res_while = constexpr_test_copy_while();
BOOST_CHECK ( constexpr_res_while );
BOOST_CXX14_CONSTEXPR bool constexpr_res_until = constexpr_test_copy_until();
BOOST_CHECK ( constexpr_res_until );
std::list<int> l;
for ( int i = 25; i > 15; --i )
l.push_back ( i );

View File

@ -9,6 +9,10 @@
#include <boost/config.hpp>
#include <boost/algorithm/cxx11/copy_n.hpp>
#include <boost/algorithm/cxx14/equal.hpp>
#include <boost/algorithm/cxx11/all_of.hpp>
#include "iterator_test.hpp"
#define BOOST_TEST_MAIN
#include <boost/test/unit_test.hpp>
@ -21,6 +25,8 @@
namespace ba = boost::algorithm;
// namespace ba = boost;
BOOST_CXX14_CONSTEXPR bool is_zero( int v ) { return v == 0; }
template <typename Container>
void test_sequence ( Container const &c ) {
@ -67,12 +73,38 @@ void test_sequence ( Container const &c ) {
}
BOOST_CXX14_CONSTEXPR inline bool test_constexpr() {
const size_t sz = 64;
int in_data[sz] = {0};
bool res = true;
const int* from = in_data;
const int* to = in_data + sz;
int out_data[sz] = {0};
int* out = out_data;
out = ba::copy_n ( from, 0, out ); // Copy none
res = (res && out == out_data && ba::all_of(out, out + sz, is_zero));
out = ba::copy_n ( from, sz, out ); // Copy all
res = (res && out == out_data + sz
&& ba::equal( input_iterator<const int *>(out_data), input_iterator<const int *>(out_data + sz),
input_iterator<const int *>(from), input_iterator<const int *>(to)));
return res;
}
void test_sequence1 () {
std::vector<int> v;
for ( int i = 5; i < 15; ++i )
v.push_back ( i );
test_sequence ( v );
BOOST_CXX14_CONSTEXPR bool constexpr_res = test_constexpr();
BOOST_CHECK(constexpr_res);
std::list<int> l;
for ( int i = 25; i > 15; --i )
l.push_back ( i );

View File

@ -16,7 +16,7 @@
#include <boost/test/unit_test.hpp>
template <typename T>
bool eq ( const T& a, const T& b ) { return a == b; }
BOOST_CXX14_CONSTEXPR bool eq ( const T& a, const T& b ) { return a == b; }
template <typename T>
bool never_eq ( const T&, const T& ) { return false; }
@ -123,7 +123,43 @@ void test_equal ()
}
BOOST_CXX14_CONSTEXPR bool test_constexpr_equal() {
int num[] = { 1, 1, 2, 3, 5};
const int sz = sizeof (num)/sizeof(num[0]);
bool res = true;
// Empty sequences are equal to each other
res = ( ba::equal ( input_iterator<int *>(num), input_iterator<int *>(num),
input_iterator<int *>(num), input_iterator<int *>(num))
// Identical long sequences are equal
&& ba::equal ( input_iterator<int *>(num), input_iterator<int *>(num + sz),
input_iterator<int *>(num), input_iterator<int *>(num + sz),
eq<int> )
// Different sequences are different
&& !ba::equal ( input_iterator<int *>(num + 1), input_iterator<int *>(num + sz),
input_iterator<int *>(num), input_iterator<int *>(num + sz))
);
#ifdef __cpp_lib_array_constexpr // or cpp17 compiler
// Turn on tests for random_access_iterator, because std functions used in equal are marked constexpr_res
res = ( res
// Empty sequences are equal to each other
&& ba::equal ( random_access_iterator<int *>(num), random_access_iterator<int *>(num),
random_access_iterator<int *>(num), random_access_iterator<int *>(num))
// Identical long sequences are equal
&& ba::equal ( random_access_iterator<int *>(num), random_access_iterator<int *>(num + sz),
random_access_iterator<int *>(num), random_access_iterator<int *>(num + sz),
eq<int> )
// Different sequences are different
&& !ba::equal ( random_access_iterator<int *>(num + 1), random_access_iterator<int *>(num + sz),
random_access_iterator<int *>(num), random_access_iterator<int *>(num + sz))
);
#endif
return res;
}
BOOST_AUTO_TEST_CASE( test_main )
{
test_equal ();
BOOST_CXX14_CONSTEXPR bool constexpr_res = test_constexpr_equal ();
BOOST_CHECK (constexpr_res);
}

View File

@ -0,0 +1,72 @@
/*
Copyright (c) Marshall Clow 2017.
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)
For more information, see http://www.boost.org
*/
#include <vector>
#include <functional>
#include <numeric>
#include <algorithm>
#include <boost/config.hpp>
#include <boost/algorithm/cxx11/iota.hpp>
#include <boost/algorithm/cxx17/exclusive_scan.hpp>
#include "iterator_test.hpp"
#define BOOST_TEST_MAIN
#include <boost/test/unit_test.hpp>
namespace ba = boost::algorithm;
int triangle(int n) { return n*(n+1)/2; }
void basic_tests_init()
{
{
std::vector<int> v(10);
std::fill(v.begin(), v.end(), 3);
ba::exclusive_scan(v.begin(), v.end(), v.begin(), 50);
for (size_t i = 0; i < v.size(); ++i)
BOOST_CHECK(v[i] == 50 + (int) i * 3);
}
{
std::vector<int> v(10);
ba::iota(v.begin(), v.end(), 0);
ba::exclusive_scan(v.begin(), v.end(), v.begin(), 30);
for (size_t i = 0; i < v.size(); ++i)
BOOST_CHECK(v[i] == 30 + triangle(i-1));
}
{
std::vector<int> v(10);
ba::iota(v.begin(), v.end(), 1);
ba::exclusive_scan(v.begin(), v.end(), v.begin(), 40);
for (size_t i = 0; i < v.size(); ++i)
BOOST_CHECK(v[i] == 40 + triangle(i));
}
}
void test_exclusive_scan_init()
{
basic_tests_init();
}
void test_exclusive_scan_init_op()
{
BOOST_CHECK(true);
}
BOOST_AUTO_TEST_CASE( test_main )
{
test_exclusive_scan_init();
test_exclusive_scan_init_op();
}

420
test/find_backward_test.cpp Normal file
View File

@ -0,0 +1,420 @@
/*
Copyright (c) T. Zachary Laine 2018.
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)
For more information, see http://www.boost.org
*/
#include <iostream>
#include <boost/algorithm/find_backward.hpp>
#define BOOST_TEST_MAIN
#include <boost/test/unit_test.hpp>
#include <vector>
#include <list>
namespace ba = boost::algorithm;
template <typename Container>
struct dist_t
{
dist_t(Container & cont) : cont_(cont) {}
template<typename Iter>
std::ptrdiff_t operator()(Iter it) const
{
return std::distance(cont_.begin(), it);
}
Container & cont_;
};
BOOST_CXX14_CONSTEXPR bool check_constexpr_backward()
{
int in_data[] = {1, 2, 3, 4, 5};
bool res = true;
const int* from = in_data;
const int* to = in_data + 5;
const int* start = ba::find_backward(from, to, 1); // stops on first
res = (res && start == from);
start = ba::find_backward(in_data, 1); // stops on first
res = (res && start == from);
const int* end = ba::find_backward(from, to, 6); // stops on the end
res = (res && end == to);
end = ba::find_backward(in_data, 6); // stops on the end
res = (res && end == to);
const int* three = ba::find_backward(from, to, 3); // stops on third element
res = (res && three == in_data + 2);
three = ba::find_backward(in_data, 3); // stops on third element
res = (res && three == in_data + 2);
return res;
}
void test_find_backward()
{
{
std::vector<int> v1;
const dist_t<std::vector<int> > dist(v1);
for (int i = 5; i < 15; ++i)
v1.push_back(i);
BOOST_CHECK_EQUAL(
dist(ba::find_backward(v1.begin(), v1.end(), 0)), v1.size());
BOOST_CHECK_EQUAL(
dist(ba::find_backward(v1.begin(), v1.end(), 100)), v1.size());
BOOST_CHECK_EQUAL(
dist(ba::find_backward(v1.begin(), v1.end(), v1.back())),
v1.size() - 1);
BOOST_CHECK_EQUAL(
dist(ba::find_backward(v1.begin(), v1.end(), v1.front())), 0);
BOOST_CHECK_EQUAL(dist(ba::find_backward(v1, 0)), v1.size());
BOOST_CHECK_EQUAL(dist(ba::find_backward(v1, 100)), v1.size());
BOOST_CHECK_EQUAL(
dist(ba::find_backward(v1, v1.back())), v1.size() - 1);
BOOST_CHECK_EQUAL(dist(ba::find_backward(v1, v1.front())), 0);
}
// With bidirectional iterators.
{
std::list<int> l1;
const dist_t<std::list<int> > dist(l1);
for (int i = 5; i < 15; ++i)
l1.push_back(i);
BOOST_CHECK_EQUAL(
dist(ba::find_backward(l1.begin(), l1.end(), 0)), l1.size());
BOOST_CHECK_EQUAL(
dist(ba::find_backward(l1.begin(), l1.end(), 100)), l1.size());
BOOST_CHECK_EQUAL(
dist(ba::find_backward(l1.begin(), l1.end(), l1.back())),
l1.size() - 1);
BOOST_CHECK_EQUAL(
dist(ba::find_backward(l1.begin(), l1.end(), l1.front())), 0);
BOOST_CHECK_EQUAL(dist(ba::find_backward(l1, 0)), l1.size());
BOOST_CHECK_EQUAL(dist(ba::find_backward(l1, 100)), l1.size());
BOOST_CHECK_EQUAL(
dist(ba::find_backward(l1, l1.back())), l1.size() - 1);
BOOST_CHECK_EQUAL(dist(ba::find_backward(l1, l1.front())), 0);
}
BOOST_CXX14_CONSTEXPR bool ce_result = check_constexpr_backward();
BOOST_CHECK(ce_result);
}
struct equals
{
BOOST_CXX14_CONSTEXPR equals(int n) : n_(n) {}
BOOST_CXX14_CONSTEXPR bool operator()(int i) { return i == n_; }
int n_;
};
BOOST_CXX14_CONSTEXPR bool check_constexpr_if_backward()
{
int in_data[] = {1, 2, 3, 4, 5};
bool res = true;
const int* from = in_data;
const int* to = in_data + 5;
const int* start = ba::find_if_backward(from, to, equals(1)); // stops on first
res = (res && start == from);
start = ba::find_if_backward(in_data, equals(1)); // stops on first
res = (res && start == from);
const int* end = ba::find_if_backward(from, to, equals(6)); // stops on the end
res = (res && end == to);
end = ba::find_if_backward(in_data, equals(6)); // stops on the end
res = (res && end == to);
const int* three = ba::find_if_backward(from, to, equals(3)); // stops on third element
res = (res && three == in_data + 2);
three = ba::find_if_backward(in_data, equals(3)); // stops on third element
res = (res && three == in_data + 2);
return res;
}
void test_find_if_backward()
{
{
std::vector<int> v1;
const dist_t<std::vector<int> > dist(v1);
for (int i = 5; i < 15; ++i)
v1.push_back(i);
BOOST_CHECK_EQUAL(
dist(ba::find_if_backward(v1.begin(), v1.end(), equals(0))),
v1.size());
BOOST_CHECK_EQUAL(
dist(ba::find_if_backward(v1.begin(), v1.end(), equals(100))),
v1.size());
BOOST_CHECK_EQUAL(
dist(ba::find_if_backward(v1.begin(), v1.end(), equals(v1.back()))),
v1.size() - 1);
BOOST_CHECK_EQUAL(
dist(
ba::find_if_backward(v1.begin(), v1.end(), equals(v1.front()))),
0);
BOOST_CHECK_EQUAL(dist(ba::find_if_backward(v1, equals(0))), v1.size());
BOOST_CHECK_EQUAL(
dist(ba::find_if_backward(v1, equals(100))), v1.size());
BOOST_CHECK_EQUAL(
dist(ba::find_if_backward(v1, equals(v1.back()))), v1.size() - 1);
BOOST_CHECK_EQUAL(
dist(ba::find_if_backward(v1, equals(v1.front()))), 0);
}
// With bidirectional iterators.
{
std::list<int> l1;
const dist_t<std::list<int> > dist(l1);
for (int i = 5; i < 15; ++i)
l1.push_back(i);
BOOST_CHECK_EQUAL(
dist(ba::find_if_backward(l1.begin(), l1.end(), equals(0))),
l1.size());
BOOST_CHECK_EQUAL(
dist(ba::find_if_backward(l1.begin(), l1.end(), equals(100))),
l1.size());
BOOST_CHECK_EQUAL(
dist(ba::find_if_backward(l1.begin(), l1.end(), equals(l1.back()))),
l1.size() - 1);
BOOST_CHECK_EQUAL(
dist(
ba::find_if_backward(l1.begin(), l1.end(), equals(l1.front()))),
0);
BOOST_CHECK_EQUAL(dist(ba::find_if_backward(l1, equals(0))), l1.size());
BOOST_CHECK_EQUAL(
dist(ba::find_if_backward(l1, equals(100))), l1.size());
BOOST_CHECK_EQUAL(
dist(ba::find_if_backward(l1, equals(l1.back()))), l1.size() - 1);
BOOST_CHECK_EQUAL(
dist(ba::find_if_backward(l1, equals(l1.front()))), 0);
}
BOOST_CXX14_CONSTEXPR bool ce_result = check_constexpr_if_backward();
BOOST_CHECK(ce_result);
}
struct not_equals
{
BOOST_CXX14_CONSTEXPR not_equals(int n) : n_(n) {}
BOOST_CXX14_CONSTEXPR bool operator()(int i) { return i != n_; }
int n_;
};
BOOST_CXX14_CONSTEXPR bool check_constexpr_if_not_backward()
{
int in_data[] = {1, 2, 3, 4, 5};
bool res = true;
const int* from = in_data;
const int* to = in_data + 5;
const int* start = ba::find_if_not_backward(from, to, not_equals(1)); // stops on first
res = (res && start == from);
start = ba::find_if_not_backward(in_data, not_equals(1)); // stops on first
res = (res && start == from);
const int* end = ba::find_if_not_backward(from, to, not_equals(6)); // stops on the end
res = (res && end == to);
end = ba::find_if_not_backward(in_data, not_equals(6)); // stops on the end
res = (res && end == to);
const int* three = ba::find_if_not_backward(from, to, not_equals(3)); // stops on third element
res = (res && three == in_data + 2);
three = ba::find_if_not_backward(in_data, not_equals(3)); // stops on third element
res = (res && three == in_data + 2);
return res;
}
void test_find_if_not_backward()
{
{
std::vector<int> v1;
const dist_t<std::vector<int> > dist(v1);
for (int i = 5; i < 15; ++i)
v1.push_back(i);
BOOST_CHECK_EQUAL(
dist(ba::find_if_not_backward(v1.begin(), v1.end(), not_equals(0))),
v1.size());
BOOST_CHECK_EQUAL(
dist(ba::find_if_not_backward(
v1.begin(), v1.end(), not_equals(100))),
v1.size());
BOOST_CHECK_EQUAL(
dist(ba::find_if_not_backward(
v1.begin(), v1.end(), not_equals(v1.back()))),
v1.size() - 1);
BOOST_CHECK_EQUAL(
dist(ba::find_if_not_backward(
v1.begin(), v1.end(), not_equals(v1.front()))),
0);
BOOST_CHECK_EQUAL(
dist(ba::find_if_not_backward(v1, not_equals(0))), v1.size());
BOOST_CHECK_EQUAL(
dist(ba::find_if_not_backward(v1, not_equals(100))), v1.size());
BOOST_CHECK_EQUAL(
dist(ba::find_if_not_backward(v1, not_equals(v1.back()))),
v1.size() - 1);
BOOST_CHECK_EQUAL(
dist(ba::find_if_not_backward(v1, not_equals(v1.front()))), 0);
}
// With bidirectional iterators.
{
std::list<int> l1;
const dist_t<std::list<int> > dist(l1);
for (int i = 5; i < 15; ++i)
l1.push_back(i);
BOOST_CHECK_EQUAL(
dist(ba::find_if_not_backward(l1.begin(), l1.end(), not_equals(0))),
l1.size());
BOOST_CHECK_EQUAL(
dist(ba::find_if_not_backward(
l1.begin(), l1.end(), not_equals(100))),
l1.size());
BOOST_CHECK_EQUAL(
dist(ba::find_if_not_backward(
l1.begin(), l1.end(), not_equals(l1.back()))),
l1.size() - 1);
BOOST_CHECK_EQUAL(
dist(ba::find_if_not_backward(
l1.begin(), l1.end(), not_equals(l1.front()))),
0);
BOOST_CHECK_EQUAL(
dist(ba::find_if_not_backward(l1, not_equals(0))), l1.size());
BOOST_CHECK_EQUAL(
dist(ba::find_if_not_backward(l1, not_equals(100))), l1.size());
BOOST_CHECK_EQUAL(
dist(ba::find_if_not_backward(l1, not_equals(l1.back()))),
l1.size() - 1);
BOOST_CHECK_EQUAL(
dist(ba::find_if_not_backward(l1, not_equals(l1.front()))), 0);
}
BOOST_CXX14_CONSTEXPR bool ce_result = check_constexpr_if_not_backward();
BOOST_CHECK(ce_result);
}
BOOST_CXX14_CONSTEXPR bool check_constexpr_not_backward()
{
int in_data[] = {1, 5, 5, 5, 5};
bool res = true;
const int* from = in_data;
const int* to = in_data + 5;
const int* start = ba::find_not_backward(from, to, 5); // stops on first
res = (res && start == from);
start = ba::find_not_backward(in_data, 5); // stops on first
res = (res && start == from);
const int in_data_2[] = {6, 6, 6, 6, 6};
const int* end = ba::find_not_backward(in_data_2, in_data_2 + 5, 6); // stops on the end
res = (res && end == in_data_2 + 5);
end = ba::find_not_backward(in_data_2, 6); // stops on the end
res = (res && end == in_data_2 + 5);
return res;
}
void test_find_not_backward()
{
{
std::vector<int> v1;
const dist_t<std::vector<int> > dist(v1);
for (int i = 0; i < 5; ++i)
v1.push_back(0);
for (int i = 0; i < 5; ++i)
v1.push_back(1);
BOOST_CHECK_EQUAL(
dist(ba::find_not_backward(v1.begin(), v1.end(), 1)), 4);
BOOST_CHECK_EQUAL(
dist(ba::find_not_backward(v1.begin(), v1.end(), 0)),
v1.size() - 1);
BOOST_CHECK_EQUAL(
dist(ba::find_not_backward(v1.begin(), v1.end(), 2)),
v1.size() - 1);
BOOST_CHECK_EQUAL(dist(ba::find_not_backward(v1, 1)), 4);
BOOST_CHECK_EQUAL(dist(ba::find_not_backward(v1, 0)), v1.size() - 1);
BOOST_CHECK_EQUAL(dist(ba::find_not_backward(v1, 2)), v1.size() - 1);
v1.resize(5);
BOOST_CHECK_EQUAL(
dist(ba::find_not_backward(v1.begin(), v1.end(), 0)), v1.size());
BOOST_CHECK_EQUAL(dist(ba::find_not_backward(v1, 0)), v1.size());
}
// With bidirectional iterators.
{
std::list<int> l1;
const dist_t<std::list<int> > dist(l1);
for (int i = 0; i < 5; ++i)
l1.push_back(0);
for (int i = 0; i < 5; ++i)
l1.push_back(1);
BOOST_CHECK_EQUAL(
dist(ba::find_not_backward(l1.begin(), l1.end(), 1)), 4);
BOOST_CHECK_EQUAL(
dist(ba::find_not_backward(l1.begin(), l1.end(), 0)),
l1.size() - 1);
BOOST_CHECK_EQUAL(
dist(ba::find_not_backward(l1.begin(), l1.end(), 2)),
l1.size() - 1);
BOOST_CHECK_EQUAL(dist(ba::find_not_backward(l1, 1)), 4);
BOOST_CHECK_EQUAL(dist(ba::find_not_backward(l1, 0)), l1.size() - 1);
BOOST_CHECK_EQUAL(dist(ba::find_not_backward(l1, 2)), l1.size() - 1);
l1.resize(5);
BOOST_CHECK_EQUAL(
dist(ba::find_not_backward(l1.begin(), l1.end(), 0)), l1.size());
BOOST_CHECK_EQUAL(dist(ba::find_not_backward(l1, 0)), l1.size());
}
BOOST_CXX14_CONSTEXPR bool ce_result = check_constexpr_not_backward();
BOOST_CHECK(ce_result);
}
BOOST_AUTO_TEST_CASE(test_main)
{
test_find_backward();
test_find_if_backward();
test_find_if_not_backward();
test_find_not_backward();
}

View File

@ -22,6 +22,29 @@
namespace ba = boost::algorithm;
// namespace ba = boost;
BOOST_CXX14_CONSTEXPR bool is_true ( int v ) { return true; }
BOOST_CXX14_CONSTEXPR bool is_false ( int v ) { return false; }
BOOST_CXX14_CONSTEXPR bool is_not_three ( int v ) { return v != 3; }
BOOST_CXX14_CONSTEXPR bool check_constexpr() {
int in_data[] = {1, 2, 3, 4, 5};
bool res = true;
const int* from = in_data;
const int* to = in_data + 5;
const int* start = ba::find_if_not (from, to, is_false); // stops on first
res = (res && start == from);
const int* end = ba::find_if_not(from, to, is_true); // stops on the end
res = (res && end == to);
const int* three = ba::find_if_not(from, to, is_not_three); // stops on third element
res = (res && three == in_data + 2);
return res;
}
template <typename Container>
typename Container::iterator offset_to_iter ( Container &v, int offset ) {
typename Container::iterator retval;

134
test/find_not_test.cpp Normal file
View File

@ -0,0 +1,134 @@
/*
Copyright (c) T. Zachary Laine 2018.
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)
For more information, see http://www.boost.org
*/
#include <iostream>
#include <boost/algorithm/find_not.hpp>
#define BOOST_TEST_MAIN
#include <boost/test/unit_test.hpp>
#include <vector>
#include <list>
namespace ba = boost::algorithm;
template <typename Container>
struct dist_t
{
dist_t(Container & cont) : cont_(cont) {}
template<typename Iter>
std::ptrdiff_t operator()(Iter it) const
{
return std::distance(cont_.begin(), it);
}
Container & cont_;
};
BOOST_CXX14_CONSTEXPR bool check_constexpr()
{
int in_data[] = {2, 2, 3, 4, 5};
bool res = true;
const int* from = in_data;
const int* to = in_data + 5;
const int* start = ba::find_not(from, to, 1); // stops on first
res = (res && start == from);
start = ba::find_not(in_data, 1); // stops on first
res = (res && start == from);
int in_data_2[] = {6, 6, 6, 6, 6};
const int* end = ba::find_not(in_data_2, in_data_2 + 5, 6); // stops on the end
res = (res && end == in_data_2 + 5);
end = ba::find_not(in_data_2, 6); // stops on the end
res = (res && end == in_data_2 + 5);
const int* three = ba::find_not(from, to, 2); // stops on third element
res = (res && three == in_data + 2);
three = ba::find_not(in_data, 2); // stops on third element
res = (res && three == in_data + 2);
return res;
}
void test_sequence()
{
{
std::vector<int> v1;
const dist_t<std::vector<int> > dist(v1);
for (int i = 5; i < 15; ++i)
v1.push_back(i);
BOOST_CHECK_EQUAL(dist(ba::find_not(v1.begin(), v1.end(), 0)), 0);
BOOST_CHECK_EQUAL(
dist(ba::find_not(v1.begin(), v1.end(), v1.back())), 0);
BOOST_CHECK_EQUAL(
dist(ba::find_not(v1.begin(), v1.end(), v1.front())), 1);
BOOST_CHECK_EQUAL(dist(ba::find_not(v1, 0)), 0);
BOOST_CHECK_EQUAL(dist(ba::find_not(v1, v1.back())), 0);
BOOST_CHECK_EQUAL(dist(ba::find_not(v1, v1.front())), 1);
v1 = std::vector<int>(10, 2);
BOOST_CHECK_EQUAL(dist(ba::find_not(v1.begin(), v1.end(), 0)), 0);
BOOST_CHECK_EQUAL(
dist(ba::find_not(v1.begin(), v1.end(), v1.back())), v1.size());
BOOST_CHECK_EQUAL(
dist(ba::find_not(v1.begin(), v1.end(), v1.front())), v1.size());
BOOST_CHECK_EQUAL(dist(ba::find_not(v1, 0)), 0);
BOOST_CHECK_EQUAL(dist(ba::find_not(v1, v1.back())), v1.size());
BOOST_CHECK_EQUAL(dist(ba::find_not(v1, v1.front())), v1.size());
}
// With bidirectional iterators.
{
std::list<int> l1;
const dist_t<std::list<int> > dist(l1);
for (int i = 5; i < 15; ++i)
l1.push_back(i);
BOOST_CHECK_EQUAL(dist(ba::find_not(l1.begin(), l1.end(), 0)), 0);
BOOST_CHECK_EQUAL(
dist(ba::find_not(l1.begin(), l1.end(), l1.back())), 0);
BOOST_CHECK_EQUAL(
dist(ba::find_not(l1.begin(), l1.end(), l1.front())), 1);
BOOST_CHECK_EQUAL(dist(ba::find_not(l1, 0)), 0);
BOOST_CHECK_EQUAL(dist(ba::find_not(l1, l1.back())), 0);
BOOST_CHECK_EQUAL(dist(ba::find_not(l1, l1.front())), 1);
l1.clear();
for (int i = 0; i < 10; ++i)
l1.push_back(2);
BOOST_CHECK_EQUAL(dist(ba::find_not(l1.begin(), l1.end(), 0)), 0);
BOOST_CHECK_EQUAL(
dist(ba::find_not(l1.begin(), l1.end(), l1.back())), l1.size());
BOOST_CHECK_EQUAL(
dist(ba::find_not(l1.begin(), l1.end(), l1.front())), l1.size());
BOOST_CHECK_EQUAL(dist(ba::find_not(l1, 0)), 0);
BOOST_CHECK_EQUAL(dist(ba::find_not(l1, l1.back())), l1.size());
BOOST_CHECK_EQUAL(dist(ba::find_not(l1, l1.front())), l1.size());
}
BOOST_CXX14_CONSTEXPR bool ce_result = check_constexpr();
BOOST_CHECK(ce_result);
}
BOOST_AUTO_TEST_CASE(test_main)
{
test_sequence();
}

66
test/for_each_n_test.cpp Normal file
View File

@ -0,0 +1,66 @@
/*
Copyright (c) Marshall Clow 2013.
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)
For more information, see http://www.boost.org
*/
#include <boost/config.hpp>
#include <boost/algorithm/cxx17/for_each_n.hpp>
#include "iterator_test.hpp"
#define BOOST_TEST_MAIN
#include <boost/test/unit_test.hpp>
namespace ba = boost::algorithm;
struct for_each_test
{
for_each_test() {}
static int count;
void operator()(int& i) {++i; ++count;}
};
int for_each_test::count = 0;
void test_for_each_n ()
{
typedef input_iterator<int*> Iter;
int ia[] = {0, 1, 2, 3, 4, 5};
const unsigned s = sizeof(ia)/sizeof(ia[0]);
{
for_each_test::count = 0;
Iter it = ba::for_each_n(Iter(ia), 0, for_each_test());
BOOST_CHECK(it == Iter(ia));
BOOST_CHECK(for_each_test::count == 0);
}
{
for_each_test::count = 0;
Iter it = ba::for_each_n(Iter(ia), s, for_each_test());
BOOST_CHECK(it == Iter(ia+s));
BOOST_CHECK(for_each_test::count == s);
for (unsigned i = 0; i < s; ++i)
BOOST_CHECK(ia[i] == static_cast<int>(i+1));
}
{
for_each_test::count = 0;
Iter it = ba::for_each_n(Iter(ia), 1, for_each_test());
BOOST_CHECK(it == Iter(ia+1));
BOOST_CHECK(for_each_test::count == 1);
for (unsigned i = 0; i < 1; ++i)
BOOST_CHECK(ia[i] == static_cast<int>(i+2));
}
}
BOOST_AUTO_TEST_CASE( test_main )
{
test_for_each_n ();
}

View File

@ -0,0 +1,153 @@
/*
Copyright (c) Marshall Clow 2017.
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)
For more information, see http://www.boost.org
*/
#include <vector>
#include <functional>
#include <numeric>
#include <algorithm>
#include <boost/config.hpp>
#include <boost/algorithm/cxx11/iota.hpp>
#include <boost/algorithm/cxx17/inclusive_scan.hpp>
#include "iterator_test.hpp"
#define BOOST_TEST_MAIN
#include <boost/test/unit_test.hpp>
namespace ba = boost::algorithm;
int triangle(int n) { return n*(n+1)/2; }
void basic_tests_op()
{
{
std::vector<int> v(10);
std::fill(v.begin(), v.end(), 3);
ba::inclusive_scan(v.begin(), v.end(), v.begin(), std::plus<int>());
for (size_t i = 0; i < v.size(); ++i)
assert(v[i] == (int)(i+1) * 3);
}
{
std::vector<int> v(10);
ba::iota(v.begin(), v.end(), 0);
ba::inclusive_scan(v.begin(), v.end(), v.begin(), std::plus<int>());
for (size_t i = 0; i < v.size(); ++i)
assert(v[i] == triangle(i));
}
{
std::vector<int> v(10);
ba::iota(v.begin(), v.end(), 1);
ba::inclusive_scan(v.begin(), v.end(), v.begin(), std::plus<int>());
for (size_t i = 0; i < v.size(); ++i)
assert(v[i] == triangle(i + 1));
}
{
std::vector<int> v, res;
ba::inclusive_scan(v.begin(), v.end(), std::back_inserter(res), std::plus<int>());
assert(res.empty());
}
}
void test_inclusive_scan_op()
{
basic_tests_op();
BOOST_CHECK(true);
}
void basic_tests_init()
{
{
std::vector<int> v(10);
std::fill(v.begin(), v.end(), 3);
ba::inclusive_scan(v.begin(), v.end(), v.begin(), std::plus<int>(), 50);
for (size_t i = 0; i < v.size(); ++i)
assert(v[i] == 50 + (int)(i+1) * 3);
}
{
std::vector<int> v(10);
ba::iota(v.begin(), v.end(), 0);
ba::inclusive_scan(v.begin(), v.end(), v.begin(), std::plus<int>(), 40);
for (size_t i = 0; i < v.size(); ++i)
assert(v[i] == 40 + triangle(i));
}
{
std::vector<int> v(10);
ba::iota(v.begin(), v.end(), 1);
ba::inclusive_scan(v.begin(), v.end(), v.begin(), std::plus<int>(), 30);
for (size_t i = 0; i < v.size(); ++i)
assert(v[i] == 30 + triangle(i + 1));
}
{
std::vector<int> v, res;
ba::inclusive_scan(v.begin(), v.end(), std::back_inserter(res), std::plus<int>(), 40);
assert(res.empty());
}
}
void test_inclusive_scan_init()
{
basic_tests_init();
BOOST_CHECK(true);
}
void basic_tests_op_init()
{
{
std::vector<int> v(10);
std::fill(v.begin(), v.end(), 3);
ba::inclusive_scan(v.begin(), v.end(), v.begin(), std::plus<int>(), 50);
for (size_t i = 0; i < v.size(); ++i)
BOOST_CHECK(v[i] == 50 + (int)(i+1) * 3);
}
{
std::vector<int> v(10);
ba::iota(v.begin(), v.end(), 0);
ba::inclusive_scan(v.begin(), v.end(), v.begin(), std::plus<int>(), 40);
for (size_t i = 0; i < v.size(); ++i)
BOOST_CHECK(v[i] == 40 + triangle(i));
}
{
std::vector<int> v(10);
ba::iota(v.begin(), v.end(), 1);
ba::inclusive_scan(v.begin(), v.end(), v.begin(), std::plus<int>(), 30);
for (size_t i = 0; i < v.size(); ++i)
BOOST_CHECK(v[i] == 30 + triangle(i + 1));
}
{
std::vector<int> v, res;
ba::inclusive_scan(v.begin(), v.end(), std::back_inserter(res), std::plus<int>(), 40);
BOOST_CHECK(res.empty());
}
}
void test_inclusive_scan_op_init()
{
basic_tests_op_init();
BOOST_CHECK(true);
}
BOOST_AUTO_TEST_CASE( test_main )
{
test_inclusive_scan_op();
test_inclusive_scan_init();
test_inclusive_scan_op_init();
}

View File

@ -20,7 +20,7 @@
// Test to make sure a sequence is "correctly formed"; i.e, ascending by one
template <typename Iterator, typename T>
bool test_iota_results ( Iterator first, Iterator last, T initial_value ) {
BOOST_CXX14_CONSTEXPR bool test_iota_results ( Iterator first, Iterator last, T initial_value ) {
if ( first == last ) return true;
if ( initial_value != *first ) return false;
Iterator prev = first;
@ -32,12 +32,13 @@ bool test_iota_results ( Iterator first, Iterator last, T initial_value ) {
return true;
}
template <typename Range, typename T>
bool test_iota_results ( const Range &r, T initial_value ) {
BOOST_CXX14_CONSTEXPR bool test_iota_results ( const Range &r, T initial_value ) {
return test_iota_results (boost::begin (r), boost::end (r), initial_value );
}
void test_ints () {
std::vector<int> v;
std::list<int> l;
@ -76,9 +77,38 @@ void test_ints () {
boost::algorithm::iota_n ( std::front_inserter(l), 123, 20 );
BOOST_CHECK ( test_iota_results ( l.rbegin (), l.rend (), 123 ));
}
BOOST_CXX14_CONSTEXPR inline bool test_constexpr_iota() {
bool res = true;
int data[] = {0, 0, 0};
boost::algorithm::iota(data, data, 1); // fill none
res = (res && data[0] == 0);
boost::algorithm::iota(data, data + 3, 1); // fill all
res = (res && test_iota_results(data, data + 3, 1));
return res;
}
BOOST_CXX14_CONSTEXPR inline bool test_constexpr_iota_n() {
bool res = true;
int data[] = {0, 0, 0};
boost::algorithm::iota_n(data, 1, 0); // fill none
res = (res && data[0] == 0);
boost::algorithm::iota_n(data, 1, 3); // fill all
res = (res && test_iota_results(data, 1));
return res;
}
BOOST_AUTO_TEST_CASE( test_main )
{
test_ints ();
BOOST_CXX14_CONSTEXPR bool constexpr_iota_res = test_constexpr_iota ();
BOOST_CHECK(constexpr_iota_res);
BOOST_CXX14_CONSTEXPR bool constexpr_iota_n_res = test_constexpr_iota_n ();
BOOST_CHECK(constexpr_iota_n_res);
}

View File

@ -38,9 +38,6 @@ struct functorComparator
}
};
#define Begin(arr) (arr)
#define End(arr) (arr+(sizeof(arr)/(sizeof(arr[0]))))
void test_is_palindrome()
{
const std::list<int> empty;
@ -54,14 +51,14 @@ void test_is_palindrome()
// Test a default operator==
BOOST_CHECK ( ba::is_palindrome(empty));
BOOST_CHECK ( ba::is_palindrome(singleElement));
BOOST_CHECK (!ba::is_palindrome(Begin(oddNonPalindrome), End(oddNonPalindrome)));
BOOST_CHECK ( ba::is_palindrome(Begin(oddPalindrome), End(oddPalindrome)));
BOOST_CHECK ( ba::is_palindrome(Begin(evenPalindrome), End(evenPalindrome)));
BOOST_CHECK (!ba::is_palindrome(Begin(evenNonPalindrome), End(evenNonPalindrome)));
BOOST_CHECK (!ba::is_palindrome(boost::begin(oddNonPalindrome), boost::end(oddNonPalindrome)));
BOOST_CHECK ( ba::is_palindrome(boost::begin(oddPalindrome), boost::end(oddPalindrome)));
BOOST_CHECK ( ba::is_palindrome(boost::begin(evenPalindrome), boost::end(evenPalindrome)));
BOOST_CHECK (!ba::is_palindrome(boost::begin(evenNonPalindrome), boost::end(evenNonPalindrome)));
//Test the custom comparators
BOOST_CHECK ( ba::is_palindrome(empty.begin(), empty.end(), functorComparator()));
BOOST_CHECK (!ba::is_palindrome(Begin(oddNonPalindrome), End(oddNonPalindrome), funcComparator<int>));
BOOST_CHECK (!ba::is_palindrome(boost::begin(oddNonPalindrome), boost::end(oddNonPalindrome), funcComparator<int>));
BOOST_CHECK ( ba::is_palindrome(evenPalindrome, std::equal_to<int>()));
//Test C-strings like cases

View File

@ -25,16 +25,27 @@ namespace ba = boost::algorithm;
template <typename T>
struct less_than {
public:
less_than ( T foo ) : val ( foo ) {}
less_than ( const less_than &rhs ) : val ( rhs.val ) {}
BOOST_CXX14_CONSTEXPR less_than ( T foo ) : val ( foo ) {}
BOOST_CXX14_CONSTEXPR less_than ( const less_than &rhs ) : val ( rhs.val ) {}
bool operator () ( const T &v ) const { return v < val; }
BOOST_CXX14_CONSTEXPR bool operator () ( const T &v ) const { return v < val; }
private:
less_than ();
less_than operator = ( const less_than &rhs );
T val;
};
BOOST_CXX14_CONSTEXPR bool test_constexpr() {
int v[] = { 4, 5, 6, 7, 8, 9, 10 };
bool res = true;
res = ( res && ba::is_partitioned ( v, less_than<int>(3))); // no elements
res = ( res && ba::is_partitioned ( v, less_than<int>(5))); // only the first element
res = ( res && ba::is_partitioned ( v, less_than<int>(8))); // in the middle somewhere
res = ( res && ba::is_partitioned ( v, less_than<int>(99))); // all elements
return res;
}
void test_sequence1 () {
std::vector<int> v;
@ -61,4 +72,6 @@ void test_sequence1 () {
BOOST_AUTO_TEST_CASE( test_main )
{
test_sequence1 ();
BOOST_CXX14_CONSTEXPR bool constexpr_res = test_constexpr ();
BOOST_CHECK ( constexpr_res );
}

View File

@ -0,0 +1,63 @@
/*
Copyright (c) Marshall Clow 2011-2012, Alexander Zaitsev <zamazan4ik@gmail.com>, 2017.
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)
For more information, see http://www.boost.org
*/
#include <iostream>
#include <boost/config.hpp>
#include <boost/algorithm/is_partitioned_until.hpp>
#define BOOST_TEST_MAIN
#include <boost/test/unit_test.hpp>
#include <string>
#include <vector>
#include <list>
namespace ba = boost::algorithm;
// namespace ba = boost;
template <typename T>
struct less_than {
public:
less_than ( T foo ) : val ( foo ) {}
less_than ( const less_than &rhs ) : val ( rhs.val ) {}
bool operator () ( const T &v ) const { return v < val; }
private:
less_than ();
less_than operator = ( const less_than &rhs );
T val;
};
void test_sequence1 () {
std::vector<int> v;
v.clear ();
for ( int i = 5; i < 15; ++i )
v.push_back ( i );
BOOST_CHECK ( ba::is_partitioned_until ( v, less_than<int>(3)) == v.end()); // no elements
BOOST_CHECK ( ba::is_partitioned_until ( v, less_than<int>(6)) == v.end()); // only the first element
BOOST_CHECK ( ba::is_partitioned_until ( v, less_than<int>(10)) == v.end()); // in the middle somewhere
BOOST_CHECK ( ba::is_partitioned_until ( v, less_than<int>(99)) == v.end()); // all elements satisfy
// With bidirectional iterators.
std::list<int> l;
for ( int i = 5; i < 15; ++i )
l.push_back ( i );
BOOST_CHECK ( ba::is_partitioned_until ( l.begin (), l.end (), less_than<int>(3)) == l.end()); // no elements
BOOST_CHECK ( ba::is_partitioned_until ( l.begin (), l.end (), less_than<int>(6)) == l.end()); // only the first element
BOOST_CHECK ( ba::is_partitioned_until ( l.begin (), l.end (), less_than<int>(10)) == l.end()); // in the middle somewhere
BOOST_CHECK ( ba::is_partitioned_until ( l.begin (), l.end (), less_than<int>(99)) == l.end()); // all elements satisfy
}
BOOST_AUTO_TEST_CASE( test_main )
{
test_sequence1 ();
}

View File

@ -30,23 +30,23 @@ public:
typedef It pointer;
typedef typename std::iterator_traits<It>::reference reference;
It base() const {return it_;}
BOOST_CXX14_CONSTEXPR It base() const {return it_;}
input_iterator() : it_() {}
explicit input_iterator(It it) : it_(it) {}
BOOST_CXX14_CONSTEXPR input_iterator() : it_() {}
BOOST_CXX14_CONSTEXPR explicit input_iterator(It it) : it_(it) {}
template <typename U>
input_iterator(const input_iterator<U>& u) :it_(u.it_) {}
BOOST_CXX14_CONSTEXPR input_iterator(const input_iterator<U>& u) :it_(u.it_) {}
reference operator*() const {return *it_;}
pointer operator->() const {return it_;}
BOOST_CXX14_CONSTEXPR reference operator*() const {return *it_;}
BOOST_CXX14_CONSTEXPR pointer operator->() const {return it_;}
input_iterator& operator++() {++it_; return *this;}
input_iterator operator++(int) {input_iterator tmp(*this); ++(*this); return tmp;}
BOOST_CXX14_CONSTEXPR input_iterator& operator++() {++it_; return *this;}
BOOST_CXX14_CONSTEXPR input_iterator operator++(int) {input_iterator tmp(*this); ++(*this); return tmp;}
friend bool operator==(const input_iterator& x, const input_iterator& y)
BOOST_CXX14_CONSTEXPR friend bool operator==(const input_iterator& x, const input_iterator& y)
{return x.it_ == y.it_;}
friend bool operator!=(const input_iterator& x, const input_iterator& y)
BOOST_CXX14_CONSTEXPR friend bool operator!=(const input_iterator& x, const input_iterator& y)
{return !(x == y);}
private:
@ -55,14 +55,14 @@ private:
};
template <typename T, typename U>
inline bool
BOOST_CXX14_CONSTEXPR inline bool
operator==(const input_iterator<T>& x, const input_iterator<U>& y)
{
return x.base() == y.base();
}
template <typename T, typename U>
inline bool
BOOST_CXX14_CONSTEXPR inline bool
operator!=(const input_iterator<T>& x, const input_iterator<U>& y)
{
return !(x == y);
@ -79,22 +79,22 @@ public:
typedef It pointer;
typedef typename std::iterator_traits<It>::reference reference;
It base() const {return it_;}
BOOST_CXX14_CONSTEXPR It base() const {return it_;}
forward_iterator() : it_() {}
explicit forward_iterator(It it) : it_(it) {}
BOOST_CXX14_CONSTEXPR forward_iterator() : it_() {}
BOOST_CXX14_CONSTEXPR explicit forward_iterator(It it) : it_(it) {}
template <typename U>
forward_iterator(const forward_iterator<U>& u) :it_(u.it_) {}
BOOST_CXX14_CONSTEXPR forward_iterator(const forward_iterator<U>& u) :it_(u.it_) {}
reference operator*() const {return *it_;}
pointer operator->() const {return it_;}
BOOST_CXX14_CONSTEXPR reference operator*() const {return *it_;}
BOOST_CXX14_CONSTEXPR pointer operator->() const {return it_;}
forward_iterator& operator++() {++it_; return *this;}
forward_iterator operator++(int) {forward_iterator tmp(*this); ++(*this); return tmp;}
BOOST_CXX14_CONSTEXPR forward_iterator& operator++() {++it_; return *this;}
BOOST_CXX14_CONSTEXPR forward_iterator operator++(int) {forward_iterator tmp(*this); ++(*this); return tmp;}
friend bool operator==(const forward_iterator& x, const forward_iterator& y)
BOOST_CXX14_CONSTEXPR friend bool operator==(const forward_iterator& x, const forward_iterator& y)
{return x.it_ == y.it_;}
friend bool operator!=(const forward_iterator& x, const forward_iterator& y)
BOOST_CXX14_CONSTEXPR friend bool operator!=(const forward_iterator& x, const forward_iterator& y)
{return !(x == y);}
private:
It it_;
@ -103,14 +103,14 @@ private:
};
template <typename T, typename U>
inline bool
BOOST_CXX14_CONSTEXPR inline bool
operator==(const forward_iterator<T>& x, const forward_iterator<U>& y)
{
return x.base() == y.base();
}
template <typename T, typename U>
inline bool
BOOST_CXX14_CONSTEXPR inline bool
operator!=(const forward_iterator<T>& x, const forward_iterator<U>& y)
{
return !(x == y);
@ -127,35 +127,35 @@ public:
typedef It pointer;
typedef typename std::iterator_traits<It>::reference reference;
It base() const {return it_;}
BOOST_CXX14_CONSTEXPR It base() const {return it_;}
bidirectional_iterator() : it_() {}
explicit bidirectional_iterator(It it) : it_(it) {}
BOOST_CXX14_CONSTEXPR bidirectional_iterator() : it_() {}
BOOST_CXX14_CONSTEXPR explicit bidirectional_iterator(It it) : it_(it) {}
template <typename U>
bidirectional_iterator(const bidirectional_iterator<U>& u) :it_(u.it_) {}
BOOST_CXX14_CONSTEXPR bidirectional_iterator(const bidirectional_iterator<U>& u) :it_(u.it_) {}
reference operator*() const {return *it_;}
pointer operator->() const {return it_;}
BOOST_CXX14_CONSTEXPR reference operator*() const {return *it_;}
BOOST_CXX14_CONSTEXPR pointer operator->() const {return it_;}
bidirectional_iterator& operator++() {++it_; return *this;}
bidirectional_iterator operator++(int) {bidirectional_iterator tmp(*this); ++(*this); return tmp;}
BOOST_CXX14_CONSTEXPR bidirectional_iterator& operator++() {++it_; return *this;}
BOOST_CXX14_CONSTEXPR bidirectional_iterator operator++(int) {bidirectional_iterator tmp(*this); ++(*this); return tmp;}
bidirectional_iterator& operator--() {--it_; return *this;}
bidirectional_iterator operator--(int) {bidirectional_iterator tmp(*this); --(*this); return tmp;}
BOOST_CXX14_CONSTEXPR bidirectional_iterator& operator--() {--it_; return *this;}
BOOST_CXX14_CONSTEXPR bidirectional_iterator operator--(int) {bidirectional_iterator tmp(*this); --(*this); return tmp;}
private:
It it_;
template <typename U> friend class bidirectional_iterator;
};
template <typename T, typename U>
inline bool
BOOST_CXX14_CONSTEXPR inline bool
operator==(const bidirectional_iterator<T>& x, const bidirectional_iterator<U>& y)
{
return x.base() == y.base();
}
template <typename T, typename U>
inline bool
BOOST_CXX14_CONSTEXPR inline bool
operator!=(const bidirectional_iterator<T>& x, const bidirectional_iterator<U>& y)
{
return !(x == y);
@ -172,30 +172,30 @@ public:
typedef It pointer;
typedef typename std::iterator_traits<It>::reference reference;
It base() const {return it_;}
BOOST_CXX14_CONSTEXPR It base() const {return it_;}
random_access_iterator() : it_() {}
explicit random_access_iterator(It it) : it_(it) {}
BOOST_CXX14_CONSTEXPR random_access_iterator() : it_() {}
BOOST_CXX14_CONSTEXPR explicit random_access_iterator(It it) : it_(it) {}
template <typename U>
random_access_iterator(const random_access_iterator<U>& u) :it_(u.it_) {}
BOOST_CXX14_CONSTEXPR random_access_iterator(const random_access_iterator<U>& u) :it_(u.it_) {}
reference operator*() const {return *it_;}
pointer operator->() const {return it_;}
BOOST_CXX14_CONSTEXPR reference operator*() const {return *it_;}
BOOST_CXX14_CONSTEXPR pointer operator->() const {return it_;}
random_access_iterator& operator++() {++it_; return *this;}
random_access_iterator operator++(int) {random_access_iterator tmp(*this); ++(*this); return tmp;}
BOOST_CXX14_CONSTEXPR random_access_iterator& operator++() {++it_; return *this;}
BOOST_CXX14_CONSTEXPR random_access_iterator operator++(int) {random_access_iterator tmp(*this); ++(*this); return tmp;}
random_access_iterator& operator--() {--it_; return *this;}
random_access_iterator operator--(int) {random_access_iterator tmp(*this); --(*this); return tmp;}
BOOST_CXX14_CONSTEXPR random_access_iterator& operator--() {--it_; return *this;}
BOOST_CXX14_CONSTEXPR random_access_iterator operator--(int) {random_access_iterator tmp(*this); --(*this); return tmp;}
random_access_iterator& operator+=(difference_type n) {it_ += n; return *this;}
random_access_iterator operator+ (difference_type n) const {random_access_iterator tmp(*this); tmp += n; return tmp;}
friend random_access_iterator operator+(difference_type n, random_access_iterator x) {x += n; return x;}
BOOST_CXX14_CONSTEXPR random_access_iterator& operator+=(difference_type n) {it_ += n; return *this;}
BOOST_CXX14_CONSTEXPR random_access_iterator operator+ (difference_type n) const {random_access_iterator tmp(*this); tmp += n; return tmp;}
BOOST_CXX14_CONSTEXPR friend random_access_iterator operator+(difference_type n, random_access_iterator x) {x += n; return x;}
random_access_iterator& operator-=(difference_type n) {return *this += -n;}
random_access_iterator operator- (difference_type n) const {random_access_iterator tmp(*this); tmp -= n; return tmp;}
BOOST_CXX14_CONSTEXPR random_access_iterator& operator-=(difference_type n) {return *this += -n;}
BOOST_CXX14_CONSTEXPR random_access_iterator operator- (difference_type n) const {random_access_iterator tmp(*this); tmp -= n; return tmp;}
reference operator[](difference_type n) const {return it_[n];}
BOOST_CXX14_CONSTEXPR reference operator[](difference_type n) const {return it_[n];}
private:
It it_;
@ -203,49 +203,49 @@ private:
};
template <typename T, typename U>
inline bool
BOOST_CXX14_CONSTEXPR inline bool
operator==(const random_access_iterator<T>& x, const random_access_iterator<U>& y)
{
return x.base() == y.base();
}
template <typename T, typename U>
inline bool
BOOST_CXX14_CONSTEXPR inline bool
operator!=(const random_access_iterator<T>& x, const random_access_iterator<U>& y)
{
return !(x == y);
}
template <typename T, typename U>
inline bool
BOOST_CXX14_CONSTEXPR inline bool
operator<(const random_access_iterator<T>& x, const random_access_iterator<U>& y)
{
return x.base() < y.base();
}
template <typename T, typename U>
inline bool
BOOST_CXX14_CONSTEXPR inline bool
operator<=(const random_access_iterator<T>& x, const random_access_iterator<U>& y)
{
return !(y < x);
}
template <typename T, typename U>
inline bool
BOOST_CXX14_CONSTEXPR inline bool
operator>(const random_access_iterator<T>& x, const random_access_iterator<U>& y)
{
return y < x;
}
template <typename T, typename U>
inline bool
BOOST_CXX14_CONSTEXPR inline bool
operator>=(const random_access_iterator<T>& x, const random_access_iterator<U>& y)
{
return !(x < y);
}
template <typename T, typename U>
inline typename std::iterator_traits<T>::difference_type
BOOST_CXX14_CONSTEXPR inline typename std::iterator_traits<T>::difference_type
operator-(const random_access_iterator<T>& x, const random_access_iterator<U>& y)
{
return x.base() - y.base();
@ -262,18 +262,18 @@ public:
typedef It pointer;
typedef typename std::iterator_traits<It>::reference reference;
It base() const {return it_;}
BOOST_CXX14_CONSTEXPR It base() const {return it_;}
output_iterator () {}
explicit output_iterator(It it) : it_(it) {}
BOOST_CXX14_CONSTEXPR output_iterator () {}
BOOST_CXX14_CONSTEXPR explicit output_iterator(It it) : it_(it) {}
template <typename U>
output_iterator(const output_iterator<U>& u) :it_(u.it_) {}
BOOST_CXX14_CONSTEXPR output_iterator(const output_iterator<U>& u) :it_(u.it_) {}
reference operator*() const {return *it_;}
BOOST_CXX14_CONSTEXPR reference operator*() const {return *it_;}
output_iterator& operator++() {++it_; return *this;}
output_iterator operator++(int) {output_iterator tmp(*this); ++(*this); return tmp;}
BOOST_CXX14_CONSTEXPR output_iterator& operator++() {++it_; return *this;}
BOOST_CXX14_CONSTEXPR output_iterator operator++(int) {output_iterator tmp(*this); ++(*this); return tmp;}
private:
It it_;
@ -285,21 +285,21 @@ private:
// == Get the base of an iterator; used for comparisons ==
template <typename Iter>
inline Iter base(output_iterator<Iter> i) { return i.base(); }
BOOST_CXX14_CONSTEXPR inline Iter base(output_iterator<Iter> i) { return i.base(); }
template <typename Iter>
inline Iter base(input_iterator<Iter> i) { return i.base(); }
BOOST_CXX14_CONSTEXPR inline Iter base(input_iterator<Iter> i) { return i.base(); }
template <typename Iter>
inline Iter base(forward_iterator<Iter> i) { return i.base(); }
BOOST_CXX14_CONSTEXPR inline Iter base(forward_iterator<Iter> i) { return i.base(); }
template <typename Iter>
inline Iter base(bidirectional_iterator<Iter> i) { return i.base(); }
BOOST_CXX14_CONSTEXPR inline Iter base(bidirectional_iterator<Iter> i) { return i.base(); }
template <typename Iter>
inline Iter base(random_access_iterator<Iter> i) { return i.base(); }
BOOST_CXX14_CONSTEXPR inline Iter base(random_access_iterator<Iter> i) { return i.base(); }
template <typename Iter> // everything else
inline Iter base(Iter i) { return i; }
BOOST_CXX14_CONSTEXPR inline Iter base(Iter i) { return i; }
#endif // ITERATORS_H

View File

@ -16,149 +16,176 @@
#include <boost/test/unit_test.hpp>
template <typename T>
bool eq ( const T& a, const T& b ) { return a == b; }
BOOST_CXX14_CONSTEXPR bool eq ( const T& a, const T& b ) { return a == b; }
template <typename T>
bool never_eq ( const T&, const T& ) { return false; }
BOOST_CXX14_CONSTEXPR bool never_eq ( const T&, const T& ) { return false; }
namespace ba = boost::algorithm;
template <typename Iter1, typename Iter2>
bool iter_eq ( std::pair<Iter1, Iter2> pr, Iter1 first, Iter2 second ) {
BOOST_CXX14_CONSTEXPR bool iter_eq ( std::pair<Iter1, Iter2> pr, Iter1 first, Iter2 second ) {
return pr.first == first && pr.second == second;
}
void test_mismatch ()
{
// Note: The literal values here are tested against directly, careful if you change them:
int num[] = { 1, 1, 2, 3, 5 };
BOOST_CXX14_CONSTEXPR int num[] = { 1, 1, 2, 3, 5 };
const int sz = sizeof (num)/sizeof(num[0]);
// No mismatch for empty sequences
BOOST_CHECK ( iter_eq (
ba::mismatch ( input_iterator<int *>(num), input_iterator<int *>(num),
input_iterator<int *>(num), input_iterator<int *>(num)),
input_iterator<int *>(num), input_iterator<int *>(num)));
ba::mismatch ( input_iterator<const int *>(num), input_iterator<const int *>(num),
input_iterator<const int *>(num), input_iterator<const int *>(num)),
input_iterator<const int *>(num), input_iterator<const int *>(num)));
BOOST_CHECK ( iter_eq (
ba::mismatch ( input_iterator<int *>(num), input_iterator<int *>(num),
input_iterator<int *>(num), input_iterator<int *>(num),
ba::mismatch ( input_iterator<const int *>(num), input_iterator<const int *>(num),
input_iterator<const int *>(num), input_iterator<const int *>(num),
never_eq<int> ),
input_iterator<int *>(num), input_iterator<int *>(num)));
input_iterator<const int *>(num), input_iterator<const int *>(num)));
BOOST_CHECK ( iter_eq (
ba::mismatch ( random_access_iterator<int *>(num), random_access_iterator<int *>(num),
random_access_iterator<int *>(num), random_access_iterator<int *>(num),
ba::mismatch ( random_access_iterator<const int *>(num), random_access_iterator<const int *>(num),
random_access_iterator<const int *>(num), random_access_iterator<const int *>(num),
never_eq<int> ),
random_access_iterator<int *>(num), random_access_iterator<int *>(num)));
random_access_iterator<const int *>(num), random_access_iterator<const int *>(num)));
// Empty vs. non-empty mismatch immediately
BOOST_CHECK ( iter_eq (
ba::mismatch ( input_iterator<int *>(num), input_iterator<int *>(num),
input_iterator<int *>(num), input_iterator<int *>(num + 1)),
input_iterator<int *>(num), input_iterator<int *>(num)));
ba::mismatch ( input_iterator<const int *>(num), input_iterator<const int *>(num),
input_iterator<const int *>(num), input_iterator<const int *>(num + 1)),
input_iterator<const int *>(num), input_iterator<const int *>(num)));
BOOST_CHECK ( iter_eq (
ba::mismatch ( input_iterator<int *>(num + 1), input_iterator<int *>(num + 2),
input_iterator<int *>(num), input_iterator<int *>(num)),
input_iterator<int *>(num + 1), input_iterator<int *>(num)));
ba::mismatch ( input_iterator<const int *>(num + 1), input_iterator<const int *>(num + 2),
input_iterator<const int *>(num), input_iterator<const int *>(num)),
input_iterator<const int *>(num + 1), input_iterator<const int *>(num)));
BOOST_CHECK ( iter_eq (
ba::mismatch ( random_access_iterator<int *>(num + 1), random_access_iterator<int *>(num + 2),
random_access_iterator<int *>(num), random_access_iterator<int *>(num)),
random_access_iterator<int *>(num + 1), random_access_iterator<int *>(num)));
ba::mismatch ( random_access_iterator<const int *>(num + 1), random_access_iterator<const int *>(num + 2),
random_access_iterator<const int *>(num), random_access_iterator<const int *>(num)),
random_access_iterator<const int *>(num + 1), random_access_iterator<const int *>(num)));
// Single element sequences are equal if they contain the same value
BOOST_CHECK ( iter_eq (
ba::mismatch ( input_iterator<int *>(num), input_iterator<int *>(num + 1),
input_iterator<int *>(num), input_iterator<int *>(num + 1)),
input_iterator<int *>(num + 1), input_iterator<int *>(num + 1)));
ba::mismatch ( input_iterator<const int *>(num), input_iterator<const int *>(num + 1),
input_iterator<const int *>(num), input_iterator<const int *>(num + 1)),
input_iterator<const int *>(num + 1), input_iterator<const int *>(num + 1)));
BOOST_CHECK ( iter_eq (
ba::mismatch ( input_iterator<int *>(num), input_iterator<int *>(num + 1),
input_iterator<int *>(num), input_iterator<int *>(num + 1),
ba::mismatch ( input_iterator<const int *>(num), input_iterator<const int *>(num + 1),
input_iterator<const int *>(num), input_iterator<const int *>(num + 1),
eq<int> ),
input_iterator<int *>(num + 1), input_iterator<int *>(num + 1)));
input_iterator<const int *>(num + 1), input_iterator<const int *>(num + 1)));
BOOST_CHECK ( iter_eq (
ba::mismatch ( random_access_iterator<int *>(num), random_access_iterator<int *>(num + 1),
random_access_iterator<int *>(num), random_access_iterator<int *>(num + 1),
ba::mismatch ( random_access_iterator<const int *>(num), random_access_iterator<const int *>(num + 1),
random_access_iterator<const int *>(num), random_access_iterator<const int *>(num + 1),
eq<int> ),
random_access_iterator<int *>(num + 1), random_access_iterator<int *>(num + 1)));
random_access_iterator<const int *>(num + 1), random_access_iterator<const int *>(num + 1)));
BOOST_CHECK ( iter_eq (
ba::mismatch ( input_iterator<int *>(num), input_iterator<int *>(num + 1),
input_iterator<int *>(num), input_iterator<int *>(num + 1),
ba::mismatch ( input_iterator<const int *>(num), input_iterator<const int *>(num + 1),
input_iterator<const int *>(num), input_iterator<const int *>(num + 1),
never_eq<int> ),
input_iterator<int *>(num), input_iterator<int *>(num)));
input_iterator<const int *>(num), input_iterator<const int *>(num)));
BOOST_CHECK ( iter_eq (
ba::mismatch ( random_access_iterator<int *>(num), random_access_iterator<int *>(num + 1),
random_access_iterator<int *>(num), random_access_iterator<int *>(num + 1),
ba::mismatch ( random_access_iterator<const int *>(num), random_access_iterator<const int *>(num + 1),
random_access_iterator<const int *>(num), random_access_iterator<const int *>(num + 1),
never_eq<int> ),
random_access_iterator<int *>(num), random_access_iterator<int *>(num)));
random_access_iterator<const int *>(num), random_access_iterator<const int *>(num)));
BOOST_CHECK ( iter_eq (
ba::mismatch ( input_iterator<int *>(num), input_iterator<int *>(num + 1),
input_iterator<int *>(num + 1), input_iterator<int *>(num + 2)),
input_iterator<int *>(num + 1), input_iterator<int *>(num + 2)));
ba::mismatch ( input_iterator<const int *>(num), input_iterator<const int *>(num + 1),
input_iterator<const int *>(num + 1), input_iterator<const int *>(num + 2)),
input_iterator<const int *>(num + 1), input_iterator<const int *>(num + 2)));
BOOST_CHECK ( iter_eq (
ba::mismatch ( input_iterator<int *>(num), input_iterator<int *>(num + 1),
input_iterator<int *>(num + 1), input_iterator<int *>(num + 2),
ba::mismatch ( input_iterator<const int *>(num), input_iterator<const int *>(num + 1),
input_iterator<const int *>(num + 1), input_iterator<const int *>(num + 2),
eq<int> ),
input_iterator<int *>(num + 1), input_iterator<int *>(num + 2)));
input_iterator<const int *>(num + 1), input_iterator<const int *>(num + 2)));
BOOST_CHECK ( iter_eq (
ba::mismatch ( input_iterator<int *>(num + 2), input_iterator<int *>(num + 3),
input_iterator<int *>(num), input_iterator<int *>(num + 1)),
input_iterator<int *>(num + 2), input_iterator<int *>(num)));
ba::mismatch ( input_iterator<const int *>(num + 2), input_iterator<const int *>(num + 3),
input_iterator<const int *>(num), input_iterator<const int *>(num + 1)),
input_iterator<const int *>(num + 2), input_iterator<const int *>(num)));
BOOST_CHECK ( iter_eq (
ba::mismatch ( input_iterator<int *>(num + 2), input_iterator<int *>(num + 3),
input_iterator<int *>(num), input_iterator<int *>(num + 1),
ba::mismatch ( input_iterator<const int *>(num + 2), input_iterator<const int *>(num + 3),
input_iterator<const int *>(num), input_iterator<const int *>(num + 1),
eq<int> ),
input_iterator<int *>(num + 2), input_iterator<int *>(num)));
input_iterator<const int *>(num + 2), input_iterator<const int *>(num)));
// Identical long sequences are equal.
BOOST_CHECK ( iter_eq (
ba::mismatch ( input_iterator<int *>(num), input_iterator<int *>(num + sz),
input_iterator<int *>(num), input_iterator<int *>(num + sz)),
input_iterator<int *>(num + sz), input_iterator<int *>(num + sz)));
ba::mismatch ( input_iterator<const int *>(num), input_iterator<const int *>(num + sz),
input_iterator<const int *>(num), input_iterator<const int *>(num + sz)),
input_iterator<const int *>(num + sz), input_iterator<const int *>(num + sz)));
BOOST_CHECK ( iter_eq (
ba::mismatch ( input_iterator<int *>(num), input_iterator<int *>(num + sz),
input_iterator<int *>(num), input_iterator<int *>(num + sz),
ba::mismatch ( input_iterator<const int *>(num), input_iterator<const int *>(num + sz),
input_iterator<const int *>(num), input_iterator<const int *>(num + sz),
eq<int> ),
input_iterator<int *>(num + sz), input_iterator<int *>(num + sz)));
input_iterator<const int *>(num + sz), input_iterator<const int *>(num + sz)));
BOOST_CHECK ( iter_eq (
ba::mismatch ( input_iterator<int *>(num), input_iterator<int *>(num + sz),
input_iterator<int *>(num), input_iterator<int *>(num + sz),
ba::mismatch ( input_iterator<const int *>(num), input_iterator<const int *>(num + sz),
input_iterator<const int *>(num), input_iterator<const int *>(num + sz),
never_eq<int> ),
input_iterator<int *>(num), input_iterator<int *>(num)));
input_iterator<const int *>(num), input_iterator<const int *>(num)));
BOOST_CHECK ( iter_eq (
ba::mismatch ( input_iterator<int *>(num), input_iterator<int *>(num + sz),
random_access_iterator<int *>(num), random_access_iterator<int *>(num + sz),
ba::mismatch ( input_iterator<const int *>(num), input_iterator<const int *>(num + sz),
random_access_iterator<const int *>(num), random_access_iterator<const int *>(num + sz),
never_eq<int> ),
input_iterator<int *>(num), random_access_iterator<int *>(num)));
input_iterator<const int *>(num), random_access_iterator<const int *>(num)));
// different sequences are different
// Different sequences are different
BOOST_CHECK ( iter_eq (
ba::mismatch ( input_iterator<int *>(num + 1), input_iterator<int *>(num + sz),
input_iterator<int *>(num), input_iterator<int *>(num + sz)),
input_iterator<int *>(num + 2), input_iterator<int *>(num + 1)));
ba::mismatch ( input_iterator<const int *>(num + 1), input_iterator<const int *>(num + sz),
input_iterator<const int *>(num), input_iterator<const int *>(num + sz)),
input_iterator<const int *>(num + 2), input_iterator<const int *>(num + 1)));
BOOST_CHECK ( iter_eq (
ba::mismatch ( input_iterator<int *>(num + 1), input_iterator<int *>(num + sz),
input_iterator<int *>(num), input_iterator<int *>(num + sz),
ba::mismatch ( input_iterator<const int *>(num + 1), input_iterator<const int *>(num + sz),
input_iterator<const int *>(num), input_iterator<const int *>(num + sz),
eq<int> ),
input_iterator<int *>(num + 2), input_iterator<int *>(num + 1)));
input_iterator<const int *>(num + 2), input_iterator<const int *>(num + 1)));
// Checks constexpr
BOOST_CXX14_CONSTEXPR bool res = (
// No mismatch for empty
iter_eq (
ba::mismatch ( input_iterator<const int *>(num), input_iterator<const int *>(num),
input_iterator<const int *>(num), input_iterator<const int *>(num)),
input_iterator<const int *>(num), input_iterator<const int *>(num))
// Empty vs. non-empty mismatch immediately
&& iter_eq (
ba::mismatch ( input_iterator<const int *>(num), input_iterator<const int *>(num),
input_iterator<const int *>(num), input_iterator<const int *>(num + 1)),
input_iterator<const int *>(num), input_iterator<const int *>(num))
// Single element sequences are equal if they contain the same value
&& iter_eq (
ba::mismatch ( input_iterator<const int *>(num), input_iterator<const int *>(num + 1),
input_iterator<const int *>(num), input_iterator<const int *>(num + 1),
eq<int>),
input_iterator<const int *>(num + 1), input_iterator<const int *>(num + 1))
// Identical long sequences are equal.
&& iter_eq (
ba::mismatch ( input_iterator<const int *>(num), input_iterator<const int *>(num + sz),
input_iterator<const int *>(num), input_iterator<const int *>(num + sz),
eq<int> ),
input_iterator<const int *>(num + sz), input_iterator<const int *>(num + sz))
);
BOOST_CHECK ( res );
}

View File

@ -18,10 +18,9 @@
#include <list>
template<typename T>
struct is_ : public std::unary_function<T, bool> {
is_ ( T v ) : val_ ( v ) {}
~is_ () {}
bool operator () ( T comp ) const { return val_ == comp; }
struct is_ {
BOOST_CXX14_CONSTEXPR is_ ( T v ) : val_ ( v ) {}
BOOST_CXX14_CONSTEXPR bool operator () ( T comp ) const { return val_ == comp; }
private:
is_ (); // need a value
@ -33,7 +32,7 @@ namespace ba = boost::algorithm;
void test_none()
{
// Note: The literal values here are tested against directly, careful if you change them:
int some_numbers[] = { 1, 5, 0, 18, 1 };
BOOST_CXX14_CONSTEXPR int some_numbers[] = { 1, 5, 0, 18, 1 };
std::vector<int> vi(some_numbers, some_numbers + 5);
std::list<int> li(vi.begin(), vi.end ());
@ -89,6 +88,15 @@ void test_none()
BOOST_CHECK ( ba::none_of_equal ( li.begin(), l_iter, 18 ));
BOOST_CHECK ( ba::none_of ( li.begin(), l_iter, is_<int> ( 18 )));
BOOST_CHECK (!ba::none_of ( li.begin(), l_iter, is_<int> ( 5 )));
BOOST_CXX14_CONSTEXPR bool constexpr_res =
!ba::none_of_equal ( some_numbers, 1 ) &&
!ba::none_of ( some_numbers, is_<int> ( 1 )) &&
ba::none_of_equal ( some_numbers, some_numbers + 3, 100 ) &&
ba::none_of ( some_numbers, some_numbers + 3, is_<int> ( 100 )) &&
true;
BOOST_CHECK ( constexpr_res );
}
BOOST_AUTO_TEST_CASE( test_main )

View File

@ -18,10 +18,9 @@
#include <list>
template<typename T>
struct is_ : public std::unary_function<T, bool> {
is_ ( T v ) : val_ ( v ) {}
~is_ () {}
bool operator () ( T comp ) const { return val_ == comp; }
struct is_ {
BOOST_CXX14_CONSTEXPR is_ ( T v ) : val_ ( v ) {}
BOOST_CXX14_CONSTEXPR bool operator () ( T comp ) const { return val_ == comp; }
private:
is_ (); // need a value
@ -33,7 +32,7 @@ namespace ba = boost::algorithm;
void test_one ()
{
// Note: The literal values here are tested against directly, careful if you change them:
int some_numbers[] = { 1, 1, 2, 3, 5 };
BOOST_CXX14_CONSTEXPR int some_numbers[] = { 1, 1, 2, 3, 5 };
std::vector<int> vi(some_numbers, some_numbers + 5);
std::list<int> li(vi.begin(), vi.end ());
@ -92,7 +91,13 @@ void test_one ()
BOOST_CHECK ( ba::one_of ( li.begin(), l_iter, is_<int> ( 2 )));
BOOST_CHECK (!ba::one_of_equal ( li.begin(), l_iter, 3 ));
BOOST_CHECK (!ba::one_of ( li.begin(), l_iter, is_<int> ( 3 )));
BOOST_CXX14_CONSTEXPR bool constexpr_res =
!ba::one_of ( some_numbers, is_<int> ( 6 )) &&
ba::one_of ( some_numbers + 1, some_numbers + 3, is_<int> ( 1 )) &&
true;
BOOST_CHECK ( constexpr_res );
}

View File

@ -29,11 +29,13 @@ using namespace boost;
namespace ba = boost::algorithm;
BOOST_CXX14_CONSTEXPR bool less( int x, int y ) { return x < y; }
static void
test_ordered(void)
{
const int strictlyIncreasingValues[] = { 1, 2, 3, 4, 5 };
const int randomValues[] = { 3, 6, 1, 2, 7 };
BOOST_CXX14_CONSTEXPR const int strictlyIncreasingValues[] = { 1, 2, 3, 4, 5 };
BOOST_CXX14_CONSTEXPR const int randomValues[] = { 3, 6, 1, 2, 7 };
const int constantValues[] = { 1, 2, 2, 2, 5 };
int nonConstantArray[] = { 1, 2, 2, 2, 5 };
const int inOrderUntilTheEnd [] = { 0, 1, 2, 3, 4, 5, 6, 7, 6 };
@ -74,18 +76,26 @@ test_ordered(void)
BOOST_CHECK ( ba::is_sorted_until ( a_begin(randomValues), a_begin(randomValues)) == a_begin(randomValues));
BOOST_CHECK ( ba::is_sorted_until ( a_begin(randomValues), a_begin(randomValues) + 1, std::equal_to<int>()) == a_begin(randomValues) + 1);
BOOST_CHECK ( ba::is_sorted_until ( a_begin(randomValues), a_begin(randomValues) + 1 ) == a_begin(randomValues) + 1);
BOOST_CXX14_CONSTEXPR bool constexpr_res = (
ba::is_sorted ( boost::begin(strictlyIncreasingValues), boost::end(strictlyIncreasingValues) )
&& !ba::is_sorted (a_range(randomValues))
&& ba::is_sorted_until ( boost::begin(strictlyIncreasingValues), boost::end(strictlyIncreasingValues), less) == a_end(strictlyIncreasingValues)
&& ba::is_sorted_until ( randomValues, less) == &randomValues[2]
);
BOOST_CHECK ( constexpr_res );
}
static void
test_increasing_decreasing(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 };
BOOST_CXX14_CONSTEXPR const int strictlyIncreasingValues[] = { 1, 2, 3, 4, 5 };
BOOST_CXX14_CONSTEXPR const int strictlyDecreasingValues[] = { 9, 8, 7, 6, 5 };
BOOST_CXX14_CONSTEXPR const int increasingValues[] = { 1, 2, 2, 2, 5 };
BOOST_CXX14_CONSTEXPR const int decreasingValues[] = { 9, 7, 7, 7, 5 };
BOOST_CXX14_CONSTEXPR const int randomValues[] = { 3, 6, 1, 2, 7 };
BOOST_CXX14_CONSTEXPR const int constantValues[] = { 7, 7, 7, 7, 7 };
// Test a strictly increasing sequence
BOOST_CHECK ( ba::is_strictly_increasing (b_e(strictlyIncreasingValues)));
@ -146,6 +156,15 @@ test_increasing_decreasing(void)
BOOST_CHECK ( !ba::is_strictly_decreasing (strictlyIncreasingValues, strictlyIncreasingValues+2));
BOOST_CHECK ( !ba::is_decreasing (strictlyIncreasingValues, strictlyIncreasingValues+2));
BOOST_CXX14_CONSTEXPR bool constexpr_res = (
ba::is_increasing (boost::begin(increasingValues), boost::end(increasingValues))
&& ba::is_decreasing (boost::begin(decreasingValues), boost::end(decreasingValues))
&& ba::is_strictly_increasing (boost::begin(strictlyIncreasingValues), boost::end(strictlyIncreasingValues))
&& ba::is_strictly_decreasing (boost::begin(strictlyDecreasingValues), boost::end(strictlyDecreasingValues))
&& !ba::is_strictly_increasing (boost::begin(increasingValues), boost::end(increasingValues))
&& !ba::is_strictly_decreasing (boost::begin(decreasingValues), boost::end(decreasingValues))
);
BOOST_CHECK ( constexpr_res );
}
BOOST_AUTO_TEST_CASE( test_main )

View File

@ -47,10 +47,10 @@ void test_sequence ( const Container &c, Predicate comp ) {
template <typename T>
struct less_than {
public:
less_than ( T foo ) : val ( foo ) {}
less_than ( const less_than &rhs ) : val ( rhs.val ) {}
BOOST_CXX14_CONSTEXPR less_than ( T foo ) : val ( foo ) {}
BOOST_CXX14_CONSTEXPR less_than ( const less_than &rhs ) : val ( rhs.val ) {}
bool operator () ( const T &v ) const { return v < val; }
BOOST_CXX14_CONSTEXPR bool operator () ( const T &v ) const { return v < val; }
private:
less_than ();
less_than operator = ( const less_than &rhs );
@ -81,8 +81,30 @@ void test_sequence1 () {
}
BOOST_CXX14_CONSTEXPR bool test_constexpr () {
int in[] = {1, 1, 2};
int out_true[3] = {0};
int out_false[3] = {0};
bool res = true;
ba::partition_copy( in, in + 3, out_true, out_false, less_than<int>(2) );
res = (res && ba::all_of(out_true, out_true + 2, less_than<int>(2)) );
res = (res && ba::none_of(out_false, out_false + 1, less_than<int>(2)) );
// clear elements
out_true [0] = 0;
out_true [1] = 0;
out_false[0] = 0;
ba::partition_copy( in, out_true, out_false, less_than<int>(2));
res = ( res && ba::all_of(out_true, out_true + 2, less_than<int>(2)));
res = ( res && ba::none_of(out_false, out_false + 1, less_than<int>(2)));
return res;
}
BOOST_AUTO_TEST_CASE( test_main )
{
test_sequence1 ();
BOOST_CXX14_CONSTEXPR bool constexpr_res = test_constexpr ();
BOOST_CHECK ( constexpr_res );
}

View File

@ -7,6 +7,20 @@
#include <vector>
#include <iostream>
#if (__cplusplus >= 201103L) || defined(BOOST_NO_CXX98_RANDOM_SHUFFLE)
#include <random>
std::default_random_engine gen;
template<typename RandomIt>
void do_shuffle(RandomIt first, RandomIt last)
{ std::shuffle(first, last, gen); }
#else
template<typename RandomIt>
void do_shuffle(RandomIt first, RandomIt last)
{ std::random_shuffle(first, last); }
#endif
namespace ba = boost::algorithm;
template <typename Iter>
@ -20,7 +34,7 @@ void check_sequence ( Iter first, Iter last, Iter sf, Iter sl )
// }
// if (sl == last) std::cout << "<";
// std::cout << '\n';
if (sf == sl) return;
for (Iter i = first; i < sf; ++i)
BOOST_CHECK(*i < *sf);
@ -62,7 +76,7 @@ BOOST_AUTO_TEST_CASE( test_main )
std::vector<int> v;
for ( int i = 0; i < 10; ++i )
v.push_back(i);
const std::vector<int>::iterator b = v.begin();
ba::partition_subrange(b, v.end(), b + 3, b + 6);
check_sequence (b, v.end(), b + 3, b + 6);
@ -70,16 +84,16 @@ BOOST_AUTO_TEST_CASE( test_main )
// BOOST_CHECK_EQUAL(v[3], 3);
// BOOST_CHECK_EQUAL(v[4], 4);
// BOOST_CHECK_EQUAL(v[5], 5);
// Mix them up and try again - single element
std::random_shuffle(v.begin(), v.end());
do_shuffle(v.begin(), v.end());
ba::partition_subrange(b, v.end(), b + 7, b + 8);
check_sequence (b, v.end(), b + 7, b + 8);
// BOOST_CHECK_EQUAL(v[7], 7);
// Mix them up and try again - at the end
std::random_shuffle(v.begin(), v.end());
do_shuffle(v.begin(), v.end());
ba::partition_subrange(b, v.end(), b + 7, v.end());
check_sequence (b, v.end(), b + 7, v.end());
@ -88,7 +102,7 @@ BOOST_AUTO_TEST_CASE( test_main )
// BOOST_CHECK_EQUAL(v[9], 9);
// Mix them up and try again - at the beginning
std::random_shuffle(v.begin(), v.end());
do_shuffle(v.begin(), v.end());
ba::partition_subrange(b, v.end(), b, b + 2);
check_sequence (b, v.end(), b, b + 2);
@ -96,12 +110,12 @@ BOOST_AUTO_TEST_CASE( test_main )
// BOOST_CHECK_EQUAL(v[1], 1);
// Mix them up and try again - empty subrange
std::random_shuffle(v.begin(), v.end());
do_shuffle(v.begin(), v.end());
ba::partition_subrange(b, v.end(), b, b);
check_sequence (b, v.end(), b, b);
// Mix them up and try again - entire subrange
std::random_shuffle(v.begin(), v.end());
do_shuffle(v.begin(), v.end());
ba::partition_subrange(b, v.end(), b, v.end());
check_sequence (b, v.end(), b, v.end());
}
@ -110,7 +124,7 @@ BOOST_AUTO_TEST_CASE( test_main )
std::vector<int> v;
for ( int i = 0; i < 10; ++i )
v.push_back(i);
const std::vector<int>::iterator b = v.begin();
ba::partition_subrange(b, v.end(), b + 3, b + 6, std::greater<int>());
check_sequence (b, v.end(), b + 3, b + 6, std::greater<int>());
@ -120,14 +134,14 @@ BOOST_AUTO_TEST_CASE( test_main )
// BOOST_CHECK_EQUAL(v[5], 4);
// Mix them up and try again - single element
std::random_shuffle(v.begin(), v.end());
do_shuffle(v.begin(), v.end());
ba::partition_subrange(b, v.end(), b + 7, b + 8, std::greater<int>());
check_sequence (b, v.end(), b + 7, b + 8, std::greater<int>());
// BOOST_CHECK_EQUAL(v[7], 2);
// Mix them up and try again - at the end
std::random_shuffle(v.begin(), v.end());
do_shuffle(v.begin(), v.end());
ba::partition_subrange(b, v.end(), b + 7, v.end(), std::greater<int>());
check_sequence (b, v.end(), b + 7, v.end(), std::greater<int>());
@ -136,7 +150,7 @@ BOOST_AUTO_TEST_CASE( test_main )
// BOOST_CHECK_EQUAL(v[9], 0);
// Mix them up and try again - at the beginning
std::random_shuffle(v.begin(), v.end());
do_shuffle(v.begin(), v.end());
ba::partition_subrange(b, v.end(), b, b + 2, std::greater<int>());
check_sequence (b, v.end(), b, b + 2, std::greater<int>());
@ -144,12 +158,12 @@ BOOST_AUTO_TEST_CASE( test_main )
// BOOST_CHECK_EQUAL(v[1], 8);
// Mix them up and try again - empty subrange
std::random_shuffle(v.begin(), v.end());
do_shuffle(v.begin(), v.end());
ba::partition_subrange(b, v.end(), b, b, std::greater<int>());
check_sequence (b, v.end(), b, b, std::greater<int>());
// Mix them up and try again - entire subrange
std::random_shuffle(v.begin(), v.end());
do_shuffle(v.begin(), v.end());
ba::partition_subrange(b, v.end(), b, v.end(), std::greater<int>());
check_sequence (b, v.end(), b, v.end(), std::greater<int>());
}

View File

@ -18,7 +18,7 @@
namespace ba = boost::algorithm;
BOOST_AUTO_TEST_CASE( test_main )
void test_power ()
{
BOOST_CHECK ( ba::power(0, 0) == 1);
BOOST_CHECK ( ba::power(5, 0) == 1);
@ -34,3 +34,51 @@ BOOST_AUTO_TEST_CASE( test_main )
BOOST_CHECK ( ba::power(3,2) == ba::power(3,2, std::multiplies<int>()));
BOOST_CHECK ( ba::power(3,2, std::plus<int>()) == 6);
}
void test_power_constexpr ()
{
BOOST_CXX14_CONSTEXPR bool check_zero_power1 =
ba::power(0, 0) == 1;
BOOST_CHECK(check_zero_power1);
BOOST_CXX14_CONSTEXPR bool check_zero_power2 =
ba::power(5, 0) == 1;
BOOST_CHECK(check_zero_power2);
BOOST_CXX14_CONSTEXPR bool check_one_base1 =
ba::power(1, 1) == 1;
BOOST_CHECK(check_one_base1);
BOOST_CXX14_CONSTEXPR bool check_one_base2 =
ba::power(1, 4) == 1;
BOOST_CHECK(check_one_base2);
BOOST_CXX14_CONSTEXPR bool check_power1 =
ba::power(3, 2) == 9;
BOOST_CHECK(check_power1);
BOOST_CXX14_CONSTEXPR bool check_power2 =
ba::power(2, 3) == 8;
BOOST_CHECK(check_power2);
BOOST_CXX14_CONSTEXPR bool check_power3 =
ba::power(3, 3) == 27;
BOOST_CHECK(check_power3);
BOOST_CXX14_CONSTEXPR bool check_power4 =
ba::power(2, 30) == 0x40000000;
BOOST_CHECK(check_power4);
BOOST_CXX14_CONSTEXPR bool check_power5 =
ba::power(5L, 10) == 3125*3125;
BOOST_CHECK(check_power5);
BOOST_CXX14_CONSTEXPR bool check_power6 =
ba::power(18, 3) == 18*18*18;
BOOST_CHECK(check_power6);
BOOST_CXX14_CONSTEXPR bool check_multiple =
ba::power(3, 2, std::multiplies<int>()) == ba::power(3, 2);
BOOST_CHECK(check_multiple);
BOOST_CXX14_CONSTEXPR bool check_plus =
ba::power(3, 2, std::plus<int>()) == 6;
BOOST_CHECK(check_plus);
}
BOOST_AUTO_TEST_CASE( test_main ) {
test_power ();
test_power_constexpr ();
}

128
test/reduce_test.cpp Normal file
View File

@ -0,0 +1,128 @@
/*
Copyright (c) Marshall Clow 2013.
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)
For more information, see http://www.boost.org
*/
#include <vector>
#include <functional>
#include <boost/config.hpp>
#include <boost/algorithm/cxx17/reduce.hpp>
#include "iterator_test.hpp"
#define BOOST_TEST_MAIN
#include <boost/test/unit_test.hpp>
namespace ba = boost::algorithm;
template <class Iter, class T, class Op>
void
test_reduce(Iter first, Iter last, T init, Op op, T x)
{
BOOST_CHECK(ba::reduce(first, last, init, op) == x);
}
template <class Iter, class T, class Op>
void
test_reduce(Iter first, Iter last, Op op, T x)
{
BOOST_CHECK(ba::reduce(first, last, op) == x);
}
template <class Iter, class T>
void
test_reduce(Iter first, Iter last, T x)
{
BOOST_CHECK(ba::reduce(first, last) == x);
}
template <class Iter>
void
test_init_op()
{
int ia[] = {1, 2, 3, 4, 5, 6};
unsigned sa = sizeof(ia) / sizeof(ia[0]);
test_reduce(Iter(ia), Iter(ia), 0, std::plus<int>(), 0);
test_reduce(Iter(ia), Iter(ia), 1, std::multiplies<int>(), 1);
test_reduce(Iter(ia), Iter(ia+1), 0, std::plus<int>(), 1);
test_reduce(Iter(ia), Iter(ia+1), 2, std::multiplies<int>(), 2);
test_reduce(Iter(ia), Iter(ia+2), 0, std::plus<int>(), 3);
test_reduce(Iter(ia), Iter(ia+2), 3, std::multiplies<int>(), 6);
test_reduce(Iter(ia), Iter(ia+sa), 0, std::plus<int>(), 21);
test_reduce(Iter(ia), Iter(ia+sa), 4, std::multiplies<int>(), 2880);
}
void test_reduce_init_op()
{
test_init_op<input_iterator<const int*> >();
test_init_op<forward_iterator<const int*> >();
test_init_op<bidirectional_iterator<const int*> >();
test_init_op<random_access_iterator<const int*> >();
test_init_op<const int*>();
{
char ia[] = {1, 2, 3, 4, 5, 6, 7, 8};
unsigned sa = sizeof(ia) / sizeof(ia[0]);
unsigned res = boost::algorithm::reduce(ia, ia+sa, 1U, std::multiplies<unsigned>());
BOOST_CHECK(res == 40320); // 8! will not fit into a char
}
}
template <class Iter>
void
test_init()
{
int ia[] = {1, 2, 3, 4, 5, 6};
unsigned sa = sizeof(ia) / sizeof(ia[0]);
test_reduce(Iter(ia), Iter(ia), 0, 0);
test_reduce(Iter(ia), Iter(ia), 1, 1);
test_reduce(Iter(ia), Iter(ia+1), 0, 1);
test_reduce(Iter(ia), Iter(ia+1), 2, 3);
test_reduce(Iter(ia), Iter(ia+2), 0, 3);
test_reduce(Iter(ia), Iter(ia+2), 3, 6);
test_reduce(Iter(ia), Iter(ia+sa), 0, 21);
test_reduce(Iter(ia), Iter(ia+sa), 4, 25);
}
void test_reduce_init()
{
test_init<input_iterator<const int*> >();
test_init<forward_iterator<const int*> >();
test_init<bidirectional_iterator<const int*> >();
test_init<random_access_iterator<const int*> >();
test_init<const int*>();
}
template <class Iter>
void
test()
{
int ia[] = {1, 2, 3, 4, 5, 6};
unsigned sa = sizeof(ia) / sizeof(ia[0]);
test_reduce(Iter(ia), Iter(ia), 0);
test_reduce(Iter(ia), Iter(ia+1), 1);
test_reduce(Iter(ia), Iter(ia+2), 3);
test_reduce(Iter(ia), Iter(ia+sa), 21);
}
void test_reduce()
{
test<input_iterator<const int*> >();
test<forward_iterator<const int*> >();
test<bidirectional_iterator<const int*> >();
test<random_access_iterator<const int*> >();
test<const int*>();
}
BOOST_AUTO_TEST_CASE( test_main )
{
test_reduce();
test_reduce_init();
test_reduce_init_op();
}

View File

@ -7,6 +7,20 @@
#include <vector>
#include <iostream>
#if (__cplusplus >= 201103L) || defined(BOOST_NO_CXX98_RANDOM_SHUFFLE)
#include <random>
std::default_random_engine gen;
template<typename RandomIt>
void do_shuffle(RandomIt first, RandomIt last)
{ std::shuffle(first, last, gen); }
#else
template<typename RandomIt>
void do_shuffle(RandomIt first, RandomIt last)
{ std::random_shuffle(first, last); }
#endif
namespace ba = boost::algorithm;
template <typename Iter>
@ -43,7 +57,7 @@ BOOST_AUTO_TEST_CASE( test_main )
std::vector<int> v;
for ( int i = 0; i < 10; ++i )
v.push_back(i);
const std::vector<int>::iterator b = v.begin();
ba::sort_subrange(b, v.end(), b + 3, b + 6);
check_sequence (b, v.end(), b + 3, b + 6);
@ -51,16 +65,16 @@ BOOST_AUTO_TEST_CASE( test_main )
BOOST_CHECK_EQUAL(v[3], 3);
BOOST_CHECK_EQUAL(v[4], 4);
BOOST_CHECK_EQUAL(v[5], 5);
// Mix them up and try again - single element
std::random_shuffle(v.begin(), v.end());
do_shuffle(v.begin(), v.end());
ba::sort_subrange(b, v.end(), b + 7, b + 8);
check_sequence (b, v.end(), b + 7, b + 8);
BOOST_CHECK_EQUAL(v[7], 7);
// Mix them up and try again - at the end
std::random_shuffle(v.begin(), v.end());
do_shuffle(v.begin(), v.end());
ba::sort_subrange(b, v.end(), b + 7, v.end());
check_sequence (b, v.end(), b + 7, v.end());
@ -69,7 +83,7 @@ BOOST_AUTO_TEST_CASE( test_main )
BOOST_CHECK_EQUAL(v[9], 9);
// Mix them up and try again - at the beginning
std::random_shuffle(v.begin(), v.end());
do_shuffle(v.begin(), v.end());
ba::sort_subrange(b, v.end(), b, b + 2);
check_sequence (b, v.end(), b, b + 2);
@ -77,12 +91,12 @@ BOOST_AUTO_TEST_CASE( test_main )
BOOST_CHECK_EQUAL(v[1], 1);
// Mix them up and try again - empty subrange
std::random_shuffle(v.begin(), v.end());
do_shuffle(v.begin(), v.end());
ba::sort_subrange(b, v.end(), b, b);
check_sequence (b, v.end(), b, b);
// Mix them up and try again - entire subrange
std::random_shuffle(v.begin(), v.end());
do_shuffle(v.begin(), v.end());
ba::sort_subrange(b, v.end(), b, v.end());
check_sequence (b, v.end(), b, v.end());
}
@ -91,7 +105,7 @@ BOOST_AUTO_TEST_CASE( test_main )
std::vector<int> v;
for ( int i = 0; i < 10; ++i )
v.push_back(i);
const std::vector<int>::iterator b = v.begin();
ba::sort_subrange(b, v.end(), b + 3, b + 6, std::greater<int>());
check_sequence (b, v.end(), b + 3, b + 6, std::greater<int>());
@ -101,14 +115,14 @@ BOOST_AUTO_TEST_CASE( test_main )
BOOST_CHECK_EQUAL(v[5], 4);
// Mix them up and try again - single element
std::random_shuffle(v.begin(), v.end());
do_shuffle(v.begin(), v.end());
ba::sort_subrange(b, v.end(), b + 7, b + 8, std::greater<int>());
check_sequence (b, v.end(), b + 7, b + 8, std::greater<int>());
BOOST_CHECK_EQUAL(v[7], 2);
// Mix them up and try again - at the end
std::random_shuffle(v.begin(), v.end());
do_shuffle(v.begin(), v.end());
ba::sort_subrange(b, v.end(), b + 7, v.end(), std::greater<int>());
check_sequence (b, v.end(), b + 7, v.end(), std::greater<int>());
@ -117,7 +131,7 @@ BOOST_AUTO_TEST_CASE( test_main )
BOOST_CHECK_EQUAL(v[9], 0);
// Mix them up and try again - at the beginning
std::random_shuffle(v.begin(), v.end());
do_shuffle(v.begin(), v.end());
ba::sort_subrange(b, v.end(), b, b + 2, std::greater<int>());
check_sequence (b, v.end(), b, b + 2, std::greater<int>());
@ -125,12 +139,12 @@ BOOST_AUTO_TEST_CASE( test_main )
BOOST_CHECK_EQUAL(v[1], 8);
// Mix them up and try again - empty subrange
std::random_shuffle(v.begin(), v.end());
do_shuffle(v.begin(), v.end());
ba::sort_subrange(b, v.end(), b, b, std::greater<int>());
check_sequence (b, v.end(), b, b, std::greater<int>());
// Mix them up and try again - entire subrange
std::random_shuffle(v.begin(), v.end());
do_shuffle(v.begin(), v.end());
ba::sort_subrange(b, v.end(), b, v.end(), std::greater<int>());
check_sequence (b, v.end(), b, v.end(), std::greater<int>());
}

View File

@ -0,0 +1,137 @@
/*
Copyright (c) Marshall Clow 2017.
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)
For more information, see http://www.boost.org
*/
#include <vector>
#include <functional>
#include <numeric>
#include <boost/config.hpp>
#include <boost/algorithm/cxx11/iota.hpp>
#include <boost/algorithm/cxx17/transform_exclusive_scan.hpp>
#include "iterator_test.hpp"
#define BOOST_TEST_MAIN
#include <boost/test/unit_test.hpp>
namespace ba = boost::algorithm;
int triangle(int n) { return n*(n+1)/2; }
template <class _Tp>
struct identity
{
const _Tp& operator()(const _Tp& __x) const { return __x;}
};
template <class Iter1, class BOp, class UOp, class T, class Iter2>
void
test(Iter1 first, Iter1 last, BOp bop, UOp uop, T init, Iter2 rFirst, Iter2 rLast)
{
std::vector<typename std::iterator_traits<Iter1>::value_type> v;
// Test not in-place
ba::transform_exclusive_scan(first, last, std::back_inserter(v), init, bop, uop);
BOOST_CHECK(std::distance(rFirst, rLast) == v.size());
BOOST_CHECK(std::equal(v.begin(), v.end(), rFirst));
// Test in-place
v.clear();
v.assign(first, last);
ba::transform_exclusive_scan(v.begin(), v.end(), v.begin(), init, bop, uop);
BOOST_CHECK(std::distance(rFirst, rLast) == v.size());
BOOST_CHECK(std::equal(v.begin(), v.end(), rFirst));
}
template <class Iter>
void
test()
{
int ia[] = { 1, 3, 5, 7, 9};
const int pResI0[] = { 0, 1, 4, 9, 16}; // with identity
const int mResI0[] = { 0, 0, 0, 0, 0};
const int pResN0[] = { 0, -1, -4, -9, -16}; // with negate
const int mResN0[] = { 0, 0, 0, 0, 0};
const int pResI2[] = { 2, 3, 6, 11, 18}; // with identity
const int mResI2[] = { 2, 2, 6, 30, 210};
const int pResN2[] = { 2, 1, -2, -7, -14}; // with negate
const int mResN2[] = { 2, -2, 6, -30, 210};
const unsigned sa = sizeof(ia) / sizeof(ia[0]);
BOOST_CHECK(sa == sizeof(pResI0) / sizeof(pResI0[0])); // just to be sure
BOOST_CHECK(sa == sizeof(mResI0) / sizeof(mResI0[0])); // just to be sure
BOOST_CHECK(sa == sizeof(pResN0) / sizeof(pResN0[0])); // just to be sure
BOOST_CHECK(sa == sizeof(mResN0) / sizeof(mResN0[0])); // just to be sure
BOOST_CHECK(sa == sizeof(pResI2) / sizeof(pResI2[0])); // just to be sure
BOOST_CHECK(sa == sizeof(mResI2) / sizeof(mResI2[0])); // just to be sure
BOOST_CHECK(sa == sizeof(pResN2) / sizeof(pResN2[0])); // just to be sure
BOOST_CHECK(sa == sizeof(mResN2) / sizeof(mResN2[0])); // just to be sure
for (unsigned int i = 0; i < sa; ++i ) {
test(Iter(ia), Iter(ia + i), std::plus<int>(), identity<int>(), 0, pResI0, pResI0 + i);
test(Iter(ia), Iter(ia + i), std::multiplies<int>(), identity<int>(), 0, mResI0, mResI0 + i);
test(Iter(ia), Iter(ia + i), std::plus<int>(), std::negate<int>(), 0, pResN0, pResN0 + i);
test(Iter(ia), Iter(ia + i), std::multiplies<int>(), std::negate<int>(), 0, mResN0, mResN0 + i);
test(Iter(ia), Iter(ia + i), std::plus<int>(), identity<int>(), 2, pResI2, pResI2 + i);
test(Iter(ia), Iter(ia + i), std::multiplies<int>(), identity<int>(), 2, mResI2, mResI2 + i);
test(Iter(ia), Iter(ia + i), std::plus<int>(), std::negate<int>(), 2, pResN2, pResN2 + i);
test(Iter(ia), Iter(ia + i), std::multiplies<int>(), std::negate<int>(), 2, mResN2, mResN2 + i);
}
}
void basic_tests()
{
{
std::vector<int> v(10);
std::fill(v.begin(), v.end(), 3);
ba::transform_exclusive_scan(v.begin(), v.end(), v.begin(), 50, std::plus<int>(), identity<int>());
for (size_t i = 0; i < v.size(); ++i)
BOOST_CHECK(v[i] == 50 + (int) i * 3);
}
{
std::vector<int> v(10);
ba::iota(v.begin(), v.end(), 0);
ba::transform_exclusive_scan(v.begin(), v.end(), v.begin(), 30, std::plus<int>(), identity<int>());
for (size_t i = 0; i < v.size(); ++i)
BOOST_CHECK(v[i] == 30 + triangle(i-1));
}
{
std::vector<int> v(10);
ba::iota(v.begin(), v.end(), 1);
ba::transform_exclusive_scan(v.begin(), v.end(), v.begin(), 40, std::plus<int>(), identity<int>());
for (size_t i = 0; i < v.size(); ++i)
BOOST_CHECK(v[i] == 40 + triangle(i));
}
{
std::vector<int> v, res;
ba::transform_exclusive_scan(v.begin(), v.end(), std::back_inserter(res), 40, std::plus<int>(), identity<int>());
BOOST_CHECK(res.empty());
}
}
void test_transform_exclusive_scan_init()
{
basic_tests();
// All the iterator categories
test<input_iterator <const int*> >();
test<forward_iterator <const int*> >();
test<bidirectional_iterator<const int*> >();
test<random_access_iterator<const int*> >();
test<const int*>();
test< int*>();
}
BOOST_AUTO_TEST_CASE( test_main )
{
test_transform_exclusive_scan_init();
}

View File

@ -0,0 +1,233 @@
/*
Copyright (c) Marshall Clow 2017.
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)
For more information, see http://www.boost.org
*/
#include <vector>
#include <functional>
#include <numeric>
#include <boost/config.hpp>
#include <boost/algorithm/cxx11/iota.hpp>
#include <boost/algorithm/cxx17/transform_inclusive_scan.hpp>
#include "iterator_test.hpp"
#define BOOST_TEST_MAIN
#include <boost/test/unit_test.hpp>
namespace ba = boost::algorithm;
int triangle(int n) { return n*(n+1)/2; }
template <class _Tp>
struct identity
{
const _Tp& operator()(const _Tp& __x) const { return __x;}
};
template <class Iter1, class BOp, class UOp, class Iter2>
void
transform_inclusive_scan_test(Iter1 first, Iter1 last, BOp bop, UOp uop, Iter2 rFirst, Iter2 rLast)
{
std::vector<typename std::iterator_traits<Iter1>::value_type> v;
// Test not in-place
ba::transform_inclusive_scan(first, last, std::back_inserter(v), bop, uop);
BOOST_CHECK(std::distance(first, last) == v.size());
BOOST_CHECK(std::equal(v.begin(), v.end(), rFirst));
// Test in-place
v.clear();
v.assign(first, last);
ba::transform_inclusive_scan(v.begin(), v.end(), v.begin(), bop, uop);
BOOST_CHECK(std::distance(first, last) == v.size());
BOOST_CHECK(std::equal(v.begin(), v.end(), rFirst));
}
template <class Iter>
void
transform_inclusive_scan_test()
{
int ia[] = { 1, 3, 5, 7, 9};
const int pResI0[] = { 1, 4, 9, 16, 25}; // with identity
const int mResI0[] = { 1, 3, 15, 105, 945};
const int pResN0[] = { -1, -4, -9, -16, -25}; // with negate
const int mResN0[] = { -1, 3, -15, 105, -945};
const unsigned sa = sizeof(ia) / sizeof(ia[0]);
BOOST_CHECK(sa == sizeof(pResI0) / sizeof(pResI0[0])); // just to be sure
BOOST_CHECK(sa == sizeof(mResI0) / sizeof(mResI0[0])); // just to be sure
BOOST_CHECK(sa == sizeof(pResN0) / sizeof(pResN0[0])); // just to be sure
BOOST_CHECK(sa == sizeof(mResN0) / sizeof(mResN0[0])); // just to be sure
for (unsigned int i = 0; i < sa; ++i ) {
transform_inclusive_scan_test(Iter(ia), Iter(ia + i), std::plus<int>(), identity<int>(), pResI0, pResI0 + i);
transform_inclusive_scan_test(Iter(ia), Iter(ia + i), std::multiplies<int>(), identity<int>(), mResI0, mResI0 + i);
transform_inclusive_scan_test(Iter(ia), Iter(ia + i), std::plus<int>(), std::negate<int>(), pResN0, pResN0 + i);
transform_inclusive_scan_test(Iter(ia), Iter(ia + i), std::multiplies<int>(), std::negate<int>(), mResN0, mResN0 + i);
}
}
// Basic sanity
void basic_transform_inclusive_scan_tests()
{
{
std::vector<int> v(10);
std::fill(v.begin(), v.end(), 3);
ba::transform_inclusive_scan(v.begin(), v.end(), v.begin(), std::plus<int>(), identity<int>());
for (size_t i = 0; i < v.size(); ++i)
BOOST_CHECK(v[i] == (int)(i+1) * 3);
}
{
std::vector<int> v(10);
ba::iota(v.begin(), v.end(), 0);
ba::transform_inclusive_scan(v.begin(), v.end(), v.begin(), std::plus<int>(), identity<int>());
for (size_t i = 0; i < v.size(); ++i)
BOOST_CHECK(v[i] == triangle(i));
}
{
std::vector<int> v(10);
ba::iota(v.begin(), v.end(), 1);
ba::transform_inclusive_scan(v.begin(), v.end(), v.begin(), std::plus<int>(), identity<int>());
for (size_t i = 0; i < v.size(); ++i)
BOOST_CHECK(v[i] == triangle(i + 1));
}
{
std::vector<int> v, res;
ba::transform_inclusive_scan(v.begin(), v.end(), std::back_inserter(res), std::plus<int>(), identity<int>());
BOOST_CHECK(res.empty());
}
}
void test_transform_inclusive_scan()
{
basic_transform_inclusive_scan_tests();
// All the iterator categories
transform_inclusive_scan_test<input_iterator <const int*> >();
transform_inclusive_scan_test<forward_iterator <const int*> >();
transform_inclusive_scan_test<bidirectional_iterator<const int*> >();
transform_inclusive_scan_test<random_access_iterator<const int*> >();
transform_inclusive_scan_test<const int*>();
transform_inclusive_scan_test< int*>();
}
template <class Iter1, class BOp, class UOp, class T, class Iter2>
void
transform_inclusive_scan_init_test(Iter1 first, Iter1 last, BOp bop, UOp uop, T init, Iter2 rFirst, Iter2 rLast)
{
std::vector<typename std::iterator_traits<Iter1>::value_type> v;
// Test not in-place
ba::transform_inclusive_scan(first, last, std::back_inserter(v), bop, uop, init);
BOOST_CHECK(std::distance(rFirst, rLast) == v.size());
BOOST_CHECK(std::equal(v.begin(), v.end(), rFirst));
// Test in-place
v.clear();
v.assign(first, last);
ba::transform_inclusive_scan(v.begin(), v.end(), v.begin(), bop, uop, init);
BOOST_CHECK(std::distance(rFirst, rLast) == v.size());
BOOST_CHECK(std::equal(v.begin(), v.end(), rFirst));
}
template <class Iter>
void
transform_inclusive_scan_init_test()
{
int ia[] = { 1, 3, 5, 7, 9};
const int pResI0[] = { 1, 4, 9, 16, 25}; // with identity
const int mResI0[] = { 0, 0, 0, 0, 0};
const int pResN0[] = { -1, -4, -9, -16, -25}; // with negate
const int mResN0[] = { 0, 0, 0, 0, 0};
const int pResI2[] = { 3, 6, 11, 18, 27}; // with identity
const int mResI2[] = { 2, 6, 30, 210, 1890};
const int pResN2[] = { 1, -2, -7, -14, -23}; // with negate
const int mResN2[] = { -2, 6, -30, 210, -1890};
const unsigned sa = sizeof(ia) / sizeof(ia[0]);
BOOST_CHECK(sa == sizeof(pResI0) / sizeof(pResI0[0])); // just to be sure
BOOST_CHECK(sa == sizeof(mResI0) / sizeof(mResI0[0])); // just to be sure
BOOST_CHECK(sa == sizeof(pResN0) / sizeof(pResN0[0])); // just to be sure
BOOST_CHECK(sa == sizeof(mResN0) / sizeof(mResN0[0])); // just to be sure
BOOST_CHECK(sa == sizeof(pResI2) / sizeof(pResI2[0])); // just to be sure
BOOST_CHECK(sa == sizeof(mResI2) / sizeof(mResI2[0])); // just to be sure
BOOST_CHECK(sa == sizeof(pResN2) / sizeof(pResN2[0])); // just to be sure
BOOST_CHECK(sa == sizeof(mResN2) / sizeof(mResN2[0])); // just to be sure
for (unsigned int i = 0; i < sa; ++i ) {
transform_inclusive_scan_init_test(Iter(ia), Iter(ia + i), std::plus<int>(), identity<int>(), 0, pResI0, pResI0 + i);
transform_inclusive_scan_init_test(Iter(ia), Iter(ia + i), std::multiplies<int>(), identity<int>(), 0, mResI0, mResI0 + i);
transform_inclusive_scan_init_test(Iter(ia), Iter(ia + i), std::plus<int>(), std::negate<int>(), 0, pResN0, pResN0 + i);
transform_inclusive_scan_init_test(Iter(ia), Iter(ia + i), std::multiplies<int>(), std::negate<int>(), 0, mResN0, mResN0 + i);
transform_inclusive_scan_init_test(Iter(ia), Iter(ia + i), std::plus<int>(), identity<int>(), 2, pResI2, pResI2 + i);
transform_inclusive_scan_init_test(Iter(ia), Iter(ia + i), std::multiplies<int>(), identity<int>(), 2, mResI2, mResI2 + i);
transform_inclusive_scan_init_test(Iter(ia), Iter(ia + i), std::plus<int>(), std::negate<int>(), 2, pResN2, pResN2 + i);
transform_inclusive_scan_init_test(Iter(ia), Iter(ia + i), std::multiplies<int>(), std::negate<int>(), 2, mResN2, mResN2 + i);
}
}
// Basic sanity
void basic_transform_inclusive_scan_init_tests()
{
{
std::vector<int> v(10);
std::fill(v.begin(), v.end(), 3);
ba::transform_inclusive_scan(v.begin(), v.end(), v.begin(), std::plus<int>(), identity<int>(), 50);
for (size_t i = 0; i < v.size(); ++i)
BOOST_CHECK(v[i] == 50 + (int) (i + 1) * 3);
}
{
std::vector<int> v(10);
ba::iota(v.begin(), v.end(), 0);
ba::transform_inclusive_scan(v.begin(), v.end(), v.begin(), std::plus<int>(), identity<int>(), 30);
for (size_t i = 0; i < v.size(); ++i)
BOOST_CHECK(v[i] == 30 + triangle(i));
}
{
std::vector<int> v(10);
ba::iota(v.begin(), v.end(), 1);
ba::transform_inclusive_scan(v.begin(), v.end(), v.begin(), std::plus<int>(), identity<int>(), 40);
for (size_t i = 0; i < v.size(); ++i)
BOOST_CHECK(v[i] == 40 + triangle(i + 1));
}
{
std::vector<int> v, res;
ba::transform_inclusive_scan(v.begin(), v.end(), std::back_inserter(res), std::plus<int>(), identity<int>(), 1);
BOOST_CHECK(res.empty());
}
}
void test_transform_inclusive_scan_init()
{
basic_transform_inclusive_scan_init_tests();
// All the iterator categories
transform_inclusive_scan_init_test<input_iterator <const int*> >();
transform_inclusive_scan_init_test<forward_iterator <const int*> >();
transform_inclusive_scan_init_test<bidirectional_iterator<const int*> >();
transform_inclusive_scan_init_test<random_access_iterator<const int*> >();
transform_inclusive_scan_init_test<const int*>();
transform_inclusive_scan_init_test< int*>();
}
BOOST_AUTO_TEST_CASE( test_main )
{
test_transform_inclusive_scan();
test_transform_inclusive_scan_init();
}

View File

@ -0,0 +1,188 @@
/*
Copyright (c) Marshall Clow 2013.
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)
For more information, see http://www.boost.org
*/
#include <boost/config.hpp>
#include <boost/algorithm/cxx17/transform_reduce.hpp>
#include "iterator_test.hpp"
#define BOOST_TEST_MAIN
#include <boost/test/unit_test.hpp>
namespace ba = boost::algorithm;
template <class _Tp>
struct identity
{
const _Tp& operator()(const _Tp& __x) const { return __x;}
};
template <class _Tp>
struct twice
{
const _Tp operator()(const _Tp& __x) const { return 2 * __x; }
};
template <class Iter1, class T, class BOp, class UOp>
void
test_init_bop_uop(Iter1 first1, Iter1 last1, T init, BOp bOp, UOp uOp, T x)
{
BOOST_CHECK(ba::transform_reduce(first1, last1, init, bOp, uOp) == x);
}
template <class Iter>
void
test_init_bop_uop()
{
int ia[] = {1, 2, 3, 4, 5, 6};
unsigned sa = sizeof(ia) / sizeof(ia[0]);
test_init_bop_uop(Iter(ia), Iter(ia), 0, std::plus<int>(), identity<int>(), 0);
test_init_bop_uop(Iter(ia), Iter(ia), 1, std::multiplies<int>(), identity<int>(), 1);
test_init_bop_uop(Iter(ia), Iter(ia+1), 0, std::multiplies<int>(), identity<int>(), 0);
test_init_bop_uop(Iter(ia), Iter(ia+1), 2, std::plus<int>(), identity<int>(), 3);
test_init_bop_uop(Iter(ia), Iter(ia+2), 0, std::plus<int>(), identity<int>(), 3);
test_init_bop_uop(Iter(ia), Iter(ia+2), 3, std::multiplies<int>(), identity<int>(), 6);
test_init_bop_uop(Iter(ia), Iter(ia+sa), 4, std::multiplies<int>(), identity<int>(), 2880);
test_init_bop_uop(Iter(ia), Iter(ia+sa), 4, std::plus<int>(), identity<int>(), 25);
test_init_bop_uop(Iter(ia), Iter(ia), 0, std::plus<int>(), twice<int>(), 0);
test_init_bop_uop(Iter(ia), Iter(ia), 1, std::multiplies<int>(), twice<int>(), 1);
test_init_bop_uop(Iter(ia), Iter(ia+1), 0, std::multiplies<int>(), twice<int>(), 0);
test_init_bop_uop(Iter(ia), Iter(ia+1), 2, std::plus<int>(), twice<int>(), 4);
test_init_bop_uop(Iter(ia), Iter(ia+2), 0, std::plus<int>(), twice<int>(), 6);
test_init_bop_uop(Iter(ia), Iter(ia+2), 3, std::multiplies<int>(), twice<int>(), 24);
test_init_bop_uop(Iter(ia), Iter(ia+sa), 4, std::multiplies<int>(), twice<int>(), 184320); // 64 * 2880
test_init_bop_uop(Iter(ia), Iter(ia+sa), 4, std::plus<int>(), twice<int>(), 46);
}
void test_transform_reduce_init_bop_uop()
{
BOOST_CHECK ( true );
}
template <class Iter1, class Iter2, class T, class Op1, class Op2>
void
test_init_bop_bop(Iter1 first1, Iter1 last1, Iter2 first2, T init, Op1 op1, Op2 op2, T x)
{
BOOST_CHECK(ba::transform_reduce(first1, last1, first2, init, op1, op2) == x);
}
template <class SIter, class UIter>
void
test_init_bop_bop()
{
int ia[] = {1, 2, 3, 4, 5, 6};
unsigned int ua[] = {2, 4, 6, 8, 10,12};
unsigned sa = sizeof(ia) / sizeof(ia[0]);
BOOST_CHECK(sa == sizeof(ua) / sizeof(ua[0])); // just to be sure
test_init_bop_bop(SIter(ia), SIter(ia), UIter(ua), 0, std::plus<int>(), std::multiplies<int>(), 0);
test_init_bop_bop(UIter(ua), UIter(ua), SIter(ia), 1, std::multiplies<int>(), std::plus<int>(), 1);
test_init_bop_bop(SIter(ia), SIter(ia+1), UIter(ua), 0, std::multiplies<int>(), std::plus<int>(), 0);
test_init_bop_bop(UIter(ua), UIter(ua+1), SIter(ia), 2, std::plus<int>(), std::multiplies<int>(), 4);
test_init_bop_bop(SIter(ia), SIter(ia+2), UIter(ua), 0, std::plus<int>(), std::multiplies<int>(), 10);
test_init_bop_bop(UIter(ua), UIter(ua+2), SIter(ia), 3, std::multiplies<int>(), std::plus<int>(), 54);
test_init_bop_bop(SIter(ia), SIter(ia+sa), UIter(ua), 4, std::multiplies<int>(), std::plus<int>(), 2099520);
test_init_bop_bop(UIter(ua), UIter(ua+sa), SIter(ia), 4, std::plus<int>(), std::multiplies<int>(), 186);
}
void test_transform_reduce_init_bop_bop()
{
// All the iterator categories
test_init_bop_bop<input_iterator <const int*>, input_iterator <const unsigned int*> >();
test_init_bop_bop<input_iterator <const int*>, forward_iterator <const unsigned int*> >();
test_init_bop_bop<input_iterator <const int*>, bidirectional_iterator<const unsigned int*> >();
test_init_bop_bop<input_iterator <const int*>, random_access_iterator<const unsigned int*> >();
test_init_bop_bop<forward_iterator <const int*>, input_iterator <const unsigned int*> >();
test_init_bop_bop<forward_iterator <const int*>, forward_iterator <const unsigned int*> >();
test_init_bop_bop<forward_iterator <const int*>, bidirectional_iterator<const unsigned int*> >();
test_init_bop_bop<forward_iterator <const int*>, random_access_iterator<const unsigned int*> >();
test_init_bop_bop<bidirectional_iterator<const int*>, input_iterator <const unsigned int*> >();
test_init_bop_bop<bidirectional_iterator<const int*>, forward_iterator <const unsigned int*> >();
test_init_bop_bop<bidirectional_iterator<const int*>, bidirectional_iterator<const unsigned int*> >();
test_init_bop_bop<bidirectional_iterator<const int*>, random_access_iterator<const unsigned int*> >();
test_init_bop_bop<random_access_iterator<const int*>, input_iterator <const unsigned int*> >();
test_init_bop_bop<random_access_iterator<const int*>, forward_iterator <const unsigned int*> >();
test_init_bop_bop<random_access_iterator<const int*>, bidirectional_iterator<const unsigned int*> >();
test_init_bop_bop<random_access_iterator<const int*>, random_access_iterator<const unsigned int*> >();
// just plain pointers (const vs. non-const, too)
test_init_bop_bop<const int*, const unsigned int *>();
test_init_bop_bop<const int*, unsigned int *>();
test_init_bop_bop< int*, const unsigned int *>();
test_init_bop_bop< int*, unsigned int *>();
}
template <class Iter1, class Iter2, class T>
void
test_init(Iter1 first1, Iter1 last1, Iter2 first2, T init, T x)
{
BOOST_CHECK(ba::transform_reduce(first1, last1, first2, init) == x);
}
template <class SIter, class UIter>
void
test_init()
{
int ia[] = {1, 2, 3, 4, 5, 6};
unsigned int ua[] = {2, 4, 6, 8, 10,12};
unsigned sa = sizeof(ia) / sizeof(ia[0]);
BOOST_CHECK(sa == sizeof(ua) / sizeof(ua[0])); // just to be sure
test_init(SIter(ia), SIter(ia), UIter(ua), 0, 0);
test_init(UIter(ua), UIter(ua), SIter(ia), 1, 1);
test_init(SIter(ia), SIter(ia+1), UIter(ua), 0, 2);
test_init(UIter(ua), UIter(ua+1), SIter(ia), 2, 4);
test_init(SIter(ia), SIter(ia+2), UIter(ua), 0, 10);
test_init(UIter(ua), UIter(ua+2), SIter(ia), 3, 13);
test_init(SIter(ia), SIter(ia+sa), UIter(ua), 0, 182);
test_init(UIter(ua), UIter(ua+sa), SIter(ia), 4, 186);
}
void test_transform_reduce_init()
{
// All the iterator categories
test_init<input_iterator <const int*>, input_iterator <const unsigned int*> >();
test_init<input_iterator <const int*>, forward_iterator <const unsigned int*> >();
test_init<input_iterator <const int*>, bidirectional_iterator<const unsigned int*> >();
test_init<input_iterator <const int*>, random_access_iterator<const unsigned int*> >();
test_init<forward_iterator <const int*>, input_iterator <const unsigned int*> >();
test_init<forward_iterator <const int*>, forward_iterator <const unsigned int*> >();
test_init<forward_iterator <const int*>, bidirectional_iterator<const unsigned int*> >();
test_init<forward_iterator <const int*>, random_access_iterator<const unsigned int*> >();
test_init<bidirectional_iterator<const int*>, input_iterator <const unsigned int*> >();
test_init<bidirectional_iterator<const int*>, forward_iterator <const unsigned int*> >();
test_init<bidirectional_iterator<const int*>, bidirectional_iterator<const unsigned int*> >();
test_init<bidirectional_iterator<const int*>, random_access_iterator<const unsigned int*> >();
test_init<random_access_iterator<const int*>, input_iterator <const unsigned int*> >();
test_init<random_access_iterator<const int*>, forward_iterator <const unsigned int*> >();
test_init<random_access_iterator<const int*>, bidirectional_iterator<const unsigned int*> >();
test_init<random_access_iterator<const int*>, random_access_iterator<const unsigned int*> >();
// just plain pointers (const vs. non-const, too)
test_init<const int*, const unsigned int *>();
test_init<const int*, unsigned int *>();
test_init< int*, const unsigned int *>();
test_init< int*, unsigned int *>();
}
BOOST_AUTO_TEST_CASE( test_main )
{
test_transform_reduce_init();
test_transform_reduce_init_bop_uop();
test_transform_reduce_init_bop_bop();
}