Skip to content

Commit 9bee197

Browse files
committed
Added all files to the new repository
1 parent 5314d59 commit 9bee197

File tree

7 files changed

+356
-0
lines changed

7 files changed

+356
-0
lines changed

doc/algorithm.qbk

+1
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ Thanks to all the people who have reviewed this library and made suggestions for
6666
[include clamp-hpp.qbk]
6767
[include gather.qbk]
6868
[include hex.qbk]
69+
[include is_palindrome.qbk]
6970
[endsect]
7071

7172

doc/is_palindrome.qbk

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
[/ File is_palindrome.qbk]
2+
3+
[section:is_palindrome is_palindrome]
4+
5+
[/license
6+
Copyright (c) 2016 Alexander Zaitsev
7+
8+
Distributed under the Boost Software License, Version 1.0.
9+
(See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
10+
]
11+
12+
The header file 'is_palindrome.hpp' contains four variants of a single algorithm, is_palindrome.
13+
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.
14+
15+
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.
16+
17+
The routine come in 4 forms; the first one takes two iterators to define the range. The second form takes two iterators to define the range and a predicate.
18+
The third form takes a single range parameter, and uses Boost.Range to traverse it. And the fourth form takes a single range parameter ( uses Boost.Range to traverse it) and a predicate.
19+
20+
21+
[heading interface]
22+
23+
The function `is_palindrome` returns true if the predicate returns true any tested by algorithm items in the sequence.
24+
There are four versions:
25+
1) takes two iterators.
26+
2) takes two iterators and a predicate.
27+
3) takes a range.
28+
4) takes a range and a predicate.
29+
30+
``
31+
template<typename BidirectionalIterator>
32+
bool is_palindrome ( BidirectionalIterator begin, BidirectionalIterator end );
33+
template<typename BidirectionalIterator, typename Predicate>
34+
bool is_palindrome ( BidirectionalIterator begin, BidirectionalIterator end, Predicate p );
35+
template<typename Range>
36+
bool is_palindrome ( const Range &r );
37+
template<typename Range, typename Predicate>
38+
bool is_palindrome ( const Range &r, Predicate p );
39+
``
40+
41+
42+
[heading Examples]
43+
44+
Given the containers:
45+
const std::list<int> empty,
46+
const std::vector<char> singleElement{'z'},
47+
int oddNonPalindrome[] = {3,2,2},
48+
const int evenPalindrome[] = {1,2,2,1}, then
49+
``
50+
51+
is_palindrome(empty)) --> true //empty range
52+
is_palindrome(singleElement)) --> true
53+
is_palindrome(std::begin(oddNonPalindrome), std::end(oddNonPalindrome))) --> false
54+
is_palindrome(std::begin(evenPalindrome), std::end(evenPalindrome))) --> true
55+
is_palindrome(empty.begin(), empty.end(), functorComparator())) --> true //empty range
56+
is_palindrome(std::begin(oddNonPalindrome), std::end(oddNonPalindrome), funcComparator<int>)) --> false
57+
is_palindrome(evenPalindrome, std::equal_to<int>())) --> true
58+
``
59+
60+
[heading Iterator Requirements]
61+
62+
`is_palindrome` work on Bidirectional and RandomAccess iterators.
63+
64+
[heading Complexity]
65+
66+
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.
67+
68+
[heading Exception Safety]
69+
70+
All of the variants of `is_palindrome` 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.
71+
72+
[heading Notes]
73+
74+
* `is_palindrome` returns true for empty ranges and for single element ranges.
75+
76+
* If you use version of 'is_palindrome' without custom predicate, 'is_palindrome' uses default 'operator==' for elements. If you want use custom predicate, you must redefine 'operator=='.
77+
78+
[endsect]
79+
80+
[/ File is_palindrome.qbk
81+
Copyright 2016 Alexander Zaitsev
82+
Distributed under the Boost Software License, Version 1.0.
83+
(See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt).
84+
]

example/Jamfile.v2

+2
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,5 @@ project /boost/algorithm/example
2020

2121
exe clamp_example : clamp_example.cpp ;
2222
exe search_example : search_example.cpp ;
23+
exe is_palindrome_example : is_palindrome_example.cpp;
24+

example/is_palindrome_example.cpp

+94
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/*
2+
Copyright (c) Alexander Zaitsev <[email protected]>, 2016
3+
4+
Distributed under the Boost Software License, Version 1.0. (See accompanying
5+
file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6+
7+
For more information, see http://www.boost.org
8+
*/
9+
10+
#include <vector>
11+
#include <list>
12+
#include <iterator>
13+
#include <functional>
14+
#include <iostream>
15+
16+
#include <boost/algorithm/is_palindrome.hpp>
17+
18+
19+
namespace ba = boost::algorithm;
20+
21+
template <typename T>
22+
bool funcComparator(const T& v1, const T& v2)
23+
{
24+
return v1 == v2;
25+
}
26+
27+
struct functorComparator
28+
{
29+
template <typename T>
30+
bool operator()(const T& v1, const T& v2) const
31+
{
32+
return v1 == v2;
33+
}
34+
};
35+
36+
37+
int main ( int /*argc*/, char * /*argv*/ [] )
38+
{
39+
//You can this algorithm with iterators(minimum Bidirectional)
40+
std::vector<int> vec{1,2,1};
41+
if(ba::is_palindrome(vec.begin(), vec.end()))
42+
std::cout << "This container is palindrome" << std::endl;
43+
else
44+
std::cout << "This container is not palindrome" << std::endl;
45+
46+
47+
//Of course, you can use const iterators
48+
if(ba::is_palindrome(vec.cbegin(), vec.cend()))
49+
std::cout << "This container is palindrome" << std::endl;
50+
else
51+
std::cout << "This container is not palindrome" << std::endl;
52+
53+
54+
//Example with bidirectional iterators
55+
std::list<int> list{1,2,1};
56+
if(ba::is_palindrome(list.begin(), list.end()))
57+
std::cout << "This container is palindrome" << std::endl;
58+
else
59+
std::cout << "This container is not palindrome" << std::endl;
60+
61+
62+
//You can use custom comparators like functions, functors, lambdas
63+
auto lambdaComparator = [](int v1, int v2){ return v1 == v2; };
64+
auto objFunc = std::function<bool(int, int)>(lambdaComparator);
65+
66+
if(ba::is_palindrome(vec.begin(), vec.end(), lambdaComparator))
67+
std::cout << "This container is palindrome" << std::endl;
68+
else
69+
std::cout << "This container is not palindrome" << std::endl;
70+
71+
if(ba::is_palindrome(vec.begin(), vec.end(), funcComparator<int>))
72+
std::cout << "This container is palindrome" << std::endl;
73+
else
74+
std::cout << "This container is not palindrome" << std::endl;
75+
76+
if(ba::is_palindrome(vec.begin(), vec.end(), functorComparator()))
77+
std::cout << "This container is palindrome" << std::endl;
78+
else
79+
std::cout << "This container is not palindrome" << std::endl;
80+
81+
if(ba::is_palindrome(vec.begin(), vec.end(), objFunc))
82+
std::cout << "This container is palindrome" << std::endl;
83+
else
84+
std::cout << "This container is not palindrome" << std::endl;
85+
86+
87+
//You can use ranges
88+
if(ba::is_palindrome(vec))
89+
std::cout << "This container is palindrome" << std::endl;
90+
else
91+
std::cout << "This container is not palindrome" << std::endl;
92+
93+
return 0;
94+
}
+107
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
/*
2+
Copyright (c) Alexander Zaitsev <[email protected]>, 2016
3+
4+
Distributed under the Boost Software License, Version 1.0. (See
5+
accompanying file LICENSE_1_0.txt or copy at
6+
http://www.boost.org/LICENSE_1_0.txt)
7+
8+
See http://www.boost.org/ for latest version.
9+
*/
10+
11+
/// \file is_palindrome.hpp
12+
/// \brief Checks the input sequence on palindrome.
13+
/// \author Alexander Zaitsev
14+
15+
#ifndef BOOST_ALGORITHM_is_palindrome_HPP
16+
#define BOOST_ALGORITHM_is_palindrome_HPP
17+
18+
#include <iterator>
19+
20+
#include <boost/range/begin.hpp>
21+
#include <boost/range/end.hpp>
22+
23+
namespace boost { namespace algorithm {
24+
25+
/// \fn is_palindrome ( BidirectionalIterator begin, BidirectionalIterator end, Predicate p )
26+
/// \return true if the entire sequence is palindrome
27+
///
28+
/// \param begin The start of the input sequence
29+
/// \param end One past the end of the input sequence
30+
/// \param p A predicate used to compare the values.
31+
///
32+
/// \note This function will return true for empty sequences and for palindromes.
33+
/// For other sequences function will return false.
34+
/// Complexity: O(N).
35+
template <typename BidirectionalIterator, typename Predicate>
36+
bool is_palindrome(BidirectionalIterator begin, BidirectionalIterator end, Predicate p)
37+
{
38+
if(begin == end)
39+
{
40+
return true;
41+
}
42+
43+
--end;
44+
while(begin != end)
45+
{
46+
if(!p(*begin, *end))
47+
{
48+
return false;
49+
}
50+
++begin;
51+
if(begin == end)
52+
{
53+
break;
54+
}
55+
--end;
56+
}
57+
return true;
58+
}
59+
60+
/// \fn is_palindrome ( BidirectionalIterator begin, BidirectionalIterator end )
61+
/// \return true if the entire sequence is palindrome
62+
///
63+
/// \param begin The start of the input sequence
64+
/// \param end One past the end of the input sequence
65+
///
66+
/// \note This function will return true for empty sequences and for palindromes.
67+
/// For other sequences function will return false.
68+
/// Complexity: O(N).
69+
template <typename BidirectionalIterator>
70+
bool is_palindrome(BidirectionalIterator begin, BidirectionalIterator end)
71+
{
72+
return is_palindrome(begin, end,
73+
std::equal_to<typename std::iterator_traits<BidirectionalIterator>::value_type> ());
74+
}
75+
76+
/// \fn is_palindrome ( const R& range )
77+
/// \return true if the entire sequence is palindrome
78+
///
79+
/// \param range The range to be tested.
80+
///
81+
/// \note This function will return true for empty sequences and for palindromes.
82+
/// For other sequences function will return false.
83+
/// Complexity: O(N).
84+
template <typename R>
85+
bool is_palindrome(const R& range)
86+
{
87+
return is_palindrome(boost::begin(range), boost::end(range));
88+
}
89+
90+
/// \fn is_palindrome ( const R& range, Predicate p )
91+
/// \return true if the entire sequence is palindrome
92+
///
93+
/// \param range The range to be tested.
94+
/// \param p A predicate used to compare the values.
95+
///
96+
/// \note This function will return true for empty sequences and for palindromes.
97+
/// For other sequences function will return false.
98+
/// Complexity: O(N).
99+
template <typename R, typename Predicate>
100+
bool is_palindrome(const R& range, Predicate p)
101+
{
102+
return is_palindrome(boost::begin(range), boost::end(range), p);
103+
}
104+
105+
}}
106+
107+
#endif // BOOST_ALGORITHM_is_palindrome_HPP

test/Jamfile.v2

+2
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ alias unit_test_framework
6868
[ run sort_subrange_test.cpp unit_test_framework : : : : sort_subrange_test ]
6969
[ run partition_subrange_test.cpp unit_test_framework : : : : partition_subrange_test ]
7070

71+
# Is_palindrome tests
72+
[ run is_palindrome_test.cpp unit_test_framework : : : : is_palindrome_test ]
7173
;
7274
}
7375

test/is_palindrome_test.cpp

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
Copyright (c) Alexander Zaitsev <[email protected]>, 2016
3+
4+
Distributed under the Boost Software License, Version 1.0. (See
5+
accompanying file LICENSE_1_0.txt or copy at
6+
http://www.boost.org/LICENSE_1_0.txt)
7+
8+
See http://www.boost.org/ for latest version.
9+
*/
10+
11+
#include <algorithm>
12+
#include <iostream>
13+
#include <list>
14+
#include <vector>
15+
16+
#include <boost/algorithm/is_palindrome.hpp>
17+
#include <boost/test/included/test_exec_monitor.hpp>
18+
19+
namespace ba = boost::algorithm;
20+
21+
22+
template <typename T>
23+
bool funcComparator(const T& v1, const T& v2)
24+
{
25+
return v1 == v2;
26+
}
27+
28+
struct functorComparator
29+
{
30+
template <typename T>
31+
bool operator()(const T& v1, const T& v2) const
32+
{
33+
return v1 == v2;
34+
}
35+
};
36+
37+
38+
static void test_is_palindrome()
39+
{
40+
const std::list<int> empty;
41+
const std::vector<char> singleElement{'z'};
42+
int oddNonPalindrome[] = {3,2,2};
43+
const int evenPalindrome[] = {1,2,2,1};
44+
45+
// Test a default operator==
46+
BOOST_CHECK ( ba::is_palindrome(empty));
47+
BOOST_CHECK ( ba::is_palindrome(singleElement));
48+
BOOST_CHECK (!ba::is_palindrome(std::begin(oddNonPalindrome), std::end(oddNonPalindrome)));
49+
BOOST_CHECK ( ba::is_palindrome(std::begin(evenPalindrome), std::end(evenPalindrome)));
50+
51+
//Test the custom comparators
52+
BOOST_CHECK ( ba::is_palindrome(empty.begin(), empty.end(), functorComparator()));
53+
BOOST_CHECK (!ba::is_palindrome(std::begin(oddNonPalindrome), std::end(oddNonPalindrome), funcComparator<int>));
54+
BOOST_CHECK ( ba::is_palindrome(evenPalindrome, std::equal_to<int>()));
55+
56+
//Only C++14 or newer
57+
//auto lambdaComparator = [](const auto& v1, const auto& v2){ return v1 == v2; };
58+
//BOOST_CHECK ( ba::is_palindrome(singleElement, lambdaComparator));
59+
}
60+
61+
int test_main( int, char * [] )
62+
{
63+
test_is_palindrome();
64+
65+
return 0;
66+
}

0 commit comments

Comments
 (0)