GF2::Vector — Alter the Capacity
Potentially change the capacity of a bit-vector.
constexpr Vector &reserve(std::size_t n); (1)
constexpr Vector &shrink_to_fit(); (2)
| 1 | Increases the bit-vector’s capacity so it can hold n elements.
Does nothing if n fits inside the current capacity. |
| 2 | This is a request to minimize the unused/excess capacity. May do nothing. |
The idea is to make it as efficient as possible to append a (known) large number of elements to a bit-vector by allocating the needed storage up-front rather than in pieces.
These methods both return *this so you can chain them with other calls.
These methods do not change the size() of a bit-vector.
No elements are added or deleted.
| If the capacity does change, all the old values are unaltered, however, references will be invalidated. |
Example
#include <GF2/GF2.h>
int main()
{
GF2::Vector<> v("10101010");
std::cout << v << ": size " << v.size() << ", capacity " << v.capacity() << '\n'; (1)
v.reserve(99); (2)
std::cout << v << ": size " << v.size() << ", capacity " << v.capacity() << '\n'; (3)
v.shrink_to_fit(); (4)
std::cout << v << ": size " << v.size() << ", capacity " << v.capacity() << '\n';
}
| 1 | We’re using the default 64-bit blocks so v can hold 64 elements (though it only has 8 at present). |
| 2 | Get v "ready" to hold a total of 99 elements. |
| 3 | Given the blocks are all 64-bits long we need 2 of them for those 99 elements so the capacity actually increases to 128. |
| 4 | Changed our mind and just want to shrink v to minimum size. Note that the elements in v never changed! |
Output
10101010: size 8, capacity 64
10101010: size 8, capacity 128
10101010: size 8, capacity 64
See Also