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