-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontiguousMap.h
239 lines (193 loc) · 4.4 KB
/
contiguousMap.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
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
#pragma once
#include <utility>
namespace ecss {
template<typename Key, typename Value>
class ContiguousMap {
public:
friend bool operator==(const ContiguousMap& lhs, const ContiguousMap& rhs) {
return lhs.mSize == rhs.mSize && lhs.mCapacity == rhs.mCapacity && lhs.mData == rhs.mData;
}
friend bool operator!=(const ContiguousMap& lhs, const ContiguousMap& rhs) {
return !(lhs == rhs);
}
ContiguousMap(const ContiguousMap& other)
: mSize(other.mSize),
mCapacity(other.mCapacity) {
mData = new std::pair<Key, Value>[mCapacity];
for (auto i = 0; i < mSize; i++) {
mData[i] = other.mData[i];
}
}
ContiguousMap(ContiguousMap&& other) noexcept
: mSize(other.mSize),
mCapacity(other.mCapacity),
mData(other.mData) {
other.mData = nullptr;
}
ContiguousMap& operator=(const ContiguousMap& other) {
if (this == &other)
return *this;
mSize = other.mSize;
mCapacity = other.mCapacity;
mData = new std::pair<Key, Value>[mCapacity];
for (auto i = 0; i < mSize; i++) {
mData[i] = other.mData[i];
}
return *this;
}
ContiguousMap& operator=(ContiguousMap&& other) noexcept {
if (this == &other)
return *this;
mSize = other.mSize;
mCapacity = other.mCapacity;
mData = other.mData;
other.mData = nullptr;
return *this;
}
ContiguousMap() = default;
~ContiguousMap() {
delete[] mData;
}
Value& operator[](Key key) {
size_t idx = 0;
const auto pair = search(key, idx);
if (pair) {
return pair->second;
}
if (mCapacity <= mSize) {
setCapacity(mCapacity ? mCapacity * 2 : 1);
}
mSize++;
shiftDataRight(idx);
mData[idx].first = key;
return mData[idx].second;
}
Value& insert(Key key, Value value) {
size_t idx = 0;
const auto pair = search(key, idx);
if (pair) {
pair->second = std::move(value);
return pair->second;
}
if (mCapacity <= mSize) {
setCapacity(mCapacity ? mCapacity * 2 : 1);
}
mSize++;
shiftDataRight(idx);
mData[idx].first = key;
mData[idx].second = std::move(value);
return mData[idx].second;
}
const std::pair<Key,Value>& find(Key key) {
size_t idx = 0;
if (auto pair = search(key, idx)) {
return *pair;
}
return {};
}
bool contains(Key key) const {
size_t idx = 0;
if (search(key, idx)) {
return true;
}
return false;
}
Value at(Key key) const {
size_t idx = 0;
if (auto pair = search(key, idx)) {
return pair->second;
}
return {};
}
class Iterator {
public:
std::pair<Key, Value>* ptr;
Iterator(std::pair<Key, Value>* ptr) : ptr(ptr) {}
std::pair<Key, Value>& operator*() const {
return *ptr;
}
bool operator!=(Iterator& other) const {
return ptr != other.ptr;
}
Iterator& operator++() {
return ++ptr, *this;
}
};
Iterator begin() const {
return { mData };
}
Iterator end() const {
return { mData + mSize };
}
void shrinkToFit() {
setCapacity(mSize);
}
private:
void shiftDataRight(size_t begin) {
if (!mData) {
return;
}
for(auto i = mSize - 1; i > begin; i--) {
mData[i] = std::move(mData[i - 1]);
}
}
void setCapacity(size_t capacity) {
if (capacity == mCapacity) {
return;
}
if (mCapacity == 0) {
mCapacity = capacity;
mData = new std::pair<Key, Value>[mCapacity];
}
else {
mCapacity = capacity;
auto newData = new std::pair<Key, Value>[mCapacity];
for (auto i = 0u; i < mSize; i++) {
newData[i] = std::move(mData[i]);
}
delete[] mData;
mData = newData;
}
}
std::pair<Key, Value>* search(Key key, size_t& idx) const {
auto right = mSize;
if (right == 0 || mData[0].first > key) {
idx = 0;
return nullptr;
}
if (mData[right - 1].first < key) {
idx = right;
return nullptr;
}
size_t left = 0u;
std::pair<Key, Value>* result = nullptr;
while (true) {
if (mData[left].first == key) {
idx = left;
result = &mData[left];
break;
}
const auto dist = right - left;
if (dist == 1) {
idx = left + 1;
break;
}
const auto mid = left + dist / 2;
if (mData[mid].first > key) {
right = mid;
continue;
}
if (mData[mid].first == key) {
idx = mid;
result = &mData[mid];
break;
}
left = mid;
}
return result;
}
size_t mSize = 0;
size_t mCapacity = 0;
std::pair<Key,Value>* mData = nullptr;
};
}