rippled
Loading...
Searching...
No Matches
algorithm.h
1//------------------------------------------------------------------------------
2/*
3 This file is part of rippled: https://github.com/ripple/rippled
4 Copyright (c) 2019 Ripple Labs Inc.
5
6 Permission to use, copy, modify, and/or distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20#ifndef RIPPLE_ALGORITHM_H_INCLUDED
21#define RIPPLE_ALGORITHM_H_INCLUDED
22
23#include <utility>
24
25namespace ripple {
26
27// Requires: [first1, last1) and [first2, last2) are ordered ranges according to
28// comp.
29
30// Effects: For each pair of elements {i, j} in the intersection of the sorted
31// sequences [first1, last1) and [first2, last2), perform action(i, j).
32
33// Note: This algorithm is evolved from std::set_intersection.
34template <class InputIter1, class InputIter2, class Action, class Comp>
35void
37 InputIter1 first1,
38 InputIter1 last1,
39 InputIter2 first2,
40 InputIter2 last2,
41 Action action,
42 Comp comp)
43{
44 while (first1 != last1 && first2 != last2)
45 {
46 if (comp(*first1, *first2)) // if *first1 < *first2
47 ++first1; // then reduce first range
48 else
49 {
50 if (!comp(*first2, *first1)) // if *first1 == *first2
51 { // then this is an intersection
52 action(*first1, *first2); // do the action
53 ++first1; // reduce first range
54 }
55 ++first2; // Reduce second range because *first2 <= *first1
56 }
57 }
58}
59
60// Requires: [first1, last1) and [first2, last2) are ordered ranges according to
61// comp.
62
63// Effects: Eliminates all the elements i in the range [first1, last1) which are
64// equivalent to some value in [first2, last2) or for which pred(i) returns
65// true.
66
67// Returns: A FwdIter1 E such that [first1, E) is the range of elements not
68// removed by this algorithm.
69
70// Note: This algorithm is evolved from std::remove_if and
71// std::set_intersection.
72template <class FwdIter1, class InputIter2, class Pred, class Comp>
73FwdIter1
75 FwdIter1 first1,
76 FwdIter1 last1,
77 InputIter2 first2,
78 InputIter2 last2,
79 Pred pred,
80 Comp comp)
81{
82 // [original-first1, current-first1) is the set of elements to be preserved.
83 // [current-first1, i) is the set of elements that have been removed.
84 // [i, last1) is the set of elements not tested yet.
85
86 // Test each *i in [first1, last1) against [first2, last2) and pred
87 for (auto i = first1; i != last1;)
88 {
89 // if (*i is not in [first2, last2)
90 if (first2 == last2 || comp(*i, *first2))
91 {
92 if (!pred(*i))
93 {
94 // *i should not be removed, so append it to the preserved set
95 if (i != first1)
96 *first1 = std::move(*i);
97 ++first1;
98 }
99 // *i has been fully tested, prepare for next i by
100 // appending i to the removed set, whether or not
101 // it has been moved from above.
102 ++i;
103 }
104 else // *i might be in [first2, last2) because *i >= *first2
105 {
106 if (!comp(*first2, *i)) // if *i == *first2
107 ++i; // then append *i to the removed set
108 // All elements in [i, last1) are known to be greater than *first2,
109 // so reduce the second range.
110 ++first2;
111 }
112 // Continue to test *i against [first2, last2) and pred
113 }
114 return first1;
115}
116
117} // namespace ripple
118
119#endif
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:25
void generalized_set_intersection(InputIter1 first1, InputIter1 last1, InputIter2 first2, InputIter2 last2, Action action, Comp comp)
Definition algorithm.h:36
FwdIter1 remove_if_intersect_or_match(FwdIter1 first1, FwdIter1 last1, InputIter2 first2, InputIter2 last2, Pred pred, Comp comp)
Definition algorithm.h:74