qxLib
all_of_equal.h
Go to the documentation of this file.
1 /**
2 
3  @file all_of_equal.h
4  @author Khrapov
5  @date 7.02.2026
6  @copyright © Nick Khrapov, 2026. All right reserved.
7 
8 **/
9 #pragma once
10 
11 #include <algorithm>
12 
13 namespace qx
14 {
15 
16 /**
17  @brief Returns true if the predicate yields the same value for all elements in the range [itBegin, itEnd).
18  @details This implements a logical biconditional (logical equivalence) over the range:
19  predicate(x1) <-> predicate(x2) <-> ... <-> predicate(xn)
20  @tparam fwd_it_t - forward iterator type
21  @tparam predicate_t - predicate type
22  @param itBegin - range begin iterator
23  @param itEnd - range end iterator
24  @param predicate - predicate to check
25  @retval - true if the predicate yields the same value for all elements in the range, otherwise false
26 **/
27 template<class fwd_it_t, class predicate_t>
28 bool all_of_equal(fwd_it_t itBegin, fwd_it_t itEnd, const predicate_t& predicate)
29 {
30  if (itBegin == itEnd)
31  return true;
32 
33  auto&& value = std::invoke(predicate, *itBegin);
34  return std::all_of(
35  ++itBegin,
36  itEnd,
37  [&value, &predicate](const auto& x)
38  {
39  return std::invoke(predicate, x) == value;
40  });
41 }
42 
43 /**
44  @brief Returns true if the predicate yields the same value for all elements in the range [itBegin, itEnd).
45  @details This implements a logical biconditional (logical equivalence) over the range:
46  predicate(x1) <-> predicate(x2) <-> ... <-> predicate(xn)
47  @tparam container_t - container type
48  @tparam predicate_t - predicate type
49  @param container - input range
50  @param predicate - predicate to check
51  @retval - true if the predicate yields the same value for all elements in the range, otherwise false
52 **/
53 template<class container_t, class predicate_t>
54 bool all_of_equal(const container_t& container, const predicate_t& predicate)
55 {
56  return all_of_equal(std::begin(container), std::end(container), predicate);
57 }
58 
59 } // namespace qx
bool all_of_equal(fwd_it_t itBegin, fwd_it_t itEnd, const predicate_t &predicate)
Returns true if the predicate yields the same value for all elements in the range [itBegin,...
Definition: all_of_equal.h:28