forked from Gregable/pq-trees
-
Notifications
You must be signed in to change notification settings - Fork 0
/
set_methods.h
58 lines (51 loc) · 2.05 KB
/
set_methods.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// Methods that define commonly used operations on stl sets.
// This file is part of the PQ Tree library.
//
// The PQ Tree library is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by the
// Free Software Foundation, either version 3 of the License, or (at your
// option) any later version.
//
// The PQ Tree Library is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
// or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
// for more details.
//
// You should have received a copy of the GNU General Public License along
// with the PQ Tree Library. If not, see <http://www.gnu.org/licenses/>.
#include <algorithm>
#include <set>
using namespace std;
class SetMethods {
public:
// Computes the set intersection between sets |A| and |B|.
template <class T> static set<T> SetIntersection(const set<T>& A,
const set<T>& B) {
set<T> out;
set_intersection(A.begin(), A.end(), B.begin(), B.end(),
inserter(out, out.begin()));
return out;
}
// Computes the set union between sets |A| and |B|
template <class T> static set<T> SetUnion(const set<T>& A, const set<T>& B) {
set<T> out;
set_union(A.begin(), A.end(), B.begin(), B.end(),
inserter(out, out.begin()));
return out;
}
// Searches for |needle| in |haystack|.
template <class T> static bool SetFind(const set<T>& haystack,
const T& needle) {
return haystack.count(needle) > 0;
}
// Computes the set difference between sets |pos| and |neg|.
// Args:
// pos: set from which we subtract elements from neg.
// neg: set whose elements we subtract from pos.
template <class T> static set<T> SetDifference(set<T> pos, set<T> neg) {
set<T> out;
set_difference(pos.begin(), pos.end(), neg.begin(), neg.end(),
inserter(out, out.begin()));
return out;
}
};