Compare commits

...

74 Commits

Author SHA1 Message Date
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
055075aa61 Bug fixes and is_partititioned_XXX for the 1.65.0 release 2017-06-14 18:49:39 -07: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
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
d029402f05 Add, and update, documentation build targets. 2016-10-10 11:24:26 -05: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
24d2cceb27 Fix a couple of test failures for the 1.62.0 release 2016-08-17 18:11:47 -07:00
b3dabe10e4 Add missing error_info include 2016-08-17 13:54:58 -07:00
f1e9d3140c Made is_palindrome work for C++03 2016-08-17 13:49:16 -07:00
c5b41eba46 Merge to master for 1.62.0 release 2016-08-17 13:32:45 -07:00
18bcbba8f4 Merge pull request #22 from ZaMaZaN4iK/feature_branch/is_palindromic
Disabled 'is_palindrome' with 'const char*'
2016-08-16 18:19:15 -07:00
ff79a9c2db Updated tests 2016-08-16 16:22:01 +03:00
a53b0121b9 Updated documentation for is_palindrome 2016-08-16 16:20:22 +03:00
42bbfdeb4c Deleted support for nullptr, NULL and 0.
I think user shouldn't send to 'is_palindrome' zero, NULL or nullptr as parameter. As value of const char* it's possible, of course. But cases 'is_palindrome(0)', 'is_palindrome(NULL)' and 'is_palindrome(nullptr)' is silly and it should be restricted by design.
2016-08-16 16:18:21 +03:00
3c25ce1090 Added C-String support for 'is_palindrome'
Updated doc, example and tests.
2016-08-16 05:14:56 +03:00
0f5136de65 Merged branch feature_branch/is_palindromic into feature_branch/is_palindromic 2016-08-15 04:10:10 +03:00
774fb437f3 Disabled 'is_palindrome' for 'const char*' 2016-08-15 04:09:48 +03:00
f6d20e612b Merge pull request #20 from MarcelRaad/patch-1
Remove unused bad include; remind me in a week (after the tests have cycled), and I'll merge to master. Thanks!
2016-07-14 11:46:33 -07:00
71ed4a159f Remove unused bad include
boost/exception/errinfo_errno.hpp, included in boost/exception/all.hpp, pushes the warning level to 1 on MSVC, but emits a level 1 warning with /sdl. Only exception.hpp and throw_exception.hpp are necessary.
2016-07-14 11:36:47 +02:00
712f16d000 Merge pull request #19 from ZaMaZaN4iK/feature_branch/is_palindromic
Added `is_palindrome` from @ ZaMaZaN4iK
2016-07-11 08:27:48 -07:00
1a34a6935b [micro] Added "#include <functional>" 2016-07-11 18:26:18 +03:00
093900a8f3 [micro] Replaced constructor of singleElement
Replaced bracket initializes constructor to simply constructor with two arguments in std::vector
2016-07-11 18:25:04 +03:00
4fcb7afa84 Merge branch 'feature_branch/is_palindromic' of https://github.com/ZaMaZaN4iK/algorithm into feature_branch/is_palindromic 2016-07-10 22:53:57 +03:00
52f91139af Fix comments 2016-07-10 22:52:55 +03:00
fb964d72d7 Updated documentation for is_palindrome
Added two lines with examples.
2016-07-06 23:00:55 +03:00
366274ff0a Added new tests to is_palindrome_test
Added evenNonPalindrome and oddPalindrome tests.
2016-07-06 22:56:02 +03:00
1ec1cd3045 Fixed is_palindrome_test 2016-07-06 11:57:05 +03:00
9bee197bd8 Added all files to the new repository 2016-07-06 11:42:18 +03:00
5314d592e3 Fix up misleading indentation; Fixes https://svn.boost.org/trac/boost/ticket/12206 2016-05-16 07:47:09 -07:00
3aef0ab9ac Merge pull request #17 from kundor/develop
Remove some includes, fix some comments and docs. Thanks! Please remind me in a week or so to merge to master; after the tests have cycled.
2016-04-29 16:07:27 -07:00
c11878cd8a typo 2016-04-29 16:20:43 -04:00
e066bfae81 Fix documentation to reflect commit 4dac507 2016-04-29 16:12:49 -04:00
f06dc424dd In cxx14, remove unnecessary #include <algorithm> and correct some comments 2016-04-29 16:04:17 -04:00
1da90fcc4a Remove unnecessary #include <algorithm> and correct some comments 2016-04-29 15:37:09 -04:00
795c6c69e5 Removed doc comment for replace_all that said it returned something; Fixes https://svn.boost.org/trac/boost/ticket/12163 2016-04-28 08:47:47 -07:00
94bed863f0 Merge pull request #16 from aldonin/fix-minmax-example
Fix missing include <iterator>
2016-04-27 10:56:56 -07:00
352768cf66 Fix missing include <iterator> 2016-04-27 17:56:17 +03:00
dc2149fbc5 Merge pull request #15 from nigels-com/develop
Implement algorithm::hex_lower #7064 (rebased on develop)
2016-03-07 21:54:42 -08:00
3cedd051fa Test coverage for algorithm::hex_lower, adapting existing coverage for algorithm::hex 2016-03-08 09:47:52 +10:00
b7d46e6531 Another overload of algorithm::hex_lower as lower-case alternative to algorithm::hex 2016-03-08 09:47:41 +10:00
d558476f41 Implement algorithm::hex_lower as lower-case alternative to algorithm::hex 2016-03-08 09:47:35 +10:00
205f5ff4bb Update searchers to return a pair of iterators 2016-02-15 22:23:58 -08:00
782d7665dc Comment out unused parameter to silence a warning. Thanks to @meetingcpp for the report 2016-02-07 13:20:22 -05:00
61a4bd45fb Merge pull request #12 from trel/patch-1
comment typo fix
2015-12-14 10:56:18 -08:00
f646230db8 comment typo fix 2015-12-14 13:53:12 -05:00
1a79438687 New algorithm 'partition_subrange'. Name not 100% final, bue we've got tests. No docs yet. 2015-10-01 11:59:31 -07:00
1085f31e7e Add include to algorithm.hpp; this was causing a failure on MSVC 8.0 2015-09-29 07:20:16 -07:00
7a2ae62f22 Added new algorithm 'sort_subrange' from Sean Parent's CppCon keynote. Docs to come 2015-09-28 10:28:05 -07:00
4fbc56bae9 Merge pull request #11 from thtrummer/develop
Add missing include for std::multiplies and std::plus. Thanks to @thtrummer for the patch!
2015-05-17 11:55:43 -07:00
3acaddd044 Add missing include for std::multiplies and std::plus 2015-05-17 19:59:08 +02:00
7f7ebc36ed Fixed the docs for clamp. Refs #10081 2015-04-10 08:10:16 -07:00
77 changed files with 2924 additions and 268 deletions

View File

@ -44,3 +44,13 @@ boostbook standalone
<xsl:param>toc.max.depth=2
<xsl:param>generate.section.toc.level=1
;
###############################################################################
alias boostdoc
: ../string/doc/string_algo.xml
:
: <dependency>../string/doc//autodoc
: ;
explicit boostdoc ;
alias boostrelease : standalone ;
explicit boostrelease ;

View File

@ -66,6 +66,8 @@ Thanks to all the people who have reviewed this library and made suggestions for
[include clamp-hpp.qbk]
[include gather.qbk]
[include hex.qbk]
[include is_palindrome.qbk]
[include is_partitioned_until.qbk]
[endsect]

View File

@ -73,7 +73,7 @@ All of the variants of `all_of` and `all_of_equal` take their parameters by valu
[heading Notes]
* The routine `all_of` is part of the C++11 standard. When compiled using a C++11 implementation, the implementation from the standard library will be used.
* The routine `all_of` is also available as part of the C++11 standard.
* `all_of` and `all_of_equal` both return true for empty ranges, no matter what is passed to test against. When there are no items in the sequence to test, they all satisfy the condition to be tested against.

View File

@ -73,7 +73,7 @@ All of the variants of `any_of` and `any_of_equal` take their parameters by valu
[heading Notes]
* The routine `any_of` is part of the C++11 standard. When compiled using a C++11 implementation, the implementation from the standard library will be used.
* The routine `any_of` is also available as part of the C++11 standard.
* `any_of` and `any_of_equal` both return false for empty ranges, no matter what is passed to test against.

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]

View File

@ -27,11 +27,11 @@ Note: using `clamp` with floating point numbers may give unexpected results if o
There is also a version that allows the caller to specify a comparison predicate to use instead of `operator <`.
``
template<typename V>
V clamp ( V val, V lo, V hi );
template<typename T>
const T& clamp ( const T& val, const T& lo, const T& hi );
template<typename V, typename Pred>
V clamp ( V val, V lo, V hi, Pred p );
template<typename T, typename Pred>
const T& clamp ( const T& val, const T& lo, const T& hi, Pred p );
``
The following code: ``

98
doc/is_palindrome.qbk Normal file
View File

@ -0,0 +1,98 @@
[/ File is_palindrome.qbk]
[section:is_palindrome is_palindrome]
[/license
Copyright (c) 2016 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_palindrome.hpp' contains six variants of a single algorithm, is_palindrome.
The algorithm tests the sequence and returns true if the sequence is a palindrome; i.e, it is identical when traversed either backwards or frontwards.
The routine `is_palindrome` takes a sequence and, optionally, a predicate. It will return true if the predicate returns true for tested elements by algorithm in the sequence.
The routine come in 6 forms; the first one takes two iterators to define the range. The second form takes two iterators to define the range and a predicate.
The third form takes a single range parameter, and uses Boost.Range to traverse it. The fourth form takes a single range parameter ( uses Boost.Range to traverse it) and a predicate.
The fifth form takes a single C-string and a predicate. The sixth form takes a single C-string.
[heading interface]
The function `is_palindrome` returns true if the predicate returns true any tested by algorithm items in the sequence.
There are six versions:
1) takes two iterators.
2) takes two iterators and a predicate.
3) takes a range.
4) takes a range and a predicate.
5) takes a C-string and a predicate.
6) takes a C-string.
``
template<typename BidirectionalIterator>
bool is_palindrome ( BidirectionalIterator begin, BidirectionalIterator end );
template<typename BidirectionalIterator, typename Predicate>
bool is_palindrome ( BidirectionalIterator begin, BidirectionalIterator end, Predicate p );
template<typename Range>
bool is_palindrome ( const Range &r );
template<typename Range, typename Predicate>
bool is_palindrome ( const Range &r, Predicate p );
template<typename Predicate>
bool is_palindrome ( const char* str, Predicate p );
bool is_palindrome(const char* str);
``
[heading Examples]
Given the containers:
const std::list<int> empty,
const std::vector<char> singleElement{'z'},
int oddNonPalindrome[] = {3,2,2},
const int oddPalindrome[] = {1,2,3,2,1},
const int evenPalindrome[] = {1,2,2,1},
int evenNonPalindrome[] = {1,4,8,8}, then
``
is_palindrome(empty)) --> true //empty range
is_palindrome(singleElement)) --> true
is_palindrome(std::begin(oddNonPalindrome), std::end(oddNonPalindrome))) --> false
is_palindrome(std::begin(evenPalindrome), std::end(evenPalindrome))) --> true
is_palindrome(empty.begin(), empty.end(), functorComparator())) --> true //empty range
is_palindrome(std::begin(oddNonPalindrome), std::end(oddNonPalindrome), funcComparator<int>)) --> false
is_palindrome(std::begin(oddPalindrome), std::end(oddPalindrome)) --> true
is_palindrome(evenPalindrome, std::equal_to<int>())) --> true
is_palindrome(std::begin(evenNonPalindrome), std::end(evenNonPalindrome)) --> false
is_palindrome("a") --> true
is_palindrome("aba", std::equal_to<char>()) --> true
``
[heading Iterator Requirements]
`is_palindrome` work on Bidirectional and RandomAccess iterators.
[heading Complexity]
All of the variants of `is_palindrome` run in ['O(N)] (linear) time; that is, they compare against each element in the list once. If any of the comparisons not succeed, the algorithm will terminate immediately, without examining the remaining members of the sequence.
[heading Exception Safety]
All of the variants of `is_palindrome` take their parameters by value, const pointer 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_palindrome` returns true for empty ranges, const char* null pointers and for single element ranges.
* If you use version of 'is_palindrome' without custom predicate, 'is_palindrome' uses default 'operator==()' for elements.
* Be careful with using not null pointer 'const char*' without '\0' - if you use it with 'is_palindrome', it's a undefined behaviour.
[endsect]
[/ File is_palindrome.qbk
Copyright 2016 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

@ -55,9 +55,9 @@ Both of the variants of `is_partitioned` take their parameters by value or const
[heading Notes]
* The iterator-based version of the routine `is_partitioned` is part of the C++11 standard. When compiled using a C++11 implementation, the implementation from the standard library will be used.
* 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

@ -71,7 +71,7 @@ All of the variants of `is_permutation` take their parameters by value, and do n
[heading Notes]
* The three iterator versions of the routine `is_permutation` are part of the C++11 standard. When compiled using a C++11 implementation, the implementation from the standard library will be used.
* The three iterator versions of the routine `is_permutation` are also available as part of the C++11 standard.
* The four iterator versions of the routine `is_permutation` are part of the proposed C++14 standard. When C++14 standard libraries become available, the implementation should be changed to use the implementation from the standard library (if available).

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

@ -74,7 +74,7 @@ All of the variants of `none_of` and `none_of_equal` take their parameters by va
[heading Notes]
* The routine `none_of` is part of the C++11 standard. When compiled using a C++11 implementation, the implementation from the standard library will be used.
* The routine `none_of` is also available as part of the C++11 standard.
* `none_of` and `none_of_equal` both return true for empty ranges, no matter what is passed to test against.

View File

@ -54,7 +54,7 @@ Both of the variants of `partition_point` take their parameters by value or cons
[heading Notes]
* The iterator-based version of the routine `partition_point` is part of the C++11 standard. When compiled using a C++11 implementation, the implementation from the standard library will be used.
* The iterator-based version of the routine `partition_point` is also available as part of the C++11 standard.
* For empty ranges, the partition point is the end of the range.

View File

@ -20,3 +20,6 @@ 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;

View File

@ -0,0 +1,99 @@
/*
Copyright (c) Alexander Zaitsev <zamazan4ik@gmail.by>, 2016
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 <list>
#include <iterator>
#include <functional>
#include <iostream>
#include <boost/algorithm/is_palindrome.hpp>
namespace ba = boost::algorithm;
template <typename T>
bool funcComparator(const T& v1, const T& v2)
{
return v1 == v2;
}
struct functorComparator
{
template <typename T>
bool operator()(const T& v1, const T& v2) const
{
return v1 == v2;
}
};
int main ( int /*argc*/, char * /*argv*/ [] )
{
//You can this algorithm with iterators(minimum Bidirectional)
std::vector<int> vec{1,2,1};
if(ba::is_palindrome(vec.begin(), vec.end()))
std::cout << "This container is palindrome" << std::endl;
else
std::cout << "This container is not palindrome" << std::endl;
//Of course, you can use const iterators
if(ba::is_palindrome(vec.cbegin(), vec.cend()))
std::cout << "This container is palindrome" << std::endl;
else
std::cout << "This container is not palindrome" << std::endl;
//Example with bidirectional iterators
std::list<int> list{1,2,1};
if(ba::is_palindrome(list.begin(), list.end()))
std::cout << "This container is palindrome" << std::endl;
else
std::cout << "This container is not palindrome" << std::endl;
//You can use custom comparators like functions, functors, lambdas
auto lambdaComparator = [](int v1, int v2){ return v1 == v2; };
auto objFunc = std::function<bool(int, int)>(lambdaComparator);
if(ba::is_palindrome(vec.begin(), vec.end(), lambdaComparator))
std::cout << "This container is palindrome" << std::endl;
else
std::cout << "This container is not palindrome" << std::endl;
if(ba::is_palindrome(vec.begin(), vec.end(), funcComparator<int>))
std::cout << "This container is palindrome" << std::endl;
else
std::cout << "This container is not palindrome" << std::endl;
if(ba::is_palindrome(vec.begin(), vec.end(), functorComparator()))
std::cout << "This container is palindrome" << std::endl;
else
std::cout << "This container is not palindrome" << std::endl;
if(ba::is_palindrome(vec.begin(), vec.end(), objFunc))
std::cout << "This container is palindrome" << std::endl;
else
std::cout << "This container is not palindrome" << std::endl;
//You can use ranges
if(ba::is_palindrome(vec))
std::cout << "This container is palindrome" << std::endl;
else
std::cout << "This container is not palindrome" << std::endl;
//You can use C-strings
if(ba::is_palindrome("aba"))
std::cout << "This C-string is palindrome" << std::endl;
else
std::cout << "This C-string is not palindrome" << 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

@ -17,6 +17,8 @@
#ifndef BOOST_ALGORITHM_HPP
#define BOOST_ALGORITHM_HPP
#include <functional> // for plus and multiplies
#include <boost/utility/enable_if.hpp> // for boost::disable_if
#include <boost/type_traits/is_integral.hpp>
@ -56,7 +58,7 @@ power (T x, Integer n) {
/// \fn power ( T x, Integer n, Operation op )
/// \return the value "x" raised to the power "n"
/// using the operaton "op".
/// using the operation "op".
///
/// \param x The value to be exponentiated
/// \param n The exponent (must be >= 0)

View File

@ -12,7 +12,6 @@
#ifndef BOOST_ALGORITHM_ALL_OF_HPP
#define BOOST_ALGORITHM_ALL_OF_HPP
#include <algorithm> // for std::all_of, if available
#include <boost/range/begin.hpp>
#include <boost/range/end.hpp>
@ -27,8 +26,6 @@ namespace boost { namespace algorithm {
/// \param p A predicate for testing the elements of the sequence
///
/// \note This function is part of the C++2011 standard library.
/// We will use the standard one if it is available,
/// otherwise we have our own implementation.
template<typename InputIterator, typename Predicate>
bool all_of ( InputIterator first, InputIterator last, Predicate p )
{

View File

@ -14,7 +14,6 @@
#ifndef BOOST_ALGORITHM_ANY_OF_HPP
#define BOOST_ALGORITHM_ANY_OF_HPP
#include <algorithm> // for std::any_of, if available
#include <boost/range/begin.hpp>
#include <boost/range/end.hpp>

View File

@ -12,7 +12,7 @@
#ifndef BOOST_ALGORITHM_COPY_IF_HPP
#define BOOST_ALGORITHM_COPY_IF_HPP
#include <algorithm> // for std::copy_if, if available
#include <utility> // for std::pair, std::make_pair
#include <boost/range/begin.hpp>
#include <boost/range/end.hpp>
@ -28,8 +28,6 @@ namespace boost { namespace algorithm {
/// \param result An output iterator to write the results into
/// \param p A predicate for testing the elements of the range
/// \note This function is part of the C++2011 standard library.
/// We will use the standard one if it is available,
/// otherwise we have our own implementation.
template<typename InputIterator, typename OutputIterator, typename Predicate>
OutputIterator copy_if ( InputIterator first, InputIterator last, OutputIterator result, Predicate p )
{

View File

@ -12,8 +12,6 @@
#ifndef BOOST_ALGORITHM_COPY_N_HPP
#define BOOST_ALGORITHM_COPY_N_HPP
#include <algorithm> // for std::copy_n, if available
namespace boost { namespace algorithm {
/// \fn copy_n ( InputIterator first, Size n, OutputIterator result )
@ -25,8 +23,6 @@ namespace boost { namespace algorithm {
/// \param n The number of elements to copy
/// \param result An output iterator to write the results into
/// \note This function is part of the C++2011 standard library.
/// We will use the standard one if it is available,
/// otherwise we have our own implementation.
template <typename InputIterator, typename Size, typename OutputIterator>
OutputIterator copy_n ( InputIterator first, Size n, OutputIterator result )
{

View File

@ -12,8 +12,6 @@
#ifndef BOOST_ALGORITHM_FIND_IF_NOT_HPP
#define BOOST_ALGORITHM_FIND_IF_NOT_HPP
#include <algorithm> // for std::find_if_not, if it exists
#include <boost/range/begin.hpp>
#include <boost/range/end.hpp>
@ -27,8 +25,6 @@ namespace boost { namespace algorithm {
/// \param last One past the end of the input sequence
/// \param p A predicate for testing the elements of the range
/// \note This function is part of the C++2011 standard library.
/// We will use the standard one if it is available,
/// otherwise we have our own implementation.
template<typename InputIterator, typename Predicate>
InputIterator find_if_not ( InputIterator first, InputIterator last, Predicate p )
{

View File

@ -12,8 +12,6 @@
#ifndef BOOST_ALGORITHM_IOTA_HPP
#define BOOST_ALGORITHM_IOTA_HPP
#include <numeric>
#include <boost/range/begin.hpp>
#include <boost/range/end.hpp>
@ -26,8 +24,6 @@ namespace boost { namespace algorithm {
/// \param last One past the end of the input sequence
/// \param value The initial value of the sequence to be generated
/// \note This function is part of the C++2011 standard library.
/// We will use the standard one if it is available,
/// otherwise we have our own implementation.
template <typename ForwardIterator, typename T>
void iota ( ForwardIterator first, ForwardIterator last, T value )
{

View File

@ -12,22 +12,19 @@
#ifndef BOOST_ALGORITHM_IS_PARTITIONED_HPP
#define BOOST_ALGORITHM_IS_PARTITIONED_HPP
#include <algorithm> // for std::is_partitioned, if available
#include <boost/range/begin.hpp>
#include <boost/range/end.hpp>
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.
/// We will use the standard one if it is available,
/// otherwise we have our own implementation.
template <typename InputIterator, typename UnaryPredicate>
bool is_partitioned ( InputIterator first, InputIterator last, UnaryPredicate p )
{
@ -43,7 +40,8 @@ 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

View File

@ -12,8 +12,8 @@
#ifndef BOOST_ALGORITHM_IS_PERMUTATION11_HPP
#define BOOST_ALGORITHM_IS_PERMUTATION11_HPP
#include <algorithm> // for std::less, tie, mismatch and is_permutation (if available)
#include <utility> // for std::make_pair
#include <algorithm> // for std::find_if, count_if, mismatch
#include <utility> // for std::pair
#include <functional> // for std::equal_to
#include <iterator>
@ -108,8 +108,6 @@ namespace detail {
/// \param p The predicate to compare elements with
///
/// \note This function is part of the C++2011 standard library.
/// We will use the standard one if it is available,
/// otherwise we have our own implementation.
template< class ForwardIterator1, class ForwardIterator2, class BinaryPredicate >
bool is_permutation ( ForwardIterator1 first1, ForwardIterator1 last1,
ForwardIterator2 first2, BinaryPredicate p )
@ -135,8 +133,6 @@ bool is_permutation ( ForwardIterator1 first1, ForwardIterator1 last1,
/// \param last2 One past the end of the input sequence
/// \param first2 The start of the second sequence
/// \note This function is part of the C++2011 standard library.
/// We will use the standard one if it is available,
/// otherwise we have our own implementation.
template< class ForwardIterator1, class ForwardIterator2 >
bool is_permutation ( ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2 )
{

View File

@ -13,7 +13,6 @@
#ifndef BOOST_ALGORITHM_ORDERED_HPP
#define BOOST_ALGORITHM_ORDERED_HPP
#include <algorithm>
#include <functional>
#include <iterator>

View File

@ -12,7 +12,6 @@
#ifndef BOOST_ALGORITHM_NONE_OF_HPP
#define BOOST_ALGORITHM_NONE_OF_HPP
#include <algorithm> // for std::none_of, if available
#include <boost/range/begin.hpp>
#include <boost/range/end.hpp>
@ -29,7 +28,7 @@ namespace boost { namespace algorithm {
template<typename InputIterator, typename Predicate>
bool none_of ( InputIterator first, InputIterator last, Predicate p )
{
for ( ; first != last; ++first )
for ( ; first != last; ++first )
if ( p(*first))
return false;
return true;

View File

@ -12,8 +12,7 @@
#ifndef BOOST_ALGORITHM_PARTITION_COPY_HPP
#define BOOST_ALGORITHM_PARTITION_COPY_HPP
#include <algorithm> // for std::partition_copy, if available
#include <utility> // for make_pair
#include <utility> // for std::pair
#include <boost/range/begin.hpp>
#include <boost/range/end.hpp>
@ -34,8 +33,6 @@ namespace boost { namespace algorithm {
/// \param p A predicate for dividing the elements of the input sequence.
///
/// \note This function is part of the C++2011 standard library.
/// We will use the standard one if it is available,
/// otherwise we have our own implementation.
template <typename InputIterator,
typename OutputIterator1, typename OutputIterator2, typename UnaryPredicate>
std::pair<OutputIterator1, OutputIterator2>

View File

@ -12,7 +12,7 @@
#ifndef BOOST_ALGORITHM_PARTITION_POINT_HPP
#define BOOST_ALGORITHM_PARTITION_POINT_HPP
#include <algorithm> // for std::partition_point, if available
#include <iterator> // for std::distance, advance
#include <boost/range/begin.hpp>
#include <boost/range/end.hpp>
@ -27,8 +27,6 @@ namespace boost { namespace algorithm {
/// \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.
/// We will use the standard one if it is available,
/// otherwise we have our own implementation.
template <typename ForwardIterator, typename Predicate>
ForwardIterator partition_point ( ForwardIterator first, ForwardIterator last, Predicate p )
{

View File

@ -13,14 +13,14 @@
#define BOOST_ALGORITHM_EQUAL_HPP
#include <algorithm> // for std::equal
#include <functional> // for std::equal_to
#include <iterator>
namespace boost { namespace algorithm {
namespace detail {
template <class T1, class T2>
struct eq : public std::binary_function<T1, T2, bool> {
struct eq {
bool operator () ( const T1& v1, const T2& v2 ) const { return v1 == v2 ;}
};

View File

@ -12,8 +12,7 @@
#ifndef BOOST_ALGORITHM_IS_PERMUTATION14_HPP
#define BOOST_ALGORITHM_IS_PERMUTATION14_HPP
#include <algorithm> // for std::less, tie, mismatch and is_permutation (if available)
#include <utility> // for std::make_pair
#include <utility> // for std::pair
#include <functional> // for std::equal_to
#include <iterator>
@ -31,8 +30,6 @@ namespace boost { namespace algorithm {
/// \param first2 The start of the second sequence
/// \param last1 One past the end of the second sequence
/// \note This function is part of the C++2014 standard library.
/// We will use the standard one if it is available,
/// otherwise we have our own implementation.
template< class ForwardIterator1, class ForwardIterator2 >
bool is_permutation ( ForwardIterator1 first1, ForwardIterator1 last1,
ForwardIterator2 first2, ForwardIterator2 last2 )
@ -62,8 +59,6 @@ bool is_permutation ( ForwardIterator1 first1, ForwardIterator1 last1,
/// \param pred The predicate to compare elements with
///
/// \note This function is part of the C++2014 standard library.
/// We will use the standard one if it is available,
/// otherwise we have our own implementation.
template< class ForwardIterator1, class ForwardIterator2, class BinaryPredicate >
bool is_permutation ( ForwardIterator1 first1, ForwardIterator1 last1,
ForwardIterator2 first2, ForwardIterator2 last2,

View File

@ -12,7 +12,6 @@
#ifndef BOOST_ALGORITHM_MISMATCH_HPP
#define BOOST_ALGORITHM_MISMATCH_HPP
#include <algorithm> // for std::mismatch
#include <utility> // for std::pair
namespace boost { namespace algorithm {

View File

@ -0,0 +1,52 @@
/*
Copyright (c) Marshall Clow 2017.
Distributed under the Boost Software License, Version 1.0. (See accompanying
file LICENSE10.txt or copy at http://www.boost.org/LICENSE10.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 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 LICENSE10.txt or copy at http://www.boost.org/LICENSE10.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 LICENSE10.txt or copy at http://www.boost.org/LICENSE10.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 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 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 LICENSE10.txt or copy at http://www.boost.org/LICENSE10.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 reduce(first, last, init, std::plus<VT>());
}
template<class InputIterator>
typename std::iterator_traits<InputIterator>::value_type
reduce(InputIterator first, InputIterator last)
{
return reduce(first, last,
typename std::iterator_traits<InputIterator>::value_type());
}
template<class Range>
typename boost::range_value<Range>::type
reduce(const Range &r)
{
return 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 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 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 LICENSE10.txt or copy at http://www.boost.org/LICENSE10.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,58 @@
/*
Copyright (c) Marshall Clow 2017.
Distributed under the Boost Software License, Version 1.0. (See accompanying
file LICENSE10.txt or copy at http://www.boost.org/LICENSE10.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 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 LICENSE10.txt or copy at http://www.boost.org/LICENSE10.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 transform_reduce(first1, last1, first2, init,
std::plus<T>(), std::multiplies<T>());
}
}} // namespace boost and algorithm
#endif // BOOST_ALGORITHM_TRANSFORM_REDUCE_HPP

View File

@ -25,7 +25,9 @@
#include <boost/range/begin.hpp>
#include <boost/range/end.hpp>
#include <boost/exception/all.hpp>
#include <boost/exception/exception.hpp>
#include <boost/exception/info.hpp>
#include <boost/throw_exception.hpp>
#include <boost/utility/enable_if.hpp>
#include <boost/type_traits/is_integral.hpp>
@ -54,12 +56,12 @@ namespace detail {
/// \cond DOXYGEN_HIDE
template <typename T, typename OutputIterator>
OutputIterator encode_one ( T val, OutputIterator out ) {
OutputIterator encode_one ( T val, OutputIterator out, const char * hexDigits ) {
const std::size_t num_hex_digits = 2 * sizeof ( T );
char res [ num_hex_digits ];
char *p = res + num_hex_digits;
for ( std::size_t i = 0; i < num_hex_digits; ++i, val >>= 4 )
*--p = "0123456789ABCDEF" [ val & 0x0F ];
*--p = hexDigits [ val & 0x0F ];
return std::copy ( res, res + num_hex_digits, out );
}
@ -71,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.
@ -148,7 +150,24 @@ template <typename InputIterator, typename OutputIterator>
typename boost::enable_if<boost::is_integral<typename detail::hex_iterator_traits<InputIterator>::value_type>, OutputIterator>::type
hex ( InputIterator first, InputIterator last, OutputIterator out ) {
for ( ; first != last; ++first )
out = detail::encode_one ( *first, out );
out = detail::encode_one ( *first, out, "0123456789ABCDEF" );
return out;
}
/// \fn hex_lower ( InputIterator first, InputIterator last, OutputIterator out )
/// \brief Converts a sequence of integral types into a lower case hexadecimal sequence of characters.
///
/// \param first The start of the input sequence
/// \param last One past the end of the input sequence
/// \param out An output iterator to the results into
/// \return The updated output iterator
/// \note Based on the MySQL function of the same name
template <typename InputIterator, typename OutputIterator>
typename boost::enable_if<boost::is_integral<typename detail::hex_iterator_traits<InputIterator>::value_type>, OutputIterator>::type
hex_lower ( InputIterator first, InputIterator last, OutputIterator out ) {
for ( ; first != last; ++first )
out = detail::encode_one ( *first, out, "0123456789abcdef" );
return out;
}
@ -164,10 +183,27 @@ template <typename T, typename OutputIterator>
typename boost::enable_if<boost::is_integral<T>, OutputIterator>::type
hex ( const T *ptr, OutputIterator out ) {
while ( *ptr )
out = detail::encode_one ( *ptr++, out );
out = detail::encode_one ( *ptr++, out, "0123456789ABCDEF" );
return out;
}
/// \fn hex_lower ( const T *ptr, OutputIterator out )
/// \brief Converts a sequence of integral types into a lower case hexadecimal sequence of characters.
///
/// \param ptr A pointer to a 0-terminated sequence of data.
/// \param out An output iterator to the results into
/// \return The updated output iterator
/// \note Based on the MySQL function of the same name
template <typename T, typename OutputIterator>
typename boost::enable_if<boost::is_integral<T>, OutputIterator>::type
hex_lower ( const T *ptr, OutputIterator out ) {
while ( *ptr )
out = detail::encode_one ( *ptr++, out, "0123456789abcdef" );
return out;
}
/// \fn hex ( const Range &r, OutputIterator out )
/// \brief Converts a sequence of integral types into a hexadecimal sequence of characters.
///
@ -182,6 +218,20 @@ hex ( const Range &r, OutputIterator out ) {
}
/// \fn hex_lower ( const Range &r, OutputIterator out )
/// \brief Converts a sequence of integral types into a lower case hexadecimal sequence of characters.
///
/// \param r The input range
/// \param out An output iterator to the results into
/// \return The updated output iterator
/// \note Based on the MySQL function of the same name
template <typename Range, typename OutputIterator>
typename boost::enable_if<boost::is_integral<typename detail::hex_iterator_traits<typename Range::iterator>::value_type>, OutputIterator>::type
hex_lower ( const Range &r, OutputIterator out ) {
return hex_lower (boost::begin(r), boost::end(r), out);
}
/// \fn unhex ( InputIterator first, InputIterator last, OutputIterator out )
/// \brief Converts a sequence of hexadecimal characters into a sequence of integers.
///
@ -242,6 +292,21 @@ String hex ( const String &input ) {
return output;
}
/// \fn String hex_lower ( const String &input )
/// \brief Converts a sequence of integral types into a lower case hexadecimal sequence of characters.
///
/// \param input A container to be converted
/// \return A container with the encoded text
template<typename String>
String hex_lower ( const String &input ) {
String output;
output.reserve (input.size () * (2 * sizeof (typename String::value_type)));
(void) hex_lower (input, std::back_inserter (output));
return output;
}
/// \fn String unhex ( const String &input )
/// \brief Converts a sequence of hexadecimal characters into a sequence of characters.
///

View File

@ -0,0 +1,140 @@
/*
Copyright (c) Alexander Zaitsev <zamazan4ik@gmail.com>, 2016
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.
*/
/// \file is_palindrome.hpp
/// \brief Checks the input sequence on palindrome.
/// \author Alexander Zaitsev
#ifndef BOOST_ALGORITHM_IS_PALINDROME_HPP
#define BOOST_ALGORITHM_IS_PALINDROME_HPP
#include <iterator>
#include <functional>
#include <cstring>
#include <boost/range/begin.hpp>
#include <boost/range/end.hpp>
namespace boost { namespace algorithm {
/// \fn is_palindrome ( BidirectionalIterator begin, BidirectionalIterator end, Predicate p )
/// \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 p A predicate used to compare the values.
///
/// \note This function will return true for empty sequences and for palindromes.
/// For other sequences function will return false.
/// Complexity: O(N).
template <typename BidirectionalIterator, typename Predicate>
bool is_palindrome(BidirectionalIterator begin, BidirectionalIterator end, Predicate p)
{
if(begin == end)
{
return true;
}
--end;
while(begin != end)
{
if(!p(*begin, *end))
{
return false;
}
++begin;
if(begin == end)
{
break;
}
--end;
}
return true;
}
/// \fn is_palindrome ( BidirectionalIterator begin, BidirectionalIterator end )
/// \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
///
/// \note This function will return true for empty sequences and for palindromes.
/// For other sequences function will return false.
/// Complexity: O(N).
template <typename BidirectionalIterator>
bool is_palindrome(BidirectionalIterator begin, BidirectionalIterator end)
{
return is_palindrome(begin, end,
std::equal_to<typename std::iterator_traits<BidirectionalIterator>::value_type> ());
}
/// \fn is_palindrome ( const R& range )
/// \return true if the entire sequence is palindrome
///
/// \param range The range to be tested.
///
/// \note This function will return true for empty sequences and for palindromes.
/// For other sequences function will return false.
/// Complexity: O(N).
template <typename R>
bool is_palindrome(const R& range)
{
return is_palindrome(boost::begin(range), boost::end(range));
}
/// \fn is_palindrome ( const R& range, Predicate p )
/// \return true if the entire sequence is palindrome
///
/// \param range The range to be tested.
/// \param p A predicate used to compare the values.
///
/// \note This function will return true for empty sequences and for palindromes.
/// For other sequences function will return false.
/// Complexity: O(N).
template <typename R, typename Predicate>
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
///
/// \param str C-string to be tested.
///
/// \note This function will return true for empty sequences and for palindromes.
/// For other sequences function will return false.
/// Complexity: O(N).
bool is_palindrome(const char* str)
{
if(!str)
return true;
return is_palindrome(str, str + strlen(str));
}
/// \fn is_palindrome ( const char* str, Predicate p )
/// \return true if the entire sequence is palindrome
///
/// \param str C-string to be tested.
/// \param p A predicate used to compare the values.
///
/// \note This function will return true for empty sequences and for palindromes.
/// For other sequences function will return false.
/// Complexity: O(N).
template<typename Predicate>
bool is_palindrome(const char* str, Predicate p)
{
if(!str)
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

@ -75,25 +75,27 @@ Requirements:
/// \param corpus_last One past the end of the data to search
///
template <typename corpusIter>
corpusIter operator () ( corpusIter corpus_first, corpusIter corpus_last ) const {
std::pair<corpusIter, corpusIter>
operator () ( corpusIter corpus_first, corpusIter corpus_last ) const {
BOOST_STATIC_ASSERT (( boost::is_same<
typename std::iterator_traits<patIter>::value_type,
typename std::iterator_traits<corpusIter>::value_type>::value ));
if ( corpus_first == corpus_last ) return corpus_last; // if nothing to search, we didn't find it!
if ( pat_first == pat_last ) return corpus_first; // empty pattern matches at start
if ( corpus_first == corpus_last ) return std::make_pair(corpus_last, corpus_last); // if nothing to search, we didn't find it!
if ( pat_first == pat_last ) return std::make_pair(corpus_first, corpus_first); // empty pattern matches at start
const difference_type k_corpus_length = std::distance ( corpus_first, corpus_last );
// If the pattern is larger than the corpus, we can't find it!
if ( k_corpus_length < k_pattern_length )
return corpus_last;
return std::make_pair(corpus_last, corpus_last);
// Do the search
return this->do_search ( corpus_first, corpus_last );
}
template <typename Range>
typename boost::range_iterator<Range>::type operator () ( Range &r ) const {
std::pair<typename boost::range_iterator<Range>::type, typename boost::range_iterator<Range>::type>
operator () ( Range &r ) const {
return (*this) (boost::begin(r), boost::end(r));
}
@ -112,7 +114,8 @@ Requirements:
/// \param p A predicate used for the search comparisons.
///
template <typename corpusIter>
corpusIter do_search ( corpusIter corpus_first, corpusIter corpus_last ) const {
std::pair<corpusIter, corpusIter>
do_search ( corpusIter corpus_first, corpusIter corpus_last ) const {
/* ---- Do the matching ---- */
corpusIter curPos = corpus_first;
const corpusIter lastPos = corpus_last - k_pattern_length;
@ -126,7 +129,7 @@ Requirements:
j--;
// We matched - we're done!
if ( j == 0 )
return curPos;
return std::make_pair(curPos, curPos + k_pattern_length);
}
// Since we didn't match, figure out how far to skip forward
@ -138,7 +141,7 @@ Requirements:
curPos += suffix_ [ j ];
}
return corpus_last; // We didn't find anything
return std::make_pair(corpus_last, corpus_last); // We didn't find anything
}
@ -149,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 );
@ -158,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 );
@ -211,7 +214,7 @@ Requirements:
/// \param pat_last One past the end of the data to search for
///
template <typename patIter, typename corpusIter>
corpusIter boyer_moore_search (
std::pair<corpusIter, corpusIter> boyer_moore_search (
corpusIter corpus_first, corpusIter corpus_last,
patIter pat_first, patIter pat_last )
{
@ -220,7 +223,7 @@ Requirements:
}
template <typename PatternRange, typename corpusIter>
corpusIter boyer_moore_search (
std::pair<corpusIter, corpusIter> boyer_moore_search (
corpusIter corpus_first, corpusIter corpus_last, const PatternRange &pattern )
{
typedef typename boost::range_iterator<const PatternRange>::type pattern_iterator;
@ -229,8 +232,9 @@ Requirements:
}
template <typename patIter, typename CorpusRange>
typename boost::lazy_disable_if_c<
boost::is_same<CorpusRange, patIter>::value, typename boost::range_iterator<CorpusRange> >
typename boost::disable_if_c<
boost::is_same<CorpusRange, patIter>::value,
std::pair<typename boost::range_iterator<CorpusRange>::type, typename boost::range_iterator<CorpusRange>::type> >
::type
boyer_moore_search ( CorpusRange &corpus, patIter pat_first, patIter pat_last )
{
@ -239,7 +243,7 @@ Requirements:
}
template <typename PatternRange, typename CorpusRange>
typename boost::range_iterator<CorpusRange>::type
std::pair<typename boost::range_iterator<CorpusRange>::type, typename boost::range_iterator<CorpusRange>::type>
boyer_moore_search ( CorpusRange &corpus, const PatternRange &pattern )
{
typedef typename boost::range_iterator<const PatternRange>::type pattern_iterator;

View File

@ -64,33 +64,34 @@ http://www-igm.univ-mlv.fr/%7Elecroq/string/node18.html
~boyer_moore_horspool () {}
/// \fn operator ( corpusIter corpus_first, corpusIter corpus_last, Pred p )
/// \fn operator ( corpusIter corpus_first, corpusIter corpus_last)
/// \brief Searches the corpus for the pattern that was passed into the constructor
///
/// \param corpus_first The start of the data to search (Random Access Iterator)
/// \param corpus_last One past the end of the data to search
/// \param p A predicate used for the search comparisons.
///
template <typename corpusIter>
corpusIter operator () ( corpusIter corpus_first, corpusIter corpus_last ) const {
std::pair<corpusIter, corpusIter>
operator () ( corpusIter corpus_first, corpusIter corpus_last ) const {
BOOST_STATIC_ASSERT (( boost::is_same<
typename std::iterator_traits<patIter>::value_type,
typename std::iterator_traits<corpusIter>::value_type>::value ));
if ( corpus_first == corpus_last ) return corpus_last; // if nothing to search, we didn't find it!
if ( pat_first == pat_last ) return corpus_first; // empty pattern matches at start
if ( corpus_first == corpus_last ) return std::make_pair(corpus_last, corpus_last); // if nothing to search, we didn't find it!
if ( pat_first == pat_last ) return std::make_pair(corpus_first, corpus_first); // empty pattern matches at start
const difference_type k_corpus_length = std::distance ( corpus_first, corpus_last );
// If the pattern is larger than the corpus, we can't find it!
if ( k_corpus_length < k_pattern_length )
return corpus_last;
return std::make_pair(corpus_last, corpus_last);
// Do the search
return this->do_search ( corpus_first, corpus_last );
}
template <typename Range>
typename boost::range_iterator<Range>::type operator () ( Range &r ) const {
std::pair<typename boost::range_iterator<Range>::type, typename boost::range_iterator<Range>::type>
operator () ( Range &r ) const {
return (*this) (boost::begin(r), boost::end(r));
}
@ -108,7 +109,8 @@ http://www-igm.univ-mlv.fr/%7Elecroq/string/node18.html
/// \param k_corpus_length The length of the corpus to search
///
template <typename corpusIter>
corpusIter do_search ( corpusIter corpus_first, corpusIter corpus_last ) const {
std::pair<corpusIter, corpusIter>
do_search ( corpusIter corpus_first, corpusIter corpus_last ) const {
corpusIter curPos = corpus_first;
const corpusIter lastPos = corpus_last - k_pattern_length;
while ( curPos <= lastPos ) {
@ -117,14 +119,14 @@ http://www-igm.univ-mlv.fr/%7Elecroq/string/node18.html
while ( pat_first [j] == curPos [j] ) {
// We matched - we're done!
if ( j == 0 )
return curPos;
return std::make_pair(curPos, curPos + k_pattern_length);
j--;
}
curPos += skip_ [ curPos [ k_pattern_length - 1 ]];
}
return corpus_last;
return std::make_pair(corpus_last, corpus_last);
}
// \endcond
};
@ -142,7 +144,7 @@ http://www-igm.univ-mlv.fr/%7Elecroq/string/node18.html
/// \param pat_last One past the end of the data to search for
///
template <typename patIter, typename corpusIter>
corpusIter boyer_moore_horspool_search (
std::pair<corpusIter, corpusIter> boyer_moore_horspool_search (
corpusIter corpus_first, corpusIter corpus_last,
patIter pat_first, patIter pat_last )
{
@ -151,7 +153,7 @@ http://www-igm.univ-mlv.fr/%7Elecroq/string/node18.html
}
template <typename PatternRange, typename corpusIter>
corpusIter boyer_moore_horspool_search (
std::pair<corpusIter, corpusIter> boyer_moore_horspool_search (
corpusIter corpus_first, corpusIter corpus_last, const PatternRange &pattern )
{
typedef typename boost::range_iterator<const PatternRange>::type pattern_iterator;
@ -160,8 +162,9 @@ http://www-igm.univ-mlv.fr/%7Elecroq/string/node18.html
}
template <typename patIter, typename CorpusRange>
typename boost::lazy_disable_if_c<
boost::is_same<CorpusRange, patIter>::value, typename boost::range_iterator<CorpusRange> >
typename boost::disable_if_c<
boost::is_same<CorpusRange, patIter>::value,
std::pair<typename boost::range_iterator<CorpusRange>::type, typename boost::range_iterator<CorpusRange>::type> >
::type
boyer_moore_horspool_search ( CorpusRange &corpus, patIter pat_first, patIter pat_last )
{
@ -170,7 +173,7 @@ http://www-igm.univ-mlv.fr/%7Elecroq/string/node18.html
}
template <typename PatternRange, typename CorpusRange>
typename boost::range_iterator<CorpusRange>::type
std::pair<typename boost::range_iterator<CorpusRange>::type, typename boost::range_iterator<CorpusRange>::type>
boyer_moore_horspool_search ( CorpusRange &corpus, const PatternRange &pattern )
{
typedef typename boost::range_iterator<const PatternRange>::type pattern_iterator;

View File

@ -79,7 +79,7 @@ namespace boost { namespace algorithm { namespace detail {
skip_map skip_;
const value_type k_default_value;
public:
skip_table ( std::size_t patSize, value_type default_value ) : k_default_value ( default_value ) {
skip_table ( std::size_t /*patSize*/, value_type default_value ) : k_default_value ( default_value ) {
std::fill_n ( skip_.begin(), skip_.size(), default_value );
}

View File

@ -69,23 +69,26 @@ namespace boost { namespace algorithm {
/// \param p A predicate used for the search comparisons.
///
template <typename corpusIter>
corpusIter operator () ( corpusIter corpus_first, corpusIter corpus_last ) const {
std::pair<corpusIter, corpusIter>
operator () ( corpusIter corpus_first, corpusIter corpus_last ) const {
BOOST_STATIC_ASSERT (( boost::is_same<
typename std::iterator_traits<patIter>::value_type,
typename std::iterator_traits<corpusIter>::value_type>::value ));
if ( corpus_first == corpus_last ) return corpus_last; // if nothing to search, we didn't find it!
if ( pat_first == pat_last ) return corpus_first; // empty pattern matches at start
if ( corpus_first == corpus_last ) return std::make_pair(corpus_last, corpus_last); // if nothing to search, we didn't find it!
if ( pat_first == pat_last ) return std::make_pair(corpus_first, corpus_first); // empty pattern matches at start
const difference_type k_corpus_length = std::distance ( corpus_first, corpus_last );
// If the pattern is larger than the corpus, we can't find it!
if ( k_corpus_length < k_pattern_length )
return corpus_last;
return std::make_pair(corpus_last, corpus_last);
return do_search ( corpus_first, corpus_last, k_corpus_length );
}
template <typename Range>
typename boost::range_iterator<Range>::type operator () ( Range &r ) const {
std::pair<typename boost::range_iterator<Range>::type, typename boost::range_iterator<Range>::type>
operator () ( Range &r ) const {
return (*this) (boost::begin(r), boost::end(r));
}
@ -103,7 +106,8 @@ namespace boost { namespace algorithm {
/// \param p A predicate used for the search comparisons.
///
template <typename corpusIter>
corpusIter do_search ( corpusIter corpus_first, corpusIter corpus_last,
std::pair<corpusIter, corpusIter>
do_search ( corpusIter corpus_first, corpusIter corpus_last,
difference_type k_corpus_length ) const {
difference_type match_start = 0; // position in the corpus that we're matching
@ -135,7 +139,7 @@ namespace boost { namespace algorithm {
while ( match_start <= last_match ) {
while ( pat_first [ idx ] == corpus_first [ match_start + idx ] ) {
if ( ++idx == k_pattern_length )
return corpus_first + match_start;
return std::make_pair(corpus_first + match_start, corpus_first + match_start + k_pattern_length);
}
// Figure out where to start searching again
// assert ( idx - skip_ [ idx ] > 0 ); // we're always moving forward
@ -146,14 +150,14 @@ namespace boost { namespace algorithm {
#endif
// We didn't find anything
return corpus_last;
return std::make_pair(corpus_last, corpus_last);
}
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;
@ -173,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 ];
@ -202,7 +206,7 @@ namespace boost { namespace algorithm {
/// \param pat_last One past the end of the data to search for
///
template <typename patIter, typename corpusIter>
corpusIter knuth_morris_pratt_search (
std::pair<corpusIter, corpusIter> knuth_morris_pratt_search (
corpusIter corpus_first, corpusIter corpus_last,
patIter pat_first, patIter pat_last )
{
@ -211,7 +215,7 @@ namespace boost { namespace algorithm {
}
template <typename PatternRange, typename corpusIter>
corpusIter knuth_morris_pratt_search (
std::pair<corpusIter, corpusIter> knuth_morris_pratt_search (
corpusIter corpus_first, corpusIter corpus_last, const PatternRange &pattern )
{
typedef typename boost::range_iterator<const PatternRange>::type pattern_iterator;
@ -220,8 +224,9 @@ namespace boost { namespace algorithm {
}
template <typename patIter, typename CorpusRange>
typename boost::lazy_disable_if_c<
boost::is_same<CorpusRange, patIter>::value, typename boost::range_iterator<CorpusRange> >
typename boost::disable_if_c<
boost::is_same<CorpusRange, patIter>::value,
std::pair<typename boost::range_iterator<CorpusRange>::type, typename boost::range_iterator<CorpusRange>::type> >
::type
knuth_morris_pratt_search ( CorpusRange &corpus, patIter pat_first, patIter pat_last )
{
@ -230,7 +235,7 @@ namespace boost { namespace algorithm {
}
template <typename PatternRange, typename CorpusRange>
typename boost::range_iterator<CorpusRange>::type
std::pair<typename boost::range_iterator<CorpusRange>::type, typename boost::range_iterator<CorpusRange>::type>
knuth_morris_pratt_search ( CorpusRange &corpus, const PatternRange &pattern )
{
typedef typename boost::range_iterator<const PatternRange>::type pattern_iterator;

View File

@ -0,0 +1,109 @@
/*
Copyright (c) Marshall Clow 2008-2012.
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)
Revision history:
28 Sep 2015 mtc First version
*/
/// \file sort_subrange.hpp
/// \brief Sort a subrange
/// \author Marshall Clow
///
/// Suggested by Sean Parent in his CppCon 2015 keynote
#ifndef BOOST_ALGORITHM_SORT_SUBRANGE_HPP
#define BOOST_ALGORITHM_SORT_SUBRANGE_HPP
#include <functional> // For std::less
#include <iterator> // For std::iterator_traits
#include <algorithm> // For nth_element and partial_sort
#include <boost/range/begin.hpp>
#include <boost/range/end.hpp>
namespace boost { namespace algorithm {
/// \fn sort_subrange ( T const& val,
/// Iterator first, Iterator last,
/// Iterator sub_first, Iterator sub_last,
/// Pred p )
/// \brief Sort the subrange [sub_first, sub_last) that is inside
/// the range [first, last) as if you had sorted the entire range.
///
/// \param first The start of the larger range
/// \param last The end of the larger range
/// \param sub_first The start of the sub range
/// \param sub_last The end of the sub range
/// \param p A predicate to use to compare the values.
/// p ( a, b ) returns a boolean.
///
template<typename Iterator, typename Pred>
void sort_subrange (
Iterator first, Iterator last,
Iterator sub_first, Iterator sub_last,
Pred p)
{
if (sub_first == sub_last) return; // the empty sub-range is already sorted.
if (sub_first != first) { // sub-range is at the start, don't need to partition
(void) std::nth_element(first, sub_first, last, p);
++sub_first;
}
std::partial_sort(sub_first, sub_last, last, p);
}
template<typename Iterator>
void sort_subrange (Iterator first, Iterator last, Iterator sub_first, Iterator sub_last)
{
typedef typename std::iterator_traits<Iterator>::value_type value_type;
return sort_subrange(first, last, sub_first, sub_last, std::less<value_type>());
}
/// range versions?
/// \fn partition_subrange ( T const& val,
/// Iterator first, Iterator last,
/// Iterator sub_first, Iterator sub_last,
/// Pred p )
/// \brief Gather the elements of the subrange [sub_first, sub_last) that is
/// inside the range [first, last) as if you had sorted the entire range.
///
/// \param first The start of the larger range
/// \param last The end of the larger range
/// \param sub_first The start of the sub range
/// \param sub_last The end of the sub range
/// \param p A predicate to use to compare the values.
/// p ( a, b ) returns a boolean.
///
template<typename Iterator, typename Pred>
void partition_subrange (
Iterator first, Iterator last,
Iterator sub_first, Iterator sub_last,
Pred p)
{
if (sub_first != first) {
(void) std::nth_element(first, sub_first, last, p);
++sub_first;
}
if (sub_last != last)
(void) std::nth_element(sub_first, sub_last, last, p);
}
template<typename Iterator>
void partition_subrange (Iterator first, Iterator last, Iterator sub_first, Iterator sub_last)
{
typedef typename std::iterator_traits<Iterator>::value_type value_type;
return partition_subrange(first, last, sub_first, sub_last, std::less<value_type>());
}
}}
#endif // BOOST_ALGORITHM_SORT_SUBRANGE_HPP

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

@ -401,7 +401,6 @@ namespace boost {
\param Search A substring to be searched for
\param Format A substitute string
\param Loc A locale used for case insensitive comparison
\return A reference to the modified input
*/
template<typename SequenceT, typename Range1T, typename Range2T>
inline void ireplace_last(
@ -643,7 +642,6 @@ namespace boost {
\param Input An input string
\param Search A substring to be searched for
\param Format A substitute string
\return A reference to the modified input
*/
template<typename SequenceT, typename Range1T, typename Range2T>
inline void replace_all(

View File

@ -8,6 +8,7 @@
#include <cstdlib>
#include <cassert>
#include <iostream>
#include <iterator>
#include <boost/algorithm/minmax.hpp>
#include <boost/algorithm/minmax_element.hpp>

View File

@ -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 ]
@ -64,6 +74,15 @@ alias unit_test_framework
[ run gather_test1.cpp unit_test_framework : : : : gather_test1 ]
[ compile-fail gather_fail1.cpp ]
# SortSubrange tests
[ run sort_subrange_test.cpp unit_test_framework : : : : sort_subrange_test ]
[ run partition_subrange_test.cpp unit_test_framework : : : : partition_subrange_test ]
# 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 ]
;
}

View File

@ -18,7 +18,7 @@
#include <list>
template<typename T>
struct is_ : public std::unary_function<T, bool> {
struct is_ {
is_ ( T v ) : val_ ( v ) {}
~is_ () {}
bool operator () ( T comp ) const { return val_ == comp; }

View File

@ -18,7 +18,7 @@
#include <list>
template<typename T>
struct is_ : public std::unary_function<T, bool> {
struct is_ {
is_ ( T v ) : val_ ( v ) {}
~is_ () {}
bool operator () ( T comp ) const { return val_ == comp; }

View File

@ -26,56 +26,56 @@ BOOST_AUTO_TEST_CASE( test_main )
BOOST_CHECK (
boost::algorithm::boyer_moore_search (
cs.begin (), cs.end (), estr.begin (), estr.end ())
== cs.begin ()
== std::make_pair(cs.begin(), cs.begin())
);
BOOST_CHECK (
boost::algorithm::boyer_moore_horspool_search (
cs.begin (), cs.end (), estr.begin (), estr.end ())
== cs.begin ()
== std::make_pair(cs.begin(), cs.begin())
);
BOOST_CHECK (
boost::algorithm::knuth_morris_pratt_search (
cs.begin (), cs.end (), estr.begin (), estr.end ())
== cs.begin ()
== std::make_pair(cs.begin(), cs.begin())
);
// empty corpus, non-empty pattern
BOOST_CHECK (
boost::algorithm::boyer_moore_search (
estr.begin (), estr.end (), str.begin (), str.end ())
== estr.end ()
== std::make_pair(estr.end(), estr.end())
);
BOOST_CHECK (
boost::algorithm::boyer_moore_horspool_search (
estr.begin (), estr.end (), str.begin (), str.end ())
== estr.end ()
== std::make_pair(estr.end(), estr.end())
);
BOOST_CHECK (
boost::algorithm::knuth_morris_pratt_search (
estr.begin (), estr.end (), str.begin (), str.end ())
== estr.end ()
== std::make_pair(estr.end(), estr.end())
);
// non-empty corpus, empty pattern
BOOST_CHECK (
boost::algorithm::boyer_moore_search (
str.begin (), str.end (), estr.begin (), estr.end ())
== str.begin ()
== std::make_pair(str.begin(), str.begin())
);
BOOST_CHECK (
boost::algorithm::boyer_moore_horspool_search (
str.begin (), str.end (), estr.begin (), estr.end ())
== str.begin ()
== std::make_pair(str.begin(), str.begin())
);
BOOST_CHECK (
boost::algorithm::knuth_morris_pratt_search (
str.begin (), str.end (), estr.begin (), estr.end ())
== str.begin ()
== std::make_pair(str.begin(), str.begin())
);
}

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();
}

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

@ -9,6 +9,7 @@
#include <boost/config.hpp>
#include <boost/algorithm/hex.hpp>
#include <boost/algorithm/string/case_conv.hpp>
#define BOOST_TEST_MAIN
#include <boost/test/unit_test.hpp>
@ -42,6 +43,31 @@ void test_to_hex ( const typename String::value_type ** tests ) {
}
}
template<typename String>
void test_to_hex_lower ( const typename String::value_type ** tests ) {
for ( const typename String::value_type **p = tests; *p; p++ ) {
String arg, argh, one, two, three, four;
arg.assign ( *p );
boost::algorithm::hex_lower ( *p, std::back_inserter ( one ));
boost::algorithm::hex_lower ( arg, std::back_inserter ( two ));
boost::algorithm::hex_lower ( arg.begin (), arg.end (), std::back_inserter ( three ));
four = boost::algorithm::hex_lower ( arg );
BOOST_CHECK ( one == two );
BOOST_CHECK ( one == three );
BOOST_CHECK ( one == four );
argh = one;
one.clear (); two.clear (); three.clear (); four.clear ();
boost::algorithm::unhex ( argh.c_str (), std::back_inserter ( one ));
boost::algorithm::unhex ( argh, std::back_inserter ( two ));
boost::algorithm::unhex ( argh.begin (), argh.end (), std::back_inserter ( three ));
four = boost::algorithm::unhex ( argh );
BOOST_CHECK ( one == two );
BOOST_CHECK ( one == three );
BOOST_CHECK ( one == four );
BOOST_CHECK ( one == arg );
}
}
template<typename String>
void test_from_hex_success ( const typename String::value_type ** tests ) {
@ -61,6 +87,11 @@ void test_from_hex_success ( const typename String::value_type ** tests ) {
boost::algorithm::hex ( argh, std::back_inserter ( two ));
boost::algorithm::hex ( argh.begin (), argh.end (), std::back_inserter ( three ));
four = boost::algorithm::hex ( argh );
boost::algorithm::to_lower( arg );
boost::algorithm::to_lower( one );
boost::algorithm::to_lower( two );
boost::algorithm::to_lower( three );
boost::algorithm::to_lower( four );
BOOST_CHECK ( one == two );
BOOST_CHECK ( one == three );
BOOST_CHECK ( one == four );
@ -113,6 +144,7 @@ const wchar_t *tohex_w [] = {
const char *fromhex [] = {
"20",
"2122234556FF",
"2122234556ff",
NULL // End of the list
};
@ -120,6 +152,7 @@ const char *fromhex [] = {
const wchar_t *fromhex_w [] = {
L"00101020",
L"2122234556FF3456",
L"2122234556ff3456",
NULL // End of the list
};
@ -129,6 +162,8 @@ const char *fromhex_fail [] = {
"H",
"234",
"21222G4556FF",
"h",
"21222g4556ff",
NULL // End of the list
};
@ -139,6 +174,8 @@ const wchar_t *fromhex_fail_w [] = {
L"H",
L"234",
L"21222G4556FF",
L"h",
L"21222g4556ff",
NULL // End of the list
};
@ -146,10 +183,12 @@ const wchar_t *fromhex_fail_w [] = {
BOOST_AUTO_TEST_CASE( test_main )
{
test_to_hex<std::string> ( tohex );
test_to_hex_lower<std::string> ( tohex );
test_from_hex_success<std::string> ( fromhex );
test_from_hex_failure<std::string> ( fromhex_fail );
test_to_hex<std::wstring> ( tohex_w );
test_to_hex_lower<std::wstring> ( tohex_w );
test_from_hex_success<std::wstring> ( fromhex_w );
test_from_hex_failure<std::wstring> ( fromhex_fail_w );
}

View File

@ -11,6 +11,7 @@ Try ostream_iterators
#include <boost/config.hpp>
#include <boost/algorithm/hex.hpp>
#include <boost/exception/get_error_info.hpp>
#define BOOST_TEST_MAIN
#include <boost/test/unit_test.hpp>

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

@ -0,0 +1,76 @@
/*
Copyright (c) Alexander Zaitsev <zamazan4ik@gmail.com>, 2016
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 <boost/config.hpp>
#include <boost/algorithm/is_palindrome.hpp>
#define BOOST_TEST_MAIN
#include <boost/test/unit_test.hpp>
#include <algorithm>
#include <iostream>
#include <list>
#include <vector>
namespace ba = boost::algorithm;
template <typename T>
bool funcComparator(const T& v1, const T& v2)
{
return v1 == v2;
}
struct functorComparator
{
template <typename T>
bool operator()(const T& v1, const T& v2) const
{
return v1 == v2;
}
};
void test_is_palindrome()
{
const std::list<int> empty;
const std::vector<char> singleElement(1, 'z');
int oddNonPalindrome[] = {3,2,2};
const int oddPalindrome[] = {1,2,3,2,1};
const int evenPalindrome[] = {1,2,2,1};
int evenNonPalindrome[] = {1,4,8,8};
const char* stringNullPtr = NULL;
// Test a default operator==
BOOST_CHECK ( ba::is_palindrome(empty));
BOOST_CHECK ( ba::is_palindrome(singleElement));
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(boost::begin(oddNonPalindrome), boost::end(oddNonPalindrome), funcComparator<int>));
BOOST_CHECK ( ba::is_palindrome(evenPalindrome, std::equal_to<int>()));
//Test C-strings like cases
BOOST_CHECK ( ba::is_palindrome(stringNullPtr));
BOOST_CHECK ( ba::is_palindrome(""));
BOOST_CHECK ( ba::is_palindrome("a"));
BOOST_CHECK ( ba::is_palindrome("abacaba", std::equal_to<char>()));
BOOST_CHECK ( ba::is_palindrome("abba"));
BOOST_CHECK (!ba::is_palindrome("acab"));
}
BOOST_AUTO_TEST_CASE( test_main )
{
test_is_palindrome ();
}

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

@ -18,7 +18,7 @@
#include <list>
template<typename T>
struct is_ : public std::unary_function<T, bool> {
struct is_ {
is_ ( T v ) : val_ ( v ) {}
~is_ () {}
bool operator () ( T comp ) const { return val_ == comp; }

View File

@ -18,7 +18,7 @@
#include <list>
template<typename T>
struct is_ : public std::unary_function<T, bool> {
struct is_ {
is_ ( T v ) : val_ ( v ) {}
~is_ () {}
bool operator () ( T comp ) const { return val_ == comp; }

View File

@ -0,0 +1,170 @@
#include <boost/config.hpp>
#include <boost/algorithm/sort_subrange.hpp>
#include <boost/algorithm/cxx11/is_sorted.hpp>
#define BOOST_TEST_MAIN
#include <boost/test/unit_test.hpp>
#include <vector>
#include <iostream>
#if __cplusplus >= 201103L
#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>
void check_sequence ( Iter first, Iter last, Iter sf, Iter sl )
{
// for (Iter i = first; i < last; ++i) {
// if (i != first) std::cout << ' ';
// if (i == sf) std::cout << ">";
// std::cout << *i;
// if (i == sl) std::cout << "<";
// }
// if (sl == last) std::cout << "<";
// std::cout << '\n';
if (sf == sl) return;
for (Iter i = first; i < sf; ++i)
BOOST_CHECK(*i < *sf);
for (Iter i = sf; i < sl; ++i) {
if (first != sf) // if there is an element before the subrange
BOOST_CHECK(*i > *(sf-1));
if (last != sl) // if there is an element after the subrange
BOOST_CHECK(*i < *sl);
}
for (Iter i = sl; i < last; ++i)
BOOST_CHECK(*(sl-1) < *i);
}
template <typename Iter, typename Pred>
void check_sequence ( Iter first, Iter last, Iter sf, Iter sl, Pred p )
{
if (sf == sl) return;
for (Iter i = first; i < sf; ++i)
BOOST_CHECK(p(*i, *sf));
for (Iter i = sf; i < sl; ++i) {
if (first != sf) // if there is an element before the subrange
BOOST_CHECK(p(*(sf-1), *i));
if (last != sl) // if there is an element after the subrange
BOOST_CHECK(p(*i, *sl));
}
for (Iter i = sl; i < last; ++i)
BOOST_CHECK(p(*(sl-1), *i));
}
// for ( int i = 0; i < v.size(); ++i )
// std::cout << v[i] << ' ';
// std::cout << std::endl;
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);
// 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
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
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());
// BOOST_CHECK_EQUAL(v[7], 7);
// BOOST_CHECK_EQUAL(v[8], 8);
// BOOST_CHECK_EQUAL(v[9], 9);
// Mix them up and try again - at the beginning
do_shuffle(v.begin(), v.end());
ba::partition_subrange(b, v.end(), b, b + 2);
check_sequence (b, v.end(), b, b + 2);
// BOOST_CHECK_EQUAL(v[0], 0);
// BOOST_CHECK_EQUAL(v[1], 1);
// Mix them up and try again - empty subrange
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
do_shuffle(v.begin(), v.end());
ba::partition_subrange(b, v.end(), b, v.end());
check_sequence (b, v.end(), b, v.end());
}
{
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>());
// BOOST_CHECK_EQUAL(v[3], 6);
// BOOST_CHECK_EQUAL(v[4], 5);
// BOOST_CHECK_EQUAL(v[5], 4);
// Mix them up and try again - single element
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
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>());
// BOOST_CHECK_EQUAL(v[7], 2);
// BOOST_CHECK_EQUAL(v[8], 1);
// BOOST_CHECK_EQUAL(v[9], 0);
// Mix them up and try again - at the beginning
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>());
// BOOST_CHECK_EQUAL(v[0], 9);
// BOOST_CHECK_EQUAL(v[1], 8);
// Mix them up and try again - empty subrange
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
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

@ -8,6 +8,7 @@
*/
#include <iostream>
#include <functional>
#include <boost/config.hpp>
#include <boost/algorithm/algorithm.hpp>

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

@ -34,6 +34,7 @@ namespace {
template<typename Container>
void check_one_iter ( const Container &haystack, const std::string &needle, int expected ) {
typedef typename Container::const_iterator iter_type;
typedef typename std::pair<iter_type, iter_type> ret_type;
typedef std::string::const_iterator pattern_type;
iter_type hBeg = haystack.begin ();
@ -41,33 +42,40 @@ namespace {
pattern_type nBeg = needle.begin ();
pattern_type nEnd = needle.end ();
// iter_type ret0 = std::search (hBeg, hEnd, nBeg, nEnd);
ret_type ret1 = ba::boyer_moore_search (hBeg, hEnd, nBeg, nEnd);
ret_type ret1r = ba::boyer_moore_search (haystack, nBeg, nEnd);
ret_type ret2 = ba::boyer_moore_horspool_search (hBeg, hEnd, nBeg, nEnd);
ret_type ret3 = ba::knuth_morris_pratt_search (hBeg, hEnd, nBeg, nEnd);
iter_type it0 = std::search (hBeg, hEnd, nBeg, nEnd);
iter_type it1 = ba::boyer_moore_search (hBeg, hEnd, nBeg, nEnd);
iter_type it1r = ba::boyer_moore_search (haystack, nBeg, nEnd);
iter_type it2 = ba::boyer_moore_horspool_search (hBeg, hEnd, nBeg, nEnd);
iter_type it3 = ba::knuth_morris_pratt_search (hBeg, hEnd, nBeg, nEnd);
const int dist = it1 == hEnd ? -1 : std::distance ( hBeg, it1 );
// iter_type it1 = ret1.first;
// iter_type it1r = ret1r.first;
// iter_type it2 = ret2.first;
// iter_type it3 = ret3.first;
const int dist = ret1.first == hEnd ? -1 : std::distance ( hBeg, ret1.first );
std::cout << "(Iterators) Pattern is " << needle.length () << ", haysstack is " << haystack.length () << " chars long; " << std::endl;
try {
if ( it0 != it1 ) {
if ( it0 != ret1.first ) {
throw std::runtime_error (
std::string ( "results mismatch between std::search and boyer-moore search" ));
}
if ( it1 != it1r ) {
if ( ret1.first != ret1r.first || ret1.second != ret1r.second ) {
throw std::runtime_error (
std::string ( "results mismatch between iterator and range boyer_moore search" ));
}
if ( it1 != it2 ) {
if ( ret1.first != ret2.first || ret1.second != ret2.second ) {
throw std::runtime_error (
std::string ( "results mismatch between boyer-moore and boyer-moore-horspool search" ));
}
if ( it1 != it3 )
if ( ret1.first != ret3.first || ret1.second != ret3.second ) {
throw std::runtime_error (
std::string ( "results mismatch between boyer-moore and knuth-morris-pratt search" ));
}
}
@ -75,10 +83,10 @@ namespace {
std::cout << "Searching for: " << needle << std::endl;
std::cout << "Expected: " << expected << "\n";
std::cout << " std: " << std::distance ( hBeg, it0 ) << "\n";
std::cout << " bm: " << std::distance ( hBeg, it1 ) << "\n";
std::cout << " bm(r): " << std::distance ( hBeg, it1r ) << "\n";
std::cout << " bmh: " << std::distance ( hBeg, it2 ) << "\n";
std::cout << " kpm: " << std::distance ( hBeg, it3 )<< "\n";
std::cout << " bm: " << std::distance ( hBeg, ret1.first ) << "\n";
std::cout << " bm(r): " << std::distance ( hBeg, ret1r.first ) << "\n";
std::cout << " bmh: " << std::distance ( hBeg, ret2.first ) << "\n";
std::cout << " kpm: " << std::distance ( hBeg, ret3.first )<< "\n";
std::cout << std::flush;
throw ;
}
@ -91,32 +99,35 @@ namespace {
template<typename Container>
void check_one_pointer ( const Container &haystack, const std::string &needle, int expected ) {
typedef const typename Container::value_type *ptr_type;
typedef typename std::pair<ptr_type, ptr_type> ret_type;
ptr_type hBeg = haystack.size () == 0 ? NULL : &*haystack.begin ();
ptr_type hEnd = hBeg + haystack.size ();
ptr_type nBeg = needle.size () == 0 ? NULL : &*needle.begin ();
ptr_type nEnd = nBeg + needle.size ();
ptr_type it0 = std::search (hBeg, hEnd, nBeg, nEnd);
ptr_type it1 = ba::boyer_moore_search (hBeg, hEnd, nBeg, nEnd);
ptr_type it2 = ba::boyer_moore_horspool_search (hBeg, hEnd, nBeg, nEnd);
ptr_type it3 = ba::knuth_morris_pratt_search (hBeg, hEnd, nBeg, nEnd);
const int dist = it1 == hEnd ? -1 : std::distance ( hBeg, it1 );
ret_type ret1 = ba::boyer_moore_search (hBeg, hEnd, nBeg, nEnd);
ret_type ret2 = ba::boyer_moore_horspool_search (hBeg, hEnd, nBeg, nEnd);
ret_type ret3 = ba::knuth_morris_pratt_search (hBeg, hEnd, nBeg, nEnd);
const int dist = ret1.first == hEnd ? -1 : std::distance ( hBeg, ret1.first );
std::cout << "(Pointers) Pattern is " << needle.length () << ", haysstack is " << haystack.length () << " chars long; " << std::endl;
try {
if ( it0 != it1 ) {
if ( it0 != ret1.first ) {
throw std::runtime_error (
std::string ( "results mismatch between std::search and boyer-moore search" ));
}
if ( it1 != it2 ) {
if ( ret1.first != ret2.first || ret1.second != ret2.second ) {
throw std::runtime_error (
std::string ( "results mismatch between boyer-moore and boyer-moore-horspool search" ));
}
if ( it1 != it3 )
if ( ret1.first != ret3.first || ret1.second != ret3.second ) {
throw std::runtime_error (
std::string ( "results mismatch between boyer-moore and knuth-morris-pratt search" ));
}
}
@ -124,9 +135,9 @@ namespace {
std::cout << "Searching for: " << needle << std::endl;
std::cout << "Expected: " << expected << "\n";
std::cout << " std: " << std::distance ( hBeg, it0 ) << "\n";
std::cout << " bm: " << std::distance ( hBeg, it1 ) << "\n";
std::cout << " bmh: " << std::distance ( hBeg, it2 ) << "\n";
std::cout << " kpm: " << std::distance ( hBeg, it3 )<< "\n";
std::cout << " bm: " << std::distance ( hBeg, ret1.first ) << "\n";
std::cout << " bmh: " << std::distance ( hBeg, ret2.first ) << "\n";
std::cout << " kpm: " << std::distance ( hBeg, ret3.first )<< "\n";
std::cout << std::flush;
throw ;
}
@ -138,6 +149,7 @@ namespace {
template<typename Container>
void check_one_object ( const Container &haystack, const std::string &needle, int expected ) {
typedef typename Container::const_iterator iter_type;
typedef typename std::pair<iter_type, iter_type> ret_type;
typedef std::string::const_iterator pattern_type;
iter_type hBeg = haystack.begin ();
@ -151,44 +163,45 @@ namespace {
ba::knuth_morris_pratt<pattern_type> kmp ( nBeg, nEnd );
iter_type it0 = std::search (hBeg, hEnd, nBeg, nEnd);
iter_type it1 = bm (hBeg, hEnd);
iter_type it1r = bm (haystack);
iter_type rt1 = bm_r (hBeg, hEnd);
iter_type rt1r = bm_r (haystack);
iter_type it2 = bmh (hBeg, hEnd);
iter_type it3 = kmp (hBeg, hEnd);
const int dist = it1 == hEnd ? -1 : std::distance ( hBeg, it1 );
ret_type ret1 = bm (hBeg, hEnd);
ret_type ret1r = bm (haystack);
ret_type retr1 = bm_r (hBeg, hEnd);
ret_type retr1r = bm_r (haystack);
ret_type ret2 = bmh (hBeg, hEnd);
ret_type ret3 = kmp (hBeg, hEnd);
const int dist = ret1.first == hEnd ? -1 : std::distance ( hBeg, ret1.first );
std::cout << "(Objects) Pattern is " << needle.length () << ", haysstack is " << haystack.length () << " chars long; " << std::endl;
try {
if ( it0 != it1 ) {
if ( it0 != ret1.first ) {
throw std::runtime_error (
std::string ( "results mismatch between std::search and boyer-moore search" ));
}
if ( it1 != it1r ) {
if ( ret1.first != ret1r.first || ret1.second != ret1r.second ) {
throw std::runtime_error (
std::string ( "results mismatch between iterator and range boyer_moore search(1)" ));
}
if ( it1 != rt1 ) {
if ( ret1.first != retr1.first || ret1.second != retr1.second ) {
throw std::runtime_error (
std::string ( "results mismatch between iterator and range boyer_moore search(2)" ));
}
if ( rt1 != rt1r ) {
if ( ret1.first != retr1r.first || ret1.second != retr1r.second ) {
throw std::runtime_error (
std::string ( "results mismatch between iterator and range boyer_moore search(3)" ));
}
if ( it1 != it2 ) {
if ( ret1.first != ret2.first || ret1.second != ret2.second ) {
throw std::runtime_error (
std::string ( "results mismatch between boyer-moore and boyer-moore-horspool search" ));
}
if ( it1 != it3 )
if ( ret1.first != ret3.first || ret1.second != ret3.second ) {
throw std::runtime_error (
std::string ( "results mismatch between boyer-moore and knuth-morris-pratt search" ));
}
}
@ -196,12 +209,12 @@ namespace {
std::cout << "Searching for: " << needle << std::endl;
std::cout << "Expected: " << expected << "\n";
std::cout << " std: " << std::distance ( hBeg, it0 ) << "\n";
std::cout << " bm: " << std::distance ( hBeg, it1 ) << "\n";
std::cout << " bm(r1): " << std::distance ( hBeg, it1r ) << "\n";
std::cout << " bm(r2): " << std::distance ( hBeg, rt1 ) << "\n";
std::cout << " bm(r3): " << std::distance ( hBeg, rt1r ) << "\n";
std::cout << " bmh: " << std::distance ( hBeg, it2 ) << "\n";
std::cout << " kpm: " << std::distance ( hBeg, it3 )<< "\n";
std::cout << " bm: " << std::distance ( hBeg, ret1.first ) << "\n";
std::cout << " bm(r1): " << std::distance ( hBeg, ret1r.first ) << "\n";
std::cout << " bm(r2): " << std::distance ( hBeg, retr1.first ) << "\n";
std::cout << " bm(r3): " << std::distance ( hBeg, retr1r.first ) << "\n";
std::cout << " bmh: " << std::distance ( hBeg, ret2.first ) << "\n";
std::cout << " kpm: " << std::distance ( hBeg, ret3.first )<< "\n";
std::cout << std::flush;
throw ;
}

View File

@ -33,8 +33,8 @@ typedef std::vector<char> vec;
needle.begin (), needle.end ()); \
if ( res != exp ) { \
std::cout << "On run # " << i << " expected " \
<< exp - haystack.begin () << " got " \
<< res - haystack.begin () << std::endl; \
<< exp.first - haystack.begin () << " got " \
<< res.first - haystack.begin () << std::endl; \
throw std::runtime_error \
( "Unexpected result from " #call ); \
} \
@ -51,8 +51,8 @@ typedef std::vector<char> vec;
res = s_o ( haystack.begin (), haystack.end ()); \
if ( res != exp ) { \
std::cout << "On run # " << i << " expected " \
<< exp - haystack.begin () << " got " \
<< res - haystack.begin () << std::endl; \
<< exp.first - haystack.begin () << " got " \
<< res.first - haystack.begin () << std::endl; \
throw std::runtime_error \
( "Unexpected result from " #obj " object" ); \
} \
@ -90,27 +90,33 @@ namespace {
std::clock_t sTime;
unsigned long stdDiff;
vec::const_iterator res;
vec::const_iterator exp; // the expected result
std::pair<vec::const_iterator, vec::const_iterator> res;
std::pair<vec::const_iterator, vec::const_iterator> exp; // the expected result
vec::const_iterator exp_start;
if ( expected >= 0 )
exp = haystack.begin () + expected;
exp_start = haystack.begin () + expected;
else if ( expected == -1 )
exp = haystack.end (); // we didn't find it!
exp_start = haystack.end (); // we didn't find it!
else if ( expected == -2 )
exp = std::search ( haystack.begin (), haystack.end (), needle.begin (), needle.end ());
exp_start = std::search ( haystack.begin (), haystack.end (), needle.begin (), needle.end ());
else
throw std::logic_error ( "Expected must be -2, -1, or >= 0" );
if ( expected == -1 )
exp = std::make_pair(haystack.end(), haystack.end());
else
exp = std::make_pair(exp_start, exp_start + needle.size());
std::cout << "Pattern is " << needle.size () << " entries long" << std::endl;
std::cout << "Corpus is " << haystack.size () << " entries long" << std::endl;
// First, the std library search
sTime = std::clock ();
for ( i = 0; i < NUM_TRIES; ++i ) {
res = std::search ( haystack.begin (), haystack.end (), needle.begin (), needle.end ());
if ( res != exp ) {
std::cout << "On run # " << i << " expected " << exp - haystack.begin () << " got " << res - haystack.begin () << std::endl;
vec::const_iterator s_res = std::search ( haystack.begin (), haystack.end (), needle.begin (), needle.end ());
if ( s_res != exp.first ) {
std::cout << "On run # " << i << " expected " << exp.first - haystack.begin () << " got " << s_res - haystack.begin () << std::endl;
throw std::runtime_error ( "Unexpected result from std::search" );
}
}

View File

@ -34,8 +34,8 @@ typedef std::vector<std::string> vec;
needle.begin (), needle.end ()); \
if ( res != exp ) { \
std::cout << "On run # " << i << " expected " \
<< exp - haystack.begin () << " got " \
<< res - haystack.begin () << std::endl; \
<< exp.first - haystack.begin () << " got " \
<< res.first - haystack.begin () << std::endl; \
throw std::runtime_error \
( "Unexpected result from " #call ); \
} \
@ -52,8 +52,8 @@ typedef std::vector<std::string> vec;
res = s_o ( haystack.begin (), haystack.end ()); \
if ( res != exp ) { \
std::cout << "On run # " << i << " expected " \
<< exp - haystack.begin () << " got " \
<< res - haystack.begin () << std::endl; \
<< exp.first - haystack.begin () << " got " \
<< res.first - haystack.begin () << std::endl; \
throw std::runtime_error \
( "Unexpected result from " #obj " object" ); \
} \
@ -90,27 +90,33 @@ namespace {
std::clock_t sTime;
unsigned long stdDiff;
vec::const_iterator res;
vec::const_iterator exp; // the expected result
std::pair<vec::const_iterator, vec::const_iterator> res;
std::pair<vec::const_iterator, vec::const_iterator> exp; // the expected result
vec::const_iterator exp_start;
if ( expected >= 0 )
exp = haystack.begin () + expected;
exp_start = haystack.begin () + expected;
else if ( expected == -1 )
exp = haystack.end (); // we didn't find it1
exp_start = haystack.end (); // we didn't find it1
else if ( expected == -2 )
exp = std::search ( haystack.begin (), haystack.end (), needle.begin (), needle.end ());
exp_start = std::search ( haystack.begin (), haystack.end (), needle.begin (), needle.end ());
else
throw std::logic_error ( "Expected must be -2, -1, or >= 0" );
if ( expected == -1 )
exp = std::make_pair(haystack.end(), haystack.end());
else
exp = std::make_pair(exp_start, exp_start + needle.size());
std::cout << "Pattern is " << needle.size () << " entries long" << std::endl;
std::cout << "Corpus is " << haystack.size () << " entries long" << std::endl;
// First, the std library search
sTime = std::clock ();
for ( i = 0; i < NUM_TRIES; ++i ) {
res = std::search ( haystack.begin (), haystack.end (), needle.begin (), needle.end ());
if ( res != exp ) {
std::cout << "On run # " << i << " expected " << exp - haystack.begin () << " got " << res - haystack.begin () << std::endl;
vec::const_iterator s_res = std::search ( haystack.begin (), haystack.end (), needle.begin (), needle.end ());
if ( s_res != exp.first ) {
std::cout << "On run # " << i << " expected " << exp.first - haystack.begin () << " got " << s_res - haystack.begin () << std::endl;
throw std::runtime_error ( "Unexpected result from std::search" );
}
}

View File

@ -30,8 +30,8 @@ typedef std::vector<std::string> vec;
res = boost::algorithm::call ( haystack, needle ); \
if ( res != exp ) { \
std::cout << "Expected " \
<< exp - haystack.begin () << " got " \
<< res - haystack.begin () << std::endl; \
<< exp.first - haystack.begin () << " got " \
<< res.first - haystack.begin () << std::endl; \
throw std::runtime_error \
( "Unexpected result from " #call ); \
} \
@ -43,8 +43,8 @@ typedef std::vector<std::string> vec;
res = s_o ( haystack ); \
if ( res != exp ) { \
std::cout << "Expected " \
<< exp - haystack.begin () << " got " \
<< res - haystack.begin () << std::endl; \
<< exp.first - haystack.begin () << " got " \
<< res.first - haystack.begin () << std::endl; \
throw std::runtime_error \
( "Unexpected result from " #obj " object" ); \
} \
@ -64,25 +64,31 @@ namespace {
void check_one ( const vec &haystack, const vec &needle, int expected ) {
vec::const_iterator res;
vec::const_iterator exp; // the expected result
std::pair<vec::const_iterator, vec::const_iterator> res;
std::pair<vec::const_iterator, vec::const_iterator> exp; // the expected result
vec::const_iterator exp_start;
if ( expected >= 0 )
exp = haystack.begin () + expected;
exp_start = haystack.begin () + expected;
else if ( expected == -1 )
exp = haystack.end (); // we didn't find it1
exp_start = haystack.end (); // we didn't find it1
else if ( expected == -2 )
exp = std::search ( haystack.begin (), haystack.end (), needle.begin (), needle.end ());
exp_start = std::search ( haystack.begin (), haystack.end (), needle.begin (), needle.end ());
else
throw std::logic_error ( "Expected must be -2, -1, or >= 0" );
if ( expected == -1 )
exp = std::make_pair(haystack.end(), haystack.end());
else
exp = std::make_pair(exp_start, exp_start + needle.size());
std::cout << "Pattern is " << needle.size () << " entries long" << std::endl;
std::cout << "Corpus is " << haystack.size () << " entries long" << std::endl;
// First, the std library search
res = std::search ( haystack.begin (), haystack.end (), needle.begin (), needle.end ());
if ( res != exp ) {
std::cout << "Expected " << exp - haystack.begin () << " got " << res - haystack.begin () << std::endl;
vec::const_iterator s_res = std::search ( haystack.begin (), haystack.end (), needle.begin (), needle.end ());
if ( s_res != exp.first ) {
std::cout << "Expected " << exp.first - haystack.begin () << " got " << s_res - haystack.begin () << std::endl;
throw std::runtime_error ( "Unexpected result from std::search" );
}

151
test/sort_subrange_test.cpp Normal file
View File

@ -0,0 +1,151 @@
#include <boost/config.hpp>
#include <boost/algorithm/sort_subrange.hpp>
#include <boost/algorithm/cxx11/is_sorted.hpp>
#define BOOST_TEST_MAIN
#include <boost/test/unit_test.hpp>
#include <vector>
#include <iostream>
#if __cplusplus >= 201103L
#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>
void check_sequence ( Iter first, Iter last, Iter sf, Iter sl )
{
if (sf == sl) return;
for (Iter i = first; i < sf; ++i)
BOOST_CHECK(*i < *sf);
BOOST_CHECK(ba::is_sorted(sf, sl));
for (Iter i = sl; i < last; ++i)
BOOST_CHECK(*(sl-1) < *i);
}
template <typename Iter, typename Pred>
void check_sequence ( Iter first, Iter last, Iter sf, Iter sl, Pred p )
{
if (sf == sl) return;
for (Iter i = first; i < sf; ++i)
BOOST_CHECK(p(*i, *sf));
BOOST_CHECK(ba::is_sorted(sf, sl, p));
for (Iter i = sl; i < last; ++i)
BOOST_CHECK(p(*(sl-1), *i));
}
// for ( int i = 0; i < v.size(); ++i )
// std::cout << v[i] << ' ';
// std::cout << std::endl;
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);
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
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
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());
BOOST_CHECK_EQUAL(v[7], 7);
BOOST_CHECK_EQUAL(v[8], 8);
BOOST_CHECK_EQUAL(v[9], 9);
// Mix them up and try again - at the beginning
do_shuffle(v.begin(), v.end());
ba::sort_subrange(b, v.end(), b, b + 2);
check_sequence (b, v.end(), b, b + 2);
BOOST_CHECK_EQUAL(v[0], 0);
BOOST_CHECK_EQUAL(v[1], 1);
// Mix them up and try again - empty subrange
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
do_shuffle(v.begin(), v.end());
ba::sort_subrange(b, v.end(), b, v.end());
check_sequence (b, v.end(), b, v.end());
}
{
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>());
BOOST_CHECK_EQUAL(v[3], 6);
BOOST_CHECK_EQUAL(v[4], 5);
BOOST_CHECK_EQUAL(v[5], 4);
// Mix them up and try again - single element
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
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>());
BOOST_CHECK_EQUAL(v[7], 2);
BOOST_CHECK_EQUAL(v[8], 1);
BOOST_CHECK_EQUAL(v[9], 0);
// Mix them up and try again - at the beginning
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>());
BOOST_CHECK_EQUAL(v[0], 9);
BOOST_CHECK_EQUAL(v[1], 8);
// Mix them up and try again - empty subrange
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
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();
}