qxLib
equal.inl
Go to the documentation of this file.
1 /**
2 
3  @file equal.inl
4  @author Khrapov
5  @date 20.08.2023
6  @copyright © Nick Khrapov, 2023. All right reserved.
7 
8 **/
9 
10 namespace qx
11 {
12 
13 template<class it_1_t, class it_2_t, class pred_t>
14 inline bool equal_rotated(
15  it_1_t range1Begin,
16  it_1_t range1End,
17  it_2_t range2Begin,
18  it_2_t range2End,
19  const pred_t& pred) noexcept
20 {
21  auto increment_range_1_it = [range1Begin, range1End](it_1_t itCurrent)
22  {
23  it_1_t itNext = ++itCurrent;
24  return itNext != range1End ? itNext : range1Begin;
25  };
26 
27  auto compare_starting_with_it = [increment_range_1_it, &pred, range2Begin, range2End](it_1_t range1Start)
28  {
29  it_1_t range1Current = range1Start;
30  for (it_2_t it = range2Begin; it != range2End; ++it)
31  {
32  if (!pred(*range1Current, *it))
33  return false;
34 
35  range1Current = increment_range_1_it(range1Current);
36  }
37 
38  return range1Current == range1Start;
39  };
40 
41  for (it_1_t it = range1Begin; it != range1End; ++it)
42  if (compare_starting_with_it(it))
43  return true;
44 
45  return false;
46 }
47 
48 template<class range_1_t, class range_2_t, class pred_t>
49 inline bool equal_rotated(const range_1_t& range1, const range_2_t& range2, const pred_t& pred) noexcept
50 {
51  return equal_rotated(range1.begin(), range1.end(), range2.begin(), range2.end(), pred);
52 }
53 
54 template<class it_1_t, class it_2_t>
55 inline bool equal_rotated(it_1_t range1Begin, it_1_t range1End, it_2_t range2Begin, it_2_t range2End) noexcept
56 {
57  return equal_rotated(
58  range1Begin,
59  range1End,
60  range2Begin,
61  range2End,
62  [](const auto& value1, const auto& value2)
63  {
64  return value1 == value2;
65  });
66 }
67 
68 template<class range_1_t, class range_2_t>
69 inline bool equal_rotated(const range_1_t& range1, const range_2_t& range2) noexcept
70 {
71  return equal_rotated(range1.begin(), range1.end(), range2.begin(), range2.end());
72 }
73 
74 } // namespace qx
bool equal_rotated(it_1_t range1Begin, it_1_t range1End, it_2_t range2Begin, it_2_t range2End, const pred_t &pred) noexcept
Check if two ranges contains same values in a certain order, which may be shifted.
Definition: equal.inl:14