|
27 | 27 | // 64 * 2^16. 2^17 values, each value can store 64 bits. |
28 | 28 | #define kMaxSizeBits 8388608 |
29 | 29 |
|
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 | + } |
37 | 39 |
|
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 | + } |
39 | 44 |
|
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 | + } |
41 | 49 |
|
42 | | - SmallVector& operator=(const SmallVector& other) |
43 | | - { |
| 50 | + InlineVector& operator=(const InlineVector& other) & { |
44 | 51 | count_ = other.count_; |
45 | 52 | for (size_type i = 0; i < other.count_; i++) v_[i] = other.v_[i]; |
46 | 53 | return (*this); |
47 | 54 | } |
48 | 55 |
|
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); |
64 | 58 |
|
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]; |
75 | 61 | return (*this); |
76 | 62 | } |
77 | 63 |
|
78 | 64 | size_type size() const noexcept { return count_; } |
79 | 65 |
|
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; |
83 | 76 | }; |
84 | 77 |
|
| 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 | + |
85 | 85 | /* |
86 | 86 | * This class represents an array of bits. These are stored in an |
87 | 87 | * array of integers, allowing for efficient bit manipulations. The Bits class provides |
|
0 commit comments