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