|
| 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 | +] |
0 commit comments