Skip to content

Commit c71e3eb

Browse files
committed
Common template for inline vector implementation.
Allows simplier using inline vectors of different sizes and types. Signed-off-by: Ostap Slavking <[email protected]> If you like my commits cheer me with some chia here: xch16es77qggpwgzucqmmvhvgcueckmt9g3e7exa46vhmucz7z8j2a2spm7c7e
1 parent 632c9d7 commit c71e3eb

File tree

2 files changed

+71
-39
lines changed

2 files changed

+71
-39
lines changed

src/bits.hpp

Lines changed: 39 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -27,61 +27,61 @@
2727
// 64 * 2^16. 2^17 values, each value can store 64 bits.
2828
#define kMaxSizeBits 8388608
2929

30-
// A stack vector of length 5, having the functions of std::vector needed for Bits.
31-
struct SmallVector {
32-
typedef uint16_t size_type;
33-
34-
SmallVector() noexcept { count_ = 0; }
35-
36-
uint64_t& operator[](const uint16_t index) { return v_[index]; }
30+
template <class item_type, class size_type_arg, unsigned capacity>
31+
class InlineVector {
32+
public:
33+
typedef size_type_arg size_type;
34+
35+
item_type& operator[](const size_type index) {
36+
assert(index < capacity);
37+
return v_[index];
38+
}
3739

38-
uint64_t operator[](const uint16_t index) const { return v_[index]; }
40+
item_type operator[](const size_type index) const {
41+
assert(index < capacity);
42+
return v_[index];
43+
}
3944

40-
void push_back(uint64_t value) { v_[count_++] = value; }
45+
void push_back(item_type value) {
46+
assert(count_ < capacity);
47+
v_[count_++] = value;
48+
}
4149

42-
SmallVector& operator=(const SmallVector& other)
43-
{
50+
InlineVector& operator=(const InlineVector& other) & {
4451
count_ = other.count_;
4552
for (size_type i = 0; i < other.count_; i++) v_[i] = other.v_[i];
4653
return (*this);
4754
}
4855

49-
size_type size() const noexcept { return count_; }
50-
51-
void resize(const size_type n) { count_ = n; }
52-
53-
private:
54-
uint64_t v_[10];
55-
size_type count_;
56-
};
57-
58-
// A stack vector of length 1024, having the functions of std::vector needed for Bits.
59-
// The max number of Bits that can be stored is 1024 * 64
60-
struct ParkVector {
61-
typedef uint32_t size_type;
62-
63-
ParkVector() noexcept { count_ = 0; }
56+
InlineVector& operator=(const std::vector<item_type>& other) & {
57+
assert(other.size() <= capacity);
6458

65-
uint64_t& operator[](const uint32_t index) { return v_[index]; }
66-
67-
uint64_t operator[](const uint32_t index) const { return v_[index]; }
68-
69-
void push_back(uint64_t value) { v_[count_++] = value; }
70-
71-
ParkVector& operator=(const ParkVector& other)
72-
{
73-
count_ = other.count_;
74-
for (size_type i = 0; i < other.count_; i++) v_[i] = other.v_[i];
59+
count_ = other.size();
60+
for (size_type i = 0; i < static_cast<size_type>(other.size()); i++) v_[i] = other[i];
7561
return (*this);
7662
}
7763

7864
size_type size() const noexcept { return count_; }
7965

80-
private:
81-
uint64_t v_[2048];
82-
size_type count_;
66+
void resize(const size_type n) {
67+
assert(n <= capacity);
68+
count_ = n;
69+
}
70+
71+
size_type max_size() const { return capacity; }
72+
73+
private:
74+
item_type v_[capacity];
75+
size_type count_ = 0;
8376
};
8477

78+
// A stack vector of length 10, having the functions of std::vector needed for Bits.
79+
using SmallVector = InlineVector<uint64_t, uint8_t, 10>;
80+
81+
// A stack vector of length 2048, having the functions of std::vector needed for Bits.
82+
// The max number of Bits that can be stored is 2048 * 64
83+
using ParkVector = InlineVector<uint64_t, uint16_t, 2048>;
84+
8585
/*
8686
* This class represents an array of bits. These are stored in an
8787
* array of integers, allowing for efficient bit manipulations. The Bits class provides

tests/test.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,38 @@ TEST_CASE("Util")
169169

170170
TEST_CASE("Bits")
171171
{
172+
SECTION("Inline Vector")
173+
{
174+
const size_t test_size = 16384;
175+
InlineVector<uint64_t, uint16_t, test_size> test_vector;
176+
for(size_t i = 0; i < test_vector.max_size(); i++)
177+
test_vector.push_back(i & (64 - 1));
178+
179+
REQUIRE(test_vector.size() == test_size);
180+
for(size_t i = 0; i < test_vector.size(); i++)
181+
REQUIRE(test_vector[static_cast<uint16_t>(i)] == (i & (64 - 1)));
182+
183+
std::vector<uint64_t> std_vector;
184+
for(size_t i = 0; i < test_size / 2; i++)
185+
std_vector.push_back(test_size - i);
186+
187+
test_vector = std_vector;
188+
REQUIRE(test_vector.size() == std_vector.size());
189+
190+
for(size_t i = 0; i < test_vector.size(); i++)
191+
REQUIRE(test_vector[static_cast<uint16_t>(i)] == (test_size - i));
192+
193+
InlineVector<uint64_t, uint16_t, test_size> other_vector;
194+
for(size_t i = 0; i < test_size / 2 + 256; i++)
195+
other_vector.push_back(test_size + i);
196+
197+
test_vector = other_vector;
198+
REQUIRE(test_vector.size() == other_vector.size());
199+
200+
for(size_t i = 0; i < test_vector.size(); i++)
201+
REQUIRE(test_vector[static_cast<uint16_t>(i)] == (test_size + i));
202+
}
203+
172204
SECTION("Slicing and manipulating")
173205
{
174206
Bits g = Bits(13271, 15);

0 commit comments

Comments
 (0)