mirror of
https://github.com/boostorg/algorithm.git
synced 2025-07-29 12:07:18 +02:00
Bug fixes and is_partititioned_XXX for the 1.65.0 release
This commit is contained in:
@ -16,17 +16,17 @@ using quickbook ;
|
||||
using doxygen ;
|
||||
using boostbook ;
|
||||
|
||||
doxygen autodoc
|
||||
:
|
||||
[ glob ../../../boost/algorithm/*.hpp
|
||||
doxygen autodoc
|
||||
:
|
||||
[ glob ../../../boost/algorithm/*.hpp
|
||||
../../../boost/algorithm/searching/*.hpp
|
||||
../../../boost/algorithm/cxx11/*.hpp
|
||||
../../../boost/algorithm/cxx14/*.hpp
|
||||
]
|
||||
:
|
||||
:
|
||||
<doxygen:param>"PREDEFINED=\"BOOST_ALGORITHM_DOXYGEN=1\""
|
||||
<doxygen:param>WARNINGS=YES # Default NO, but useful to see warnings, especially in a logfile.
|
||||
;
|
||||
;
|
||||
|
||||
|
||||
xml algorithm : algorithm.qbk ;
|
||||
@ -35,7 +35,7 @@ boostbook standalone
|
||||
:
|
||||
algorithm
|
||||
:
|
||||
<dependency>autodoc
|
||||
<dependency>autodoc
|
||||
<xsl:param>boost.root=../../../..
|
||||
<xsl:param>"boost.doxygen.reftitle=Boost.Algorithms C++ Reference"
|
||||
<xsl:param>chapter.autolabel=0
|
||||
@ -47,10 +47,10 @@ boostbook standalone
|
||||
|
||||
###############################################################################
|
||||
alias boostdoc
|
||||
: algorithm ../string/doc/string_algo.xml
|
||||
: ../string/doc/string_algo.xml
|
||||
:
|
||||
: <dependency>autodoc <dependency>../string/doc//autodoc
|
||||
: <dependency>../string/doc//autodoc
|
||||
: ;
|
||||
explicit boostdoc ;
|
||||
alias boostrelease ;
|
||||
alias boostrelease : standalone ;
|
||||
explicit boostrelease ;
|
||||
|
@ -67,6 +67,7 @@ Thanks to all the people who have reviewed this library and made suggestions for
|
||||
[include gather.qbk]
|
||||
[include hex.qbk]
|
||||
[include is_palindrome.qbk]
|
||||
[include is_partitioned_until.qbk]
|
||||
[endsect]
|
||||
|
||||
|
||||
|
@ -37,7 +37,7 @@ public:
|
||||
~boyer_moore ();
|
||||
|
||||
template <typename corpusIter>
|
||||
corpusIter operator () ( corpusIter corpus_first, corpusIter corpus_last );
|
||||
pair<corpusIter, corpusIter> operator () ( corpusIter corpus_first, corpusIter corpus_last );
|
||||
};
|
||||
``
|
||||
|
||||
@ -45,14 +45,28 @@ and here is the corresponding procedural interface:
|
||||
|
||||
``
|
||||
template <typename patIter, typename corpusIter>
|
||||
corpusIter boyer_moore_search (
|
||||
pair<corpusIter, corpusIter> boyer_moore_search (
|
||||
corpusIter corpus_first, corpusIter corpus_last,
|
||||
patIter pat_first, patIter pat_last );
|
||||
``
|
||||
|
||||
Each of the functions is passed two pairs of iterators. The first two define the corpus and the second two define the pattern. Note that the two pairs need not be of the same type, but they do need to "point" at the same type. In other words, `patIter::value_type` and `curpusIter::value_type` need to be the same type.
|
||||
|
||||
The return value of the function is an iterator pointing to the start of the pattern in the corpus. If the pattern is not found, it returns the end of the corpus (`corpus_last`).
|
||||
The return value of the function is a pair of iterators pointing to the position of the pattern in the corpus. If the pattern is empty, it returns at empty range at the start of the corpus (`corpus_first`, `corpus_first`). If the pattern is not found, it returns at empty range at the end of the corpus (`corpus_last`, `corpus_last`).
|
||||
|
||||
[heading Compatibility Note]
|
||||
|
||||
Earlier versions of this searcher returned only a single iterator. As explained in [@https://cplusplusmusings.wordpress.com/2016/02/01/sometimes-you-get-things-wrong/], this was a suboptimal interface choice, and has been changed, starting in the 1.62.0 release. Old code that is expecting a single iterator return value can be updated by replacing the return value of the searcher's `operator ()` with the `.first` field of the pair.
|
||||
|
||||
Instead of:
|
||||
``
|
||||
iterator foo = searcher(a, b);
|
||||
``
|
||||
|
||||
you now write:
|
||||
``
|
||||
iterator foo = searcher(a, b).first;
|
||||
``
|
||||
|
||||
[heading Performance]
|
||||
|
||||
|
@ -35,7 +35,7 @@ public:
|
||||
~boyer_moore_horspool ();
|
||||
|
||||
template <typename corpusIter>
|
||||
corpusIter operator () ( corpusIter corpus_first, corpusIter corpus_last );
|
||||
pair<corpusIter, corpusIter> operator () ( corpusIter corpus_first, corpusIter corpus_last );
|
||||
};
|
||||
``
|
||||
|
||||
@ -43,14 +43,28 @@ and here is the corresponding procedural interface:
|
||||
|
||||
``
|
||||
template <typename patIter, typename corpusIter>
|
||||
corpusIter boyer_moore_horspool_search (
|
||||
pair<corpusIter, corpusIter> boyer_moore_horspool_search (
|
||||
corpusIter corpus_first, corpusIter corpus_last,
|
||||
patIter pat_first, patIter pat_last );
|
||||
``
|
||||
|
||||
Each of the functions is passed two pairs of iterators. The first two define the corpus and the second two define the pattern. Note that the two pairs need not be of the same type, but they do need to "point" at the same type. In other words, `patIter::value_type` and `curpusIter::value_type` need to be the same type.
|
||||
|
||||
The return value of the function is an iterator pointing to the start of the pattern in the corpus. If the pattern is not found, it returns the end of the corpus (`corpus_last`).
|
||||
The return value of the function is a pair of iterators pointing to the position of the pattern in the corpus. If the pattern is empty, it returns at empty range at the start of the corpus (`corpus_first`, `corpus_first`). If the pattern is not found, it returns at empty range at the end of the corpus (`corpus_last`, `corpus_last`).
|
||||
|
||||
[heading Compatibility Note]
|
||||
|
||||
Earlier versions of this searcher returned only a single iterator. As explained in [@https://cplusplusmusings.wordpress.com/2016/02/01/sometimes-you-get-things-wrong/], this was a suboptimal interface choice, and has been changed, starting in the 1.62.0 release. Old code that is expecting a single iterator return value can be updated by replacing the return value of the searcher's `operator ()` with the `.first` field of the pair.
|
||||
|
||||
Instead of:
|
||||
``
|
||||
iterator foo = searcher(a, b);
|
||||
``
|
||||
|
||||
you now write:
|
||||
``
|
||||
iterator foo = searcher(a, b).first;
|
||||
``
|
||||
|
||||
[heading Performance]
|
||||
|
||||
|
@ -57,7 +57,7 @@ Both of the variants of `is_partitioned` take their parameters by value or const
|
||||
|
||||
* The iterator-based version of the routine `is_partitioned` is also available as part of the C++11 standard.
|
||||
|
||||
* `is_partitioned` returns true for empty ranges, no matter what predicate is passed to test against.
|
||||
* `is_partitioned` returns true for empty and single-element ranges, no matter what predicate is passed to test against.
|
||||
|
||||
[endsect]
|
||||
|
||||
|
67
doc/is_partitioned_until.qbk
Normal file
67
doc/is_partitioned_until.qbk
Normal file
@ -0,0 +1,67 @@
|
||||
[/ File is_partitioned_until.qbk]
|
||||
|
||||
[section:is_partitioned_until is_partitioned_until ]
|
||||
|
||||
[/license
|
||||
Copyright (c) 2017 Alexander Zaitsev
|
||||
|
||||
Distributed under the Boost Software License, Version 1.0.
|
||||
(See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
]
|
||||
|
||||
The header file 'is_partitioned_until.hpp' contains two variants of a single algorithm, `is_partitioned_until`. The algorithm tests to see if a sequence is partitioned according to a predicate; in other words, all the items in the sequence that satisfy the predicate are at the beginning of the sequence.
|
||||
|
||||
The routine `is_partitioned_until` takes a sequence and a predicate. It returns the last iterator 'it' in the sequence [begin, end) for which the is_partitioned(begin, it) is true.
|
||||
|
||||
`is_partitioned_until` come in two forms; the first one takes two iterators to define the range. The second form takes a single range parameter, and uses Boost.Range to traverse it.
|
||||
|
||||
|
||||
[heading interface]
|
||||
|
||||
The function `is_partitioned_until` returns the last iterator 'it' in the sequence [begin, end) for which the is_partitioned(begin, it) is true. There are two versions; one takes two iterators, and the other takes a range.
|
||||
|
||||
``
|
||||
template<typename InputIterator, typename Predicate>
|
||||
InputIterator is_partitioned_until ( InputIterator first, InputIterator last, Predicate p );
|
||||
template<typename Range, typename Predicate>
|
||||
typename boost::range_iterator<const Range>::type is_partitioned_until ( const Range &r, Predicate p );
|
||||
``
|
||||
|
||||
[heading Examples]
|
||||
|
||||
Given the container `c` containing `{ 0, 1, 2, 3, 14, 15 }`, then
|
||||
``
|
||||
bool isOdd ( int i ) { return i % 2 == 1; }
|
||||
bool lessThan10 ( int i ) { return i < 10; }
|
||||
|
||||
is_partitioned_until ( c, isOdd ) --> iterator to '1'
|
||||
is_partitioned_until ( c, lessThan10 ) --> end
|
||||
is_partitioned_until ( c.begin (), c.end (), lessThan10 ) --> end
|
||||
is_partitioned_until ( c.begin (), c.begin () + 3, lessThan10 ) --> end
|
||||
is_partitioned_until ( c.end (), c.end (), isOdd ) --> end // empty range
|
||||
``
|
||||
|
||||
[heading Iterator Requirements]
|
||||
|
||||
`is_partitioned_until` works on all iterators except output iterators.
|
||||
|
||||
[heading Complexity]
|
||||
|
||||
Both of the variants of `is_partitioned_until` run in ['O(N)] (linear) time; that is, they compare against each element in the list once. If the sequence is found to be not partitioned at any point, the routine will terminate immediately, without examining the rest of the elements.
|
||||
|
||||
[heading Exception Safety]
|
||||
|
||||
Both of the variants of `is_partitioned_until` take their parameters by value or const reference, and do not depend upon any global state. Therefore, all the routines in this file provide the strong exception guarantee.
|
||||
|
||||
[heading Notes]
|
||||
|
||||
* `is_partitioned_until` returns iterator to the end for empty and single-element ranges, no matter what predicate is passed to test against.
|
||||
|
||||
[endsect]
|
||||
|
||||
[/ File is_partitioned_until.qbk
|
||||
Copyright 2017 Alexander Zaitsev
|
||||
Distributed under the Boost Software License, Version 1.0.
|
||||
(See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt).
|
||||
]
|
||||
|
@ -39,7 +39,7 @@ public:
|
||||
~knuth_morris_pratt ();
|
||||
|
||||
template <typename corpusIter>
|
||||
corpusIter operator () ( corpusIter corpus_first, corpusIter corpus_last );
|
||||
pair<corpusIter, corpusIter> operator () ( corpusIter corpus_first, corpusIter corpus_last );
|
||||
};
|
||||
``
|
||||
|
||||
@ -47,15 +47,28 @@ and here is the corresponding procedural interface:
|
||||
|
||||
``
|
||||
template <typename patIter, typename corpusIter>
|
||||
corpusIter knuth_morris_pratt_search (
|
||||
pair<corpusIter, corpusIter> knuth_morris_pratt_search (
|
||||
corpusIter corpus_first, corpusIter corpus_last,
|
||||
patIter pat_first, patIter pat_last );
|
||||
``
|
||||
|
||||
Each of the functions is passed two pairs of iterators. The first two define the corpus and the second two define the pattern. Note that the two pairs need not be of the same type, but they do need to "point" at the same type. In other words, `patIter::value_type` and `curpusIter::value_type` need to be the same type.
|
||||
|
||||
The return value of the function is an iterator pointing to the start of the pattern in the corpus. If the pattern is not found, it returns the end of the corpus (`corpus_last`).
|
||||
The return value of the function is a pair of iterators pointing to the position of the pattern in the corpus. If the pattern is empty, it returns at empty range at the start of the corpus (`corpus_first`, `corpus_first`). If the pattern is not found, it returns at empty range at the end of the corpus (`corpus_last`, `corpus_last`).
|
||||
|
||||
[heading Compatibility Note]
|
||||
|
||||
Earlier versions of this searcher returned only a single iterator. As explained in [@https://cplusplusmusings.wordpress.com/2016/02/01/sometimes-you-get-things-wrong/], this was a suboptimal interface choice, and has been changed, starting in the 1.62.0 release. Old code that is expecting a single iterator return value can be updated by replacing the return value of the searcher's `operator ()` with the `.first` field of the pair.
|
||||
|
||||
Instead of:
|
||||
``
|
||||
iterator foo = searcher(a, b);
|
||||
``
|
||||
|
||||
you now write:
|
||||
``
|
||||
iterator foo = searcher(a, b).first;
|
||||
``
|
||||
[heading Performance]
|
||||
|
||||
The execution time of the Knuth-Morris-Pratt algorithm is linear in the size of the string being searched. Generally the algorithm gets faster as the pattern being searched for becomes longer. Its efficiency derives from the fact that with each unsuccessful attempt to find a match between the search string and the text it is searching, it uses the information gained from that attempt to rule out as many positions of the text as possible where the string cannot match.
|
||||
|
@ -21,4 +21,5 @@ project /boost/algorithm/example
|
||||
exe clamp_example : clamp_example.cpp ;
|
||||
exe search_example : search_example.cpp ;
|
||||
exe is_palindrome_example : is_palindrome_example.cpp;
|
||||
exe is_partitioned_until_example : is_partitioned_until_example.cpp;
|
||||
|
||||
|
70
example/is_partitioned_until_example.cpp
Normal file
70
example/is_partitioned_until_example.cpp
Normal file
@ -0,0 +1,70 @@
|
||||
/*
|
||||
Copyright (c) Alexander Zaitsev <zamazan4ik@gmail.by>, 2017
|
||||
|
||||
Distributed under the Boost Software License, Version 1.0. (See accompanying
|
||||
file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
For more information, see http://www.boost.org
|
||||
*/
|
||||
|
||||
#include <vector>
|
||||
#include <functional>
|
||||
#include <iostream>
|
||||
|
||||
#include <boost/algorithm/is_partitioned_until.hpp>
|
||||
|
||||
|
||||
namespace ba = boost::algorithm;
|
||||
|
||||
bool isOdd(const int v1)
|
||||
{
|
||||
return v1 % 2 != 0;
|
||||
}
|
||||
|
||||
struct isOddComp
|
||||
{
|
||||
bool operator()(const int v1) const
|
||||
{
|
||||
return v1 % 2 != 0;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
int main ( int /*argc*/, char * /*argv*/ [] )
|
||||
{
|
||||
std::vector<int> good({1, 2, 4});
|
||||
std::vector<int> bad({1, 2, 3});
|
||||
|
||||
//Use custom function
|
||||
auto it1 = ba::is_partitioned_until(good.begin(), good.end(), isOdd);
|
||||
if(it1 == good.end())
|
||||
{
|
||||
std::cout << "The sequence is partitioned\n";
|
||||
}
|
||||
else
|
||||
{
|
||||
std::cout << "is_partitioned_until check failed here: " << *it1 << std::endl;
|
||||
}
|
||||
|
||||
//Use custom comparator
|
||||
auto it2 = ba::is_partitioned_until(good.begin(), good.end(), isOddComp());
|
||||
if(it2 == good.end())
|
||||
{
|
||||
std::cout << "The sequence is partitioned\n";
|
||||
}
|
||||
else
|
||||
{
|
||||
std::cout << "is_partitioned_until check failed here: " << *it2 << std::endl;
|
||||
}
|
||||
|
||||
auto it3 = ba::is_partitioned_until(bad, isOdd);
|
||||
if(it3 == bad.end())
|
||||
{
|
||||
std::cout << "The sequence is partitioned\n";
|
||||
}
|
||||
else
|
||||
{
|
||||
std::cout << "is_partitioned_until check failed here: " << *it3 << std::endl;
|
||||
}
|
||||
return 0;
|
||||
}
|
@ -18,7 +18,8 @@
|
||||
namespace boost { namespace algorithm {
|
||||
|
||||
/// \fn is_partitioned ( InputIterator first, InputIterator last, UnaryPredicate p )
|
||||
/// \brief Tests to see if a sequence is partitioned according to a predicate
|
||||
/// \brief Tests to see if a sequence is partitioned according to a predicate.
|
||||
/// In other words, all the items in the sequence that satisfy the predicate are at the beginning of the sequence.
|
||||
///
|
||||
/// \param first The start of the input sequence
|
||||
/// \param last One past the end of the input sequence
|
||||
@ -39,7 +40,8 @@ bool is_partitioned ( InputIterator first, InputIterator last, UnaryPredicate p
|
||||
}
|
||||
|
||||
/// \fn is_partitioned ( const Range &r, UnaryPredicate p )
|
||||
/// \brief Generates an increasing sequence of values, and stores them in the input Range.
|
||||
/// \brief Tests to see if a sequence is partitioned according to a predicate.
|
||||
/// In other words, all the items in the sequence that satisfy the predicate are at the beginning of the sequence.
|
||||
///
|
||||
/// \param r The input range
|
||||
/// \param p The predicate to test the values with
|
||||
|
@ -13,7 +13,6 @@
|
||||
#define BOOST_ALGORITHM_EQUAL_HPP
|
||||
|
||||
#include <algorithm> // for std::equal
|
||||
#include <functional> // for std::binary_function
|
||||
#include <iterator>
|
||||
|
||||
namespace boost { namespace algorithm {
|
||||
@ -21,7 +20,7 @@ namespace boost { namespace algorithm {
|
||||
namespace detail {
|
||||
|
||||
template <class T1, class T2>
|
||||
struct eq : public std::binary_function<T1, T2, bool> {
|
||||
struct eq {
|
||||
bool operator () ( const T1& v1, const T2& v2 ) const { return v1 == v2 ;}
|
||||
};
|
||||
|
||||
|
@ -73,7 +73,7 @@ namespace detail {
|
||||
else if ( c >= 'A' && c <= 'F' ) retval = c - 'A' + 10;
|
||||
else if ( c >= 'a' && c <= 'f' ) retval = c - 'a' + 10;
|
||||
else BOOST_THROW_EXCEPTION (non_hex_input() << bad_char (c));
|
||||
return retval;
|
||||
return static_cast<char>(retval);
|
||||
}
|
||||
|
||||
// My own iterator_traits class.
|
||||
|
@ -35,7 +35,7 @@ namespace boost { namespace algorithm {
|
||||
/// For other sequences function will return false.
|
||||
/// Complexity: O(N).
|
||||
template <typename BidirectionalIterator, typename Predicate>
|
||||
bool is_palindrome(BidirectionalIterator begin, BidirectionalIterator end, Predicate p )
|
||||
bool is_palindrome(BidirectionalIterator begin, BidirectionalIterator end, Predicate p)
|
||||
{
|
||||
if(begin == end)
|
||||
{
|
||||
@ -63,7 +63,7 @@ bool is_palindrome(BidirectionalIterator begin, BidirectionalIterator end, Predi
|
||||
/// \return true if the entire sequence is palindrome
|
||||
///
|
||||
/// \param begin The start of the input sequence
|
||||
/// \param end One past the end of the input sequence
|
||||
/// \param end One past the end of the input sequence
|
||||
///
|
||||
/// \note This function will return true for empty sequences and for palindromes.
|
||||
/// For other sequences function will return false.
|
||||
@ -71,26 +71,8 @@ bool is_palindrome(BidirectionalIterator begin, BidirectionalIterator end, Predi
|
||||
template <typename BidirectionalIterator>
|
||||
bool is_palindrome(BidirectionalIterator begin, BidirectionalIterator end)
|
||||
{
|
||||
if(begin == end)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
--end;
|
||||
while(begin != end)
|
||||
{
|
||||
if(!(*begin == *end))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
++begin;
|
||||
if(begin == end)
|
||||
{
|
||||
break;
|
||||
}
|
||||
--end;
|
||||
}
|
||||
return true;
|
||||
return is_palindrome(begin, end,
|
||||
std::equal_to<typename std::iterator_traits<BidirectionalIterator>::value_type> ());
|
||||
}
|
||||
|
||||
/// \fn is_palindrome ( const R& range )
|
||||
@ -122,7 +104,6 @@ bool is_palindrome(const R& range, Predicate p)
|
||||
return is_palindrome(boost::begin(range), boost::end(range), p);
|
||||
}
|
||||
|
||||
|
||||
/// \fn is_palindrome ( const char* str )
|
||||
/// \return true if the entire sequence is palindrome
|
||||
///
|
||||
@ -138,7 +119,6 @@ bool is_palindrome(const char* str)
|
||||
return is_palindrome(str, str + strlen(str));
|
||||
}
|
||||
|
||||
|
||||
/// \fn is_palindrome ( const char* str, Predicate p )
|
||||
/// \return true if the entire sequence is palindrome
|
||||
///
|
||||
@ -155,7 +135,6 @@ bool is_palindrome(const char* str, Predicate p)
|
||||
return true;
|
||||
return is_palindrome(str, str + strlen(str), p);
|
||||
}
|
||||
|
||||
}}
|
||||
|
||||
#endif // BOOST_ALGORITHM_IS_PALINDROME_HPP
|
||||
|
63
include/boost/algorithm/is_partitioned_until.hpp
Normal file
63
include/boost/algorithm/is_partitioned_until.hpp
Normal file
@ -0,0 +1,63 @@
|
||||
/*
|
||||
Copyright (c) Alexander Zaitsev <zamazan4ik@gmail.by>, 2017.
|
||||
|
||||
Distributed under the Boost Software License, Version 1.0. (See accompanying
|
||||
file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
*/
|
||||
|
||||
/// \file is_partitioned_until.hpp
|
||||
/// \brief Tell if a sequence is partitioned
|
||||
/// \author Alexander Zaitsev
|
||||
|
||||
#ifndef BOOST_ALGORITHM_IS_PARTITIONED_UNTIL_HPP
|
||||
#define BOOST_ALGORITHM_IS_PARTITIONED_UNTIL_HPP
|
||||
|
||||
#include <boost/range/begin.hpp>
|
||||
#include <boost/range/end.hpp>
|
||||
|
||||
namespace boost { namespace algorithm {
|
||||
|
||||
/// \fn is_partitioned_until ( InputIterator first, InputIterator last, UnaryPredicate p )
|
||||
/// \brief Tests to see if a sequence is partitioned according to a predicate.
|
||||
/// In other words, all the items in the sequence that satisfy the predicate are at the beginning of the sequence.
|
||||
///
|
||||
/// \param first The start of the input sequence
|
||||
/// \param last One past the end of the input sequence
|
||||
/// \param p The predicate to test the values with
|
||||
///
|
||||
/// \note Returns the first iterator 'it' in the sequence [first, last) for which is_partitioned(first, it, p) is false.
|
||||
/// Returns last if the entire sequence is partitioned.
|
||||
/// Complexity: O(N).
|
||||
template <typename InputIterator, typename UnaryPredicate>
|
||||
InputIterator is_partitioned_until ( InputIterator first, InputIterator last, UnaryPredicate p )
|
||||
{
|
||||
// Run through the part that satisfy the predicate
|
||||
for ( ; first != last; ++first )
|
||||
if ( !p (*first))
|
||||
break;
|
||||
// Now the part that does not satisfy the predicate
|
||||
for ( ; first != last; ++first )
|
||||
if ( p (*first))
|
||||
return first;
|
||||
return last;
|
||||
}
|
||||
|
||||
/// \fn is_partitioned_until ( const Range &r, UnaryPredicate p )
|
||||
/// \brief Tests to see if a sequence is partitioned according to a predicate.
|
||||
/// In other words, all the items in the sequence that satisfy the predicate are at the beginning of the sequence.
|
||||
///
|
||||
/// \param r The input range
|
||||
/// \param p The predicate to test the values with
|
||||
///
|
||||
/// \note Returns the first iterator 'it' in the sequence [first, last) for which is_partitioned(first, it, p) is false.
|
||||
/// Returns last if the entire sequence is partitioned.
|
||||
/// Complexity: O(N).
|
||||
template <typename Range, typename UnaryPredicate>
|
||||
typename boost::range_iterator<const Range>::type is_partitioned_until ( const Range &r, UnaryPredicate p )
|
||||
{
|
||||
return boost::algorithm::is_partitioned_until (boost::begin(r), boost::end(r), p);
|
||||
}
|
||||
|
||||
}}
|
||||
|
||||
#endif // BOOST_ALGORITHM_IS_PARTITIONED_UNTIL_HPP
|
@ -152,8 +152,8 @@ Requirements:
|
||||
|
||||
|
||||
template<typename Iter, typename Container>
|
||||
void compute_bm_prefix ( Iter pat_first, Iter pat_last, Container &prefix ) {
|
||||
const std::size_t count = std::distance ( pat_first, pat_last );
|
||||
void compute_bm_prefix ( Iter first, Iter last, Container &prefix ) {
|
||||
const std::size_t count = std::distance ( first, last );
|
||||
BOOST_ASSERT ( count > 0 );
|
||||
BOOST_ASSERT ( prefix.size () == count );
|
||||
|
||||
@ -161,26 +161,26 @@ Requirements:
|
||||
std::size_t k = 0;
|
||||
for ( std::size_t i = 1; i < count; ++i ) {
|
||||
BOOST_ASSERT ( k < count );
|
||||
while ( k > 0 && ( pat_first[k] != pat_first[i] )) {
|
||||
while ( k > 0 && ( first[k] != first[i] )) {
|
||||
BOOST_ASSERT ( k < count );
|
||||
k = prefix [ k - 1 ];
|
||||
}
|
||||
|
||||
if ( pat_first[k] == pat_first[i] )
|
||||
if ( first[k] == first[i] )
|
||||
k++;
|
||||
prefix [ i ] = k;
|
||||
}
|
||||
}
|
||||
|
||||
void build_suffix_table ( patIter pat_first, patIter pat_last ) {
|
||||
const std::size_t count = (std::size_t) std::distance ( pat_first, pat_last );
|
||||
void build_suffix_table ( patIter first, patIter last ) {
|
||||
const std::size_t count = (std::size_t) std::distance ( first, last );
|
||||
|
||||
if ( count > 0 ) { // empty pattern
|
||||
std::vector<typename std::iterator_traits<patIter>::value_type> reversed(count);
|
||||
(void) std::reverse_copy ( pat_first, pat_last, reversed.begin ());
|
||||
(void) std::reverse_copy ( first, last, reversed.begin ());
|
||||
|
||||
std::vector<difference_type> prefix (count);
|
||||
compute_bm_prefix ( pat_first, pat_last, prefix );
|
||||
compute_bm_prefix ( first, last, prefix );
|
||||
|
||||
std::vector<difference_type> prefix_reversed (count);
|
||||
compute_bm_prefix ( reversed.begin (), reversed.end (), prefix_reversed );
|
||||
|
@ -155,9 +155,9 @@ namespace boost { namespace algorithm {
|
||||
|
||||
|
||||
void preKmp ( patIter first, patIter last ) {
|
||||
const /*std::size_t*/ int count = std::distance ( first, last );
|
||||
const difference_type count = std::distance ( first, last );
|
||||
|
||||
int i, j;
|
||||
difference_type i, j;
|
||||
|
||||
i = 0;
|
||||
j = skip_[0] = -1;
|
||||
@ -177,7 +177,7 @@ namespace boost { namespace algorithm {
|
||||
void init_skip_table ( patIter first, patIter last ) {
|
||||
const difference_type count = std::distance ( first, last );
|
||||
|
||||
int j;
|
||||
difference_type j;
|
||||
skip_ [ 0 ] = -1;
|
||||
for ( int i = 1; i <= count; ++i ) {
|
||||
j = skip_ [ i - 1 ];
|
||||
|
@ -30,8 +30,10 @@ namespace boost {
|
||||
|
||||
// a tolower functor
|
||||
template<typename CharT>
|
||||
struct to_lowerF : public std::unary_function<CharT, CharT>
|
||||
struct to_lowerF
|
||||
{
|
||||
typedef CharT argument_type;
|
||||
typedef CharT result_type;
|
||||
// Constructor
|
||||
to_lowerF( const std::locale& Loc ) : m_Loc( &Loc ) {}
|
||||
|
||||
@ -50,8 +52,10 @@ namespace boost {
|
||||
|
||||
// a toupper functor
|
||||
template<typename CharT>
|
||||
struct to_upperF : public std::unary_function<CharT, CharT>
|
||||
struct to_upperF
|
||||
{
|
||||
typedef CharT argument_type;
|
||||
typedef CharT result_type;
|
||||
// Constructor
|
||||
to_upperF( const std::locale& Loc ) : m_Loc( &Loc ) {}
|
||||
|
||||
|
@ -40,7 +40,7 @@ namespace boost {
|
||||
// Protected construction/destruction
|
||||
|
||||
// Default constructor
|
||||
find_iterator_base() {};
|
||||
find_iterator_base() {}
|
||||
// Copy construction
|
||||
find_iterator_base( const find_iterator_base& Other ) :
|
||||
m_Finder(Other.m_Finder) {}
|
||||
|
@ -89,9 +89,10 @@ namespace boost {
|
||||
template<
|
||||
typename SeqT,
|
||||
typename IteratorT=BOOST_STRING_TYPENAME SeqT::const_iterator >
|
||||
struct copy_iterator_rangeF :
|
||||
public std::unary_function< iterator_range<IteratorT>, SeqT >
|
||||
struct copy_iterator_rangeF
|
||||
{
|
||||
typedef iterator_range<IteratorT> argument_type;
|
||||
typedef SeqT result_type;
|
||||
SeqT operator()( const iterator_range<IteratorT>& Range ) const
|
||||
{
|
||||
return copy_range<SeqT>(Range);
|
||||
|
@ -70,6 +70,9 @@ alias unit_test_framework
|
||||
|
||||
# Is_palindrome tests
|
||||
[ run is_palindrome_test.cpp unit_test_framework : : : : is_palindrome_test ]
|
||||
|
||||
# Is_partitioned_until tests
|
||||
[ run is_partitioned_until_test.cpp unit_test_framework : : : : is_partitioned_until_test ]
|
||||
;
|
||||
}
|
||||
|
||||
|
@ -18,7 +18,7 @@
|
||||
#include <list>
|
||||
|
||||
template<typename T>
|
||||
struct is_ : public std::unary_function<T, bool> {
|
||||
struct is_ {
|
||||
is_ ( T v ) : val_ ( v ) {}
|
||||
~is_ () {}
|
||||
bool operator () ( T comp ) const { return val_ == comp; }
|
||||
|
@ -18,7 +18,7 @@
|
||||
#include <list>
|
||||
|
||||
template<typename T>
|
||||
struct is_ : public std::unary_function<T, bool> {
|
||||
struct is_ {
|
||||
is_ ( T v ) : val_ ( v ) {}
|
||||
~is_ () {}
|
||||
bool operator () ( T comp ) const { return val_ == comp; }
|
||||
|
@ -38,9 +38,6 @@ struct functorComparator
|
||||
}
|
||||
};
|
||||
|
||||
#define Begin(arr) (arr)
|
||||
#define End(arr) (arr+(sizeof(arr)/(sizeof(arr[0]))))
|
||||
|
||||
void test_is_palindrome()
|
||||
{
|
||||
const std::list<int> empty;
|
||||
@ -54,14 +51,14 @@ void test_is_palindrome()
|
||||
// Test a default operator==
|
||||
BOOST_CHECK ( ba::is_palindrome(empty));
|
||||
BOOST_CHECK ( ba::is_palindrome(singleElement));
|
||||
BOOST_CHECK (!ba::is_palindrome(Begin(oddNonPalindrome), End(oddNonPalindrome)));
|
||||
BOOST_CHECK ( ba::is_palindrome(Begin(oddPalindrome), End(oddPalindrome)));
|
||||
BOOST_CHECK ( ba::is_palindrome(Begin(evenPalindrome), End(evenPalindrome)));
|
||||
BOOST_CHECK (!ba::is_palindrome(Begin(evenNonPalindrome), End(evenNonPalindrome)));
|
||||
BOOST_CHECK (!ba::is_palindrome(boost::begin(oddNonPalindrome), boost::end(oddNonPalindrome)));
|
||||
BOOST_CHECK ( ba::is_palindrome(boost::begin(oddPalindrome), boost::end(oddPalindrome)));
|
||||
BOOST_CHECK ( ba::is_palindrome(boost::begin(evenPalindrome), boost::end(evenPalindrome)));
|
||||
BOOST_CHECK (!ba::is_palindrome(boost::begin(evenNonPalindrome), boost::end(evenNonPalindrome)));
|
||||
|
||||
//Test the custom comparators
|
||||
BOOST_CHECK ( ba::is_palindrome(empty.begin(), empty.end(), functorComparator()));
|
||||
BOOST_CHECK (!ba::is_palindrome(Begin(oddNonPalindrome), End(oddNonPalindrome), funcComparator<int>));
|
||||
BOOST_CHECK (!ba::is_palindrome(boost::begin(oddNonPalindrome), boost::end(oddNonPalindrome), funcComparator<int>));
|
||||
BOOST_CHECK ( ba::is_palindrome(evenPalindrome, std::equal_to<int>()));
|
||||
|
||||
//Test C-strings like cases
|
||||
|
63
test/is_partitioned_until_test.cpp
Normal file
63
test/is_partitioned_until_test.cpp
Normal file
@ -0,0 +1,63 @@
|
||||
/*
|
||||
Copyright (c) Marshall Clow 2011-2012, Alexander Zaitsev <zamazan4ik@gmail.com>, 2017.
|
||||
|
||||
Distributed under the Boost Software License, Version 1.0. (See accompanying
|
||||
file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
For more information, see http://www.boost.org
|
||||
*/
|
||||
|
||||
#include <iostream>
|
||||
|
||||
#include <boost/config.hpp>
|
||||
#include <boost/algorithm/is_partitioned_until.hpp>
|
||||
|
||||
#define BOOST_TEST_MAIN
|
||||
#include <boost/test/unit_test.hpp>
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <list>
|
||||
|
||||
namespace ba = boost::algorithm;
|
||||
// namespace ba = boost;
|
||||
|
||||
template <typename T>
|
||||
struct less_than {
|
||||
public:
|
||||
less_than ( T foo ) : val ( foo ) {}
|
||||
less_than ( const less_than &rhs ) : val ( rhs.val ) {}
|
||||
|
||||
bool operator () ( const T &v ) const { return v < val; }
|
||||
private:
|
||||
less_than ();
|
||||
less_than operator = ( const less_than &rhs );
|
||||
T val;
|
||||
};
|
||||
|
||||
|
||||
void test_sequence1 () {
|
||||
std::vector<int> v;
|
||||
|
||||
v.clear ();
|
||||
for ( int i = 5; i < 15; ++i )
|
||||
v.push_back ( i );
|
||||
BOOST_CHECK ( ba::is_partitioned_until ( v, less_than<int>(3)) == v.end()); // no elements
|
||||
BOOST_CHECK ( ba::is_partitioned_until ( v, less_than<int>(6)) == v.end()); // only the first element
|
||||
BOOST_CHECK ( ba::is_partitioned_until ( v, less_than<int>(10)) == v.end()); // in the middle somewhere
|
||||
BOOST_CHECK ( ba::is_partitioned_until ( v, less_than<int>(99)) == v.end()); // all elements satisfy
|
||||
// With bidirectional iterators.
|
||||
std::list<int> l;
|
||||
for ( int i = 5; i < 15; ++i )
|
||||
l.push_back ( i );
|
||||
BOOST_CHECK ( ba::is_partitioned_until ( l.begin (), l.end (), less_than<int>(3)) == l.end()); // no elements
|
||||
BOOST_CHECK ( ba::is_partitioned_until ( l.begin (), l.end (), less_than<int>(6)) == l.end()); // only the first element
|
||||
BOOST_CHECK ( ba::is_partitioned_until ( l.begin (), l.end (), less_than<int>(10)) == l.end()); // in the middle somewhere
|
||||
BOOST_CHECK ( ba::is_partitioned_until ( l.begin (), l.end (), less_than<int>(99)) == l.end()); // all elements satisfy
|
||||
}
|
||||
|
||||
|
||||
BOOST_AUTO_TEST_CASE( test_main )
|
||||
{
|
||||
test_sequence1 ();
|
||||
}
|
@ -18,7 +18,7 @@
|
||||
#include <list>
|
||||
|
||||
template<typename T>
|
||||
struct is_ : public std::unary_function<T, bool> {
|
||||
struct is_ {
|
||||
is_ ( T v ) : val_ ( v ) {}
|
||||
~is_ () {}
|
||||
bool operator () ( T comp ) const { return val_ == comp; }
|
||||
|
@ -18,7 +18,7 @@
|
||||
#include <list>
|
||||
|
||||
template<typename T>
|
||||
struct is_ : public std::unary_function<T, bool> {
|
||||
struct is_ {
|
||||
is_ ( T v ) : val_ ( v ) {}
|
||||
~is_ () {}
|
||||
bool operator () ( T comp ) const { return val_ == comp; }
|
||||
|
@ -7,6 +7,20 @@
|
||||
|
||||
#include <vector>
|
||||
#include <iostream>
|
||||
|
||||
#if __cplusplus >= 201103L
|
||||
#include <random>
|
||||
|
||||
std::default_random_engine gen;
|
||||
template<typename RandomIt>
|
||||
void do_shuffle(RandomIt first, RandomIt last)
|
||||
{ std::shuffle(first, last, gen); }
|
||||
#else
|
||||
template<typename RandomIt>
|
||||
void do_shuffle(RandomIt first, RandomIt last)
|
||||
{ std::random_shuffle(first, last); }
|
||||
#endif
|
||||
|
||||
namespace ba = boost::algorithm;
|
||||
|
||||
template <typename Iter>
|
||||
@ -72,14 +86,14 @@ BOOST_AUTO_TEST_CASE( test_main )
|
||||
// BOOST_CHECK_EQUAL(v[5], 5);
|
||||
|
||||
// Mix them up and try again - single element
|
||||
std::random_shuffle(v.begin(), v.end());
|
||||
do_shuffle(v.begin(), v.end());
|
||||
ba::partition_subrange(b, v.end(), b + 7, b + 8);
|
||||
check_sequence (b, v.end(), b + 7, b + 8);
|
||||
|
||||
// BOOST_CHECK_EQUAL(v[7], 7);
|
||||
|
||||
// Mix them up and try again - at the end
|
||||
std::random_shuffle(v.begin(), v.end());
|
||||
do_shuffle(v.begin(), v.end());
|
||||
ba::partition_subrange(b, v.end(), b + 7, v.end());
|
||||
check_sequence (b, v.end(), b + 7, v.end());
|
||||
|
||||
@ -88,7 +102,7 @@ BOOST_AUTO_TEST_CASE( test_main )
|
||||
// BOOST_CHECK_EQUAL(v[9], 9);
|
||||
|
||||
// Mix them up and try again - at the beginning
|
||||
std::random_shuffle(v.begin(), v.end());
|
||||
do_shuffle(v.begin(), v.end());
|
||||
ba::partition_subrange(b, v.end(), b, b + 2);
|
||||
check_sequence (b, v.end(), b, b + 2);
|
||||
|
||||
@ -96,12 +110,12 @@ BOOST_AUTO_TEST_CASE( test_main )
|
||||
// BOOST_CHECK_EQUAL(v[1], 1);
|
||||
|
||||
// Mix them up and try again - empty subrange
|
||||
std::random_shuffle(v.begin(), v.end());
|
||||
do_shuffle(v.begin(), v.end());
|
||||
ba::partition_subrange(b, v.end(), b, b);
|
||||
check_sequence (b, v.end(), b, b);
|
||||
|
||||
// Mix them up and try again - entire subrange
|
||||
std::random_shuffle(v.begin(), v.end());
|
||||
do_shuffle(v.begin(), v.end());
|
||||
ba::partition_subrange(b, v.end(), b, v.end());
|
||||
check_sequence (b, v.end(), b, v.end());
|
||||
}
|
||||
@ -120,14 +134,14 @@ BOOST_AUTO_TEST_CASE( test_main )
|
||||
// BOOST_CHECK_EQUAL(v[5], 4);
|
||||
|
||||
// Mix them up and try again - single element
|
||||
std::random_shuffle(v.begin(), v.end());
|
||||
do_shuffle(v.begin(), v.end());
|
||||
ba::partition_subrange(b, v.end(), b + 7, b + 8, std::greater<int>());
|
||||
check_sequence (b, v.end(), b + 7, b + 8, std::greater<int>());
|
||||
|
||||
// BOOST_CHECK_EQUAL(v[7], 2);
|
||||
|
||||
// Mix them up and try again - at the end
|
||||
std::random_shuffle(v.begin(), v.end());
|
||||
do_shuffle(v.begin(), v.end());
|
||||
ba::partition_subrange(b, v.end(), b + 7, v.end(), std::greater<int>());
|
||||
check_sequence (b, v.end(), b + 7, v.end(), std::greater<int>());
|
||||
|
||||
@ -136,7 +150,7 @@ BOOST_AUTO_TEST_CASE( test_main )
|
||||
// BOOST_CHECK_EQUAL(v[9], 0);
|
||||
|
||||
// Mix them up and try again - at the beginning
|
||||
std::random_shuffle(v.begin(), v.end());
|
||||
do_shuffle(v.begin(), v.end());
|
||||
ba::partition_subrange(b, v.end(), b, b + 2, std::greater<int>());
|
||||
check_sequence (b, v.end(), b, b + 2, std::greater<int>());
|
||||
|
||||
@ -144,12 +158,12 @@ BOOST_AUTO_TEST_CASE( test_main )
|
||||
// BOOST_CHECK_EQUAL(v[1], 8);
|
||||
|
||||
// Mix them up and try again - empty subrange
|
||||
std::random_shuffle(v.begin(), v.end());
|
||||
do_shuffle(v.begin(), v.end());
|
||||
ba::partition_subrange(b, v.end(), b, b, std::greater<int>());
|
||||
check_sequence (b, v.end(), b, b, std::greater<int>());
|
||||
|
||||
// Mix them up and try again - entire subrange
|
||||
std::random_shuffle(v.begin(), v.end());
|
||||
do_shuffle(v.begin(), v.end());
|
||||
ba::partition_subrange(b, v.end(), b, v.end(), std::greater<int>());
|
||||
check_sequence (b, v.end(), b, v.end(), std::greater<int>());
|
||||
}
|
||||
|
@ -7,6 +7,20 @@
|
||||
|
||||
#include <vector>
|
||||
#include <iostream>
|
||||
|
||||
#if __cplusplus >= 201103L
|
||||
#include <random>
|
||||
|
||||
std::default_random_engine gen;
|
||||
template<typename RandomIt>
|
||||
void do_shuffle(RandomIt first, RandomIt last)
|
||||
{ std::shuffle(first, last, gen); }
|
||||
#else
|
||||
template<typename RandomIt>
|
||||
void do_shuffle(RandomIt first, RandomIt last)
|
||||
{ std::random_shuffle(first, last); }
|
||||
#endif
|
||||
|
||||
namespace ba = boost::algorithm;
|
||||
|
||||
template <typename Iter>
|
||||
@ -53,14 +67,14 @@ BOOST_AUTO_TEST_CASE( test_main )
|
||||
BOOST_CHECK_EQUAL(v[5], 5);
|
||||
|
||||
// Mix them up and try again - single element
|
||||
std::random_shuffle(v.begin(), v.end());
|
||||
do_shuffle(v.begin(), v.end());
|
||||
ba::sort_subrange(b, v.end(), b + 7, b + 8);
|
||||
check_sequence (b, v.end(), b + 7, b + 8);
|
||||
|
||||
BOOST_CHECK_EQUAL(v[7], 7);
|
||||
|
||||
// Mix them up and try again - at the end
|
||||
std::random_shuffle(v.begin(), v.end());
|
||||
do_shuffle(v.begin(), v.end());
|
||||
ba::sort_subrange(b, v.end(), b + 7, v.end());
|
||||
check_sequence (b, v.end(), b + 7, v.end());
|
||||
|
||||
@ -69,7 +83,7 @@ BOOST_AUTO_TEST_CASE( test_main )
|
||||
BOOST_CHECK_EQUAL(v[9], 9);
|
||||
|
||||
// Mix them up and try again - at the beginning
|
||||
std::random_shuffle(v.begin(), v.end());
|
||||
do_shuffle(v.begin(), v.end());
|
||||
ba::sort_subrange(b, v.end(), b, b + 2);
|
||||
check_sequence (b, v.end(), b, b + 2);
|
||||
|
||||
@ -77,12 +91,12 @@ BOOST_AUTO_TEST_CASE( test_main )
|
||||
BOOST_CHECK_EQUAL(v[1], 1);
|
||||
|
||||
// Mix them up and try again - empty subrange
|
||||
std::random_shuffle(v.begin(), v.end());
|
||||
do_shuffle(v.begin(), v.end());
|
||||
ba::sort_subrange(b, v.end(), b, b);
|
||||
check_sequence (b, v.end(), b, b);
|
||||
|
||||
// Mix them up and try again - entire subrange
|
||||
std::random_shuffle(v.begin(), v.end());
|
||||
do_shuffle(v.begin(), v.end());
|
||||
ba::sort_subrange(b, v.end(), b, v.end());
|
||||
check_sequence (b, v.end(), b, v.end());
|
||||
}
|
||||
@ -101,14 +115,14 @@ BOOST_AUTO_TEST_CASE( test_main )
|
||||
BOOST_CHECK_EQUAL(v[5], 4);
|
||||
|
||||
// Mix them up and try again - single element
|
||||
std::random_shuffle(v.begin(), v.end());
|
||||
do_shuffle(v.begin(), v.end());
|
||||
ba::sort_subrange(b, v.end(), b + 7, b + 8, std::greater<int>());
|
||||
check_sequence (b, v.end(), b + 7, b + 8, std::greater<int>());
|
||||
|
||||
BOOST_CHECK_EQUAL(v[7], 2);
|
||||
|
||||
// Mix them up and try again - at the end
|
||||
std::random_shuffle(v.begin(), v.end());
|
||||
do_shuffle(v.begin(), v.end());
|
||||
ba::sort_subrange(b, v.end(), b + 7, v.end(), std::greater<int>());
|
||||
check_sequence (b, v.end(), b + 7, v.end(), std::greater<int>());
|
||||
|
||||
@ -117,7 +131,7 @@ BOOST_AUTO_TEST_CASE( test_main )
|
||||
BOOST_CHECK_EQUAL(v[9], 0);
|
||||
|
||||
// Mix them up and try again - at the beginning
|
||||
std::random_shuffle(v.begin(), v.end());
|
||||
do_shuffle(v.begin(), v.end());
|
||||
ba::sort_subrange(b, v.end(), b, b + 2, std::greater<int>());
|
||||
check_sequence (b, v.end(), b, b + 2, std::greater<int>());
|
||||
|
||||
@ -125,12 +139,12 @@ BOOST_AUTO_TEST_CASE( test_main )
|
||||
BOOST_CHECK_EQUAL(v[1], 8);
|
||||
|
||||
// Mix them up and try again - empty subrange
|
||||
std::random_shuffle(v.begin(), v.end());
|
||||
do_shuffle(v.begin(), v.end());
|
||||
ba::sort_subrange(b, v.end(), b, b, std::greater<int>());
|
||||
check_sequence (b, v.end(), b, b, std::greater<int>());
|
||||
|
||||
// Mix them up and try again - entire subrange
|
||||
std::random_shuffle(v.begin(), v.end());
|
||||
do_shuffle(v.begin(), v.end());
|
||||
ba::sort_subrange(b, v.end(), b, v.end(), std::greater<int>());
|
||||
check_sequence (b, v.end(), b, v.end(), std::greater<int>());
|
||||
}
|
||||
|
Reference in New Issue
Block a user