rippled
Loading...
Searching...
No Matches
algorithm.h
1#pragma once
2
3#include <utility>
4
5namespace xrpl {
6
7// Requires: [first1, last1) and [first2, last2) are ordered ranges according to
8// comp.
9
10// Effects: For each pair of elements {i, j} in the intersection of the sorted
11// sequences [first1, last1) and [first2, last2), perform action(i, j).
12
13// Note: This algorithm is evolved from std::set_intersection.
14template <class InputIter1, class InputIter2, class Action, class Comp>
15void
17 InputIter1 first1,
18 InputIter1 last1,
19 InputIter2 first2,
20 InputIter2 last2,
21 Action action,
22 Comp comp)
23{
24 while (first1 != last1 && first2 != last2)
25 {
26 if (comp(*first1, *first2)) // if *first1 < *first2
27 ++first1; // then reduce first range
28 else
29 {
30 if (!comp(*first2, *first1)) // if *first1 == *first2
31 { // then this is an intersection
32 action(*first1, *first2); // do the action
33 ++first1; // reduce first range
34 }
35 ++first2; // Reduce second range because *first2 <= *first1
36 }
37 }
38}
39
40// Requires: [first1, last1) and [first2, last2) are ordered ranges according to
41// comp.
42
43// Effects: Eliminates all the elements i in the range [first1, last1) which are
44// equivalent to some value in [first2, last2) or for which pred(i) returns
45// true.
46
47// Returns: A FwdIter1 E such that [first1, E) is the range of elements not
48// removed by this algorithm.
49
50// Note: This algorithm is evolved from std::remove_if and
51// std::set_intersection.
52template <class FwdIter1, class InputIter2, class Pred, class Comp>
53FwdIter1
55 FwdIter1 first1,
56 FwdIter1 last1,
57 InputIter2 first2,
58 InputIter2 last2,
59 Pred pred,
60 Comp comp)
61{
62 // [original-first1, current-first1) is the set of elements to be preserved.
63 // [current-first1, i) is the set of elements that have been removed.
64 // [i, last1) is the set of elements not tested yet.
65
66 // Test each *i in [first1, last1) against [first2, last2) and pred
67 for (auto i = first1; i != last1;)
68 {
69 // if (*i is not in [first2, last2)
70 if (first2 == last2 || comp(*i, *first2))
71 {
72 if (!pred(*i))
73 {
74 // *i should not be removed, so append it to the preserved set
75 if (i != first1)
76 *first1 = std::move(*i);
77 ++first1;
78 }
79 // *i has been fully tested, prepare for next i by
80 // appending i to the removed set, whether or not
81 // it has been moved from above.
82 ++i;
83 }
84 else // *i might be in [first2, last2) because *i >= *first2
85 {
86 if (!comp(*first2, *i)) // if *i == *first2
87 ++i; // then append *i to the removed set
88 // All elements in [i, last1) are known to be greater than *first2,
89 // so reduce the second range.
90 ++first2;
91 }
92 // Continue to test *i against [first2, last2) and pred
93 }
94 return first1;
95}
96
97} // namespace xrpl
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
FwdIter1 remove_if_intersect_or_match(FwdIter1 first1, FwdIter1 last1, InputIter2 first2, InputIter2 last2, Pred pred, Comp comp)
Definition algorithm.h:54
void generalized_set_intersection(InputIter1 first1, InputIter1 last1, InputIter2 first2, InputIter2 last2, Action action, Comp comp)
Definition algorithm.h:16