-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfast_erase_vector.h
65 lines (64 loc) · 1.31 KB
/
fast_erase_vector.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
#pragma once
#include <vector>
using std::vector;
// A random access container with fast add and remove operations.
template <class O> class FastEraseVector {
vector<O> elements;
vector<int> holes;
public:
explicit FastEraseVector(int size) {
elements.reserve(size);
holes.reserve(size >> 4);
}
void init(vector<O>* elems, vector<int>* holesVect) {
holes.swap(*holesVect);
elements.swap(*elems);
}
// Can't check if there is access violation.
const O& operator[](int num) const {
return elements[num];
}
// Can't check if there is access violation.
O& operator[](int num) {
return elements[num];
}
int add(const O& elem) {
if (holes.empty()) {
elements.push_back(elem);
return elements.size() - 1;
} else {
int hole = holes.back();
holes.pop_back();
elements[hole] = elem;
return hole;
}
}
void remove(int num) {
holes.push_back(num);
}
// Slow.
bool isHole(int node) const {
for (int i = 0; i < (int)holes.size(); ++i) {
if (holes[i] == node) {
return true;
}
}
return false;
}
vector<int> getHoles() const {
return holes;
}
void clear() {
elements.clear();
holes.clear();
}
int size() const {
return elements.size() - holes.size();
}
int maxNumber() const {
return elements.size();
}
int memory() const {
return elements.size() + holes.size();
}
};