-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVectorMap.h
More file actions
250 lines (230 loc) · 8.39 KB
/
Copy pathVectorMap.h
File metadata and controls
250 lines (230 loc) · 8.39 KB
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
240
241
242
243
244
245
246
247
248
249
250
/*
Copyright [2024] [Yao Yao]
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#pragma once
#include <vector>
#include <stdexcept>
#include "cpp_utils.h"
//@fixme: use deque and add offset for cases where keys does not start from 0
template <typename Key_, typename Value_>
class VectorMap
{
public:
using Key = Key_;
using Value = Value_;
using Index = std::conditional_t<std::is_enum_v<Key>, UnderlyingType<Key>, Key>;
static Index key2Index(Key key){
const Index idx = static_cast<Index>(key);
if constexpr (std::is_signed_v<Index>) {
REQUIRE(idx >= 0);
}
return idx;
}
static_assert(std::is_integral_v<Index>);
using key_type = Key;
using value_type = Value;
template <bool isConst>
class IteratorImpl
{
public:
using Container = std::conditional_t<isConst, const VectorMap<Key, Value>, VectorMap<Key, Value>>;
IteratorImpl(Container* container, Index key): mContainer{container}, mStorageIndex{key}{
if constexpr (std::is_signed_v<Index>) {
REQUIRE(key >= 0);
}
}
IteratorImpl<isConst>& operator++(){ // prefix
if (static_cast<size_t>(mStorageIndex) >= mContainer->getStorageSize()){
return *this;
}
const auto findBeg = mContainer->mMask.begin() + mStorageIndex + 1;
const auto iterNext = std::find(findBeg, mContainer->mMask.end(), true);
mStorageIndex = iterNext - mContainer->mMask.begin();
return *this;
}
IteratorImpl<isConst> operator++(int){ // postfix
IteratorImpl<isConst> ret = *this;
++(*this);
return ret;
}
bool operator==(const IteratorImpl<isConst>& other) const {
return mContainer == other.mContainer && key() == other.key();
}
bool operator!=(const IteratorImpl<isConst>& other) const {
return !(*this == other);
}
std::conditional_t<isConst, const Value&, Value&> operator*() const{
assert(mContainer->mMask.at(mStorageIndex));
return mContainer->at(key());
}
Key key() const {return static_cast<Key>(mStorageIndex);}
friend class VectorMap<Key, Value>;
private:
Container* mContainer;
Index mStorageIndex; // the key
};
using iterator = IteratorImpl<false>;
using const_iterator = IteratorImpl<true>;
VectorMap() = default;
VectorMap(const VectorMap<Key, Value>&) = delete;
VectorMap<Key, Value>& operator=(const VectorMap<Key, Value>&) = delete;
void swap(VectorMap& other){
std::swap(mData, other.mData);
std::swap(mMask, other.mMask);
std::swap(mSize, other.mSize);
std::swap(mStorageSize, other.mStorageSize);
}
VectorMap(VectorMap<Key, Value>&& other) {
this->swap(other);
}
VectorMap<Key, Value>& operator=(VectorMap<Key, Value>&& other){
this->swap(other);
other.clear();
return *this;
}
~VectorMap(){
clear();
}
const Value& at(Key key) const {
const Index idx = key2Index(key);
if (idx >= static_cast<Index>(getStorageSize()) || !mMask.at(idx)){
throw std::out_of_range(FILELINE);
}
assert(mMask.size() == mData.size() && getStorageSize() == mData.size());
return reinterpret_cast<const Value&>(mData[idx]);
}
Value& at(Key key) {
return const_cast<Value&>(static_cast<const VectorMap<Key, Value>*>(this)->at(key));
}
Key getKey(const Value& v) const {
if (empty()) {
throw std::out_of_range("empty container");
}
const auto idx = &v - &reinterpret_cast<const Value&>(mData[0]);
if (idx < 0 || static_cast<size_t>(idx) >= getStorageSize() || !mMask.at(idx)) {
throw std::out_of_range("No such item in the container");
}
return static_cast<Key>(static_cast<Index>(idx));
}
template <typename... Args>
std::pair<iterator, bool> try_emplace(Key key, Args&&... args) {
const size_t idx = static_cast<size_t>(key2Index(key));
if (idx < getStorageSize() && mMask.at(idx)){
return std::make_pair(iterator{this, static_cast<Index>(idx)}, false);
}
if (idx >= getStorageSize()){
if (idx < mData.capacity()){
mData.insert(mData.end(), idx + 1 - getStorageSize(), StorageType{});
}
else {
std::vector<StorageType> newData;
newData.reserve(mData.capacity() * 2);
newData.resize(idx + 1);
for (Index i = 0; i < static_cast<Index>(getStorageSize()); i++){
if (mMask[i]){
new(&newData[i]) Value{std::move(at(static_cast<Key>(i)))};
}
}
mData = std::move(newData);
}
assert(mStorageSize == mMask.size());
mMask.insert(mMask.end(), idx + 1 - mMask.size(), false);
mStorageSize = idx + 1;
assert(mData.size() == idx + 1);
}
new (&mData.at(idx)) Value{std::forward<Args>(args)...};
mMask.at(idx) = true;
mSize++;
return std::pair(iterator{this, static_cast<Index>(idx)}, true);
}
Value& operator[](Key key){
try_emplace(key);
return at(key);
}
size_t size() const {return mSize;}
bool empty() const {return size() == 0u;}
inline size_t getStorageSize() const {
assert(mData.size() == mMask.size() && mData.size() == mStorageSize);
return mStorageSize;
}
const_iterator begin() const {
const auto iterMask = std::find(mMask.begin(), mMask.end(), true);
return const_iterator{this, static_cast<Index>(iterMask - mMask.begin())};
}
iterator begin() {
return iterator{this, static_cast<const VectorMap<Key, Value>*>(this)->begin().mStorageIndex};
}
const_iterator end() const {
return const_iterator{this, static_cast<Index>(getStorageSize())};
}
iterator end() {
return iterator{this, static_cast<const VectorMap<Key, Value>*>(this)->end().mStorageIndex};
}
size_t erase(Key key) {
const Index idx = key2Index(key);
if (static_cast<size_t>(idx) < getStorageSize() && mMask[idx]) {
at(key).~Value();
mData[idx] = StorageType{};
mMask[idx] = false;
mSize--;
return 1;
}
return 0;
}
void clear() {
if (!std::is_trivially_destructible<Value>::value) {
const auto storageSize = static_cast<Index>(getStorageSize());
for (Index i = 0; i < storageSize; i++){
if (mMask[i]) {
this->at(static_cast<Key>(i)).~Value();
}
}
}
mData.clear();
mMask.clear();
mSize = 0;
mStorageSize = 0;
assert(size() == 0);
}
const_iterator find(const Key& key) const {
const Index idx = key2Index(key);
if (idx >= getStorageSize() || !mMask.at(idx)){
return end();
}
return const_iterator{this, idx};
}
iterator find(const Key& key) {
const Index idx = key2Index(key);
if (idx >= getStorageSize() || !mMask.at(idx)){
return end();
}
return iterator{this, idx};
}
bool has(const Key& key) const {
const Index idx = key2Index(key);
return idx < getStorageSize() && mMask.at(idx);
}
size_t count(const Key& key) const {
return has(key) ? 1UL : 0UL;
}
void reserve(size_t space) {
mData.reserve(space);
mMask.reserve(space);
}
private:
static constexpr int mOffset = 0;
struct alignas(alignof(Value)) StorageType{std_byte data[sizeof(Value)];};
std::vector<StorageType> mData;
std::vector<bool> mMask;
size_t mSize{0};
size_t mStorageSize{0};
};