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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
|
#ifndef IDSET_HPP
#define IDSET_HPP
#include <ostream>
#define CELL_TYPE char
#define CELL_SIZE (8 * sizeof(CELL_TYPE))
#define DIV_ROUND_UP(NUM, DEM) ((NUM + DEM - 1) / DEM)
template<typename T>
class IdSet {
public:
IdSet() : _length(0), _values(NULL) { }
IdSet(unsigned int length)
: _length(length),
_values(new CELL_TYPE[DIV_ROUND_UP(length, CELL_SIZE)]) {
for (unsigned int i = 0; i < DIV_ROUND_UP(_length, CELL_SIZE); ++i) {
_values[i] = 0;
}
}
virtual ~IdSet() {
if (_values != NULL)
delete[] _values;
}
IdSet(const IdSet& other) {
_length = other._length;
_values = new CELL_TYPE[DIV_ROUND_UP(_length, CELL_SIZE)];
for (unsigned int i = 0; i < DIV_ROUND_UP(_length, CELL_SIZE); ++i) {
_values[i] = other._values[i];
}
}
IdSet& operator=(const IdSet& other) {
if (_length != other._length) {
if (_values != NULL)
delete[] _values;
_length = other._length;
_values = new CELL_TYPE[DIV_ROUND_UP(_length, CELL_SIZE)];
}
for (unsigned int i = 0; i < DIV_ROUND_UP(_length, CELL_SIZE); ++i) {
_values[i] = other._values[i];
}
return *this;
}
bool contains(const T& obj) const {
unsigned int id = obj.id();
if (id >= _length) {
throw "Array out of bounds;";
}
unsigned int cell = id / CELL_SIZE;
unsigned int bit = id % CELL_SIZE;
return (_values[cell] & (1 << bit)) != 0;
}
void insert(const T& obj) {
unsigned int id = obj.id();
if (id >= _length) {
throw "Array out of bounds;";
}
unsigned int cell = id / CELL_SIZE;
unsigned int bit = id % CELL_SIZE;
_values[cell] |= (1 << bit);
}
void remove(const T& obj) {
unsigned int id = obj.id();
if (id >= _length) {
throw "Array out of bounds;";
}
unsigned int cell = id / CELL_SIZE;
unsigned int bit = id % CELL_SIZE;
if (this->contains(obj)) {
std::cout << obj.id() << " -> ";
} else {
std::cout << "null" << " -> ";
}
_values[cell] &= ~(1 << bit);
if (this->contains(obj)) {
std::cout << "still here" << std::endl;
} else {
std::cout << "Gone!" << std::endl;
}
}
void clear() {
for (unsigned int i = 0; i < DIV_ROUND_UP(_length, CELL_SIZE); ++i) {
_values[i] = 0;
}
}
void absorb(const IdSet& other) {
if (_length == other._length) {
for (unsigned int i = 0; i < _length; ++i) {
_values[i] |= other._values[i];
}
} else {
throw "Sets of different sizes cannot be combined.";
}
}
void filter(const IdSet& other) {
if (_length == other._length) {
for (unsigned int i = 0; i < DIV_ROUND_UP(_length, CELL_SIZE); ++i) {
_values[i] &= ~(other._values[i]);
}
} else {
throw "Sets of different sizes cannot be filtered.";
}
}
bool operator==(const IdSet& other) const {
if (_length != other._length)
return false;
for (unsigned int i = 0; i < DIV_ROUND_UP(_length, CELL_SIZE); ++i) {
if (_values[i] != other._values[i])
return false;
}
return true;
}
bool operator!=(const IdSet& other) const {
return !(*this == other);
}
struct iterator {
iterator(const IdSet& set, unsigned int index)
: set(set), index(index) {
unsigned int cell, bit;
cell = index / CELL_SIZE;
bit = index % CELL_SIZE;
while (index < set._length &&
(set._values[cell] & (1 << bit)) == 0) {
index++;
cell = index / CELL_SIZE;
bit = index % CELL_SIZE;
}
}
unsigned int operator*() const {
return index;
}
iterator& operator++() {
unsigned int cell, bit;
do {
index++;
if (index == set._length) {
return *this;
}
cell = index / CELL_SIZE;
bit = index % CELL_SIZE;
} while ((set._values[cell] & (1 << bit)) == 0);
return *this;
}
bool operator==(const iterator& other) {
return other.index == index;
}
bool operator!=(const iterator& other) {
return !(*this == other);
}
private:
const IdSet& set;
unsigned int index;
};
iterator begin() const {
return iterator(*this, 0);
}
iterator end() const {
return iterator(*this, _length);
}
private:
unsigned int _length;
CELL_TYPE* _values;
};
template<typename T>
std::ostream& operator<<(std::ostream& cout, const IdSet<T>& set) {
cout << "{";
for (typename IdSet<T>::iterator it = set.begin();
it != set.end();
++it) {
cout << *it << ", ";
}
cout << "}";
return cout;
}
#endif
|