-
Notifications
You must be signed in to change notification settings - Fork 0
/
gset.cpp
91 lines (70 loc) · 2.09 KB
/
gset.cpp
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include "gset.h"
template <typename T>
gset<T>::gset() {}
template <typename T>
bool gset<T>::operator == (const gset<T> & o) const {
return s == o.s;
}
template <typename T>
gset<T> gset<T>::add(const T& val) {
gset<T> res; // delta obj
// Insert value into the state and check if insertion was successful
auto [it, inserted] = s.insert(val);
// If insertion was successful, add the value to the delta object
if (inserted) {
res.s.insert(val);
}
return res;
}
template <typename U>
std::ostream& operator<<(std::ostream& os, const gset<U>& set) {
os << "{ ";
for (const auto& elem : set.s) {
os << elem << " ";
}
os << "}";
return os;
}
template <typename T>
bool gset<T>::in(const T& val) {
return s.count(val) == 1; // Sets have either 0 or 1 occurrences
}
//template <typename T>
//void gset<T>::join(const gset<T>& o) {
// s.insert(o.s.begin(), o.s.end());
template <typename T>
template <typename... GSets>
void gset<T>::join(const GSets&... sets) {
// Fold expression to join all sets
(... , s.insert(sets.s.begin(), sets.s.end()));
}
template <typename T>
void gset<T>::join(const std::vector<gset<T>>& sets) {
for (const auto& set : sets) {
s.insert(set.s.begin(), set.s.end());
}
}
template <typename T>
gset<T> gset<T>::operator+(const gset<T>& other) const {
gset<T> result = *this; // Start with the contents of the current set
result.join(other); // Use the join function to combine with the other set
return result;
}
template <typename T>
gset<T> gset<T>::operator-(const gset<T>& other) const {
gset<T> result = *this; // Start with the contents of the current set
for(const auto& elem : other.s) {
result.s.erase(elem); // Erase the element from the result set if it exists
}
return result;
}
template <typename T>
std::vector<gset<T>> gset<T>::split() const {
std::vector<gset<T>> result;
for (const auto& element : s) {
gset<T> individual;
individual.add(element);
result.push_back(individual);
}
return result;
}