cheshirekow  v0.1.0
set_operations.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2014 Josh Bialkowski (jbialk@mit.edu)
3  *
4  * This file is part of mpblocks.
5  *
6  * mpblocks is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * mpblocks is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with mpblocks. If not, see <http://www.gnu.org/licenses/>.
18  */
26 #ifndef MPBLOCKS_UTIL_SET_OPERATIONS_HPP_
27 #define MPBLOCKS_UTIL_SET_OPERATIONS_HPP_
28 
29 #include <algorithm>
30 
31 namespace set {
32 
34 template <typename InputIt1, typename InputIt2>
35 bool Contains(InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2);
36 
39 template <typename InputIt1, typename InputIt2, typename OutputIt1,
40  typename OutputIt2>
41 void SymmetricDifference(InputIt1 first1, InputIt1 last1, InputIt2 first2,
42  InputIt2 last2, OutputIt1 out1, OutputIt2 out2);
43 
48 template <typename InputIt1, typename InputIt2, typename OutputIt1,
49  typename OutputIt2, typename OutputIt3>
50 void IntersectionAndDifference(InputIt1 first1, InputIt1 last1, InputIt2 first2,
51  InputIt2 last2, OutputIt1 out1, OutputIt2 out2,
52  OutputIt3 intersect);
53 
54 } // namespace std
55 
56 
57 // ----------------------------------------------------------------------------
58 // Implementation
59 // ----------------------------------------------------------------------------
60 
61 namespace set {
62 
64 template <typename InputIt1, typename InputIt2>
65 bool Contains(InputIt1 first1, InputIt1 last1, InputIt2 first2,
66  InputIt2 last2) {
67  while (first2 != last2) {
68  for (; *first1 < *first2; ++first1) {
69  if (first1 == last1) {
70  return false;
71  }
72  }
73  if (*first2 < *first1) {
74  return false;
75  } else {
76  ++first2;
77  ++first1;
78  }
79  }
80  return true;
81 }
82 
83 template <typename InputIt1, typename InputIt2, typename OutputIt1,
84  typename OutputIt2>
85 void SymmetricDifference(InputIt1 first1, InputIt1 last1, InputIt2 first2,
86  InputIt2 last2, OutputIt1 out1, OutputIt2 out2) {
87  while (first1 != last1) {
88  if (first2 == last2) {
89  std::copy(first1, last1, out1);
90  return;
91  }
92  if (*first1 < *first2) {
93  *out1++ = *first1++;
94  } else {
95  if (*first2 < *first1) {
96  *out2++ = *first2;
97  } else {
98  ++first1;
99  }
100  ++first2;
101  }
102  }
103  std::copy(first2, last2, out2);
104  return;
105 }
106 
107 template <typename InputIt1, typename InputIt2, typename OutputIt1,
108  typename OutputIt2, typename OutputIt3>
109 void IntersectionAndDifference(InputIt1 first1, InputIt1 last1, InputIt2 first2,
110  InputIt2 last2, OutputIt1 out1, OutputIt2 out2,
111  OutputIt3 intersect) {
112  while (first1 != last1) {
113  if (first2 == last2) {
114  std::copy(first1, last1, out1);
115  return;
116  }
117  // *first1 is in the first set, but not the second set
118  if (*first1 < *first2) {
119  *out1++ = *first1++;
120  } else {
121  // *first2 is in the second set, but not the first
122  if (*first2 < *first1) {
123  *out2++ = *first2++;
124  // *first1 == *first2 and it is in both sets
125  } else {
126  *intersect++ = *first1++;
127  ++first2;
128  }
129  }
130  }
131  std::copy(first2, last2, out2);
132  return;
133 }
134 
135 } // namespace std
136 
137 #endif // MPBLOCKS_UTIL_SET_OPERATIONS_HPP_
void IntersectionAndDifference(InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2, OutputIt1 out1, OutputIt2 out2, OutputIt3 intersect)
Collect in out1 elements from the first set not found in the second, and collect in out2 elements fro...
void SymmetricDifference(InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2, OutputIt1 out1, OutputIt2 out2)
Collect in out1 elements from the first set not found in the second, and collect in out2 elements fro...
bool Contains(InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2)
Returns true if the seconds set is a subset of the first set.
__host__ __device__ Scalar & set(LValue< Scalar, ROWS, COLS, Exp > &M)
Definition: Access.h:60
Char8_t * copy(const Char8_t *s)