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