-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpacked_array.inl
More file actions
254 lines (215 loc) · 6.52 KB
/
packed_array.inl
File metadata and controls
254 lines (215 loc) · 6.52 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
251
252
253
254
#pragma once
#include "packed_array.h"
#include <stdexcept>
#include <tuple>
#include <format>
#include <algorithm>
template <typename T>
requires std::is_unsigned_v<T>
class PackedArray<T>::Access
{
PackedArray<T> &data_;
const std::size_t index_;
public:
Access(PackedArray &data, std::size_t index) : data_(data), index_(index) {}
Access &operator=(const T value)
{
data_.set(index_, value);
return *this;
}
operator T() const
{
return data_.get(index_);
}
};
template <typename T>
requires std::is_unsigned_v<T>
class PackedArray<T>::iterator
{
public:
using iterator_category = std::random_access_iterator_tag;
using difference_type = std::ptrdiff_t;
using value_type = T;
using reference = Access;
iterator(PackedArray<T> *array, const std::size_t index)
: array_(array), index_(index) {}
reference operator*() const { return (*array_)[index_]; }
iterator &operator++()
{
++index_;
return *this;
}
iterator operator++(int)
{
iterator tmp(*this);
operator++();
return tmp;
}
iterator &operator--()
{
--index_;
return *this;
}
iterator operator--(int)
{
iterator tmp(*this);
operator--();
return tmp;
}
iterator &operator+=(difference_type n)
{
index_ += n;
return *this;
}
iterator &operator-=(difference_type n)
{
index_ -= n;
return *this;
}
friend iterator operator+(iterator it, difference_type n) { return it += n; }
friend iterator operator+(difference_type n, iterator it) { return it += n; }
friend iterator operator-(iterator it, difference_type n) { return it -= n; }
friend difference_type operator-(iterator lhs, iterator rhs) { return lhs.index_ - rhs.index_; }
bool operator==(const iterator &other) const { return array_ == other.array_ && index_ == other.index_; }
bool operator<(const iterator &other) const { return index_ < other.index_; }
private:
PackedArray<T> *array_;
std::size_t index_;
};
template <typename T>
requires std::is_unsigned_v<T>
class PackedArray<T>::const_iterator
{
public:
using iterator_category = std::random_access_iterator_tag;
using difference_type = std::ptrdiff_t;
using value_type = T;
using reference = T;
const_iterator(const PackedArray<T> *array, const std::size_t index)
: array_(array), index_(index) {}
reference operator*() const { return (*array_)[index_]; }
const_iterator &operator++()
{
++index_;
return *this;
}
const_iterator operator++(int)
{
const_iterator tmp(*this);
operator++();
return tmp;
}
const_iterator &operator--()
{
--index_;
return *this;
}
const_iterator operator--(int)
{
const_iterator tmp(*this);
operator--();
return tmp;
}
const_iterator &operator+=(difference_type n)
{
index_ += n;
return *this;
}
const_iterator &operator-=(difference_type n)
{
index_ -= n;
return *this;
}
friend const_iterator operator+(const_iterator it, difference_type n) { return it += n; }
friend const_iterator operator+(difference_type n, const_iterator it) { return it += n; }
friend const_iterator operator-(const_iterator it, difference_type n) { return it -= n; }
friend difference_type operator-(const_iterator lhs, const_iterator rhs) { return lhs.index_ - rhs.index_; }
bool operator==(const const_iterator &other) const { return array_ == other.array_ && index_ == other.index_; }
bool operator<(const const_iterator &other) const { return index_ < other.index_; }
private:
const PackedArray<T> *array_;
std::size_t index_;
};
static auto calcIndexInfo(const std::size_t index, const std::size_t element_size, const std::size_t unit_size)
{
return std::tuple{(index * element_size) / unit_size, (index * element_size) % unit_size};
}
std::size_t minimumBitsRequired(auto n)
{
if (n == 0)
{
return 1;
}
std::size_t bits = 0;
while (n > 0)
{
n >>= 1;
bits++;
}
return bits;
}
template <typename T>
requires std::is_unsigned_v<T>
inline void PackedArray<T>::resize(const std::size_t size)
{
size_ = size;
data_.resize((size_ * element_size_ + kUnitSize - 1) / kUnitSize);
}
template <typename T>
requires std::is_unsigned_v<T>
inline void PackedArray<T>::push_back(const T value)
{
resize(size_ + 1);
set(size_ - 1, value);
}
template <typename T>
requires std::is_unsigned_v<T>
inline T PackedArray<T>::get(const std::size_t index) const
{
if (index >= size_)
throw std::out_of_range(std::format("PackedArray: index {} out of range!", index));
const auto [index_in_data, index_in_unit] = calcIndexInfo(index, element_size_, kUnitSize);
// 读取当前unit中的位
T result{static_cast<T>((data_[index_in_data] >> index_in_unit) & mask_)};
if (kUnitSize - index_in_unit < element_size_)
// 读取超出当前unit的位
result |= ((data_[index_in_data + 1] << index_in_unit) & mask_);
return result;
}
template <typename T>
requires std::is_unsigned_v<T>
inline void PackedArray<T>::set(const std::size_t index, const T value)
{
if (index >= size_)
throw std::out_of_range(std::format("PackedArray: index {} out of range!", index));
const auto [index_in_data, index_in_unit] = calcIndexInfo(index, element_size_, kUnitSize);
// 写入当前unit中的位
data_[index_in_data] = data_[index_in_data] & ~(mask_ << index_in_unit) | (value << index_in_unit);
if (kUnitSize - index_in_unit < element_size_)
// 写入超出当前unit的位
data_[index_in_data + 1] = data_[index_in_data + 1] & ~(mask_ >> (kUnitSize - index_in_unit)) | (value >> index_in_unit);
}
template <typename T>
requires std::is_unsigned_v<T>
inline void PackedArray<T>::transform(const std::size_t element_size)
{
if (element_size == element_size_)
return;
PackedArray<T> tmp(size_, element_size);
for (int i = 0; i < size_; ++i)
tmp.set(i, get(i));
*this = std::move(tmp);
}
template <typename T>
requires std::is_unsigned_v<T>
inline void PackedArray<T>::grow()
{
transform(element_size_ + 1);
}
template <typename T>
requires std::is_unsigned_v<T>
inline void PackedArray<T>::fit()
{
const T maximum = *std::max_element(cbegin(), cend());
transform(minimumBitsRequired(maximum));
}