From eecb4bed2999f773f4a0933a225d4df6af22e466 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felix=20G=C3=BCndling?= Date: Thu, 19 Dec 2024 20:31:02 +0100 Subject: [PATCH] fix issues with paged_vecvec and vecvec --- include/cista/containers/paged.h | 8 ++++++-- include/cista/containers/paged_vecvec.h | 14 +++++++++++--- include/cista/containers/vecvec.h | 9 +++++---- 3 files changed, 22 insertions(+), 9 deletions(-) diff --git a/include/cista/containers/paged.h b/include/cista/containers/paged.h index 0ae393da..2866266d 100644 --- a/include/cista/containers/paged.h +++ b/include/cista/containers/paged.h @@ -97,8 +97,12 @@ struct paged { std::memcpy(&data_[offset], &x, sizeof(T)); } - value_type* data(page_t const& p) { return &data_[p.start_]; } - value_type const* data(page_t const& p) const { return &data_[p.start_]; } + value_type* data(page_t const& p) { + return data_.empty() ? nullptr : &data_[p.start_]; + } + value_type const* data(page_t const& p) const { + return data_.empty() ? nullptr : &data_[p.start_]; + } value_type* begin(page_t const& p) { return data(p); } value_type const* begin(page_t const& p) const { return data(p); } diff --git a/include/cista/containers/paged_vecvec.h b/include/cista/containers/paged_vecvec.h index 53ab9181..4c8a98d0 100644 --- a/include/cista/containers/paged_vecvec.h +++ b/include/cista/containers/paged_vecvec.h @@ -65,7 +65,7 @@ struct paged_vecvec { return (*this)[size() - 1U]; } - reference operator*() const { return *this; } + const_bucket operator*() const { return *this; } size_type size() const { return pv_->page(i_).size_; } bool empty() const { return size() == 0U; } @@ -161,6 +161,14 @@ struct paged_vecvec { (*this)[size() - 1U] = x; } + template + iterator insert(iterator const it, Arg&& el) { + auto const old_offset = std::distance(begin(), it); + auto const old_size = size(); + push_back(data_value_type{el}); + return std::rotate(begin() + old_offset, begin() + old_size, end()); + } + template , typename = std::enable_if_t>> std::string_view view() const { @@ -196,7 +204,7 @@ struct paged_vecvec { return *(begin() + i); } - reference operator*() const { return *this; } + bucket operator*() const { return *this; } operator const_bucket() const { return {pv_, i_}; } @@ -286,7 +294,7 @@ struct paged_vecvec { const_bucket front() const { return at(Key{0}); } const_bucket back() const { return at(Key{size() - 1}); } - size_type size() const { return idx_.size(); } + base_t size() const { return idx_.size(); } bool empty() const { return idx_.empty(); } bucket begin() { return front(); } diff --git a/include/cista/containers/vecvec.h b/include/cista/containers/vecvec.h index ae4bd9ef..61acbdc3 100644 --- a/include/cista/containers/vecvec.h +++ b/include/cista/containers/vecvec.h @@ -159,10 +159,12 @@ struct basic_vecvec { private: index_value_type bucket_begin_idx() const { - return to_idx(map_->bucket_starts_[i_]); + return map_->empty() ? index_value_type{} + : to_idx(map_->bucket_starts_[i_]); } index_value_type bucket_end_idx() const { - return to_idx(map_->bucket_starts_[i_ + 1U]); + return map_->empty() ? index_value_type{} + : to_idx(map_->bucket_starts_[i_ + 1U]); } bool is_inside_bucket(std::size_t const i) const { return bucket_begin_idx() + i < bucket_end_idx(); @@ -335,11 +337,10 @@ struct basic_vecvec { if (bucket_starts_.empty()) { bucket_starts_.emplace_back(index_value_type{0U}); } - bucket_starts_.emplace_back( - static_cast(data_.size() + bucket.size())); data_.insert(std::end(data_), // std::make_move_iterator(std::begin(bucket)), std::make_move_iterator(std::end(bucket))); + bucket_starts_.emplace_back(data_.size()); } bucket add_back_sized(std::size_t const bucket_size) {