Compare commits

...

100 Commits

Author SHA1 Message Date
d029402f05 Add, and update, documentation build targets. 2016-10-10 11:24:26 -05: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
e5ea93bab1 Revert "Merge pull request #14 from nigels-com/merge-hex_lower"
This reverts commit 5412438df5, reversing
changes made to a09963bf93.
2016-03-07 15:43:55 -08:00
5412438df5 Merge pull request #14 from nigels-com/merge-hex_lower
Implement algorithm::hex_lower (Trac ticket #7064)
2016-03-07 14:57:34 -08: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
baa6eca18c Test coverage for algorithm::hex_lower, adapting existing coverage for algorithm::hex 2016-01-31 20:18:22 +10:00
073eb62f64 Another overload of algorithm::hex_lower as lower-case alternative to algorithm::hex 2016-01-31 20:17:23 +10:00
cc1392cae6 Implement algorithm::hex_lower as lower-case alternative to algorithm::hex 2016-01-31 19:11:11 +10: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
a09963bf93 Merge from develop; new feature 'power'; doc fixes; remove usage of C++11 versions of the algorithms 2015-03-18 21:31:53 -07:00
ba1894bfde Manually apply pull request #10 (since it was against master) 2015-03-18 08:39:42 -07:00
0693c80c98 Added meta/libraries.json 2015-01-26 07:08:13 -08:00
d4734356e9 Added more general power functionality as requested by Sean Parent. Also added enable_if to make sure the exponent is an integral type. 2014-12-03 15:15:15 -08:00
85adf4c74e For some reason, these routines were only compiled in for C++11 and less. Make them available all the time. 2014-12-03 15:07:33 -08:00
0c3f9a38f4 Add new algorithm boost::power, which raises a number to an integer power 2014-12-02 14:38:25 -08:00
c5c927bf25 Merge pull request #9 from jzmaddock/patch-1
Remove use of deprecated TR1 library.
2014-09-29 07:47:05 -07:00
eb9079c49c Remove use of deprecated TR1 library. 2014-09-27 13:13:27 +01:00
0a55238652 Removed some debugging code from the test 2014-09-24 10:33:46 -07:00
4dac507b77 Remove code to use standard library versions of algorithms. Always use the boost ones 2014-08-28 10:07:16 -07:00
3fd9c35138 Removed some tabs that snuck in. No functionality change 2014-07-08 08:47:15 -07:00
b9d91c59e4 Add missing ::type in the range-based partition_point implementation. Add test for this call - since there was none before. Thanks to Wygos for the fix. 2014-06-18 19:16:34 +02:00
5af84327ad Merge pull request #1 from pabigot/fixup/hex
hex: remove unreferenced type declaration. Thanks to pabigot.
2014-05-13 15:24:43 -06:00
d121a40f2b Add a missing 'not' to the description for mismatch. Thanks to K-ballo for the catch 2014-05-03 15:13:44 -07:00
cf249c090c Merge from develop: clean up is_permutation; fix clamp interface, and merge fix for bug #9335 as well 2014-03-31 12:03:45 -07:00
0f63883818 Rework the file layout and implementation of 'is_permutation' to match the various versions of the C++ standard. In particular, move the four iterator versions into the cxx14/ directory. 2014-03-23 14:56:48 -07:00
645be22fa7 Setting merge point for git merges 2014-03-23 14:33:14 -07:00
28b12d7264 Algorithm: Remove obsolete MSVC version checks.
[SVN r86024]
2014-03-23 14:30:10 -07:00
4d28d579e3 Remove BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
Process #ifdef...#endif blocks.

[SVN r86243]
2014-03-23 14:27:55 -07:00
5adab54486 Remove BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
Process #ifdef...#else...#endif blocks.

[SVN r86246]
2014-03-23 14:27:55 -07:00
a4d5f17173 #ifdef out the four argument versions of is_permutation when we are compiling with C++14, since they're in the standard library 2014-03-19 09:11:51 -07:00
848b521850 Changed parameters for the sequence-based versions of clamp from being passed by vaue to being passed by const &. Thanks to Martin Moene for the suggestion. 2014-03-11 12:09:51 -07:00
bc3b806f56 Fixed some file perms that got munged in the git conversion. 2014-02-04 08:13:44 -08:00
9155d4c1cb hex: remove unreferenced type declaration
Presence with gcc -Wunused -Werror produces error.
2013-12-30 07:03:47 -06:00
685a76f094 Merge a bunch of minor Boost.Algorithm changes to release
[SVN r86757]
2013-11-18 16:52:09 +00:00
0ac413ecb8 Updated docs for is_sorted, etc. Refs #9367
[SVN r86741]
2013-11-17 20:47:14 +00:00
747722a8da Fix an uninitialized member in a default-initialized split_iterator; Refs #9335
[SVN r86583]
2013-11-07 21:31:06 +00:00
5988a55b96 Minor merging; removing tabs from source files
[SVN r86323]
2013-10-15 15:44:55 +00:00
617ec03937 Updated license and copyright
[SVN r86311]
2013-10-14 21:35:20 +00:00
4c3b17ce77 Remove tabs
[SVN r86310]
2013-10-14 21:31:19 +00:00
ef78dde448 Remove BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
Process #ifdef...#else...#endif blocks.

[SVN r86246]
2013-10-11 23:19:17 +00:00
432b3a3da0 Remove BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
Process #ifdef...#endif blocks.

[SVN r86243]
2013-10-11 23:13:10 +00:00
850fc02667 Merge bug fix for #9063 to release
[SVN r86218]
2013-10-09 18:09:40 +00:00
b44dff3a5b Revert to old behavior for find_iterator; will not find overlapping matches. Fixes #9063
[SVN r86206]
2013-10-08 17:59:44 +00:00
5279c8f061 Merge minor Boost.Algorithm doc fixes to the release branch
[SVN r86200]
2013-10-08 15:31:14 +00:00
4b9a073780 Fixed typo in documentation for equal and mismatch; thanks to Larry for the catch
[SVN r86171]
2013-10-06 01:24:41 +00:00
083c6f5891 Algorithm: Remove obsolete MSVC version checks.
[SVN r86024]
2013-09-30 00:18:17 +00:00
8b89b5ba27 Merged changes from trunk.
[SVN r85804]
2013-09-20 17:40:42 +00:00
d528574fe9 To reduce dependencies changed includes from iterator_range.hpp to iterator_range_core.hpp.
[SVN r85731]
2013-09-17 20:37:20 +00:00
fe51eb60a9 Rewrote iota_n to use pre-increment instead of post - now the same as iota. Added a test for 0 as well.
[SVN r85465]
2013-08-25 16:29:01 +00:00
fc0fe6af52 Fixed typo "one_of_value" --> "one_of_equal". Refs #9010
[SVN r85397]
2013-08-19 17:52:56 +00:00
ca23b6f4f8 Merge bug fix to Release; Fixes #8546
[SVN r85134]
2013-07-23 16:48:27 +00:00
5f79807d02 Removed unused typedef; Refs #8546
[SVN r85106]
2013-07-22 15:46:43 +00:00
4f269a270c Fix error in compile-fail test; now fails to compile for the right reason
[SVN r84824]
2013-06-18 14:46:32 +00:00
05475fec0f Fixed some mixed singular/plural bits
[SVN r84648]
2013-06-05 20:24:00 +00:00
44d65cb4ae Added documentation for 'is_permutation'
[SVN r84538]
2013-05-28 19:36:04 +00:00
3e5f571230 fixed Doxygen comment
[SVN r84448]
2013-05-23 18:29:49 +00:00
73cfe03b97 Added docs for C++14 variants of 'equal' and 'mismatch'
[SVN r84447]
2013-05-23 18:28:23 +00:00
b9d89c559b Better 'is_permutation' implementation, tests
[SVN r84389]
2013-05-20 15:37:50 +00:00
86 changed files with 1621 additions and 736 deletions

View File

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

View File

@ -66,6 +66,7 @@ Thanks to all the people who have reviewed this library and made suggestions for
[include clamp-hpp.qbk] [include clamp-hpp.qbk]
[include gather.qbk] [include gather.qbk]
[include hex.qbk] [include hex.qbk]
[include is_palindrome.qbk]
[endsect] [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] [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. * `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] [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. * `any_of` and `any_of_equal` both return false for empty ranges, no matter what is passed to test against.

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 <`. There is also a version that allows the caller to specify a comparison predicate to use instead of `operator <`.
`` ``
template<typename V> template<typename T>
V clamp ( V val, V lo, V hi ); const T& clamp ( const T& val, const T& lo, const T& hi );
template<typename V, typename Pred> template<typename T, typename Pred>
V clamp ( V val, V lo, V hi, Pred p ); const T& clamp ( const T& val, const T& lo, const T& hi, Pred p );
`` ``
The following code: `` The following code: ``

View File

@ -22,7 +22,7 @@ Consider the two sequences:
std::equal ( seq1.begin (), seq1.end (), seq2.begin ()); // true std::equal ( seq1.begin (), seq1.end (), seq2.begin ()); // true
std::equal ( seq2.begin (), seq2.end (), seq1.begin ()); // Undefined behavior std::equal ( seq2.begin (), seq2.end (), seq1.begin ()); // Undefined behavior
std::equal ( seq1.begin (), seq1.end (), seq1.begin (), seq2.end ()); // false std::equal ( seq1.begin (), seq1.end (), seq2.begin (), seq2.end ()); // false
``` ```
You can argue that `true` is the correct answer in the first case, even though the sequences are not the same. The first N entries in `seq2` are the same as the entries in `seq1` - but that's not all that's in `seq2`. But in the second case, the algorithm will read past the end of `seq1`, resulting in undefined behavior (large earthquake, incorrect results, pregnant cat, etc). You can argue that `true` is the correct answer in the first case, even though the sequences are not the same. The first N entries in `seq2` are the same as the entries in `seq1` - but that's not all that's in `seq2`. But in the second case, the algorithm will read past the end of `seq1`, resulting in undefined behavior (large earthquake, incorrect results, pregnant cat, etc).

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,7 +55,7 @@ Both of the variants of `is_partitioned` take their parameters by value or const
[heading Notes] [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 ranges, no matter what predicate is passed to test against.

View File

@ -71,7 +71,7 @@ All of the variants of `is_permutation` take their parameters by value, and do n
[heading Notes] [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). * 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

@ -22,7 +22,7 @@ Consider the two sequences:
std::mismatch ( seq1.begin (), seq1.end (), seq2.begin ()); // <3, 3> std::mismatch ( seq1.begin (), seq1.end (), seq2.begin ()); // <3, 3>
std::mismatch ( seq2.begin (), seq2.end (), seq1.begin ()); // Undefined behavior std::mismatch ( seq2.begin (), seq2.end (), seq1.begin ()); // Undefined behavior
std::mismatch ( seq1.begin (), seq1.end (), seq1.begin (), seq2.end ()); // <3, 3> std::mismatch ( seq1.begin (), seq1.end (), seq2.begin (), seq2.end ()); // <3, 3>
``` ```
The first N entries in `seq2` are the same as the entries in `seq1` - but that's not all that's in `seq2`. In the second case, the algorithm will read past the end of `seq1`, resulting in undefined behavior (large earthquake, incorrect results, pregnant cat, etc). The first N entries in `seq2` are the same as the entries in `seq1` - but that's not all that's in `seq2`. In the second case, the algorithm will read past the end of `seq1`, resulting in undefined behavior (large earthquake, incorrect results, pregnant cat, etc).
@ -60,7 +60,7 @@ mismatch ( c1.end(), c1.end(), c2.end(), c2.end()) --> <c1.end(),
[heading Complexity] [heading Complexity]
Both of the variants of `mismatch` run in ['O(N)] (linear) time; that is, they compare against each element in the list once. If the sequence is found to be equal at any point, the routine will terminate immediately, without examining the rest of the elements. Both of the variants of `mismatch` 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 equal at any point, the routine will terminate immediately, without examining the rest of the elements.
[heading Exception Safety] [heading Exception Safety]

View File

@ -74,7 +74,7 @@ All of the variants of `none_of` and `none_of_equal` take their parameters by va
[heading Notes] [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. * `none_of` and `none_of_equal` both return true for empty ranges, no matter what is passed to test against.

View File

@ -75,7 +75,7 @@ All of the variants of `one_of` and `one_of_equal` take their parameters by valu
* `one_of` and `one_of_equal` both return false for empty ranges, no matter what is passed to test against. * `one_of` and `one_of_equal` both return false for empty ranges, no matter what is passed to test against.
* The second parameter to `one_of_value` is a template parameter, rather than deduced from the first parameter (`std::iterator_traits<InputIterator>::value_type`) because that allows more flexibility for callers, and takes advantage of built-in comparisons for the type that is pointed to by the iterator. The function is defined to return true if, for one element in the sequence, the expression `*iter == val` evaluates to true (where `iter` is an iterator to each element in the sequence) * The second parameter to `one_of_equal` is a template parameter, rather than deduced from the first parameter (`std::iterator_traits<InputIterator>::value_type`) because that allows more flexibility for callers, and takes advantage of built-in comparisons for the type that is pointed to by the iterator. The function is defined to return true if, for one element in the sequence, the expression `*iter == val` evaluates to true (where `iter` is an iterator to each element in the sequence)
[endsect] [endsect]

View File

@ -19,11 +19,11 @@ The function `is_sorted(sequence)` determines whether or not a sequence is compl
`` ``
namespace boost { namespace algorithm { namespace boost { namespace algorithm {
template <typename Iterator, typename Pred> template <typename ForwardIterator, typename Pred>
bool is_sorted ( Iterator first, Iterator last, Pred p ); bool is_sorted ( ForwardIterator first, ForwardIterator last, Pred p );
template <typename Iterator> template <typename ForwardIterator>
bool is_sorted ( Iterator first, Iterator last ); bool is_sorted ( ForwardIterator first, ForwardIterator last );
template <typename Range, typename Pred> template <typename Range, typename Pred>
@ -34,7 +34,7 @@ namespace boost { namespace algorithm {
}} }}
`` ``
Iterator requirements: The `is_sorted` functions will work on all kinds of iterators (except output iterators). Iterator requirements: The `is_sorted` functions will work forward iterators or better.
[heading is_sorted_until] [heading is_sorted_until]
@ -88,8 +88,8 @@ To test if a sequence is decreasing (each element no larger than the preceding o
`` ``
namespace boost { namespace algorithm { namespace boost { namespace algorithm {
template <typename Iterator> template <typename ForwardIterator>
bool is_decreasing ( Iterator first, Iterator last ); bool is_decreasing ( ForwardIterator first, ForwardIterator last );
template <typename R> template <typename R>
bool is_decreasing ( const R &range ); bool is_decreasing ( const R &range );
@ -99,8 +99,8 @@ namespace boost { namespace algorithm {
To test if a sequence is strictly increasing (each element larger than the preceding one): To test if a sequence is strictly increasing (each element larger than the preceding one):
`` ``
namespace boost { namespace algorithm { namespace boost { namespace algorithm {
template <typename Iterator> template <typename ForwardIterator>
bool is_strictly_increasing ( Iterator first, Iterator last ); bool is_strictly_increasing ( ForwardIterator first, ForwardIterator last );
template <typename R> template <typename R>
bool is_strictly_increasing ( const R &range ); bool is_strictly_increasing ( const R &range );
@ -110,8 +110,8 @@ namespace boost { namespace algorithm {
To test if a sequence is strictly decreasing (each element smaller than the preceding one): To test if a sequence is strictly decreasing (each element smaller than the preceding one):
`` ``
namespace boost { namespace algorithm { namespace boost { namespace algorithm {
template <typename Iterator> template <typename ForwardIterator>
bool is_strictly_decreasing ( Iterator first, Iterator last ); bool is_strictly_decreasing ( ForwardIterator first, ForwardIterator last );
template <typename R> template <typename R>
bool is_strictly_decreasing ( const R &range ); bool is_strictly_decreasing ( const R &range );

View File

@ -54,7 +54,7 @@ Both of the variants of `partition_point` take their parameters by value or cons
[heading Notes] [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. * For empty ranges, the partition point is the end of the range.

View File

@ -20,3 +20,5 @@ project /boost/algorithm/example
exe clamp_example : clamp_example.cpp ; exe clamp_example : clamp_example.cpp ;
exe search_example : search_example.cpp ; exe search_example : search_example.cpp ;
exe is_palindrome_example : is_palindrome_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,88 @@
/*
Copyright (c) Marshall Clow 2014.
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:
2 Dec 2014 mtc First version; power
*/
/// \file algorithm.hpp
/// \brief Misc Algorithms
/// \author Marshall Clow
///
#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>
namespace boost { namespace algorithm {
template <typename T>
T identity_operation ( std::multiplies<T> ) { return T(1); }
template <typename T>
T identity_operation ( std::plus<T> ) { return T(0); }
/// \fn power ( T x, Integer n )
/// \return the value "x" raised to the power "n"
///
/// \param x The value to be exponentiated
/// \param n The exponent (must be >= 0)
///
// \remark Taken from Knuth, The Art of Computer Programming, Volume 2:
// Seminumerical Algorithms, Section 4.6.3
template <typename T, typename Integer>
typename boost::enable_if<boost::is_integral<Integer>, T>::type
power (T x, Integer n) {
T y = 1; // Should be "T y{1};"
if (n == 0) return y;
while (true) {
if (n % 2 == 1) {
y = x * y;
if (n == 1)
return y;
}
n = n / 2;
x = x * x;
}
return y;
}
/// \fn power ( T x, Integer n, Operation op )
/// \return the value "x" raised to the power "n"
/// using the operation "op".
///
/// \param x The value to be exponentiated
/// \param n The exponent (must be >= 0)
/// \param op The operation used
///
// \remark Taken from Knuth, The Art of Computer Programming, Volume 2:
// Seminumerical Algorithms, Section 4.6.3
template <typename T, typename Integer, typename Operation>
typename boost::enable_if<boost::is_integral<Integer>, T>::type
power (T x, Integer n, Operation op) {
T y = identity_operation(op);
if (n == 0) return y;
while (true) {
if (n % 2 == 1) {
y = op(x, y);
if (n == 1)
return y;
}
n = n / 2;
x = op(x, x);
}
return y;
}
}}
#endif // BOOST_ALGORITHM_HPP

View File

@ -31,8 +31,8 @@
namespace boost { namespace algorithm { namespace boost { namespace algorithm {
/// \fn clamp ( T const& val, /// \fn clamp ( T const& val,
/// typename boost::mpl::identity<T>::type const& lo, /// typename boost::mpl::identity<T>::type const & lo,
/// typename boost::mpl::identity<T>::type const& hi, Pred p ) /// typename boost::mpl::identity<T>::type const & hi, Pred p )
/// \return the value "val" brought into the range [ lo, hi ] /// \return the value "val" brought into the range [ lo, hi ]
/// using the comparison predicate p. /// using the comparison predicate p.
/// If p ( val, lo ) return lo. /// If p ( val, lo ) return lo.
@ -56,8 +56,8 @@ namespace boost { namespace algorithm {
/// \fn clamp ( T const& val, /// \fn clamp ( T const& val,
/// typename boost::mpl::identity<T>::type const& lo, /// typename boost::mpl::identity<T>::type const & lo,
/// typename boost::mpl::identity<T>::type const& hi ) /// typename boost::mpl::identity<T>::type const & hi )
/// \return the value "val" brought into the range [ lo, hi ]. /// \return the value "val" brought into the range [ lo, hi ].
/// If the value is less than lo, return lo. /// If the value is less than lo, return lo.
/// If the value is greater than "hi", return hi. /// If the value is greater than "hi", return hi.
@ -76,8 +76,8 @@ namespace boost { namespace algorithm {
} }
/// \fn clamp_range ( InputIterator first, InputIterator last, OutputIterator out, /// \fn clamp_range ( InputIterator first, InputIterator last, OutputIterator out,
/// std::iterator_traits<InputIterator>::value_type lo, /// std::iterator_traits<InputIterator>::value_type const & lo,
/// std::iterator_traits<InputIterator>::value_type hi ) /// std::iterator_traits<InputIterator>::value_type const & hi )
/// \return clamp the sequence of values [first, last) into [ lo, hi ] /// \return clamp the sequence of values [first, last) into [ lo, hi ]
/// ///
/// \param first The start of the range of values /// \param first The start of the range of values
@ -88,8 +88,8 @@ namespace boost { namespace algorithm {
/// ///
template<typename InputIterator, typename OutputIterator> template<typename InputIterator, typename OutputIterator>
OutputIterator clamp_range ( InputIterator first, InputIterator last, OutputIterator out, OutputIterator clamp_range ( InputIterator first, InputIterator last, OutputIterator out,
typename std::iterator_traits<InputIterator>::value_type lo, typename std::iterator_traits<InputIterator>::value_type const & lo,
typename std::iterator_traits<InputIterator>::value_type hi ) typename std::iterator_traits<InputIterator>::value_type const & hi )
{ {
// this could also be written with bind and std::transform // this could also be written with bind and std::transform
while ( first != last ) while ( first != last )
@ -98,8 +98,8 @@ namespace boost { namespace algorithm {
} }
/// \fn clamp_range ( const Range &r, OutputIterator out, /// \fn clamp_range ( const Range &r, OutputIterator out,
/// typename std::iterator_traits<typename boost::range_iterator<const Range>::type>::value_type lo, /// typename std::iterator_traits<typename boost::range_iterator<const Range>::type>::value_type const & lo,
/// typename std::iterator_traits<typename boost::range_iterator<const Range>::type>::value_type hi ) /// typename std::iterator_traits<typename boost::range_iterator<const Range>::type>::value_type const & hi )
/// \return clamp the sequence of values [first, last) into [ lo, hi ] /// \return clamp the sequence of values [first, last) into [ lo, hi ]
/// ///
/// \param r The range of values to be clamped /// \param r The range of values to be clamped
@ -110,16 +110,16 @@ namespace boost { namespace algorithm {
template<typename Range, typename OutputIterator> template<typename Range, typename OutputIterator>
typename boost::disable_if_c<boost::is_same<Range, OutputIterator>::value, OutputIterator>::type typename boost::disable_if_c<boost::is_same<Range, OutputIterator>::value, OutputIterator>::type
clamp_range ( const Range &r, OutputIterator out, clamp_range ( const Range &r, OutputIterator out,
typename std::iterator_traits<typename boost::range_iterator<const Range>::type>::value_type lo, typename std::iterator_traits<typename boost::range_iterator<const Range>::type>::value_type const & lo,
typename std::iterator_traits<typename boost::range_iterator<const Range>::type>::value_type hi ) typename std::iterator_traits<typename boost::range_iterator<const Range>::type>::value_type const & hi )
{ {
return clamp_range ( boost::begin ( r ), boost::end ( r ), out, lo, hi ); return clamp_range ( boost::begin ( r ), boost::end ( r ), out, lo, hi );
} }
/// \fn clamp_range ( InputIterator first, InputIterator last, OutputIterator out, /// \fn clamp_range ( InputIterator first, InputIterator last, OutputIterator out,
/// std::iterator_traits<InputIterator>::value_type lo, /// std::iterator_traits<InputIterator>::value_type const & lo,
/// std::iterator_traits<InputIterator>::value_type hi, Pred p ) /// std::iterator_traits<InputIterator>::value_type const & hi, Pred p )
/// \return clamp the sequence of values [first, last) into [ lo, hi ] /// \return clamp the sequence of values [first, last) into [ lo, hi ]
/// using the comparison predicate p. /// using the comparison predicate p.
/// ///
@ -134,8 +134,8 @@ namespace boost { namespace algorithm {
/// ///
template<typename InputIterator, typename OutputIterator, typename Pred> template<typename InputIterator, typename OutputIterator, typename Pred>
OutputIterator clamp_range ( InputIterator first, InputIterator last, OutputIterator out, OutputIterator clamp_range ( InputIterator first, InputIterator last, OutputIterator out,
typename std::iterator_traits<InputIterator>::value_type lo, typename std::iterator_traits<InputIterator>::value_type const & lo,
typename std::iterator_traits<InputIterator>::value_type hi, Pred p ) typename std::iterator_traits<InputIterator>::value_type const & hi, Pred p )
{ {
// this could also be written with bind and std::transform // this could also be written with bind and std::transform
while ( first != last ) while ( first != last )
@ -144,8 +144,8 @@ namespace boost { namespace algorithm {
} }
/// \fn clamp_range ( const Range &r, OutputIterator out, /// \fn clamp_range ( const Range &r, OutputIterator out,
/// typename std::iterator_traits<typename boost::range_iterator<const Range>::type>::value_type lo, /// typename std::iterator_traits<typename boost::range_iterator<const Range>::type>::value_type const & lo,
/// typename std::iterator_traits<typename boost::range_iterator<const Range>::type>::value_type hi, /// typename std::iterator_traits<typename boost::range_iterator<const Range>::type>::value_type const & hi,
/// Pred p ) /// Pred p )
/// \return clamp the sequence of values [first, last) into [ lo, hi ] /// \return clamp the sequence of values [first, last) into [ lo, hi ]
/// using the comparison predicate p. /// using the comparison predicate p.
@ -162,8 +162,8 @@ namespace boost { namespace algorithm {
template<typename Range, typename OutputIterator, typename Pred> template<typename Range, typename OutputIterator, typename Pred>
typename boost::disable_if_c<boost::is_same<Range, OutputIterator>::value, OutputIterator>::type typename boost::disable_if_c<boost::is_same<Range, OutputIterator>::value, OutputIterator>::type
clamp_range ( const Range &r, OutputIterator out, clamp_range ( const Range &r, OutputIterator out,
typename std::iterator_traits<typename boost::range_iterator<const Range>::type>::value_type lo, typename std::iterator_traits<typename boost::range_iterator<const Range>::type>::value_type const & lo,
typename std::iterator_traits<typename boost::range_iterator<const Range>::type>::value_type hi, typename std::iterator_traits<typename boost::range_iterator<const Range>::type>::value_type const & hi,
Pred p ) Pred p )
{ {
return clamp_range ( boost::begin ( r ), boost::end ( r ), out, lo, hi, p ); return clamp_range ( boost::begin ( r ), boost::end ( r ), out, lo, hi, p );

View File

@ -12,16 +12,11 @@
#ifndef BOOST_ALGORITHM_ALL_OF_HPP #ifndef BOOST_ALGORITHM_ALL_OF_HPP
#define 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/begin.hpp>
#include <boost/range/end.hpp> #include <boost/range/end.hpp>
namespace boost { namespace algorithm { namespace boost { namespace algorithm {
#if __cplusplus >= 201103L
// Use the C++11 versions of all_of if it is available
using std::all_of; // Section 25.2.1
#else
/// \fn all_of ( InputIterator first, InputIterator last, Predicate p ) /// \fn all_of ( InputIterator first, InputIterator last, Predicate p )
/// \return true if all elements in [first, last) satisfy the predicate 'p' /// \return true if all elements in [first, last) satisfy the predicate 'p'
/// \note returns true on an empty range /// \note returns true on an empty range
@ -31,8 +26,6 @@ using std::all_of; // Section 25.2.1
/// \param p A predicate for testing the elements of the sequence /// \param p A predicate for testing the elements of the sequence
/// ///
/// \note This function is part of the C++2011 standard library. /// \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> template<typename InputIterator, typename Predicate>
bool all_of ( InputIterator first, InputIterator last, Predicate p ) bool all_of ( InputIterator first, InputIterator last, Predicate p )
{ {
@ -41,7 +34,6 @@ bool all_of ( InputIterator first, InputIterator last, Predicate p )
return false; return false;
return true; return true;
} }
#endif
/// \fn all_of ( const Range &r, Predicate p ) /// \fn all_of ( const Range &r, Predicate p )
/// \return true if all elements in the range satisfy the predicate 'p' /// \return true if all elements in the range satisfy the predicate 'p'

View File

@ -14,16 +14,11 @@
#ifndef BOOST_ALGORITHM_ANY_OF_HPP #ifndef BOOST_ALGORITHM_ANY_OF_HPP
#define 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/begin.hpp>
#include <boost/range/end.hpp> #include <boost/range/end.hpp>
namespace boost { namespace algorithm { namespace boost { namespace algorithm {
// Use the C++11 versions of any_of if it is available
#if __cplusplus >= 201103L
using std::any_of; // Section 25.2.2
#else
/// \fn any_of ( InputIterator first, InputIterator last, Predicate p ) /// \fn any_of ( InputIterator first, InputIterator last, Predicate p )
/// \return true if any of the elements in [first, last) satisfy the predicate /// \return true if any of the elements in [first, last) satisfy the predicate
/// \note returns false on an empty range /// \note returns false on an empty range
@ -40,7 +35,6 @@ bool any_of ( InputIterator first, InputIterator last, Predicate p )
return true; return true;
return false; return false;
} }
#endif
/// \fn any_of ( const Range &r, Predicate p ) /// \fn any_of ( const Range &r, Predicate p )
/// \return true if any elements in the range satisfy the predicate 'p' /// \return true if any elements in the range satisfy the predicate 'p'

View File

@ -12,16 +12,12 @@
#ifndef BOOST_ALGORITHM_COPY_IF_HPP #ifndef BOOST_ALGORITHM_COPY_IF_HPP
#define 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/begin.hpp>
#include <boost/range/end.hpp> #include <boost/range/end.hpp>
namespace boost { namespace algorithm { namespace boost { namespace algorithm {
#if __cplusplus >= 201103L
// Use the C++11 versions of copy_if if it is available
using std::copy_if; // Section 25.3.1
#else
/// \fn copy_if ( InputIterator first, InputIterator last, OutputIterator result, Predicate p ) /// \fn copy_if ( InputIterator first, InputIterator last, OutputIterator result, Predicate p )
/// \brief Copies all the elements from the input range that satisfy the /// \brief Copies all the elements from the input range that satisfy the
/// predicate to the output range. /// predicate to the output range.
@ -32,8 +28,6 @@ using std::copy_if; // Section 25.3.1
/// \param result An output iterator to write the results into /// \param result An output iterator to write the results into
/// \param p A predicate for testing the elements of the range /// \param p A predicate for testing the elements of the range
/// \note This function is part of the C++2011 standard library. /// \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> template<typename InputIterator, typename OutputIterator, typename Predicate>
OutputIterator copy_if ( InputIterator first, InputIterator last, OutputIterator result, Predicate p ) OutputIterator copy_if ( InputIterator first, InputIterator last, OutputIterator result, Predicate p )
{ {
@ -42,7 +36,6 @@ OutputIterator copy_if ( InputIterator first, InputIterator last, OutputIterator
*result++ = *first; *result++ = *first;
return result; return result;
} }
#endif
/// \fn copy_if ( const Range &r, OutputIterator result, Predicate p ) /// \fn copy_if ( const Range &r, OutputIterator result, Predicate p )
/// \brief Copies all the elements from the input range that satisfy the /// \brief Copies all the elements from the input range that satisfy the

View File

@ -12,14 +12,8 @@
#ifndef BOOST_ALGORITHM_COPY_N_HPP #ifndef BOOST_ALGORITHM_COPY_N_HPP
#define BOOST_ALGORITHM_COPY_N_HPP #define BOOST_ALGORITHM_COPY_N_HPP
#include <algorithm> // for std::copy_n, if available
namespace boost { namespace algorithm { namespace boost { namespace algorithm {
#if __cplusplus >= 201103L
// Use the C++11 versions of copy_n if it is available
using std::copy_n; // Section 25.3.1
#else
/// \fn copy_n ( InputIterator first, Size n, OutputIterator result ) /// \fn copy_n ( InputIterator first, Size n, OutputIterator result )
/// \brief Copies exactly n (n > 0) elements from the range starting at first to /// \brief Copies exactly n (n > 0) elements from the range starting at first to
/// the range starting at result. /// the range starting at result.
@ -29,8 +23,6 @@ using std::copy_n; // Section 25.3.1
/// \param n The number of elements to copy /// \param n The number of elements to copy
/// \param result An output iterator to write the results into /// \param result An output iterator to write the results into
/// \note This function is part of the C++2011 standard library. /// \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> template <typename InputIterator, typename Size, typename OutputIterator>
OutputIterator copy_n ( InputIterator first, Size n, OutputIterator result ) OutputIterator copy_n ( InputIterator first, Size n, OutputIterator result )
{ {
@ -38,7 +30,6 @@ OutputIterator copy_n ( InputIterator first, Size n, OutputIterator result )
*result = *first; *result = *first;
return result; return result;
} }
#endif
}} // namespace boost and algorithm }} // namespace boost and algorithm
#endif // BOOST_ALGORITHM_COPY_IF_HPP #endif // BOOST_ALGORITHM_COPY_IF_HPP

View File

@ -12,17 +12,11 @@
#ifndef BOOST_ALGORITHM_FIND_IF_NOT_HPP #ifndef BOOST_ALGORITHM_FIND_IF_NOT_HPP
#define 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/begin.hpp>
#include <boost/range/end.hpp> #include <boost/range/end.hpp>
namespace boost { namespace algorithm { namespace boost { namespace algorithm {
#if __cplusplus >= 201103L
// Use the C++11 versions of find_if_not if it is available
using std::find_if_not; // Section 25.2.5
#else
/// \fn find_if_not(InputIterator first, InputIterator last, Predicate p) /// \fn find_if_not(InputIterator first, InputIterator last, Predicate p)
/// \brief Finds the first element in the sequence that does not satisfy the predicate. /// \brief Finds the first element in the sequence that does not satisfy the predicate.
/// \return The iterator pointing to the desired element. /// \return The iterator pointing to the desired element.
@ -31,8 +25,6 @@ using std::find_if_not; // Section 25.2.5
/// \param last One past the end of the input sequence /// \param last One past the end of the input sequence
/// \param p A predicate for testing the elements of the range /// \param p A predicate for testing the elements of the range
/// \note This function is part of the C++2011 standard library. /// \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> template<typename InputIterator, typename Predicate>
InputIterator find_if_not ( InputIterator first, InputIterator last, Predicate p ) InputIterator find_if_not ( InputIterator first, InputIterator last, Predicate p )
{ {
@ -41,7 +33,6 @@ InputIterator find_if_not ( InputIterator first, InputIterator last, Predicate p
break; break;
return first; return first;
} }
#endif
/// \fn find_if_not ( const Range &r, Predicate p ) /// \fn find_if_not ( const Range &r, Predicate p )
/// \brief Finds the first element in the sequence that does not satisfy the predicate. /// \brief Finds the first element in the sequence that does not satisfy the predicate.

View File

@ -12,17 +12,11 @@
#ifndef BOOST_ALGORITHM_IOTA_HPP #ifndef BOOST_ALGORITHM_IOTA_HPP
#define BOOST_ALGORITHM_IOTA_HPP #define BOOST_ALGORITHM_IOTA_HPP
#include <numeric>
#include <boost/range/begin.hpp> #include <boost/range/begin.hpp>
#include <boost/range/end.hpp> #include <boost/range/end.hpp>
namespace boost { namespace algorithm { namespace boost { namespace algorithm {
#if __cplusplus >= 201103L
// Use the C++11 versions of iota if it is available
using std::iota; // Section 26.7.6
#else
/// \fn iota ( ForwardIterator first, ForwardIterator last, T value ) /// \fn iota ( ForwardIterator first, ForwardIterator last, T value )
/// \brief Generates an increasing sequence of values, and stores them in [first, last) /// \brief Generates an increasing sequence of values, and stores them in [first, last)
/// ///
@ -30,15 +24,12 @@ using std::iota; // Section 26.7.6
/// \param last One past the end of the input sequence /// \param last One past the end of the input sequence
/// \param value The initial value of the sequence to be generated /// \param value The initial value of the sequence to be generated
/// \note This function is part of the C++2011 standard library. /// \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> template <typename ForwardIterator, typename T>
void iota ( ForwardIterator first, ForwardIterator last, T value ) void iota ( ForwardIterator first, ForwardIterator last, T value )
{ {
for ( ; first != last; ++first, ++value ) for ( ; first != last; ++first, ++value )
*first = value; *first = value;
} }
#endif
/// \fn iota ( Range &r, T value ) /// \fn iota ( Range &r, T value )
/// \brief Generates an increasing sequence of values, and stores them in the input Range. /// \brief Generates an increasing sequence of values, and stores them in the input Range.
@ -63,8 +54,8 @@ void iota ( Range &r, T value )
template <typename OutputIterator, typename T> template <typename OutputIterator, typename T>
OutputIterator iota_n ( OutputIterator out, T value, std::size_t n ) OutputIterator iota_n ( OutputIterator out, T value, std::size_t n )
{ {
while ( n-- > 0 ) for ( ; n > 0; --n, ++value )
*out++ = value++; *out++ = value;
return out; return out;
} }

View File

@ -12,17 +12,11 @@
#ifndef BOOST_ALGORITHM_IS_PARTITIONED_HPP #ifndef BOOST_ALGORITHM_IS_PARTITIONED_HPP
#define 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/begin.hpp>
#include <boost/range/end.hpp> #include <boost/range/end.hpp>
namespace boost { namespace algorithm { namespace boost { namespace algorithm {
#if __cplusplus >= 201103L
// Use the C++11 versions of is_partitioned if it is available
using std::is_partitioned; // Section 25.3.13
#else
/// \fn is_partitioned ( InputIterator first, InputIterator last, UnaryPredicate p ) /// \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
/// ///
@ -30,8 +24,6 @@ using std::is_partitioned; // Section 25.3.13
/// \param last One past the end of the input sequence /// \param last One past the end of the input sequence
/// \param p The predicate to test the values with /// \param p The predicate to test the values with
/// \note This function is part of the C++2011 standard library. /// \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> template <typename InputIterator, typename UnaryPredicate>
bool is_partitioned ( InputIterator first, InputIterator last, UnaryPredicate p ) bool is_partitioned ( InputIterator first, InputIterator last, UnaryPredicate p )
{ {
@ -45,7 +37,6 @@ bool is_partitioned ( InputIterator first, InputIterator last, UnaryPredicate p
return false; return false;
return true; return true;
} }
#endif
/// \fn is_partitioned ( const Range &r, 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 Generates an increasing sequence of values, and stores them in the input Range.

View File

@ -9,11 +9,11 @@
/// \brief Is a sequence a permutation of another sequence /// \brief Is a sequence a permutation of another sequence
/// \author Marshall Clow /// \author Marshall Clow
#ifndef BOOST_ALGORITHM_IS_PERMUTATION_HPP #ifndef BOOST_ALGORITHM_IS_PERMUTATION11_HPP
#define BOOST_ALGORITHM_IS_PERMUTATION_HPP #define BOOST_ALGORITHM_IS_PERMUTATION11_HPP
#include <algorithm> // for std::less, tie, mismatch and is_permutation (if available) #include <algorithm> // for std::find_if, count_if, mismatch
#include <utility> // for std::make_pair #include <utility> // for std::pair
#include <functional> // for std::equal_to #include <functional> // for std::equal_to
#include <iterator> #include <iterator>
@ -21,7 +21,6 @@
#include <boost/range/end.hpp> #include <boost/range/end.hpp>
#include <boost/utility/enable_if.hpp> #include <boost/utility/enable_if.hpp>
#include <boost/type_traits/is_same.hpp> #include <boost/type_traits/is_same.hpp>
#include <boost/tr1/tr1/tuple> // for tie
namespace boost { namespace algorithm { namespace boost { namespace algorithm {
@ -100,11 +99,6 @@ namespace detail {
} }
/// \endcond /// \endcond
#if __cplusplus >= 201103L
// Use the C++11 versions of is_permutation if it is available
using std::is_permutation; // Section 25.2.12
#else
/// \fn is_permutation ( ForwardIterator1 first, ForwardIterator1 last, ForwardIterator2 first2, BinaryPredicate p ) /// \fn is_permutation ( ForwardIterator1 first, ForwardIterator1 last, ForwardIterator2 first2, BinaryPredicate p )
/// \brief Tests to see if the sequence [first,last) is a permutation of the sequence starting at first2 /// \brief Tests to see if the sequence [first,last) is a permutation of the sequence starting at first2
/// ///
@ -114,14 +108,11 @@ using std::is_permutation; // Section 25.2.12
/// \param p The predicate to compare elements with /// \param p The predicate to compare elements with
/// ///
/// \note This function is part of the C++2011 standard library. /// \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 > template< class ForwardIterator1, class ForwardIterator2, class BinaryPredicate >
bool is_permutation ( ForwardIterator1 first1, ForwardIterator1 last1, bool is_permutation ( ForwardIterator1 first1, ForwardIterator1 last1,
ForwardIterator2 first2, BinaryPredicate p ) ForwardIterator2 first2, BinaryPredicate p )
{ {
// Skip the common prefix (if any) // Skip the common prefix (if any)
// std::tie (first1, first2) = std::mismatch (first1, last1, first2, p);
std::pair<ForwardIterator1, ForwardIterator2> eq = std::mismatch (first1, last1, first2, p); std::pair<ForwardIterator1, ForwardIterator2> eq = std::mismatch (first1, last1, first2, p);
first1 = eq.first; first1 = eq.first;
first2 = eq.second; first2 = eq.second;
@ -142,8 +133,6 @@ bool is_permutation ( ForwardIterator1 first1, ForwardIterator1 last1,
/// \param last2 One past the end of the input sequence /// \param last2 One past the end of the input sequence
/// \param first2 The start of the second sequence /// \param first2 The start of the second sequence
/// \note This function is part of the C++2011 standard library. /// \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 > template< class ForwardIterator1, class ForwardIterator2 >
bool is_permutation ( ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2 ) bool is_permutation ( ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2 )
{ {
@ -163,58 +152,6 @@ bool is_permutation ( ForwardIterator1 first1, ForwardIterator1 last1, ForwardIt
return true; return true;
} }
#endif
/// \fn is_permutation ( ForwardIterator1 first, ForwardIterator1 last,
/// ForwardIterator2 first2, ForwardIterator2 last2 )
/// \brief Tests to see if the sequence [first,last) is a permutation of the sequence starting at first2
///
/// \param first1 The start of the input sequence
/// \param last2 One past the end of the input sequence
/// \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++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, ForwardIterator2 last2 )
{
// How should I deal with the idea that ForwardIterator1::value_type
// and ForwardIterator2::value_type could be different? Define my own comparison predicate?
return boost::algorithm::detail::is_permutation_tag (
first1, last1, first2, last2,
std::equal_to<typename std::iterator_traits<ForwardIterator1>::value_type> (),
typename std::iterator_traits<ForwardIterator1>::iterator_category (),
typename std::iterator_traits<ForwardIterator2>::iterator_category ());
}
/// \fn is_permutation ( ForwardIterator1 first, ForwardIterator1 last,
/// ForwardIterator2 first2, ForwardIterator2 last2,
/// BinaryPredicate p )
/// \brief Tests to see if the sequence [first,last) is a permutation of the sequence starting at first2
///
/// \param first1 The start of the input sequence
/// \param last1 One past the end of the input sequence
/// \param first2 The start of the second sequence
/// \param last2 One past the end of the second sequence
/// \param pred 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, ForwardIterator2 last2,
BinaryPredicate pred )
{
return boost::algorithm::detail::is_permutation_tag (
first1, last1, first2, last2, pred,
typename std::iterator_traits<ForwardIterator1>::iterator_category (),
typename std::iterator_traits<ForwardIterator2>::iterator_category ());
}
/// \fn is_permutation ( const Range &r, ForwardIterator first2 ) /// \fn is_permutation ( const Range &r, ForwardIterator first2 )
/// \brief Tests to see if the sequence [first,last) is a permutation of the sequence starting at first2 /// \brief Tests to see if the sequence [first,last) is a permutation of the sequence starting at first2
@ -245,4 +182,4 @@ is_permutation ( const Range &r, ForwardIterator first2, BinaryPredicate pred )
}} }}
#endif // BOOST_ALGORITHM_IS_PERMUTATION_HPP #endif // BOOST_ALGORITHM_IS_PERMUTATION11_HPP

View File

@ -13,7 +13,6 @@
#ifndef BOOST_ALGORITHM_ORDERED_HPP #ifndef BOOST_ALGORITHM_ORDERED_HPP
#define BOOST_ALGORITHM_ORDERED_HPP #define BOOST_ALGORITHM_ORDERED_HPP
#include <algorithm>
#include <functional> #include <functional>
#include <iterator> #include <iterator>
@ -26,11 +25,6 @@
namespace boost { namespace algorithm { namespace boost { namespace algorithm {
#if __cplusplus >= 201103L
// Use the C++11 versions of is_sorted/is_sorted_until if they are available
using std::is_sorted_until; // Section 25.4.1.5
using std::is_sorted; // Section 25.4.1.5
#else
/// \fn is_sorted_until ( ForwardIterator first, ForwardIterator last, Pred p ) /// \fn is_sorted_until ( ForwardIterator first, ForwardIterator last, Pred p )
/// \return the point in the sequence [first, last) where the elements are unordered /// \return the point in the sequence [first, last) where the elements are unordered
/// (according to the comparison predicate 'p'). /// (according to the comparison predicate 'p').
@ -91,7 +85,6 @@ using std::is_sorted; // Section 25.4.1.5
{ {
return boost::algorithm::is_sorted_until (first, last) == last; return boost::algorithm::is_sorted_until (first, last) == last;
} }
#endif
/// ///
/// -- Range based versions of the C++11 functions /// -- Range based versions of the C++11 functions

View File

@ -12,16 +12,11 @@
#ifndef BOOST_ALGORITHM_NONE_OF_HPP #ifndef BOOST_ALGORITHM_NONE_OF_HPP
#define 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/begin.hpp>
#include <boost/range/end.hpp> #include <boost/range/end.hpp>
namespace boost { namespace algorithm { namespace boost { namespace algorithm {
// Use the C++11 versions of the none_of if it is available
#if __cplusplus >= 201103L
using std::none_of; // Section 25.2.3
#else
/// \fn none_of ( InputIterator first, InputIterator last, Predicate p ) /// \fn none_of ( InputIterator first, InputIterator last, Predicate p )
/// \return true if none of the elements in [first, last) satisfy the predicate 'p' /// \return true if none of the elements in [first, last) satisfy the predicate 'p'
/// \note returns true on an empty range /// \note returns true on an empty range
@ -33,12 +28,11 @@ using std::none_of; // Section 25.2.3
template<typename InputIterator, typename Predicate> template<typename InputIterator, typename Predicate>
bool none_of ( InputIterator first, InputIterator last, Predicate p ) bool none_of ( InputIterator first, InputIterator last, Predicate p )
{ {
for ( ; first != last; ++first ) for ( ; first != last; ++first )
if ( p(*first)) if ( p(*first))
return false; return false;
return true; return true;
} }
#endif
/// \fn none_of ( const Range &r, Predicate p ) /// \fn none_of ( const Range &r, Predicate p )
/// \return true if none of the elements in the range satisfy the predicate 'p' /// \return true if none of the elements in the range satisfy the predicate 'p'

View File

@ -12,18 +12,13 @@
#ifndef BOOST_ALGORITHM_PARTITION_COPY_HPP #ifndef BOOST_ALGORITHM_PARTITION_COPY_HPP
#define BOOST_ALGORITHM_PARTITION_COPY_HPP #define BOOST_ALGORITHM_PARTITION_COPY_HPP
#include <algorithm> // for std::partition_copy, if available #include <utility> // for std::pair
#include <utility> // for make_pair
#include <boost/range/begin.hpp> #include <boost/range/begin.hpp>
#include <boost/range/end.hpp> #include <boost/range/end.hpp>
namespace boost { namespace algorithm { namespace boost { namespace algorithm {
#if __cplusplus >= 201103L
// Use the C++11 versions of partition_copy if it is available
using std::partition_copy; // Section 25.3.13
#else
/// \fn partition_copy ( InputIterator first, InputIterator last, /// \fn partition_copy ( InputIterator first, InputIterator last,
/// OutputIterator1 out_true, OutputIterator2 out_false, UnaryPredicate p ) /// OutputIterator1 out_true, OutputIterator2 out_false, UnaryPredicate p )
/// \brief Copies the elements that satisfy the predicate p from the range [first, last) /// \brief Copies the elements that satisfy the predicate p from the range [first, last)
@ -38,8 +33,6 @@ using std::partition_copy; // Section 25.3.13
/// \param p A predicate for dividing the elements of the input sequence. /// \param p A predicate for dividing the elements of the input sequence.
/// ///
/// \note This function is part of the C++2011 standard library. /// \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, template <typename InputIterator,
typename OutputIterator1, typename OutputIterator2, typename UnaryPredicate> typename OutputIterator1, typename OutputIterator2, typename UnaryPredicate>
std::pair<OutputIterator1, OutputIterator2> std::pair<OutputIterator1, OutputIterator2>
@ -53,7 +46,6 @@ partition_copy ( InputIterator first, InputIterator last,
*out_false++ = *first; *out_false++ = *first;
return std::pair<OutputIterator1, OutputIterator2> ( out_true, out_false ); return std::pair<OutputIterator1, OutputIterator2> ( out_true, out_false );
} }
#endif
/// \fn partition_copy ( const Range &r, /// \fn partition_copy ( const Range &r,
/// OutputIterator1 out_true, OutputIterator2 out_false, UnaryPredicate p ) /// OutputIterator1 out_true, OutputIterator2 out_false, UnaryPredicate p )

View File

@ -12,17 +12,13 @@
#ifndef BOOST_ALGORITHM_PARTITION_POINT_HPP #ifndef BOOST_ALGORITHM_PARTITION_POINT_HPP
#define 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/begin.hpp>
#include <boost/range/end.hpp> #include <boost/range/end.hpp>
namespace boost { namespace algorithm { namespace boost { namespace algorithm {
#if __cplusplus >= 201103L
// Use the C++11 versions of partition_point if it is available
using std::partition_point; // Section 25.3.13
#else
/// \fn partition_point ( ForwardIterator first, ForwardIterator last, Predicate p ) /// \fn partition_point ( ForwardIterator first, ForwardIterator last, Predicate p )
/// \brief Given a partitioned range, returns the partition point, i.e, the first element /// \brief Given a partitioned range, returns the partition point, i.e, the first element
/// that does not satisfy p /// that does not satisfy p
@ -31,8 +27,6 @@ using std::partition_point; // Section 25.3.13
/// \param last One past the end of the input sequence /// \param last One past the end of the input sequence
/// \param p The predicate to test the values with /// \param p The predicate to test the values with
/// \note This function is part of the C++2011 standard library. /// \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> template <typename ForwardIterator, typename Predicate>
ForwardIterator partition_point ( ForwardIterator first, ForwardIterator last, Predicate p ) ForwardIterator partition_point ( ForwardIterator first, ForwardIterator last, Predicate p )
{ {
@ -52,7 +46,6 @@ ForwardIterator partition_point ( ForwardIterator first, ForwardIterator last, P
} }
return first; return first;
} }
#endif
/// \fn partition_point ( Range &r, Predicate p ) /// \fn partition_point ( Range &r, Predicate p )
/// \brief Given a partitioned range, returns the partition point /// \brief Given a partitioned range, returns the partition point
@ -61,7 +54,7 @@ ForwardIterator partition_point ( ForwardIterator first, ForwardIterator last, P
/// \param p The predicate to test the values with /// \param p The predicate to test the values with
/// ///
template <typename Range, typename Predicate> template <typename Range, typename Predicate>
typename boost::range_iterator<Range> partition_point ( Range &r, Predicate p ) typename boost::range_iterator<Range>::type partition_point ( Range &r, Predicate p )
{ {
return boost::algorithm::partition_point (boost::begin(r), boost::end(r), p); return boost::algorithm::partition_point (boost::begin(r), boost::end(r), p);
} }

View File

@ -12,41 +12,42 @@
#ifndef BOOST_ALGORITHM_EQUAL_HPP #ifndef BOOST_ALGORITHM_EQUAL_HPP
#define BOOST_ALGORITHM_EQUAL_HPP #define BOOST_ALGORITHM_EQUAL_HPP
#include <algorithm> // for std::equal #include <algorithm> // for std::equal
#include <functional> // for std::equal_to #include <functional> // for std::binary_function
#include <iterator>
namespace boost { namespace algorithm { namespace boost { namespace algorithm {
namespace detail { namespace detail {
template <class T1, class T2> template <class T1, class T2>
struct eq : public std::binary_function<T1, T2, bool> { struct eq : public std::binary_function<T1, T2, bool> {
bool operator () ( const T1& v1, const T2& v2 ) const { return v1 == v2 ;} bool operator () ( const T1& v1, const T2& v2 ) const { return v1 == v2 ;}
}; };
template <class RandomAccessIterator1, class RandomAccessIterator2, class BinaryPredicate> template <class RandomAccessIterator1, class RandomAccessIterator2, class BinaryPredicate>
bool equal ( RandomAccessIterator1 first1, RandomAccessIterator1 last1, bool equal ( RandomAccessIterator1 first1, RandomAccessIterator1 last1,
RandomAccessIterator2 first2, RandomAccessIterator2 last2, BinaryPredicate pred, RandomAccessIterator2 first2, RandomAccessIterator2 last2, BinaryPredicate pred,
std::random_access_iterator_tag, std::random_access_iterator_tag ) std::random_access_iterator_tag, std::random_access_iterator_tag )
{ {
// Random-access iterators let is check the sizes in constant time // Random-access iterators let is check the sizes in constant time
if ( std::distance ( first1, last1 ) != std::distance ( first2, last2 )) if ( std::distance ( first1, last1 ) != std::distance ( first2, last2 ))
return false; return false;
// If we know that the sequences are the same size, the original version is fine // If we know that the sequences are the same size, the original version is fine
return std::equal ( first1, last1, first2, pred ); return std::equal ( first1, last1, first2, pred );
} }
template <class InputIterator1, class InputIterator2, class BinaryPredicate> template <class InputIterator1, class InputIterator2, class BinaryPredicate>
bool equal ( InputIterator1 first1, InputIterator1 last1, bool equal ( InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, InputIterator2 last2, BinaryPredicate pred, InputIterator2 first2, InputIterator2 last2, BinaryPredicate pred,
std::input_iterator_tag, std::input_iterator_tag ) std::input_iterator_tag, std::input_iterator_tag )
{ {
for (; first1 != last1 && first2 != last2; ++first1, ++first2 ) for (; first1 != last1 && first2 != last2; ++first1, ++first2 )
if ( !pred(*first1, *first2 )) if ( !pred(*first1, *first2 ))
return false; return false;
return first1 == last1 && first2 == last2; return first1 == last1 && first2 == last2;
} }
} }
/// \fn equal ( InputIterator1 first1, InputIterator1 last1, /// \fn equal ( InputIterator1 first1, InputIterator1 last1,
@ -63,10 +64,10 @@ template <class InputIterator1, class InputIterator2, class BinaryPredicate>
bool equal ( InputIterator1 first1, InputIterator1 last1, bool equal ( InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, InputIterator2 last2, BinaryPredicate pred ) InputIterator2 first2, InputIterator2 last2, BinaryPredicate pred )
{ {
return boost::algorithm::detail::equal ( return boost::algorithm::detail::equal (
first1, last1, first2, last2, pred, first1, last1, first2, last2, pred,
typename std::iterator_traits<InputIterator1>::iterator_category (), typename std::iterator_traits<InputIterator1>::iterator_category (),
typename std::iterator_traits<InputIterator2>::iterator_category ()); typename std::iterator_traits<InputIterator2>::iterator_category ());
} }
/// \fn equal ( InputIterator1 first1, InputIterator1 last1, /// \fn equal ( InputIterator1 first1, InputIterator1 last1,
@ -81,16 +82,16 @@ template <class InputIterator1, class InputIterator2>
bool equal ( InputIterator1 first1, InputIterator1 last1, bool equal ( InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, InputIterator2 last2 ) InputIterator2 first2, InputIterator2 last2 )
{ {
return boost::algorithm::detail::equal ( return boost::algorithm::detail::equal (
first1, last1, first2, last2, first1, last1, first2, last2,
boost::algorithm::detail::eq< boost::algorithm::detail::eq<
typename std::iterator_traits<InputIterator1>::value_type, typename std::iterator_traits<InputIterator1>::value_type,
typename std::iterator_traits<InputIterator2>::value_type> (), typename std::iterator_traits<InputIterator2>::value_type> (),
typename std::iterator_traits<InputIterator1>::iterator_category (), typename std::iterator_traits<InputIterator1>::iterator_category (),
typename std::iterator_traits<InputIterator2>::iterator_category ()); typename std::iterator_traits<InputIterator2>::iterator_category ());
} }
// There are already range-based versions of these. // There are already range-based versions of these.
}} // namespace boost and algorithm }} // namespace boost and algorithm

View File

@ -1,130 +1,79 @@
/* /*
Copyright (c) Marshall Clow 2013 Copyright (c) Marshall Clow 2014.
Distributed under the Boost Software License, Version 1.0. (See accompanying 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 LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
*/ */
/// \file equal.hpp /// \file is_permutation.hpp
/// \brief Determines if one /// \brief Is a sequence a permutation of another sequence (four iterator versions)
/// \author Marshall Clow /// \author Marshall Clow
#ifndef BOOST_ALGORITHM_IS_PERMUTATION_HPP #ifndef BOOST_ALGORITHM_IS_PERMUTATION14_HPP
#define BOOST_ALGORITHM_IS_PERMUTATION_HPP #define BOOST_ALGORITHM_IS_PERMUTATION14_HPP
#include <algorithm> #include <utility> // for std::pair
#include <functional> // for std::equal_to #include <functional> // for std::equal_to
#include <iterator>
#include <boost/algorithm/cxx11/is_permutation.hpp>
#include <boost/algorithm/cxx14/mismatch.hpp>
namespace boost { namespace algorithm { namespace boost { namespace algorithm {
namespace detail { /// \fn is_permutation ( ForwardIterator1 first, ForwardIterator1 last,
/// ForwardIterator2 first2, ForwardIterator2 last2 )
template <class T1, class T2> /// \brief Tests to see if the sequence [first,last) is a permutation of the sequence starting at first2
struct is_perm_eq : public std::binary_function<T1, T2, bool> { ///
bool operator () ( const T1& v1, const T2& v2 ) const { return v1 == v2 ;} /// \param first1 The start of the input sequence
}; /// \param last2 One past the end of the input sequence
/// \param first2 The start of the second sequence
/// \param last1 One past the end of the second sequence
template <class RandomAccessIterator1, class RandomAccessIterator2, class BinaryPredicate> /// \note This function is part of the C++2014 standard library.
bool is_permutation ( RandomAccessIterator1 first1, RandomAccessIterator1 last1, template< class ForwardIterator1, class ForwardIterator2 >
RandomAccessIterator2 first2, RandomAccessIterator2 last2, BinaryPredicate pred, bool is_permutation ( ForwardIterator1 first1, ForwardIterator1 last1,
std::random_access_iterator_tag, std::random_access_iterator_tag ) ForwardIterator2 first2, ForwardIterator2 last2 )
{
// Random-access iterators let is check the sizes in constant time
if ( std::distance ( first1, last1 ) != std::distance ( first2, last2 ))
return false;
// If we know that the sequences are the same size, the original version is fine
return std::is_permutation ( first1, last1, first2, pred );
}
template<class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
bool is_permutation (
ForwardIterator1 first1, ForwardIterator1 last1,
ForwardIterator2 first2, ForwardIterator2 last2,
BinaryPredicate pred,
std::forward_iterator_tag, std::forward_iterator_tag )
{
// Look for common prefix
for (; first1 != last1 && first2 != last2; ++first1, ++first2)
if (!pred(*first1, *first2))
goto not_done;
// We've reached the end of one of the sequences without a mismatch.
return first1 == last1 && first2 == last2;
not_done:
// Check and make sure that we have the same # of elements left
typedef typename std::iterator_traits<ForwardIterator1>::difference_type diff1_t;
diff1_t len1 = _VSTD::distance(first1, last1);
typedef typename std::iterator_traits<ForwardIterator2>::difference_type diff2_t;
diff2_t len2 = _VSTD::distance(first2, last2);
if (len1 != len2)
return false;
// For each element in [f1, l1) see if there are the
// same number of equal elements in [f2, l2)
for ( ForwardIterator1 i = first1; i != last1; ++i )
{
// Have we already counted this value?
ForwardIterator1 j;
for ( j = first1; j != i; ++j )
if (pred(*j, *i))
break;
if ( j == i ) // didn't find it...
{
// Count number of *i in [f2, l2)
diff1_t c2 = 0;
for ( ForwardIterator2 iter2 = first2; iter2 != last2; ++iter2 )
if (pred(*i, *iter2))
++c2;
if (c2 == 0)
return false;
// Count number of *i in [i, l1)
diff1_t c1 = 0;
for (_ForwardIterator1 iter1 = i; iter1 != last1; ++iter1 )
if (pred(*i, *iter1))
++c1;
if (c1 != c2)
return false;
}
}
return true;
}
}
template<class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
bool is_permutation (
ForwardIterator1 first1, ForwardIterator1 last1,
ForwardIterator2 first2, ForwardIterator2 last2,
BinaryPredicate pred )
{ {
return boost::algorithm::detail::is_permutation ( // How should I deal with the idea that ForwardIterator1::value_type
first1, last1, first2, last2, pred, // and ForwardIterator2::value_type could be different? Define my own comparison predicate?
typename std::iterator_traits<ForwardIterator1>::iterator_category (), std::pair<ForwardIterator1, ForwardIterator2> eq = boost::algorithm::mismatch
typename std::iterator_traits<ForwardIterator2>::iterator_category ()); ( first1, last1, first2, last2 );
if ( eq.first == last1 && eq.second == last2)
return true;
return boost::algorithm::detail::is_permutation_tag (
eq.first, last1, eq.second, last2,
std::equal_to<typename std::iterator_traits<ForwardIterator1>::value_type> (),
typename std::iterator_traits<ForwardIterator1>::iterator_category (),
typename std::iterator_traits<ForwardIterator2>::iterator_category ());
} }
template<class ForwardIterator1, class ForwardIterator2> /// \fn is_permutation ( ForwardIterator1 first, ForwardIterator1 last,
/// ForwardIterator2 first2, ForwardIterator2 last2,
/// BinaryPredicate p )
/// \brief Tests to see if the sequence [first,last) is a permutation of the sequence starting at first2
///
/// \param first1 The start of the input sequence
/// \param last1 One past the end of the input sequence
/// \param first2 The start of the second sequence
/// \param last2 One past the end of the second sequence
/// \param pred The predicate to compare elements with
///
/// \note This function is part of the C++2014 standard library.
template< class ForwardIterator1, class ForwardIterator2, class BinaryPredicate >
bool is_permutation ( ForwardIterator1 first1, ForwardIterator1 last1, bool is_permutation ( ForwardIterator1 first1, ForwardIterator1 last1,
ForwardIterator2 first2, ForwardIterator2 last2 ) ForwardIterator2 first2, ForwardIterator2 last2,
BinaryPredicate pred )
{ {
typedef typename iterator_traits<_ForwardIterator1>::value_type value1_t; std::pair<ForwardIterator1, ForwardIterator2> eq = boost::algorithm::mismatch
typedef typename iterator_traits<_ForwardIterator2>::value_type value2_t; ( first1, last1, first2, last2, pred );
return boost::algorithm::detail::is_permutation ( if ( eq.first == last1 && eq.second == last2)
first1, last1, first2, last2, return true;
boost::algorithm::detail::is_perm_eq< return boost::algorithm::detail::is_permutation_tag (
typename std::iterator_traits<InputIterator1>::value_type, first1, last1, first2, last2, pred,
typename std::iterator_traits<InputIterator2>::value_type> (), typename std::iterator_traits<ForwardIterator1>::iterator_category (),
typename std::iterator_traits<ForwardIterator1>::iterator_category (), typename std::iterator_traits<ForwardIterator2>::iterator_category ());
typename std::iterator_traits<ForwardIterator2>::iterator_category ());
} }
// There are already range-based versions of these. }}
}} // namespace boost and algorithm #endif // BOOST_ALGORITHM_IS_PERMUTATION14_HPP
#endif // BOOST_ALGORITHM_IS_PERMUTATION_HPP

View File

@ -12,13 +12,10 @@
#ifndef BOOST_ALGORITHM_MISMATCH_HPP #ifndef BOOST_ALGORITHM_MISMATCH_HPP
#define BOOST_ALGORITHM_MISMATCH_HPP #define BOOST_ALGORITHM_MISMATCH_HPP
#include <algorithm> // for std::mismatch #include <utility> // for std::pair
#include <utility> // for std::pair
namespace boost { namespace algorithm { namespace boost { namespace algorithm {
template <class InputIterator1, class InputIterator2, class BinaryPredicate>
/// \fn mismatch ( InputIterator1 first1, InputIterator1 last1, /// \fn mismatch ( InputIterator1 first1, InputIterator1 last1,
/// InputIterator2 first2, InputIterator2 last2, /// InputIterator2 first2, InputIterator2 last2,
/// BinaryPredicate pred ) /// BinaryPredicate pred )
@ -29,10 +26,11 @@ template <class InputIterator1, class InputIterator2, class BinaryPredicate>
/// \param first2 The start of the second range. /// \param first2 The start of the second range.
/// \param last2 One past the end of the second range. /// \param last2 One past the end of the second range.
/// \param pred A predicate for comparing the elements of the ranges /// \param pred A predicate for comparing the elements of the ranges
template <class InputIterator1, class InputIterator2, class BinaryPredicate>
std::pair<InputIterator1, InputIterator2> mismatch ( std::pair<InputIterator1, InputIterator2> mismatch (
InputIterator1 first1, InputIterator1 last1, InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, InputIterator2 last2, InputIterator2 first2, InputIterator2 last2,
BinaryPredicate pred ) BinaryPredicate pred )
{ {
for (; first1 != last1 && first2 != last2; ++first1, ++first2) for (; first1 != last1 && first2 != last2; ++first1, ++first2)
if ( !pred ( *first1, *first2 )) if ( !pred ( *first1, *first2 ))
@ -50,8 +48,8 @@ std::pair<InputIterator1, InputIterator2> mismatch (
/// \param last2 One past the end of the second range. /// \param last2 One past the end of the second range.
template <class InputIterator1, class InputIterator2> template <class InputIterator1, class InputIterator2>
std::pair<InputIterator1, InputIterator2> mismatch ( std::pair<InputIterator1, InputIterator2> mismatch (
InputIterator1 first1, InputIterator1 last1, InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, InputIterator2 last2 ) InputIterator2 first2, InputIterator2 last2 )
{ {
for (; first1 != last1 && first2 != last2; ++first1, ++first2) for (; first1 != last1 && first2 != last2; ++first1, ++first2)
if ( *first1 != *first2 ) if ( *first1 != *first2 )
@ -59,7 +57,7 @@ std::pair<InputIterator1, InputIterator2> mismatch (
return std::pair<InputIterator1, InputIterator2>(first1, first2); return std::pair<InputIterator1, InputIterator2>(first1, first2);
} }
// There are already range-based versions of these. // There are already range-based versions of these.
}} // namespace boost and algorithm }} // namespace boost and algorithm

View File

@ -1,9 +1,9 @@
/* /*
Copyright (c) Marshall Clow 2011-2012. Copyright (c) Marshall Clow 2011-2012.
Distributed under the Boost Software License, Version 1.0. (See accompanying 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 LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
Thanks to Nevin for his comments/help. Thanks to Nevin for his comments/help.
*/ */
@ -13,7 +13,7 @@
*/ */
/// \file hex.hpp /// \file hex.hpp
/// \brief Convert sequence of integral types into a sequence of hexadecimal /// \brief Convert sequence of integral types into a sequence of hexadecimal
/// characters and back. Based on the MySQL functions HEX and UNHEX /// characters and back. Based on the MySQL functions HEX and UNHEX
/// \author Marshall Clow /// \author Marshall Clow
@ -25,7 +25,9 @@
#include <boost/range/begin.hpp> #include <boost/range/begin.hpp>
#include <boost/range/end.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/utility/enable_if.hpp>
#include <boost/type_traits/is_integral.hpp> #include <boost/type_traits/is_integral.hpp>
@ -33,17 +35,17 @@
namespace boost { namespace algorithm { namespace boost { namespace algorithm {
/*! /*!
\struct hex_decode_error \struct hex_decode_error
\brief Base exception class for all hex decoding errors \brief Base exception class for all hex decoding errors
*/ /*! */ /*!
\struct non_hex_input \struct non_hex_input
\brief Thrown when a non-hex value (0-9, A-F) encountered when decoding. \brief Thrown when a non-hex value (0-9, A-F) encountered when decoding.
Contains the offending character Contains the offending character
*/ /*! */ /*!
\struct not_enough_input \struct not_enough_input
\brief Thrown when the input sequence unexpectedly ends \brief Thrown when the input sequence unexpectedly ends
*/ */
struct hex_decode_error : virtual boost::exception, virtual std::exception {}; struct hex_decode_error : virtual boost::exception, virtual std::exception {};
struct not_enough_input : virtual hex_decode_error {}; struct not_enough_input : virtual hex_decode_error {};
@ -54,12 +56,12 @@ namespace detail {
/// \cond DOXYGEN_HIDE /// \cond DOXYGEN_HIDE
template <typename T, typename OutputIterator> 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 ); const std::size_t num_hex_digits = 2 * sizeof ( T );
char res [ num_hex_digits ]; char res [ num_hex_digits ];
char *p = res + num_hex_digits; char *p = res + num_hex_digits;
for ( std::size_t i = 0; i < num_hex_digits; ++i, val >>= 4 ) 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 ); return std::copy ( res, res + num_hex_digits, out );
} }
@ -106,12 +108,12 @@ namespace detail {
typedef T value_type; typedef T value_type;
}; };
template <typename Iterator> template <typename Iterator>
bool iter_end ( Iterator current, Iterator last ) { return current == last; } bool iter_end ( Iterator current, Iterator last ) { return current == last; }
template <typename T> template <typename T>
bool ptr_end ( const T* ptr, const T* /*end*/ ) { return *ptr == '\0'; } bool ptr_end ( const T* ptr, const T* /*end*/ ) { return *ptr == '\0'; }
// What can we assume here about the inputs? // What can we assume here about the inputs?
// is std::iterator_traits<InputIterator>::value_type always 'char' ? // is std::iterator_traits<InputIterator>::value_type always 'char' ?
// Could it be wchar_t, say? Does it matter? // Could it be wchar_t, say? Does it matter?
@ -124,11 +126,11 @@ namespace detail {
// Need to make sure that we get can read that many chars here. // Need to make sure that we get can read that many chars here.
for ( std::size_t i = 0; i < 2 * sizeof ( T ); ++i, ++first ) { for ( std::size_t i = 0; i < 2 * sizeof ( T ); ++i, ++first ) {
if ( pred ( first, last )) if ( pred ( first, last ))
BOOST_THROW_EXCEPTION (not_enough_input ()); BOOST_THROW_EXCEPTION (not_enough_input ());
res = ( 16 * res ) + hex_char_to_int (*first); res = ( 16 * res ) + hex_char_to_int (*first);
} }
*out = res; *out = res;
return ++out; return ++out;
} }
@ -138,7 +140,7 @@ namespace detail {
/// \fn hex ( InputIterator first, InputIterator last, OutputIterator out ) /// \fn hex ( InputIterator first, InputIterator last, OutputIterator out )
/// \brief Converts a sequence of integral types into a hexadecimal sequence of characters. /// \brief Converts a sequence of integral types into a hexadecimal sequence of characters.
/// ///
/// \param first The start of the input sequence /// \param first The start of the input sequence
/// \param last One past the end of the input sequence /// \param last One past the end of the input sequence
/// \param out An output iterator to the results into /// \param out An output iterator to the results into
@ -148,14 +150,31 @@ template <typename InputIterator, typename OutputIterator>
typename boost::enable_if<boost::is_integral<typename detail::hex_iterator_traits<InputIterator>::value_type>, OutputIterator>::type typename boost::enable_if<boost::is_integral<typename detail::hex_iterator_traits<InputIterator>::value_type>, OutputIterator>::type
hex ( InputIterator first, InputIterator last, OutputIterator out ) { hex ( InputIterator first, InputIterator last, OutputIterator out ) {
for ( ; first != last; ++first ) for ( ; first != last; ++first )
out = detail::encode_one ( *first, out ); out = detail::encode_one ( *first, out, "0123456789ABCDEF" );
return out; 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;
}
/// \fn hex ( const T *ptr, OutputIterator out ) /// \fn hex ( const T *ptr, OutputIterator out )
/// \brief Converts a sequence of integral types into a hexadecimal sequence of characters. /// \brief Converts a sequence of integral types into a hexadecimal sequence of characters.
/// ///
/// \param ptr A pointer to a 0-terminated sequence of data. /// \param ptr A pointer to a 0-terminated sequence of data.
/// \param out An output iterator to the results into /// \param out An output iterator to the results into
/// \return The updated output iterator /// \return The updated output iterator
@ -164,13 +183,30 @@ template <typename T, typename OutputIterator>
typename boost::enable_if<boost::is_integral<T>, OutputIterator>::type typename boost::enable_if<boost::is_integral<T>, OutputIterator>::type
hex ( const T *ptr, OutputIterator out ) { hex ( const T *ptr, OutputIterator out ) {
while ( *ptr ) while ( *ptr )
out = detail::encode_one ( *ptr++, out ); out = detail::encode_one ( *ptr++, out, "0123456789ABCDEF" );
return out; 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 ) /// \fn hex ( const Range &r, OutputIterator out )
/// \brief Converts a sequence of integral types into a hexadecimal sequence of characters. /// \brief Converts a sequence of integral types into a hexadecimal sequence of characters.
/// ///
/// \param r The input range /// \param r The input range
/// \param out An output iterator to the results into /// \param out An output iterator to the results into
/// \return The updated output iterator /// \return The updated output iterator
@ -182,9 +218,23 @@ 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 ) /// \fn unhex ( InputIterator first, InputIterator last, OutputIterator out )
/// \brief Converts a sequence of hexadecimal characters into a sequence of integers. /// \brief Converts a sequence of hexadecimal characters into a sequence of integers.
/// ///
/// \param first The start of the input sequence /// \param first The start of the input sequence
/// \param last One past the end of the input sequence /// \param last One past the end of the input sequence
/// \param out An output iterator to the results into /// \param out An output iterator to the results into
@ -200,14 +250,13 @@ OutputIterator unhex ( InputIterator first, InputIterator last, OutputIterator o
/// \fn unhex ( const T *ptr, OutputIterator out ) /// \fn unhex ( const T *ptr, OutputIterator out )
/// \brief Converts a sequence of hexadecimal characters into a sequence of integers. /// \brief Converts a sequence of hexadecimal characters into a sequence of integers.
/// ///
/// \param ptr A pointer to a null-terminated input sequence. /// \param ptr A pointer to a null-terminated input sequence.
/// \param out An output iterator to the results into /// \param out An output iterator to the results into
/// \return The updated output iterator /// \return The updated output iterator
/// \note Based on the MySQL function of the same name /// \note Based on the MySQL function of the same name
template <typename T, typename OutputIterator> template <typename T, typename OutputIterator>
OutputIterator unhex ( const T *ptr, OutputIterator out ) { OutputIterator unhex ( const T *ptr, OutputIterator out ) {
typedef typename detail::hex_iterator_traits<OutputIterator>::value_type OutputType;
// If we run into the terminator while decoding, we will throw a // If we run into the terminator while decoding, we will throw a
// malformed input exception. It would be nicer to throw a 'Not enough input' // malformed input exception. It would be nicer to throw a 'Not enough input'
// exception - but how much extra work would that require? // exception - but how much extra work would that require?
@ -219,7 +268,7 @@ OutputIterator unhex ( const T *ptr, OutputIterator out ) {
/// \fn OutputIterator unhex ( const Range &r, OutputIterator out ) /// \fn OutputIterator unhex ( const Range &r, OutputIterator out )
/// \brief Converts a sequence of hexadecimal characters into a sequence of integers. /// \brief Converts a sequence of hexadecimal characters into a sequence of integers.
/// ///
/// \param r The input range /// \param r The input range
/// \param out An output iterator to the results into /// \param out An output iterator to the results into
/// \return The updated output iterator /// \return The updated output iterator
@ -232,7 +281,7 @@ OutputIterator unhex ( const Range &r, OutputIterator out ) {
/// \fn String hex ( const String &input ) /// \fn String hex ( const String &input )
/// \brief Converts a sequence of integral types into a hexadecimal sequence of characters. /// \brief Converts a sequence of integral types into a hexadecimal sequence of characters.
/// ///
/// \param input A container to be converted /// \param input A container to be converted
/// \return A container with the encoded text /// \return A container with the encoded text
template<typename String> template<typename String>
@ -243,9 +292,24 @@ String hex ( const String &input ) {
return output; 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 ) /// \fn String unhex ( const String &input )
/// \brief Converts a sequence of hexadecimal characters into a sequence of characters. /// \brief Converts a sequence of hexadecimal characters into a sequence of characters.
/// ///
/// \param input A container to be converted /// \param input A container to be converted
/// \return A container with the decoded text /// \return A container with the decoded text
template<typename String> template<typename String>

View File

@ -0,0 +1,161 @@
/*
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)
{
if(begin == end)
{
return true;
}
--end;
while(begin != end)
{
if(!(*begin == *end))
{
return false;
}
++begin;
if(begin == end)
{
break;
}
--end;
}
return true;
}
/// \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

@ -75,25 +75,27 @@ Requirements:
/// \param corpus_last One past the end of the data to search /// \param corpus_last One past the end of the data to search
/// ///
template <typename corpusIter> 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< BOOST_STATIC_ASSERT (( boost::is_same<
typename std::iterator_traits<patIter>::value_type, typename std::iterator_traits<patIter>::value_type,
typename std::iterator_traits<corpusIter>::value_type>::value )); 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 ( 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 corpus_first; // empty pattern matches at start 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 ); 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 the pattern is larger than the corpus, we can't find it!
if ( k_corpus_length < k_pattern_length ) if ( k_corpus_length < k_pattern_length )
return corpus_last; return std::make_pair(corpus_last, corpus_last);
// Do the search // Do the search
return this->do_search ( corpus_first, corpus_last ); return this->do_search ( corpus_first, corpus_last );
} }
template <typename Range> 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)); return (*this) (boost::begin(r), boost::end(r));
} }
@ -112,7 +114,8 @@ Requirements:
/// \param p A predicate used for the search comparisons. /// \param p A predicate used for the search comparisons.
/// ///
template <typename corpusIter> 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 ---- */ /* ---- Do the matching ---- */
corpusIter curPos = corpus_first; corpusIter curPos = corpus_first;
const corpusIter lastPos = corpus_last - k_pattern_length; const corpusIter lastPos = corpus_last - k_pattern_length;
@ -126,7 +129,7 @@ Requirements:
j--; j--;
// We matched - we're done! // We matched - we're done!
if ( j == 0 ) 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 // Since we didn't match, figure out how far to skip forward
@ -138,7 +141,7 @@ Requirements:
curPos += suffix_ [ j ]; curPos += suffix_ [ j ];
} }
return corpus_last; // We didn't find anything return std::make_pair(corpus_last, corpus_last); // We didn't find anything
} }
@ -211,7 +214,7 @@ Requirements:
/// \param pat_last One past the end of the data to search for /// \param pat_last One past the end of the data to search for
/// ///
template <typename patIter, typename corpusIter> template <typename patIter, typename corpusIter>
corpusIter boyer_moore_search ( std::pair<corpusIter, corpusIter> boyer_moore_search (
corpusIter corpus_first, corpusIter corpus_last, corpusIter corpus_first, corpusIter corpus_last,
patIter pat_first, patIter pat_last ) patIter pat_first, patIter pat_last )
{ {
@ -220,7 +223,7 @@ Requirements:
} }
template <typename PatternRange, typename corpusIter> 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 ) corpusIter corpus_first, corpusIter corpus_last, const PatternRange &pattern )
{ {
typedef typename boost::range_iterator<const PatternRange>::type pattern_iterator; typedef typename boost::range_iterator<const PatternRange>::type pattern_iterator;
@ -229,8 +232,9 @@ Requirements:
} }
template <typename patIter, typename CorpusRange> template <typename patIter, typename CorpusRange>
typename boost::lazy_disable_if_c< typename boost::disable_if_c<
boost::is_same<CorpusRange, patIter>::value, typename boost::range_iterator<CorpusRange> > boost::is_same<CorpusRange, patIter>::value,
std::pair<typename boost::range_iterator<CorpusRange>::type, typename boost::range_iterator<CorpusRange>::type> >
::type ::type
boyer_moore_search ( CorpusRange &corpus, patIter pat_first, patIter pat_last ) boyer_moore_search ( CorpusRange &corpus, patIter pat_first, patIter pat_last )
{ {
@ -239,7 +243,7 @@ Requirements:
} }
template <typename PatternRange, typename CorpusRange> 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 ) boyer_moore_search ( CorpusRange &corpus, const PatternRange &pattern )
{ {
typedef typename boost::range_iterator<const PatternRange>::type pattern_iterator; 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 () {} ~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 /// \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_first The start of the data to search (Random Access Iterator)
/// \param corpus_last One past the end of the data to search /// \param corpus_last One past the end of the data to search
/// \param p A predicate used for the search comparisons.
/// ///
template <typename corpusIter> 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< BOOST_STATIC_ASSERT (( boost::is_same<
typename std::iterator_traits<patIter>::value_type, typename std::iterator_traits<patIter>::value_type,
typename std::iterator_traits<corpusIter>::value_type>::value )); 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 ( 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 corpus_first; // empty pattern matches at start 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 ); 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 the pattern is larger than the corpus, we can't find it!
if ( k_corpus_length < k_pattern_length ) if ( k_corpus_length < k_pattern_length )
return corpus_last; return std::make_pair(corpus_last, corpus_last);
// Do the search // Do the search
return this->do_search ( corpus_first, corpus_last ); return this->do_search ( corpus_first, corpus_last );
} }
template <typename Range> 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)); 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 /// \param k_corpus_length The length of the corpus to search
/// ///
template <typename corpusIter> 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; corpusIter curPos = corpus_first;
const corpusIter lastPos = corpus_last - k_pattern_length; const corpusIter lastPos = corpus_last - k_pattern_length;
while ( curPos <= lastPos ) { while ( curPos <= lastPos ) {
@ -117,14 +119,14 @@ http://www-igm.univ-mlv.fr/%7Elecroq/string/node18.html
while ( pat_first [j] == curPos [j] ) { while ( pat_first [j] == curPos [j] ) {
// We matched - we're done! // We matched - we're done!
if ( j == 0 ) if ( j == 0 )
return curPos; return std::make_pair(curPos, curPos + k_pattern_length);
j--; j--;
} }
curPos += skip_ [ curPos [ k_pattern_length - 1 ]]; curPos += skip_ [ curPos [ k_pattern_length - 1 ]];
} }
return corpus_last; return std::make_pair(corpus_last, corpus_last);
} }
// \endcond // \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 /// \param pat_last One past the end of the data to search for
/// ///
template <typename patIter, typename corpusIter> template <typename patIter, typename corpusIter>
corpusIter boyer_moore_horspool_search ( std::pair<corpusIter, corpusIter> boyer_moore_horspool_search (
corpusIter corpus_first, corpusIter corpus_last, corpusIter corpus_first, corpusIter corpus_last,
patIter pat_first, patIter pat_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> 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 ) corpusIter corpus_first, corpusIter corpus_last, const PatternRange &pattern )
{ {
typedef typename boost::range_iterator<const PatternRange>::type pattern_iterator; 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> template <typename patIter, typename CorpusRange>
typename boost::lazy_disable_if_c< typename boost::disable_if_c<
boost::is_same<CorpusRange, patIter>::value, typename boost::range_iterator<CorpusRange> > boost::is_same<CorpusRange, patIter>::value,
std::pair<typename boost::range_iterator<CorpusRange>::type, typename boost::range_iterator<CorpusRange>::type> >
::type ::type
boyer_moore_horspool_search ( CorpusRange &corpus, patIter pat_first, patIter pat_last ) 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> 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 ) boyer_moore_horspool_search ( CorpusRange &corpus, const PatternRange &pattern )
{ {
typedef typename boost::range_iterator<const PatternRange>::type pattern_iterator; typedef typename boost::range_iterator<const PatternRange>::type pattern_iterator;

View File

@ -20,7 +20,11 @@
#include <boost/type_traits/remove_const.hpp> #include <boost/type_traits/remove_const.hpp>
#include <boost/array.hpp> #include <boost/array.hpp>
#include <boost/tr1/tr1/unordered_map> #ifdef BOOST_NO_CXX11_HDR_UNORDERED_MAP
#include <boost/unordered_map.hpp>
#else
#include <unordered_map>
#endif
#include <boost/algorithm/searching/detail/debugging.hpp> #include <boost/algorithm/searching/detail/debugging.hpp>
@ -35,7 +39,11 @@ namespace boost { namespace algorithm { namespace detail {
template<typename key_type, typename value_type> template<typename key_type, typename value_type>
class skip_table<key_type, value_type, false> { class skip_table<key_type, value_type, false> {
private: private:
typedef std::tr1::unordered_map<key_type, value_type> skip_map; #ifdef BOOST_NO_CXX11_HDR_UNORDERED_MAP
typedef boost::unordered_map<key_type, value_type> skip_map;
#else
typedef std::unordered_map<key_type, value_type> skip_map;
#endif
const value_type k_default_value; const value_type k_default_value;
skip_map skip_; skip_map skip_;
@ -71,7 +79,7 @@ namespace boost { namespace algorithm { namespace detail {
skip_map skip_; skip_map skip_;
const value_type k_default_value; const value_type k_default_value;
public: 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 ); 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. /// \param p A predicate used for the search comparisons.
/// ///
template <typename corpusIter> 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< BOOST_STATIC_ASSERT (( boost::is_same<
typename std::iterator_traits<patIter>::value_type, typename std::iterator_traits<patIter>::value_type,
typename std::iterator_traits<corpusIter>::value_type>::value )); 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 ); 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 the pattern is larger than the corpus, we can't find it!
if ( k_corpus_length < k_pattern_length ) 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 ); return do_search ( corpus_first, corpus_last, k_corpus_length );
} }
template <typename Range> 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)); 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. /// \param p A predicate used for the search comparisons.
/// ///
template <typename corpusIter> 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 k_corpus_length ) const {
difference_type match_start = 0; // position in the corpus that we're matching 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 ( match_start <= last_match ) {
while ( pat_first [ idx ] == corpus_first [ match_start + idx ] ) { while ( pat_first [ idx ] == corpus_first [ match_start + idx ] ) {
if ( ++idx == k_pattern_length ) 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 // Figure out where to start searching again
// assert ( idx - skip_ [ idx ] > 0 ); // we're always moving forward // assert ( idx - skip_ [ idx ] > 0 ); // we're always moving forward
@ -146,7 +150,7 @@ namespace boost { namespace algorithm {
#endif #endif
// We didn't find anything // We didn't find anything
return corpus_last; return std::make_pair(corpus_last, corpus_last);
} }
@ -202,7 +206,7 @@ namespace boost { namespace algorithm {
/// \param pat_last One past the end of the data to search for /// \param pat_last One past the end of the data to search for
/// ///
template <typename patIter, typename corpusIter> template <typename patIter, typename corpusIter>
corpusIter knuth_morris_pratt_search ( std::pair<corpusIter, corpusIter> knuth_morris_pratt_search (
corpusIter corpus_first, corpusIter corpus_last, corpusIter corpus_first, corpusIter corpus_last,
patIter pat_first, patIter pat_last ) patIter pat_first, patIter pat_last )
{ {
@ -211,7 +215,7 @@ namespace boost { namespace algorithm {
} }
template <typename PatternRange, typename corpusIter> 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 ) corpusIter corpus_first, corpusIter corpus_last, const PatternRange &pattern )
{ {
typedef typename boost::range_iterator<const PatternRange>::type pattern_iterator; typedef typename boost::range_iterator<const PatternRange>::type pattern_iterator;
@ -220,8 +224,9 @@ namespace boost { namespace algorithm {
} }
template <typename patIter, typename CorpusRange> template <typename patIter, typename CorpusRange>
typename boost::lazy_disable_if_c< typename boost::disable_if_c<
boost::is_same<CorpusRange, patIter>::value, typename boost::range_iterator<CorpusRange> > boost::is_same<CorpusRange, patIter>::value,
std::pair<typename boost::range_iterator<CorpusRange>::type, typename boost::range_iterator<CorpusRange>::type> >
::type ::type
knuth_morris_pratt_search ( CorpusRange &corpus, patIter pat_first, patIter pat_last ) 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> 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 ) knuth_morris_pratt_search ( CorpusRange &corpus, const PatternRange &pattern )
{ {
typedef typename boost::range_iterator<const PatternRange>::type pattern_iterator; 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

@ -12,7 +12,7 @@
#define BOOST_STRING_CONCEPT_HPP #define BOOST_STRING_CONCEPT_HPP
#include <boost/concept_check.hpp> #include <boost/concept_check.hpp>
#include <boost/range/iterator_range.hpp> #include <boost/range/iterator_range_core.hpp>
#include <boost/range/begin.hpp> #include <boost/range/begin.hpp>
#include <boost/range/end.hpp> #include <boost/range/end.hpp>

View File

@ -12,7 +12,7 @@
#define BOOST_STRING_FIND_FORMAT_DETAIL_HPP #define BOOST_STRING_FIND_FORMAT_DETAIL_HPP
#include <boost/algorithm/string/config.hpp> #include <boost/algorithm/string/config.hpp>
#include <boost/range/iterator_range.hpp> #include <boost/range/iterator_range_core.hpp>
#include <boost/range/const_iterator.hpp> #include <boost/range/const_iterator.hpp>
#include <boost/range/iterator.hpp> #include <boost/range/iterator.hpp>
#include <boost/algorithm/string/detail/find_format_store.hpp> #include <boost/algorithm/string/detail/find_format_store.hpp>

View File

@ -12,7 +12,7 @@
#define BOOST_STRING_FIND_FORMAT_ALL_DETAIL_HPP #define BOOST_STRING_FIND_FORMAT_ALL_DETAIL_HPP
#include <boost/algorithm/string/config.hpp> #include <boost/algorithm/string/config.hpp>
#include <boost/range/iterator_range.hpp> #include <boost/range/iterator_range_core.hpp>
#include <boost/range/const_iterator.hpp> #include <boost/range/const_iterator.hpp>
#include <boost/range/value_type.hpp> #include <boost/range/value_type.hpp>
#include <boost/algorithm/string/detail/find_format_store.hpp> #include <boost/algorithm/string/detail/find_format_store.hpp>

View File

@ -12,7 +12,7 @@
#define BOOST_STRING_FIND_FORMAT_STORE_DETAIL_HPP #define BOOST_STRING_FIND_FORMAT_STORE_DETAIL_HPP
#include <boost/algorithm/string/config.hpp> #include <boost/algorithm/string/config.hpp>
#include <boost/range/iterator_range.hpp> #include <boost/range/iterator_range_core.hpp>
namespace boost { namespace boost {
namespace algorithm { namespace algorithm {

View File

@ -12,7 +12,7 @@
#define BOOST_STRING_FIND_ITERATOR_DETAIL_HPP #define BOOST_STRING_FIND_ITERATOR_DETAIL_HPP
#include <boost/algorithm/string/config.hpp> #include <boost/algorithm/string/config.hpp>
#include <boost/range/iterator_range.hpp> #include <boost/range/iterator_range_core.hpp>
#include <boost/iterator/iterator_facade.hpp> #include <boost/iterator/iterator_facade.hpp>
#include <boost/iterator/iterator_categories.hpp> #include <boost/iterator/iterator_categories.hpp>
#include <boost/function.hpp> #include <boost/function.hpp>

View File

@ -15,7 +15,7 @@
#include <boost/algorithm/string/constants.hpp> #include <boost/algorithm/string/constants.hpp>
#include <boost/detail/iterator.hpp> #include <boost/detail/iterator.hpp>
#include <boost/range/iterator_range.hpp> #include <boost/range/iterator_range_core.hpp>
#include <boost/range/begin.hpp> #include <boost/range/begin.hpp>
#include <boost/range/end.hpp> #include <boost/range/end.hpp>
#include <boost/range/empty.hpp> #include <boost/range/empty.hpp>
@ -142,7 +142,6 @@ namespace boost {
ForwardIteratorT End, ForwardIteratorT End,
std::forward_iterator_tag ) const std::forward_iterator_tag ) const
{ {
typedef ForwardIteratorT input_iterator_type;
typedef iterator_range<ForwardIteratorT> result_type; typedef iterator_range<ForwardIteratorT> result_type;
first_finder_type first_finder( first_finder_type first_finder(
@ -263,7 +262,6 @@ namespace boost {
ForwardIteratorT End, ForwardIteratorT End,
unsigned int N) const unsigned int N) const
{ {
typedef ForwardIteratorT input_iterator_type;
typedef iterator_range<ForwardIteratorT> result_type; typedef iterator_range<ForwardIteratorT> result_type;
// Sanity check // Sanity check
@ -298,7 +296,6 @@ namespace boost {
ForwardIteratorT End, ForwardIteratorT End,
unsigned int N) const unsigned int N) const
{ {
typedef ForwardIteratorT input_iterator_type;
typedef iterator_range<ForwardIteratorT> result_type; typedef iterator_range<ForwardIteratorT> result_type;
// Sanity check // Sanity check
@ -362,7 +359,6 @@ namespace boost {
unsigned int N, unsigned int N,
std::random_access_iterator_tag ) std::random_access_iterator_tag )
{ {
typedef ForwardIteratorT input_iterator_type;
typedef iterator_range<ForwardIteratorT> result_type; typedef iterator_range<ForwardIteratorT> result_type;
if ( (End<=Begin) || ( static_cast<unsigned int>(End-Begin) < N ) ) if ( (End<=Begin) || ( static_cast<unsigned int>(End-Begin) < N ) )
@ -436,7 +432,6 @@ namespace boost {
unsigned int N, unsigned int N,
std::random_access_iterator_tag ) std::random_access_iterator_tag )
{ {
typedef ForwardIteratorT input_iterator_type;
typedef iterator_range<ForwardIteratorT> result_type; typedef iterator_range<ForwardIteratorT> result_type;
if ( (End<=Begin) || ( static_cast<unsigned int>(End-Begin) < N ) ) if ( (End<=Begin) || ( static_cast<unsigned int>(End-Begin) < N ) )
@ -627,8 +622,6 @@ namespace boost {
{ {
#if BOOST_WORKAROUND( __MWERKS__, <= 0x3003 ) #if BOOST_WORKAROUND( __MWERKS__, <= 0x3003 )
return iterator_range<const ForwardIterator2T>(this->m_Range); return iterator_range<const ForwardIterator2T>(this->m_Range);
#elif BOOST_WORKAROUND(BOOST_MSVC, <= 1300)
return iterator_range<ForwardIterator2T>(m_Range.begin(), m_Range.end());
#else #else
return m_Range; return m_Range;
#endif #endif

View File

@ -14,7 +14,7 @@
#include <boost/algorithm/string/config.hpp> #include <boost/algorithm/string/config.hpp>
#include <boost/regex.hpp> #include <boost/regex.hpp>
#include <boost/range/iterator_range.hpp> #include <boost/range/iterator_range_core.hpp>
#include <boost/range/begin.hpp> #include <boost/range/begin.hpp>
#include <boost/range/end.hpp> #include <boost/range/end.hpp>

View File

@ -12,7 +12,7 @@
#define BOOST_STRING_FORMATTER_DETAIL_HPP #define BOOST_STRING_FORMATTER_DETAIL_HPP
#include <boost/range/iterator_range.hpp> #include <boost/range/iterator_range_core.hpp>
#include <boost/range/begin.hpp> #include <boost/range/begin.hpp>
#include <boost/range/end.hpp> #include <boost/range/end.hpp>
#include <boost/range/const_iterator.hpp> #include <boost/range/const_iterator.hpp>

View File

@ -13,7 +13,7 @@
#include <boost/algorithm/string/config.hpp> #include <boost/algorithm/string/config.hpp>
#include <functional> #include <functional>
#include <boost/range/iterator_range.hpp> #include <boost/range/iterator_range_core.hpp>
namespace boost { namespace boost {
namespace algorithm { namespace algorithm {

View File

@ -13,7 +13,7 @@
#include <boost/algorithm/string/config.hpp> #include <boost/algorithm/string/config.hpp>
#include <boost/range/iterator_range.hpp> #include <boost/range/iterator_range_core.hpp>
#include <boost/range/begin.hpp> #include <boost/range/begin.hpp>
#include <boost/range/end.hpp> #include <boost/range/end.hpp>
#include <boost/range/iterator.hpp> #include <boost/range/iterator.hpp>

View File

@ -13,7 +13,7 @@
#include <boost/algorithm/string/config.hpp> #include <boost/algorithm/string/config.hpp>
#include <boost/range/iterator_range.hpp> #include <boost/range/iterator_range_core.hpp>
#include <boost/range/begin.hpp> #include <boost/range/begin.hpp>
#include <boost/range/end.hpp> #include <boost/range/end.hpp>
#include <boost/range/iterator.hpp> #include <boost/range/iterator.hpp>

View File

@ -13,7 +13,7 @@
#include <deque> #include <deque>
#include <boost/detail/iterator.hpp> #include <boost/detail/iterator.hpp>
#include <boost/range/iterator_range.hpp> #include <boost/range/iterator_range_core.hpp>
#include <boost/range/begin.hpp> #include <boost/range/begin.hpp>
#include <boost/range/end.hpp> #include <boost/range/end.hpp>
#include <boost/range/const_iterator.hpp> #include <boost/range/const_iterator.hpp>

View File

@ -15,7 +15,7 @@
#include <boost/iterator/iterator_facade.hpp> #include <boost/iterator/iterator_facade.hpp>
#include <boost/iterator/iterator_categories.hpp> #include <boost/iterator/iterator_categories.hpp>
#include <boost/range/iterator_range.hpp> #include <boost/range/iterator_range_core.hpp>
#include <boost/range/begin.hpp> #include <boost/range/begin.hpp>
#include <boost/range/end.hpp> #include <boost/range/end.hpp>
#include <boost/range/iterator.hpp> #include <boost/range/iterator.hpp>
@ -132,12 +132,7 @@ namespace boost {
// increment // increment
void increment() void increment()
{ {
if(m_Match.begin() == m_Match.end()) m_Match=this->do_find(m_Match.end(),m_End);
m_Match=this->do_find(m_Match.end(),m_End);
else {
input_iterator_type last = m_Match.begin();
m_Match=this->do_find(++last,m_End);
}
} }
// comparison // comparison
@ -235,7 +230,12 @@ namespace boost {
\post eof()==true \post eof()==true
*/ */
split_iterator() {} split_iterator() :
m_Next(),
m_End(),
m_bEof(true)
{}
//! Copy constructor //! Copy constructor
/*! /*!
Construct a copy of the split_iterator Construct a copy of the split_iterator

View File

@ -13,7 +13,7 @@
#include <boost/algorithm/string/config.hpp> #include <boost/algorithm/string/config.hpp>
#include <boost/range/iterator_range.hpp> #include <boost/range/iterator_range_core.hpp>
#include <boost/range/begin.hpp> #include <boost/range/begin.hpp>
#include <boost/range/end.hpp> #include <boost/range/end.hpp>
#include <boost/range/iterator.hpp> #include <boost/range/iterator.hpp>

View File

@ -13,7 +13,7 @@
#include <boost/detail/iterator.hpp> #include <boost/detail/iterator.hpp>
#include <boost/range/value_type.hpp> #include <boost/range/value_type.hpp>
#include <boost/range/iterator_range.hpp> #include <boost/range/iterator_range_core.hpp>
#include <boost/range/as_literal.hpp> #include <boost/range/as_literal.hpp>
#include <boost/algorithm/string/detail/formatter.hpp> #include <boost/algorithm/string/detail/formatter.hpp>

View File

@ -16,7 +16,7 @@
#include <iterator> #include <iterator>
#include <boost/iterator/transform_iterator.hpp> #include <boost/iterator/transform_iterator.hpp>
#include <boost/range/iterator_range.hpp> #include <boost/range/iterator_range_core.hpp>
#include <boost/range/begin.hpp> #include <boost/range/begin.hpp>
#include <boost/range/end.hpp> #include <boost/range/end.hpp>
#include <boost/range/iterator.hpp> #include <boost/range/iterator.hpp>

View File

@ -17,7 +17,7 @@
#include <boost/range/iterator.hpp> #include <boost/range/iterator.hpp>
#include <boost/range/const_iterator.hpp> #include <boost/range/const_iterator.hpp>
#include <boost/range/as_literal.hpp> #include <boost/range/as_literal.hpp>
#include <boost/range/iterator_range.hpp> #include <boost/range/iterator_range_core.hpp>
#include <boost/algorithm/string/compare.hpp> #include <boost/algorithm/string/compare.hpp>
#include <boost/algorithm/string/find.hpp> #include <boost/algorithm/string/find.hpp>

View File

@ -14,7 +14,7 @@
#include <boost/algorithm/string/config.hpp> #include <boost/algorithm/string/config.hpp>
#include <boost/regex.hpp> #include <boost/regex.hpp>
#include <boost/range/iterator_range.hpp> #include <boost/range/iterator_range_core.hpp>
#include <boost/range/begin.hpp> #include <boost/range/begin.hpp>
#include <boost/range/end.hpp> #include <boost/range/end.hpp>
#include <boost/range/iterator.hpp> #include <boost/range/iterator.hpp>

View File

@ -13,7 +13,7 @@
#include <boost/algorithm/string/config.hpp> #include <boost/algorithm/string/config.hpp>
#include <boost/range/iterator_range.hpp> #include <boost/range/iterator_range_core.hpp>
#include <boost/range/begin.hpp> #include <boost/range/begin.hpp>
#include <boost/range/end.hpp> #include <boost/range/end.hpp>
#include <boost/range/iterator.hpp> #include <boost/range/iterator.hpp>
@ -401,7 +401,6 @@ namespace boost {
\param Search A substring to be searched for \param Search A substring to be searched for
\param Format A substitute string \param Format A substitute string
\param Loc A locale used for case insensitive comparison \param Loc A locale used for case insensitive comparison
\return A reference to the modified input
*/ */
template<typename SequenceT, typename Range1T, typename Range2T> template<typename SequenceT, typename Range1T, typename Range2T>
inline void ireplace_last( inline void ireplace_last(
@ -643,7 +642,6 @@ namespace boost {
\param Input An input string \param Input An input string
\param Search A substring to be searched for \param Search A substring to be searched for
\param Format A substitute string \param Format A substitute string
\return A reference to the modified input
*/ */
template<typename SequenceT, typename Range1T, typename Range2T> template<typename SequenceT, typename Range1T, typename Range2T>
inline void replace_all( inline void replace_all(

View File

@ -36,47 +36,6 @@ namespace boost {
// sequence traits -----------------------------------------------// // sequence traits -----------------------------------------------//
#ifdef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
//! Native replace tester
/*!
Declare an override of this tester function with return
type boost::string_algo::yes_type for a sequence with this property.
\return yes_type if the container has basic_string like native replace
method.
*/
no_type has_native_replace_tester(...);
//! Stable iterators tester
/*!
Declare an override of this tester function with return
type boost::string_algo::yes_type for a sequence with this property.
\return yes_type if the sequence's insert/replace/erase methods do not invalidate
existing iterators.
*/
no_type has_stable_iterators_tester(...);
//! const time insert tester
/*!
Declare an override of this tester function with return
type boost::string_algo::yes_type for a sequence with this property.
\return yes_type if the sequence's insert method is working in constant time
*/
no_type has_const_time_insert_tester(...);
//! const time erase tester
/*!
Declare an override of this tester function with return
type boost::string_algo::yes_type for a sequence with this property.
\return yes_type if the sequence's erase method is working in constant time
*/
no_type has_const_time_erase_tester(...);
#endif //BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
//! Native replace trait //! Native replace trait
/*! /*!
@ -86,20 +45,12 @@ namespace boost {
class has_native_replace class has_native_replace
{ {
#ifdef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
private:
static T* t;
public:
BOOST_STATIC_CONSTANT(bool, value=(
sizeof(has_native_replace_tester(t))==sizeof(yes_type) ) );
#else // BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
public: public:
# if BOOST_WORKAROUND( __IBMCPP__, <= 600 ) # if BOOST_WORKAROUND( __IBMCPP__, <= 600 )
enum { value = false }; enum { value = false };
# else # else
BOOST_STATIC_CONSTANT(bool, value=false); BOOST_STATIC_CONSTANT(bool, value=false);
# endif // BOOST_WORKAROUND( __IBMCPP__, <= 600 ) # endif // BOOST_WORKAROUND( __IBMCPP__, <= 600 )
#endif // BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
typedef mpl::bool_<has_native_replace<T>::value> type; typedef mpl::bool_<has_native_replace<T>::value> type;
@ -114,20 +65,12 @@ namespace boost {
template< typename T > template< typename T >
class has_stable_iterators class has_stable_iterators
{ {
#ifdef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
private:
static T* t;
public:
BOOST_STATIC_CONSTANT(bool, value=(
sizeof(has_stable_iterators_tester(t))==sizeof(yes_type) ) );
#else // BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
public: public:
# if BOOST_WORKAROUND( __IBMCPP__, <= 600 ) # if BOOST_WORKAROUND( __IBMCPP__, <= 600 )
enum { value = false }; enum { value = false };
# else # else
BOOST_STATIC_CONSTANT(bool, value=false); BOOST_STATIC_CONSTANT(bool, value=false);
# endif // BOOST_WORKAROUND( __IBMCPP__, <= 600 ) # endif // BOOST_WORKAROUND( __IBMCPP__, <= 600 )
#endif // BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
typedef mpl::bool_<has_stable_iterators<T>::value> type; typedef mpl::bool_<has_stable_iterators<T>::value> type;
}; };
@ -141,20 +84,12 @@ namespace boost {
template< typename T > template< typename T >
class has_const_time_insert class has_const_time_insert
{ {
#ifdef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
private:
static T* t;
public:
BOOST_STATIC_CONSTANT(bool, value=(
sizeof(has_const_time_insert_tester(t))==sizeof(yes_type) ) );
#else // BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
public: public:
# if BOOST_WORKAROUND( __IBMCPP__, <= 600 ) # if BOOST_WORKAROUND( __IBMCPP__, <= 600 )
enum { value = false }; enum { value = false };
# else # else
BOOST_STATIC_CONSTANT(bool, value=false); BOOST_STATIC_CONSTANT(bool, value=false);
# endif // BOOST_WORKAROUND( __IBMCPP__, <= 600 ) # endif // BOOST_WORKAROUND( __IBMCPP__, <= 600 )
#endif // BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
typedef mpl::bool_<has_const_time_insert<T>::value> type; typedef mpl::bool_<has_const_time_insert<T>::value> type;
}; };
@ -168,20 +103,12 @@ namespace boost {
template< typename T > template< typename T >
class has_const_time_erase class has_const_time_erase
{ {
#ifdef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
private:
static T* t;
public:
BOOST_STATIC_CONSTANT(bool, value=(
sizeof(has_const_time_erase_tester(t))==sizeof(yes_type) ) );
#else // BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
public: public:
# if BOOST_WORKAROUND( __IBMCPP__, <= 600 ) # if BOOST_WORKAROUND( __IBMCPP__, <= 600 )
enum { value = false }; enum { value = false };
# else # else
BOOST_STATIC_CONSTANT(bool, value=false); BOOST_STATIC_CONSTANT(bool, value=false);
# endif // BOOST_WORKAROUND( __IBMCPP__, <= 600 ) # endif // BOOST_WORKAROUND( __IBMCPP__, <= 600 )
#endif // BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
typedef mpl::bool_<has_const_time_erase<T>::value> type; typedef mpl::bool_<has_const_time_erase<T>::value> type;
}; };

View File

@ -20,22 +20,6 @@ namespace boost {
// std::list<> traits -----------------------------------------------// // std::list<> traits -----------------------------------------------//
#ifdef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
// stable iterators tester
template<typename T, typename AllocT>
yes_type has_stable_iterators_tester( const ::std::list<T,AllocT>* );
// const time insert tester
template<typename T, typename AllocT>
yes_type has_const_time_insert_tester( const ::std::list<T,AllocT>* );
// const time erase tester
template<typename T, typename AllocT>
yes_type has_const_time_erase_tester( const ::std::list<T,AllocT>* );
#else // BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
// stable iterators trait // stable iterators trait
template<typename T, typename AllocT> template<typename T, typename AllocT>
@ -75,7 +59,6 @@ namespace boost {
#endif // BOOST_WORKAROUND( __IBMCPP__, <= 600 ) #endif // BOOST_WORKAROUND( __IBMCPP__, <= 600 )
typedef mpl::bool_<has_const_time_erase<T>::value> type; typedef mpl::bool_<has_const_time_erase<T>::value> type;
}; };
#endif
} // namespace algorithm } // namespace algorithm

View File

@ -20,25 +20,6 @@ namespace boost {
// SGI's std::rope<> traits -----------------------------------------------// // SGI's std::rope<> traits -----------------------------------------------//
#ifdef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
// native replace tester
template<typename T, typename TraitsT, typename AllocT>
yes_type has_native_replace_tester( const std::rope<T, TraitsT, AllocT>* );
// stable iterators tester
template<typename T, typename TraitsT, typename AllocT>
yes_type has_stable_iterators_tester( const std::rope<T, TraitsT, AllocT>* );
// const time insert tester
template<typename T, typename TraitsT, typename AllocT>
yes_type has_const_time_insert_tester( const std::rope<T, TraitsT, AllocT>* );
// const time erase tester
template<typename T, typename TraitsT, typename AllocT>
yes_type has_const_time_erase_tester( const std::rope<T, TraitsT, AllocT>* );
#else // BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
// native replace trait // native replace trait
template<typename T, typename TraitsT, typename AllocT> template<typename T, typename TraitsT, typename AllocT>
@ -91,7 +72,6 @@ namespace boost {
#endif // BOOST_WORKAROUND( __IBMCPP__, <= 600 ) #endif // BOOST_WORKAROUND( __IBMCPP__, <= 600 )
typedef mpl::bool_<value> type; typedef mpl::bool_<value> type;
}; };
#endif
} // namespace algorithm } // namespace algorithm

View File

@ -21,21 +21,6 @@ namespace boost {
// SGI's std::slist<> traits -----------------------------------------------// // SGI's std::slist<> traits -----------------------------------------------//
#ifdef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
// stable iterators tester
template<typename T, typename AllocT>
yes_type has_stable_iterators_tester( const BOOST_STD_EXTENSION_NAMESPACE::slist<T,AllocT>* );
// const time insert tester
template<typename T, typename AllocT>
yes_type has_const_time_insert_tester( const BOOST_STD_EXTENSION_NAMESPACE::slist<T,AllocT>* );
// const time erase tester
template<typename T, typename AllocT>
yes_type has_const_time_erase_tester( const BOOST_STD_EXTENSION_NAMESPACE::slist<T,AllocT>* );
#else // BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
// stable iterators trait // stable iterators trait
template<typename T, typename AllocT> template<typename T, typename AllocT>
@ -75,7 +60,6 @@ namespace boost {
#endif // BOOST_WORKAROUND( __IBMCPP__, <= 600 ) #endif // BOOST_WORKAROUND( __IBMCPP__, <= 600 )
typedef mpl::bool_<has_const_time_erase<T>::value> type; typedef mpl::bool_<has_const_time_erase<T>::value> type;
}; };
#endif
} // namespace algorithm } // namespace algorithm

View File

@ -20,13 +20,6 @@ namespace boost {
// std::basic_string<> traits -----------------------------------------------// // std::basic_string<> traits -----------------------------------------------//
#ifdef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
// native replace tester
template<typename T, typename TraitsT, typename AllocT>
yes_type has_native_replace_tester( const std::basic_string<T, TraitsT, AllocT>* );
#else // BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
// native replace trait // native replace trait
template<typename T, typename TraitsT, typename AllocT> template<typename T, typename TraitsT, typename AllocT>
@ -43,7 +36,6 @@ namespace boost {
}; };
#endif // BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
} // namespace algorithm } // namespace algorithm
} // namespace boost } // namespace boost

View File

@ -17,7 +17,7 @@
#include <boost/range/end.hpp> #include <boost/range/end.hpp>
#include <boost/range/const_iterator.hpp> #include <boost/range/const_iterator.hpp>
#include <boost/range/as_literal.hpp> #include <boost/range/as_literal.hpp>
#include <boost/range/iterator_range.hpp> #include <boost/range/iterator_range_core.hpp>
#include <boost/algorithm/string/detail/trim.hpp> #include <boost/algorithm/string/detail/trim.hpp>
#include <boost/algorithm/string/classification.hpp> #include <boost/algorithm/string/classification.hpp>

47
meta/libraries.json Normal file
View File

@ -0,0 +1,47 @@
[
{
"key": "algorithm",
"name": "Algorithm",
"authors": [
"Marshall Clow"
],
"description": "A collection of useful generic algorithms.",
"category": [
"Algorithms"
],
"maintainers": [
"Marshall Clow <marshall -at- idio.com>"
]
},
{
"key": "algorithm/minmax",
"name": "Min-Max",
"authors": [
"Hervé Brönnimann"
],
"description": "Standard library extensions for simultaneous min/max and min/max element computations.",
"documentation": "minmax/",
"category": [
"Algorithms"
],
"maintainers": [
"Marshall Clow <marshall -at- idio.com>"
]
},
{
"key": "algorithm/string",
"name": "String Algo",
"authors": [
"Pavol Droba"
],
"description": "String algorithms library.",
"documentation": "string/",
"category": [
"Algorithms",
"String"
],
"maintainers": [
"Marshall Clow <marshall -at- idio.com>"
]
}
]

View File

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

View File

@ -181,6 +181,21 @@ void find_test()
( (cv_result.begin()-str1.begin()) == 3) && ( (cv_result.begin()-str1.begin()) == 3) &&
( (cv_result.end()-str1.begin()) == 6) ); ( (cv_result.end()-str1.begin()) == 6) );
string s1("abc def ghi jkl");
find_iterator<string::iterator> fEnd;
find_iterator<string::iterator> fxIt = make_find_iterator(s1,
token_finder(is_alnum(), token_compress_on));
BOOST_CHECK((fxIt != fEnd) && (*fxIt == string("abc")));
++fxIt;
BOOST_CHECK((fxIt != fEnd) && (*fxIt == string("def")));
++fxIt;
BOOST_CHECK((fxIt != fEnd) && (*fxIt == string("ghi")));
++fxIt;
BOOST_CHECK((fxIt != fEnd) && (*fxIt == string("jkl")));
++fxIt;
BOOST_CHECK(fxIt == fEnd);
nc_result=find_token( str1, is_any_of("abc"), token_compress_off ); nc_result=find_token( str1, is_any_of("abc"), token_compress_off );
BOOST_CHECK( BOOST_CHECK(
( (nc_result.begin()-str1.begin()) == 3) && ( (nc_result.begin()-str1.begin()) == 3) &&
@ -251,19 +266,6 @@ void find_test()
osstr << find_first( str1, "abc" ); osstr << find_first( str1, "abc" );
BOOST_CHECK( osstr.str()=="abc" ); BOOST_CHECK( osstr.str()=="abc" );
// Empty string test
BOOST_TEST_CHECKPOINT( "overlapping" );
std::string overlap_target("aaaa");
std::vector<boost::iterator_range<std::string::iterator> > overlap_results;
boost::algorithm::find_all(overlap_results, overlap_target, string("aaa"));
BOOST_CHECK( overlap_results.size() == 2 );
std::string overlap_target2("aaaabbbbaaaa");
boost::algorithm::find_all(overlap_results, overlap_target2, string("bb"));
BOOST_CHECK( overlap_results.size() == 3 );
boost::algorithm::find_all(overlap_results, overlap_target2, string("aa"));
BOOST_CHECK( overlap_results.size() == 6 );
} }
// test main // test main

View File

@ -46,7 +46,7 @@ void iterator_test()
const char* pch1="xx-abc--xx-abb"; const char* pch1="xx-abc--xx-abb";
vector<string> tokens; vector<string> tokens;
vector< vector<int> > vtokens; vector< vector<int> > vtokens;
// find_all tests // find_all tests
find_all( find_all(
tokens, tokens,
@ -182,7 +182,7 @@ void iterator_test()
BOOST_CHECK(siter==split_iterator<string::iterator>()); BOOST_CHECK(siter==split_iterator<string::iterator>());
// Make sure we work with forward iterators // Make sure we work with forward iterators
// See bug #7989 // See bug #7989
list<char> l1; list<char> l1;
find_iterator<list<char>::iterator> liter=make_find_iterator(l1, first_finder("xx")); find_iterator<list<char>::iterator> liter=make_find_iterator(l1, first_finder("xx"));
} }

View File

@ -27,8 +27,10 @@ alias unit_test_framework
[ compile-fail search_fail2.cpp : : : : ] [ compile-fail search_fail2.cpp : : : : ]
[ compile-fail search_fail3.cpp : : : : ] [ compile-fail search_fail3.cpp : : : : ]
# Clamp tests # Misc tests
[ run clamp_test.cpp unit_test_framework : : : : clamp_test ] [ run clamp_test.cpp unit_test_framework : : : : clamp_test ]
[ run power_test.cpp unit_test_framework : : : : power_test ]
[ compile-fail power_fail1.cpp : : : : ]
# Cxx11 tests # Cxx11 tests
[ run all_of_test.cpp unit_test_framework : : : : all_of_test ] [ run all_of_test.cpp unit_test_framework : : : : all_of_test ]
@ -62,6 +64,12 @@ alias unit_test_framework
[ run gather_test1.cpp unit_test_framework : : : : gather_test1 ] [ run gather_test1.cpp unit_test_framework : : : : gather_test1 ]
[ compile-fail gather_fail1.cpp ] [ 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 ]
; ;
} }

View File

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

View File

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

View File

@ -42,11 +42,11 @@ void test_ints () {
std::vector<int> v; std::vector<int> v;
std::list<int> l; std::list<int> l;
v.clear (); v.reserve ( 10 ); v.clear (); v.resize ( 10 );
boost::algorithm::iota ( v.begin (), v.end (), 23 ); boost::algorithm::iota ( v.begin (), v.end (), 23 );
BOOST_CHECK ( test_iota_results ( v.begin (), v.end (), 23 )); BOOST_CHECK ( test_iota_results ( v.begin (), v.end (), 23 ));
v.clear (); v.reserve ( 19 ); v.clear (); v.resize ( 19 );
boost::algorithm::iota ( v, 18 ); boost::algorithm::iota ( v, 18 );
BOOST_CHECK ( test_iota_results ( v, 18 )); BOOST_CHECK ( test_iota_results ( v, 18 ));
@ -54,6 +54,10 @@ void test_ints () {
boost::algorithm::iota_n ( std::back_inserter(v), 99, 20 ); boost::algorithm::iota_n ( std::back_inserter(v), 99, 20 );
BOOST_CHECK ( test_iota_results ( v, 99 )); BOOST_CHECK ( test_iota_results ( v, 99 ));
v.clear ();
boost::algorithm::iota_n ( std::back_inserter(v), 99, 0 );
BOOST_CHECK ( v.size() == 0 );
/* /*
l.clear (); l.reserve ( 5 ); l.clear (); l.reserve ( 5 );
boost::algorithm::iota ( l.begin (), l.end (), 123 ); boost::algorithm::iota ( l.begin (), l.end (), 123 );

View File

@ -0,0 +1,79 @@
/*
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;
}
};
#define Begin(arr) (arr)
#define End(arr) (arr+(sizeof(arr)/(sizeof(arr[0]))))
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(Begin(oddNonPalindrome), End(oddNonPalindrome)));
BOOST_CHECK ( ba::is_palindrome(Begin(oddPalindrome), End(oddPalindrome)));
BOOST_CHECK ( ba::is_palindrome(Begin(evenPalindrome), End(evenPalindrome)));
BOOST_CHECK (!ba::is_palindrome(Begin(evenNonPalindrome), End(evenNonPalindrome)));
//Test the custom comparators
BOOST_CHECK ( ba::is_palindrome(empty.begin(), empty.end(), functorComparator()));
BOOST_CHECK (!ba::is_palindrome(Begin(oddNonPalindrome), End(oddNonPalindrome), funcComparator<int>));
BOOST_CHECK ( ba::is_palindrome(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

@ -11,6 +11,7 @@
#include <boost/config.hpp> #include <boost/config.hpp>
#include <boost/algorithm/cxx11/is_permutation.hpp> #include <boost/algorithm/cxx11/is_permutation.hpp>
#include <boost/algorithm/cxx14/is_permutation.hpp>
#define BOOST_TEST_MAIN #define BOOST_TEST_MAIN
#include <boost/test/unit_test.hpp> #include <boost/test/unit_test.hpp>

View File

@ -1,12 +1,21 @@
/*
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
*/
#ifndef ITERATOR_TEST_H #ifndef ITERATOR_TEST_H
#define ITERATOR_TEST_H #define ITERATOR_TEST_H
/* /*
A set of iterator adapters for constructing test cases A set of iterator adapters for constructing test cases
From an iterator (or a pointer), you can make any class of iterator. From an iterator (or a pointer), you can make any class of iterator.
Assuming you want to degrade the capabilities. Assuming you want to degrade the capabilities.
Modeled closely on work that Howard Hinnant did for libc++. Modeled closely on work that Howard Hinnant did for libc++.
*/ */
#include <iterator> #include <iterator>
@ -269,10 +278,10 @@ public:
private: private:
It it_; It it_;
template <typename U> friend class output_iterator; template <typename U> friend class output_iterator;
}; };
// No comparison operators for output iterators // No comparison operators for output iterators
// == Get the base of an iterator; used for comparisons == // == Get the base of an iterator; used for comparisons ==
template <typename Iter> template <typename Iter>
@ -290,7 +299,7 @@ inline Iter base(bidirectional_iterator<Iter> i) { return i.base(); }
template <typename Iter> template <typename Iter>
inline Iter base(random_access_iterator<Iter> i) { return i.base(); } inline Iter base(random_access_iterator<Iter> i) { return i.base(); }
template <typename Iter> // everything else template <typename Iter> // everything else
inline Iter base(Iter i) { return i; } inline Iter base(Iter i) { return i; }
#endif // ITERATORS_H #endif // ITERATORS_H

View File

@ -43,16 +43,17 @@ void test_sequence ( Container &v, Predicate comp, int expected ) {
res = ba::partition_point ( v.begin (), v.end (), comp ); res = ba::partition_point ( v.begin (), v.end (), comp );
exp = offset_to_iter ( v, expected ); exp = offset_to_iter ( v, expected );
std::cout << "Expected(1): " << std::distance ( v.begin (), exp )
<< ", got: " << std::distance ( v.begin (), res ) << std::endl;
BOOST_CHECK ( exp == res ); BOOST_CHECK ( exp == res );
// Duplicate the last element; this checks for any even/odd problems // Duplicate the last element; this checks for any even/odd problems
v.push_back ( * v.rbegin ()); v.push_back ( * v.rbegin ());
res = ba::partition_point ( v.begin (), v.end (), comp ); res = ba::partition_point ( v.begin (), v.end (), comp );
exp = offset_to_iter ( v, expected ); exp = offset_to_iter ( v, expected );
std::cout << "Expected(2): " << std::distance ( v.begin (), exp ) BOOST_CHECK ( exp == res );
<< ", got: " << std::distance ( v.begin (), res ) << std::endl;
// Range based test
res = ba::partition_point ( v, comp );
exp = offset_to_iter ( v, expected );
BOOST_CHECK ( exp == res ); BOOST_CHECK ( exp == res );
} }

View File

@ -0,0 +1,156 @@
#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>
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
std::random_shuffle(v.begin(), v.end());
ba::partition_subrange(b, v.end(), b + 7, b + 8);
check_sequence (b, v.end(), b + 7, b + 8);
// BOOST_CHECK_EQUAL(v[7], 7);
// Mix them up and try again - at the end
std::random_shuffle(v.begin(), v.end());
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
std::random_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
std::random_shuffle(v.begin(), v.end());
ba::partition_subrange(b, v.end(), b, b);
check_sequence (b, v.end(), b, b);
// Mix them up and try again - entire subrange
std::random_shuffle(v.begin(), v.end());
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
std::random_shuffle(v.begin(), v.end());
ba::partition_subrange(b, v.end(), b + 7, b + 8, std::greater<int>());
check_sequence (b, v.end(), b + 7, b + 8, std::greater<int>());
// BOOST_CHECK_EQUAL(v[7], 2);
// Mix them up and try again - at the end
std::random_shuffle(v.begin(), v.end());
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
std::random_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
std::random_shuffle(v.begin(), v.end());
ba::partition_subrange(b, v.end(), b, b, std::greater<int>());
check_sequence (b, v.end(), b, b, std::greater<int>());
// Mix them up and try again - entire subrange
std::random_shuffle(v.begin(), v.end());
ba::partition_subrange(b, v.end(), b, v.end(), std::greater<int>());
check_sequence (b, v.end(), b, v.end(), std::greater<int>());
}
}

24
test/power_fail1.cpp Normal file
View File

@ -0,0 +1,24 @@
/*
Copyright (c) Marshall Clow 2014.
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/algorithm.hpp>
#define BOOST_TEST_MAIN
#include <boost/test/unit_test.hpp>
namespace ba = boost::algorithm;
BOOST_AUTO_TEST_CASE( test_main )
{
// Second argument must be an integral value
BOOST_CHECK ( ba::power(1, 1.0) == 1);
}

36
test/power_test.cpp Normal file
View File

@ -0,0 +1,36 @@
/*
Copyright (c) Marshall Clow 2014.
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 <functional>
#include <boost/config.hpp>
#include <boost/algorithm/algorithm.hpp>
#define BOOST_TEST_MAIN
#include <boost/test/unit_test.hpp>
namespace ba = boost::algorithm;
BOOST_AUTO_TEST_CASE( test_main )
{
BOOST_CHECK ( ba::power(0, 0) == 1);
BOOST_CHECK ( ba::power(5, 0) == 1);
BOOST_CHECK ( ba::power(1, 1) == 1);
BOOST_CHECK ( ba::power(1, 4) == 1);
BOOST_CHECK ( ba::power(3, 2) == 9);
BOOST_CHECK ( ba::power(2, 3) == 8);
BOOST_CHECK ( ba::power(3, 3) == 27);
BOOST_CHECK ( ba::power(2, 30) == 0x40000000);
BOOST_CHECK ( ba::power(5L, 10) == 3125*3125);
BOOST_CHECK ( ba::power(18, 3) == 18*18*18);
BOOST_CHECK ( ba::power(3,2) == ba::power(3,2, std::multiplies<int>()));
BOOST_CHECK ( ba::power(3,2, std::plus<int>()) == 6);
}

View File

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

View File

@ -33,8 +33,8 @@ typedef std::vector<char> vec;
needle.begin (), needle.end ()); \ needle.begin (), needle.end ()); \
if ( res != exp ) { \ if ( res != exp ) { \
std::cout << "On run # " << i << " expected " \ std::cout << "On run # " << i << " expected " \
<< exp - haystack.begin () << " got " \ << exp.first - haystack.begin () << " got " \
<< res - haystack.begin () << std::endl; \ << res.first - haystack.begin () << std::endl; \
throw std::runtime_error \ throw std::runtime_error \
( "Unexpected result from " #call ); \ ( "Unexpected result from " #call ); \
} \ } \
@ -51,8 +51,8 @@ typedef std::vector<char> vec;
res = s_o ( haystack.begin (), haystack.end ()); \ res = s_o ( haystack.begin (), haystack.end ()); \
if ( res != exp ) { \ if ( res != exp ) { \
std::cout << "On run # " << i << " expected " \ std::cout << "On run # " << i << " expected " \
<< exp - haystack.begin () << " got " \ << exp.first - haystack.begin () << " got " \
<< res - haystack.begin () << std::endl; \ << res.first - haystack.begin () << std::endl; \
throw std::runtime_error \ throw std::runtime_error \
( "Unexpected result from " #obj " object" ); \ ( "Unexpected result from " #obj " object" ); \
} \ } \
@ -90,27 +90,33 @@ namespace {
std::clock_t sTime; std::clock_t sTime;
unsigned long stdDiff; unsigned long stdDiff;
vec::const_iterator res; std::pair<vec::const_iterator, vec::const_iterator> res;
vec::const_iterator exp; // the expected result std::pair<vec::const_iterator, vec::const_iterator> exp; // the expected result
vec::const_iterator exp_start;
if ( expected >= 0 ) if ( expected >= 0 )
exp = haystack.begin () + expected; exp_start = haystack.begin () + expected;
else if ( expected == -1 ) 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 ) 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 else
throw std::logic_error ( "Expected must be -2, -1, or >= 0" ); 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 << "Pattern is " << needle.size () << " entries long" << std::endl;
std::cout << "Corpus is " << haystack.size () << " entries long" << std::endl; std::cout << "Corpus is " << haystack.size () << " entries long" << std::endl;
// First, the std library search // First, the std library search
sTime = std::clock (); sTime = std::clock ();
for ( i = 0; i < NUM_TRIES; ++i ) { for ( i = 0; i < NUM_TRIES; ++i ) {
res = std::search ( haystack.begin (), haystack.end (), needle.begin (), needle.end ()); vec::const_iterator s_res = std::search ( haystack.begin (), haystack.end (), needle.begin (), needle.end ());
if ( res != exp ) { if ( s_res != exp.first ) {
std::cout << "On run # " << i << " expected " << exp - haystack.begin () << " got " << res - haystack.begin () << std::endl; 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" ); 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 ()); \ needle.begin (), needle.end ()); \
if ( res != exp ) { \ if ( res != exp ) { \
std::cout << "On run # " << i << " expected " \ std::cout << "On run # " << i << " expected " \
<< exp - haystack.begin () << " got " \ << exp.first - haystack.begin () << " got " \
<< res - haystack.begin () << std::endl; \ << res.first - haystack.begin () << std::endl; \
throw std::runtime_error \ throw std::runtime_error \
( "Unexpected result from " #call ); \ ( "Unexpected result from " #call ); \
} \ } \
@ -52,8 +52,8 @@ typedef std::vector<std::string> vec;
res = s_o ( haystack.begin (), haystack.end ()); \ res = s_o ( haystack.begin (), haystack.end ()); \
if ( res != exp ) { \ if ( res != exp ) { \
std::cout << "On run # " << i << " expected " \ std::cout << "On run # " << i << " expected " \
<< exp - haystack.begin () << " got " \ << exp.first - haystack.begin () << " got " \
<< res - haystack.begin () << std::endl; \ << res.first - haystack.begin () << std::endl; \
throw std::runtime_error \ throw std::runtime_error \
( "Unexpected result from " #obj " object" ); \ ( "Unexpected result from " #obj " object" ); \
} \ } \
@ -90,27 +90,33 @@ namespace {
std::clock_t sTime; std::clock_t sTime;
unsigned long stdDiff; unsigned long stdDiff;
vec::const_iterator res; std::pair<vec::const_iterator, vec::const_iterator> res;
vec::const_iterator exp; // the expected result std::pair<vec::const_iterator, vec::const_iterator> exp; // the expected result
vec::const_iterator exp_start;
if ( expected >= 0 ) if ( expected >= 0 )
exp = haystack.begin () + expected; exp_start = haystack.begin () + expected;
else if ( expected == -1 ) 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 ) 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 else
throw std::logic_error ( "Expected must be -2, -1, or >= 0" ); 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 << "Pattern is " << needle.size () << " entries long" << std::endl;
std::cout << "Corpus is " << haystack.size () << " entries long" << std::endl; std::cout << "Corpus is " << haystack.size () << " entries long" << std::endl;
// First, the std library search // First, the std library search
sTime = std::clock (); sTime = std::clock ();
for ( i = 0; i < NUM_TRIES; ++i ) { for ( i = 0; i < NUM_TRIES; ++i ) {
res = std::search ( haystack.begin (), haystack.end (), needle.begin (), needle.end ()); vec::const_iterator s_res = std::search ( haystack.begin (), haystack.end (), needle.begin (), needle.end ());
if ( res != exp ) { if ( s_res != exp.first ) {
std::cout << "On run # " << i << " expected " << exp - haystack.begin () << " got " << res - haystack.begin () << std::endl; 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" ); 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 ); \ res = boost::algorithm::call ( haystack, needle ); \
if ( res != exp ) { \ if ( res != exp ) { \
std::cout << "Expected " \ std::cout << "Expected " \
<< exp - haystack.begin () << " got " \ << exp.first - haystack.begin () << " got " \
<< res - haystack.begin () << std::endl; \ << res.first - haystack.begin () << std::endl; \
throw std::runtime_error \ throw std::runtime_error \
( "Unexpected result from " #call ); \ ( "Unexpected result from " #call ); \
} \ } \
@ -43,8 +43,8 @@ typedef std::vector<std::string> vec;
res = s_o ( haystack ); \ res = s_o ( haystack ); \
if ( res != exp ) { \ if ( res != exp ) { \
std::cout << "Expected " \ std::cout << "Expected " \
<< exp - haystack.begin () << " got " \ << exp.first - haystack.begin () << " got " \
<< res - haystack.begin () << std::endl; \ << res.first - haystack.begin () << std::endl; \
throw std::runtime_error \ throw std::runtime_error \
( "Unexpected result from " #obj " object" ); \ ( "Unexpected result from " #obj " object" ); \
} \ } \
@ -64,25 +64,31 @@ namespace {
void check_one ( const vec &haystack, const vec &needle, int expected ) { void check_one ( const vec &haystack, const vec &needle, int expected ) {
vec::const_iterator res; std::pair<vec::const_iterator, vec::const_iterator> res;
vec::const_iterator exp; // the expected result std::pair<vec::const_iterator, vec::const_iterator> exp; // the expected result
vec::const_iterator exp_start;
if ( expected >= 0 ) if ( expected >= 0 )
exp = haystack.begin () + expected; exp_start = haystack.begin () + expected;
else if ( expected == -1 ) 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 ) 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 else
throw std::logic_error ( "Expected must be -2, -1, or >= 0" ); 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 << "Pattern is " << needle.size () << " entries long" << std::endl;
std::cout << "Corpus is " << haystack.size () << " entries long" << std::endl; std::cout << "Corpus is " << haystack.size () << " entries long" << std::endl;
// First, the std library search // First, the std library search
res = std::search ( haystack.begin (), haystack.end (), needle.begin (), needle.end ()); vec::const_iterator s_res = std::search ( haystack.begin (), haystack.end (), needle.begin (), needle.end ());
if ( res != exp ) { if ( s_res != exp.first ) {
std::cout << "Expected " << exp - haystack.begin () << " got " << res - haystack.begin () << std::endl; std::cout << "Expected " << exp.first - haystack.begin () << " got " << s_res - haystack.begin () << std::endl;
throw std::runtime_error ( "Unexpected result from std::search" ); throw std::runtime_error ( "Unexpected result from std::search" );
} }

137
test/sort_subrange_test.cpp Normal file
View File

@ -0,0 +1,137 @@
#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>
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
std::random_shuffle(v.begin(), v.end());
ba::sort_subrange(b, v.end(), b + 7, b + 8);
check_sequence (b, v.end(), b + 7, b + 8);
BOOST_CHECK_EQUAL(v[7], 7);
// Mix them up and try again - at the end
std::random_shuffle(v.begin(), v.end());
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
std::random_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
std::random_shuffle(v.begin(), v.end());
ba::sort_subrange(b, v.end(), b, b);
check_sequence (b, v.end(), b, b);
// Mix them up and try again - entire subrange
std::random_shuffle(v.begin(), v.end());
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
std::random_shuffle(v.begin(), v.end());
ba::sort_subrange(b, v.end(), b + 7, b + 8, std::greater<int>());
check_sequence (b, v.end(), b + 7, b + 8, std::greater<int>());
BOOST_CHECK_EQUAL(v[7], 2);
// Mix them up and try again - at the end
std::random_shuffle(v.begin(), v.end());
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
std::random_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
std::random_shuffle(v.begin(), v.end());
ba::sort_subrange(b, v.end(), b, b, std::greater<int>());
check_sequence (b, v.end(), b, b, std::greater<int>());
// Mix them up and try again - entire subrange
std::random_shuffle(v.begin(), v.end());
ba::sort_subrange(b, v.end(), b, v.end(), std::greater<int>());
check_sequence (b, v.end(), b, v.end(), std::greater<int>());
}
}