Skip to content

Commit

Permalink
Add function to enlarge basic_vecvec::bucket
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelKutzner committed Oct 8, 2024
1 parent 762ca9a commit 4c87cfe
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
13 changes: 13 additions & 0 deletions include/cista/containers/vecvec.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <cassert>
#include <iterator>
#include <type_traits>
#include <vector>

#include "cista/containers/vector.h"
#include "cista/verify.h"
Expand Down Expand Up @@ -69,6 +70,18 @@ struct basic_vecvec {
}
}

void grow(std::size_t const n, value_type const fill = value_type{}) {
verify(n >= size(), "bucket::grow: new size < old size");
auto const growth = n - size();
auto const elements = std::vector(growth, fill);

map_->data_.insert(std::next(std::begin(map_->data_), bucket_end_idx()),
elements.begin(), elements.end());
for (auto i = i_ + 1; i != map_->bucket_starts_.size(); ++i) {
map_->bucket_starts_[i] += growth;
}
}

value_type& operator[](std::size_t const i) {
assert(is_inside_bucket(i));
return map_->data_[to_idx(map_->bucket_starts_[i_] + i)];
Expand Down
25 changes: 25 additions & 0 deletions test/vecvec_test.cc
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <array>
#include <memory>
#include <set>
#include <stdexcept>

#include "doctest.h"

Expand Down Expand Up @@ -77,6 +78,30 @@ TEST_CASE("vecvec bucket emplace_back test") {
CHECK_EQ("testx", d[key{2}].view());
}

TEST_CASE("vecvec bucket grow test") {
using key = cista::strong<unsigned, struct x_>;
using data = cista::raw::vecvec<key, char>;
using std::literals::operator""sv;

data d;
d.emplace_back("hello");
d.emplace_back("world");

{
d[key{0}].grow(10);

CHECK_EQ("hello\0\0\0\0\0"sv, d[key{0}].view());
CHECK_EQ("world", d[key{1}].view());
}
{
d[key{0}].grow(13, ' ');

CHECK_EQ("hello\0\0\0\0\0 "sv, d[key{0}].view());
CHECK_EQ("world", d[key{1}].view());
}
{ CHECK_THROWS_AS(d[key{0}].grow(5), std::runtime_error); }
}

TEST_CASE("vecvec resize test") {
using key = cista::strong<unsigned, struct x_>;
using data = cista::raw::vecvec<key, char>;
Expand Down

0 comments on commit 4c87cfe

Please sign in to comment.