-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathintset.h
107 lines (90 loc) · 1.96 KB
/
intset.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
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#ifndef PAR_DPLL_INTSET_H
#define PAR_DPLL_INTSET_H
#include <vector>
#include <cstdlib>
#include <functional>
using namespace std;
typedef struct IntSet {
vector<bool> vec;
int capacity;
int s;
IntSet() {
capacity = 0;
s = 0;
}
void resize(int n) {
vec.resize(n);
}
void reserve(int n) {
vec.reserve(n);
}
void insert(int v) {
if (v + 1 > capacity) {
vec.resize(v + 1);
capacity = v + 1;
}
if (!vec[v]) {
s++;
}
vec[v] = true;
}
void erase(int v) {
if (v >= capacity) {
return;
}
if (vec[v]) {
s--;
}
vec[v] = false;
}
[[nodiscard]] bool contains(int v) const {
if (v >= capacity) {
return false;
}
return vec[v];
}
[[nodiscard]] int size() const {
return s;
}
[[nodiscard]] bool empty() const {
return s <= 0;
}
void clear() {
for (auto i = 0; i < capacity; i++) {
vec[i] = false;
}
s = 0;
}
[[nodiscard]] int first() const {
for (auto i = 0; i < capacity; i++) {
if (vec[i]) {
return i;
}
}
throw invalid_argument("trying to get first of empty IntSet!");
}
void iterate(function<void(int)> const& func) const {
int b[s];
auto bi = 0;
for (auto i = 0; i < capacity; i++) {
if (vec[i]) {
b[bi++] = i;
}
}
auto s1 = s;
for (auto i = 0; i < s1; i++) {
func(b[i]);
}
}
[[nodiscard]] vector<int>& bag() const {
auto b = new vector<int>(s);
auto bi = 0;
for (auto i = 0; i < capacity; i++) {
if (vec[i]) {
(*b)[bi++] = i;
}
}
return *b;
}
} IntSet;
#endif //PAR_DPLL_INTSET_H