-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpadded_vector.h
58 lines (53 loc) · 1.7 KB
/
padded_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
#ifndef PADDED_VECTOR_H
#define PADDED_VECTOR_H
#include <vector>
#include "util.h"
#include <limits>
template <int record_bytes = 128, int pad = 32> class PaddedVector {
static constexpr int payload_bytes = record_bytes - sizeof(Key);
using Payload = char[payload_bytes];
struct Record {
Key k;
Payload p;
Record() {}
Record(Key k) : k(k) {}
bool operator<(const Record &r) const { return k < r.k; }
operator Key() const { return k; }
};
std::vector<Record> v;
public:
PaddedVector(size_t n) : v(n + 2 * pad) {
std::fill(this->v.begin(), this->v.begin() + pad,
std::numeric_limits<Key>::min());
std::fill(this->v.end() - pad, this->v.end(),
std::numeric_limits<Key>::max());
}
PaddedVector(const std::vector<Key> &v) : v(v.size() + 2 * pad) {
std::copy(v.begin(), v.end(), this->v.begin() + pad);
std::fill(this->v.begin(), this->v.begin() + pad,
std::numeric_limits<Key>::min());
std::fill(this->v.end() - pad, this->v.end(),
std::numeric_limits<Key>::max());
}
Key &operator[](long ix) {
// allow some inaccuracy to reduce needed precision
assert(ix >= -pad);
assert(ix <= size() + pad);
return v[ix + pad].k;
}
const Key &operator[](long ix) const {
// allow some inaccuracy to reduce needed precision
assert(ix >= -pad);
assert(ix <= size() + pad);
return v[ix + pad].k;
}
auto begin() { return v.begin() + pad; }
auto end() {
return v.end() - pad;
};
const Key *cbegin() const { return v.data() + pad; }
size_t size() const { return v.size() - 2 * pad; }
Key back() const { return (*this)[size() - 1]; }
auto get_pad() const { return pad; }
};
#endif