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