minor libutils improvements

This commit is contained in:
Mathias Agopian
2024-05-14 13:22:38 -07:00
committed by Mathias Agopian
parent 499939ed3c
commit c93aa4c90d
2 changed files with 21 additions and 1 deletions
@@ -299,6 +299,16 @@ public:
}
}
UTILS_NOINLINE
void shrink_to_fit() {
if (size() < capacity()) {
FixedCapacityVector t(construct_with_capacity, size(), allocator());
t.mSize = size();
std::uninitialized_move(begin(), end(), t.begin());
this->swap(t);
}
}
private:
enum construct_with_capacity_tag{ construct_with_capacity };
+11 -1
View File
@@ -60,6 +60,11 @@ public:
std::fill(std::begin(storage), std::end(storage), 0);
}
template<typename U, typename = typename std::enable_if_t<N == 1, U>>
explicit bitset(U value) noexcept {
storage[0] = value;
}
T getBitsAt(size_t n) const noexcept {
assert_invariant(n<N);
return storage[n];
@@ -94,6 +99,8 @@ public:
size_t size() const noexcept { return N * BITS_PER_WORD; }
bool empty() const noexcept { return none(); }
bool test(size_t bit) const noexcept { return operator[](bit); }
void set(size_t b) noexcept {
@@ -117,11 +124,14 @@ public:
storage[b / BITS_PER_WORD] ^= T(1) << (b % BITS_PER_WORD);
}
void reset() noexcept {
std::fill(std::begin(storage), std::end(storage), 0);
}
void clear() noexcept {
reset();
}
bool operator[](size_t b) const noexcept {
assert_invariant(b / BITS_PER_WORD < N);
return bool(storage[b / BITS_PER_WORD] & (T(1) << (b % BITS_PER_WORD)));