1 | /* |
2 | Copyright (c) Marshall Clow 2008-2012. |
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 | |
8 | /// \file one_of.hpp |
9 | /// \brief Test ranges to see if only one element matches a value or predicate. |
10 | /// \author Marshall Clow |
11 | |
12 | #ifndef BOOST_ALGORITHM_ONE_OF_HPP |
13 | #define BOOST_ALGORITHM_ONE_OF_HPP |
14 | |
15 | #include <algorithm> // for std::find and std::find_if |
16 | #include <boost/algorithm/cxx11/none_of.hpp> |
17 | |
18 | #include <boost/range/begin.hpp> |
19 | #include <boost/range/end.hpp> |
20 | |
21 | namespace boost { namespace algorithm { |
22 | |
23 | /// \fn one_of ( InputIterator first, InputIterator last, Predicate p ) |
24 | /// \return true if the predicate 'p' is true for exactly one item in [first, last). |
25 | /// |
26 | /// \param first The start of the input sequence |
27 | /// \param last One past the end of the input sequence |
28 | /// \param p A predicate for testing the elements of the sequence |
29 | /// |
30 | template<typename InputIterator, typename Predicate> |
31 | bool one_of ( InputIterator first, InputIterator last, Predicate p ) |
32 | { |
33 | InputIterator i = std::find_if (first, last, p); |
34 | if (i == last) |
35 | return false; // Didn't occur at all |
36 | return boost::algorithm::none_of (++i, last, p); |
37 | } |
38 | |
39 | /// \fn one_of ( const Range &r, Predicate p ) |
40 | /// \return true if the predicate 'p' is true for exactly one item in the range. |
41 | /// |
42 | /// \param r The input range |
43 | /// \param p A predicate for testing the elements of the range |
44 | /// |
45 | template<typename Range, typename Predicate> |
46 | bool one_of ( const Range &r, Predicate p ) |
47 | { |
48 | return boost::algorithm::one_of ( boost::begin (r), boost::end (r), p ); |
49 | } |
50 | |
51 | |
52 | /// \fn one_of_equal ( InputIterator first, InputIterator last, const V &val ) |
53 | /// \return true if the value 'val' exists only once in [first, last). |
54 | /// |
55 | /// \param first The start of the input sequence |
56 | /// \param last One past the end of the input sequence |
57 | /// \param val A value to compare against |
58 | /// |
59 | template<typename InputIterator, typename V> |
60 | bool one_of_equal ( InputIterator first, InputIterator last, const V &val ) |
61 | { |
62 | InputIterator i = std::find (first, last, val); // find first occurrence of 'val' |
63 | if (i == last) |
64 | return false; // Didn't occur at all |
65 | return boost::algorithm::none_of_equal (++i, last, val); |
66 | } |
67 | |
68 | /// \fn one_of_equal ( const Range &r, const V &val ) |
69 | /// \return true if the value 'val' exists only once in the range. |
70 | /// |
71 | /// \param r The input range |
72 | /// \param val A value to compare against |
73 | /// |
74 | template<typename Range, typename V> |
75 | bool one_of_equal ( const Range &r, const V &val ) |
76 | { |
77 | return boost::algorithm::one_of_equal ( boost::begin (r), boost::end (r), val ); |
78 | } |
79 | |
80 | }} // namespace boost and algorithm |
81 | |
82 | #endif // BOOST_ALGORITHM_ALL_HPP |
83 | |