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
54remove_if_intersect_or_match(FwdIter1 first1, FwdIter1 last1, InputIter2 first2, InputIter2 last2, Pred pred, Comp comp)
55{
56 // [original-first1, current-first1) is the set of elements to be preserved.
57 // [current-first1, i) is the set of elements that have been removed.
58 // [i, last1) is the set of elements not tested yet.
59
60 // Test each *i in [first1, last1) against [first2, last2) and pred
61 for (auto i = first1; i != last1;)
62 {
63 // if (*i is not in [first2, last2)
64 if (first2 == last2 || comp(*i, *first2))
65 {
66 if (!pred(*i))
67 {
68 // *i should not be removed, so append it to the preserved set
69 if (i != first1)
70 *first1 = std::move(*i);
71 ++first1;
72 }
73 // *i has been fully tested, prepare for next i by
74 // appending i to the removed set, whether or not
75 // it has been moved from above.
76 ++i;
77 }
78 else // *i might be in [first2, last2) because *i >= *first2
79 {
80 if (!comp(*first2, *i)) // if *i == *first2
81 ++i; // then append *i to the removed set
82 // All elements in [i, last1) are known to be greater than *first2,
83 // so reduce the second range.
84 ++first2;
85 }
86 // Continue to test *i against [first2, last2) and pred
87 }
88 return first1;
89}
90
91} // 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