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