• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1[/ File is_palindrome.qbk]
2
3[section:is_palindrome is_palindrome]
4
5[/license
6Copyright (c) 2016 Alexander Zaitsev
7
8Distributed 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
12The header file 'is_palindrome.hpp' contains six variants of a single algorithm, is_palindrome.
13The 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
15The 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
17The 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.
18The 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.
19The fifth form takes a single C-string and a predicate. The sixth form takes a single C-string.
20
21
22[heading interface]
23
24The function `is_palindrome` returns true if the predicate returns true any tested by algorithm items in the sequence.
25There are six versions:
261) takes two iterators.
272) takes two iterators and a predicate.
283) takes a range.
294) takes a range and a predicate.
305) takes a C-string and a predicate.
316) takes a C-string.
32
33``
34template<typename BidirectionalIterator>
35	bool is_palindrome ( BidirectionalIterator begin, BidirectionalIterator end );
36template<typename BidirectionalIterator, typename Predicate>
37	bool is_palindrome ( BidirectionalIterator begin, BidirectionalIterator end, Predicate p );
38template<typename Range>
39	bool is_palindrome ( const Range &r );
40template<typename Range, typename Predicate>
41	bool is_palindrome ( const Range &r, Predicate p );
42template<typename Predicate>
43	bool is_palindrome ( const char* str, Predicate p );
44bool is_palindrome(const char* str);
45``
46
47
48[heading Examples]
49
50Given the containers:
51const std::list<int> empty,
52const std::vector<char> singleElement{'z'},
53int oddNonPalindrome[] = {3,2,2},
54const int oddPalindrome[] = {1,2,3,2,1},
55const int evenPalindrome[] = {1,2,2,1},
56int evenNonPalindrome[] = {1,4,8,8}, then
57``
58
59is_palindrome(empty))  --> true //empty range
60is_palindrome(singleElement))  --> true
61is_palindrome(std::begin(oddNonPalindrome), std::end(oddNonPalindrome))) --> false
62is_palindrome(std::begin(evenPalindrome), std::end(evenPalindrome))) --> true
63is_palindrome(empty.begin(), empty.end(), functorComparator())) --> true //empty range
64is_palindrome(std::begin(oddNonPalindrome), std::end(oddNonPalindrome), funcComparator<int>)) --> false
65is_palindrome(std::begin(oddPalindrome), std::end(oddPalindrome)) --> true
66is_palindrome(evenPalindrome, std::equal_to<int>())) --> true
67is_palindrome(std::begin(evenNonPalindrome), std::end(evenNonPalindrome)) --> false
68is_palindrome("a") --> true
69is_palindrome("aba", std::equal_to<char>()) --> true
70``
71
72[heading Iterator Requirements]
73
74`is_palindrome` work on Bidirectional and RandomAccess iterators.
75
76[heading Complexity]
77
78All 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.
79
80[heading Exception Safety]
81
82All 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.
83
84[heading Notes]
85
86* `is_palindrome` returns true for empty ranges, const char* null pointers and for single element ranges.
87
88* If you use version of 'is_palindrome' without custom predicate, 'is_palindrome' uses default 'operator==()' for elements.
89
90* Be careful with using not null pointer 'const char*' without '\0' - if you use it with 'is_palindrome', it's a undefined behaviour.
91
92[endsect]
93
94[/ File is_palindrome.qbk
95Copyright 2016 Alexander Zaitsev
96Distributed under the Boost Software License, Version 1.0.
97(See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt).
98]
99