-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNibbleArray.cpp
More file actions
68 lines (52 loc) · 1.46 KB
/
Copy pathNibbleArray.cpp
File metadata and controls
68 lines (52 loc) · 1.46 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
//
// Created by ASUS on 7/4/2024.
//
#include "NibbleArray.h"
using namespace std;
NibbleArray::NibbleArray(const size_t size, const uint8_t val) :
size(size), arr(size / 2 + 1, val) {
}
uint8_t NibbleArray::get(const size_t pos) const {
size_t i = pos / 2;
assert(pos <= this->size);
uint8_t val = this->arr.at(i);
// Odd pos: last 4 bits
if (pos % 2) {
return val & 0x0F;
}
// Even pos: first 4 bits from the left
else {
return val >> 4;
}
}
void NibbleArray::set(const size_t pos, const uint8_t val) {
size_t i = pos / 2;
uint8_t currVal = this->arr.at(i);
assert(pos <= this->size);
if (pos % 2) {
this->arr.at(i) = (currVal & 0xF0) | (val & 0x0F);
} else {
this->arr.at(i) = (currVal & 0x0F) | (val << 4);
}
}
//Get pointer to underlying array
uint8_t *NibbleArray::data() {
return this->arr.data();
}
const uint8_t *NibbleArray::data() const {
return this->arr.data();
}
size_t NibbleArray::storageSize() const {
return this->arr.size();
}
// Move all the moves to a vector. This doubles the size, but is faster to access,
// since there is no bitwise operation needed.
void NibbleArray::inflate(vector<uint8_t> &dest) const {
dest.reserve(this->size);
for (unsigned i = 0; i < this->size; ++i)
dest.push_back(this->get(i));
}
// Reset the array
void NibbleArray::reset(const uint8_t val) {
fill(this->arr.begin(), this->arr.end(), val);
}