21 #ifndef yap_container_utils_h
22 #define yap_container_utils_h
28 template <
class InputIt1,
class InputIt2,
class BinaryPredicate>
29 bool overlap(InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2, BinaryPredicate p)
31 using T1 =
typename std::iterator_traits<InputIt1>::value_type;
32 using T2 =
typename std::iterator_traits<InputIt2>::value_type;
33 return std::any_of(first1, last1, [&](
const T1 & a) {
return std::any_of(first2, last2, [&](
const T2 & b) {
return p(a, b);}); });
38 template <
class InputIt1,
class InputIt2,
class BinaryPredicate>
39 bool disjoint(InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2, BinaryPredicate p)
41 using T1 =
typename std::iterator_traits<InputIt1>::value_type;
42 using T2 =
typename std::iterator_traits<InputIt2>::value_type;
43 return std::none_of(first1, last1, [&](
const T1 & a) {
return std::none_of(first2, last2, [&](
const T2 & b) {
return p(a, b);}); });
48 template <
class InputIt1,
class InputIt2,
class BinaryPredicate>
49 bool contains(InputIt1 first_haystack, InputIt1 last_haystack, InputIt2 first_needle, InputIt2 last_needle, BinaryPredicate p)
51 return std::all_of(first_needle, last_needle, [&](
const typename std::iterator_traits<InputIt2>::value_type& b)
52 {
return std::any_of(first_haystack, last_haystack, std::bind(p, std::placeholders::_1, b)); });
57 template <
class InputIt>
58 const bool orderless_equal(InputIt first1, InputIt last1, InputIt first2, InputIt last2)
60 return std::all_of(first1, last1, [&](
const typename std::iterator_traits<InputIt>::value_type& a)
61 {
return std::count(first1, last1, a) == std::count(first2, last2, a);});
66 template <
class InputIt>
67 const bool orderless_contains(InputIt first_haystack, InputIt last_haystack, InputIt first_needle, InputIt last_needle)
69 return std::all_of(first_needle, last_needle, [&](
const typename std::iterator_traits<InputIt>::value_type& a)
70 {
return std::count(first_needle, last_needle, a) <= std::count(first_haystack, last_haystack, a);});
77 template <
class InputIt>
81 std::vector<InputIt> v;
82 v.reserve(std::distance(first, last));
83 for (InputIt i = first; i != last; ++i)
86 std::sort(v.begin(), v.end(), [](
const InputIt & A,
const InputIt & B) {
return *A < *B;});
88 v.erase(std::unique(v.begin(), v.end(), [](
const InputIt & A,
const InputIt & B) {
return *A == *B;}), v.end());
89 std::sort(v.begin(), v.end());
92 for (InputIt i = first; i != last && j != v.size(); ++i)
94 std::iter_swap(i, first);
107 template <
class InputIt>
108 std::vector<std::vector<typename InputIt::value_type> >
combinations(InputIt first, InputIt last,
size_t n)
111 std::vector<InputIt> Its(n, last);
112 for (
size_t i = 0; i < n; ++i)
116 std::vector<std::vector<typename InputIt::value_type> > C;
119 while (Its.back() < last) {
122 std::vector<typename InputIt::value_type> v;
124 for (
const auto& it : Its)
134 for (
int i = Its.size() - 1; i >= 0 && Its[i] + (Its.size() - 1 - i ) >= last; --i) {
143 for (
size_t j = i; j < Its.size(); ++j)
144 Its[j] = Its[j - 1] + 1;
155 template <
typename T>
156 bool overlap(
const std::vector<T>& A,
const std::vector<T>& B)
157 {
return overlap(A.begin(), A.end(), B.begin(), B.end(), [](
const T & a,
const T & b) {
return a == b;}); }
160 template <
typename T>
161 bool disjoint(
const std::vector<T>& A,
const std::vector<T>& B)
162 {
return disjoint(A.begin(), A.end(), B.begin(), B.end(), [](
const T & a,
const T & b) {
return a == b;}); }
165 template <
typename T>
166 bool contains(
const std::vector<T>& A,
const std::vector<T>& B)
167 {
return contains(A.begin(), A.end(), B.begin(), B.end(), [](
const T & a,
const T & b) {
return a == b;}); }
170 template <
typename T>
171 std::vector<std::vector<T> >
combinations(
const std::vector<T>& V,
size_t n)
bool contains(InputIt1 first_haystack, InputIt1 last_haystack, InputIt2 first_needle, InputIt2 last_needle, BinaryPredicate p)
Definition: container_utils.h:49
const bool orderless_equal(InputIt first1, InputIt last1, InputIt first2, InputIt last2)
Definition: container_utils.h:58
std::vector< std::vector< typename InputIt::value_type > > combinations(InputIt first, InputIt last, size_t n)
Definition: container_utils.h:108
InputIt ordered_unique(InputIt first, InputIt last)
Definition: container_utils.h:78
bool disjoint(InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2, BinaryPredicate p)
Definition: container_utils.h:39
bool overlap(InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2, BinaryPredicate p)
Definition: container_utils.h:29
const bool orderless_contains(InputIt first_haystack, InputIt last_haystack, InputIt first_needle, InputIt last_needle)
Definition: container_utils.h:67