CombineHarvester
Algorithm.h
Go to the documentation of this file.
1 #ifndef CombineTools_Algorithm_h
2 #define CombineTools_Algorithm_h
3 #include <algorithm>
4 #include <vector>
5 #include "boost/range/begin.hpp"
6 #include "boost/range/end.hpp"
7 #include "boost/regex.hpp"
8 #include "boost/range/algorithm_ext/erase.hpp"
9 
10 namespace ch {
11 template <typename Range, typename Predicate>
12 void erase_if(Range & r, Predicate p) {
13  r.erase(std::remove_if(r.begin(), r.end(), p), r.end());
14 }
15 
16 template<typename Range, typename Predicate>
17 bool any_of(const Range &r, Predicate p) {
18  return std::any_of(boost::begin(r), boost::end(r), p);
19 }
20 
21 template<typename Range, typename T>
22 bool contains(const Range &r, T p) {
23  return std::find(boost::begin(r), boost::end(r), p) != boost::end(r);
24 }
25 
26 // This version needed for cases like: contains({"A", "B"}, "B"),
27 // which are not implicitly convertible in the above generic version
28 template<typename R, typename T>
29 bool contains(const std::initializer_list<R> &r, T p) {
30  return std::find(boost::begin(r), boost::end(r), p) != boost::end(r);
31 }
32 
33 template <typename T>
34 bool contains_rgx(const std::vector<boost::regex>& r, T p) {
35  for (auto const& rgx : r)
36  if (regex_match(p, rgx)) return true;
37  return false;
38 }
39 
40 template <typename Input, typename Filter, typename Converter>
41 void FilterContaining(Input& in, Filter const& filter, Converter fn,
42  bool cond) {
43  boost::remove_erase_if(in, [&](typename Input::value_type const& p) {
44  return cond != ch::contains(filter, fn(p));
45  });
46 }
47 
48 template <typename Input, typename Filter, typename Converter>
49 void FilterContainingRgx(Input& in, Filter const& filter, Converter fn,
50  bool cond) {
51  std::vector<boost::regex> rgx;
52  for (auto const& ele : filter) rgx.emplace_back(ele);
53  boost::remove_erase_if(in, [&](typename Input::value_type const& p) {
54  return cond != ch::contains_rgx(rgx, fn(p));
55  });
56 }
57 
58 template <typename Input, typename Filter, typename Converter, typename Funcarg>
59 void FilterContaining(Input& in, Filter const& filter, Converter fn, Funcarg arg,
60  bool cond) {
61  boost::remove_erase_if(in, [&](typename Input::value_type const& p) {
62  return cond != ch::contains(filter, fn(p,arg));
63  });
64 }
65 
66 template <typename Input, typename Filter, typename Converter, typename Funcarg>
67 void FilterContainingRgx(Input& in, Filter const& filter, Converter fn, Funcarg arg,
68  bool cond) {
69  std::vector<boost::regex> rgx;
70  for (auto const& ele : filter) rgx.emplace_back(ele);
71  boost::remove_erase_if(in, [&](typename Input::value_type const& p) {
72  return cond != ch::contains_rgx(rgx, fn(p,arg));
73  });
74 }
75 }
76 
77 #endif
Definition: Algorithm.h:10
void FilterContainingRgx(Input &in, Filter const &filter, Converter fn, bool cond)
Definition: Algorithm.h:49
void FilterContaining(Input &in, Filter const &filter, Converter fn, bool cond)
Definition: Algorithm.h:41
bool contains_rgx(const std::vector< boost::regex > &r, T p)
Definition: Algorithm.h:34
bool contains(const Range &r, T p)
Definition: Algorithm.h:22
bool any_of(const Range &r, Predicate p)
Definition: Algorithm.h:17
void erase_if(Range &r, Predicate p)
Definition: Algorithm.h:12