|
#include <thrust/device_vector.h> |
|
#include <thrust/merge.h> |
|
#include <thrust/set_operations.h> |
|
#include <thrust/iterator/discard_iterator.h> |
|
#include <iostream> |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
template <typename String, typename Vector> |
|
void print(const String& s, const Vector& v) |
|
{ |
|
std::cout << s << " ["; |
|
for(size_t i = 0; i < v.size(); i++) |
|
std::cout << " " << v[i]; |
|
std::cout << " ]\n"; |
|
} |
|
|
|
template <typename Vector> |
|
void Merge(const Vector& A, const Vector& B) |
|
{ |
|
|
|
Vector C(A.size() + B.size()); |
|
|
|
thrust::merge(A.begin(), A.end(), B.begin(), B.end(), C.begin()); |
|
|
|
print("Merge(A,B)", C); |
|
} |
|
|
|
template <typename Vector> |
|
void SetUnion(const Vector& A, const Vector& B) |
|
{ |
|
|
|
Vector C(A.size() + B.size()); |
|
|
|
|
|
typename Vector::iterator C_end; |
|
|
|
C_end = thrust::set_union(A.begin(), A.end(), B.begin(), B.end(), C.begin()); |
|
|
|
|
|
C.erase(C_end, C.end()); |
|
|
|
print("Union(A,B)", C); |
|
} |
|
|
|
template <typename Vector> |
|
void SetIntersection(const Vector& A, const Vector& B) |
|
{ |
|
|
|
Vector C(thrust::min(A.size(), B.size())); |
|
|
|
|
|
typename Vector::iterator C_end; |
|
|
|
C_end = thrust::set_intersection(A.begin(), A.end(), B.begin(), B.end(), C.begin()); |
|
|
|
|
|
C.erase(C_end, C.end()); |
|
|
|
print("Intersection(A,B)", C); |
|
} |
|
|
|
template <typename Vector> |
|
void SetDifference(const Vector& A, const Vector& B) |
|
{ |
|
|
|
Vector C(A.size()); |
|
|
|
|
|
typename Vector::iterator C_end; |
|
|
|
C_end = thrust::set_difference(A.begin(), A.end(), B.begin(), B.end(), C.begin()); |
|
|
|
|
|
C.erase(C_end, C.end()); |
|
|
|
print("Difference(A,B)", C); |
|
} |
|
|
|
template <typename Vector> |
|
void SetSymmetricDifference(const Vector& A, const Vector& B) |
|
{ |
|
|
|
Vector C(A.size() + B.size()); |
|
|
|
|
|
typename Vector::iterator C_end; |
|
|
|
C_end = thrust::set_symmetric_difference(A.begin(), A.end(), B.begin(), B.end(), C.begin()); |
|
|
|
|
|
C.erase(C_end, C.end()); |
|
|
|
print("SymmetricDifference(A,B)", C); |
|
} |
|
|
|
template <typename Vector> |
|
void SetIntersectionSize(const Vector& A, const Vector& B) |
|
{ |
|
|
|
thrust::discard_iterator<> C_begin, C_end; |
|
|
|
C_end = thrust::set_intersection(A.begin(), A.end(), B.begin(), B.end(), C_begin); |
|
|
|
std::cout << "SetIntersectionSize(A,B) " << (C_end - C_begin) << std::endl; |
|
} |
|
|
|
|
|
int main(void) |
|
{ |
|
int a[] = {0,2,4,5,6,8,9}; |
|
int b[] = {0,1,2,3,5,7,8}; |
|
|
|
thrust::device_vector<int> A(a, a + sizeof(a) / sizeof(int)); |
|
thrust::device_vector<int> B(b, b + sizeof(b) / sizeof(int)); |
|
|
|
print("Set A", A); |
|
print("Set B", B); |
|
|
|
Merge(A,B); |
|
SetUnion(A,B); |
|
SetIntersection(A,B); |
|
SetDifference(A,B); |
|
SetSymmetricDifference(A,B); |
|
|
|
SetIntersectionSize(A,B); |
|
|
|
return 0; |
|
} |
|
|
|
|