5#ifndef INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_INLINE_CONTAINER_EXTERNAL_VECTOR_HPP_
6#define INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_INLINE_CONTAINER_EXTERNAL_VECTOR_HPP_
12#include "arene/base/detail/wrapped_iterator.hpp"
13#include "arene/base/inline_container/detail/comparison_interface.hpp"
14#include "arene/base/inline_container/detail/try_construct_interface.hpp"
15#include "arene/base/inline_container/detail/unsafe_element_storage.hpp"
22#include "arene/base/stdlib_choice/cstddef.hpp"
23#include "arene/base/stdlib_choice/declval.hpp"
24#include "arene/base/stdlib_choice/enable_if.hpp"
25#include "arene/base/stdlib_choice/forward.hpp"
26#include "arene/base/stdlib_choice/ignore.hpp"
27#include "arene/base/stdlib_choice/initializer_list.hpp"
28#include "arene/base/stdlib_choice/integral_constant.hpp"
29#include "arene/base/stdlib_choice/is_assignable.hpp"
30#include "arene/base/stdlib_choice/is_constructible.hpp"
31#include "arene/base/stdlib_choice/is_copy_assignable.hpp"
32#include "arene/base/stdlib_choice/is_copy_constructible.hpp"
33#include "arene/base/stdlib_choice/is_default_constructible.hpp"
34#include "arene/base/stdlib_choice/is_move_assignable.hpp"
35#include "arene/base/stdlib_choice/is_move_constructible.hpp"
36#include "arene/base/stdlib_choice/iterator_traits.hpp"
37#include "arene/base/stdlib_choice/move_iterator.hpp"
68namespace external_vector_detail {
74class try_construct_policy {
87 static auto capacity_of(span<byte> buffer)
noexcept -> size_type {
91 return buffer.size() /
sizeof(value_type);
106 class iterator_passkey {
109 explicit iterator_passkey() =
default;
114 using value_type = T;
118 using pointer = value_type*;
120 using const_pointer = value_type
const*;
122 using iterator = detail::wrapped_iterator<pointer, iterator_passkey>;
124 using const_iterator = detail::wrapped_iterator<const_pointer, iterator_passkey>;
126 using difference_type =
typename std::iterator_traits<iterator>::difference_type;
130 using element_type = inline_container::detail::unsafe_element_storage<value_type>;
133 span<element_type> storage_;
144 static auto make_storage_array(span<byte> buffer)
noexcept -> span<element_type> {
145 auto const count = try_construct_policy<T>::capacity_of(buffer);
148 auto*
const start = (count == 0U) ?
nullptr : new (buffer.data()) element_type[count]{};
150 return {start, count};
161 explicit storage_base(span<byte> buffer) noexcept
162 : storage_{make_storage_array(buffer)},
167 ~storage_base() { storage_shrink_to(0U); }
170 storage_base(storage_base
const&) =
delete;
172 storage_base(storage_base&&) =
delete;
174 auto operator=(storage_base
const&) -> storage_base& =
delete;
176 auto operator=(storage_base&&) -> storage_base& =
delete;
180 auto storage_values() noexcept -> span<value_type> {
181 if (storage_capacity() == 0U) {
184 return {&storage_.front().object, size_};
188 auto storage_values() const noexcept -> span<value_type const> {
189 if (storage_capacity() == 0U) {
192 return {&storage_.front().object, size_};
198 auto storage_capacity() const noexcept -> size_type {
return storage_.size(); }
203 auto stored_size() const noexcept -> size_type {
return size_; }
211 void storage_emplace_back(Args&&... args)
noexcept(std::is_nothrow_constructible<value_type, Args...>::value) {
214 auto& location = storage_[size_];
215 destroy_at(&location.dummy);
226 auto storage_shrink_to(
std::size_t count)
noexcept ->
void {
229 for (
auto& location : storage_.subspan(count, size_ - count)) {
230 destroy_at(&location.object);
241 auto swap(storage_base& other)
noexcept ->
void {
260 auto verify_not_already_in_use(span<byte> buffer)
const noexcept -> span<byte> {
261 ARENE_PRECONDITION(
static_cast<void*
>(buffer.data()) !=
static_cast<void*
>(storage_.data()));
269template <
typename ValueType>
270class external_storage_base {
273 class storage_access_tag {
276 explicit storage_access_tag() =
default;
281 using derived_external_vector = external_vector<ValueType>;
285 auto get_storage() const noexcept -> storage_base<ValueType> const& {
286 return derived_external_vector::get_storage(storage_access_tag{},
this);
291 auto get_storage() noexcept -> storage_base<ValueType>& {
292 return derived_external_vector::get_storage(storage_access_tag{},
this);
297 using value_type = ValueType;
304 auto capacity() const noexcept -> size_type {
return get_storage().storage_capacity(); }
311 auto values() noexcept -> span<value_type> {
return get_storage().storage_values(); }
314 auto values() const noexcept -> span<value_type const> {
return get_storage().storage_values(); }
319 template <
typename... Args>
320 void internal_emplace_back(Args&&... args)
noexcept(std::is_nothrow_constructible<ValueType, Args...>::value) {
326 void shrink_to(size_type new_size)
noexcept { get_storage().storage_shrink_to(new_size); }
353 : external_vector_detail::storage_base<T>
354 ,
public inline_vector_detail::vector_interface<external_vector_detail::external_storage_base<T>>
355 ,
public inline_container::detail::comparison_interface<external_vector<T>>
356 ,
public inline_container_detail::try_construct_interface<
358 external_vector_detail::try_construct_policy<T>,
359 inline_container_detail::try_construct_head_args<span<byte>>> {
361 using storage_base = external_vector_detail::storage_base<T>;
364 using interface_base = inline_vector_detail::vector_interface<external_vector_detail::external_storage_base<T>>;
367 using comparison_base = inline_container::detail::comparison_interface<external_vector<T>>;
370 using try_construct_interface = inline_container_detail::try_construct_interface<
372 external_vector_detail::try_construct_policy<T>,
373 inline_container_detail::try_construct_head_args<span<byte>>>;
406 using typename interface_base::const_iterator;
407 using typename interface_base::const_pointer;
408 using typename interface_base::const_reference;
409 using typename interface_base::const_reverse_iterator;
410 using typename interface_base::difference_type;
411 using typename interface_base::iterator;
412 using typename interface_base::pointer;
413 using typename interface_base::reference;
414 using typename interface_base::reverse_iterator;
415 using typename interface_base::size_type;
416 using typename interface_base::value_type;
418 using interface_base::assign;
419 using interface_base::capacity;
420 using interface_base::max_size;
439 : storage_base{buffer},
441 comparison_base{
this},
442 try_construct_interface{
this} {}
481 while (first != last) {
500 template <class U = value_type, constraints<std::enable_if_t<std::is_default_constructible<U>::value>> =
nullptr>
503 while (count != 0U) {
523 template <class U = value_type, constraints<std::enable_if_t<std::is_copy_constructible<U>::value>> =
nullptr>
528 while (count != 0U) {
555 template <class U = value_type, constraints<std::enable_if_t<std::is_move_constructible<U>::value>> =
nullptr>
560 other.verify_not_already_in_use(buffer),
561 std::make_move_iterator(other.begin()),
562 std::make_move_iterator(other.end())
585 template <class U = value_type, constraints<std::enable_if_t<std::is_copy_constructible<U>::value>> =
nullptr>
589 :
external_vector{other.verify_not_already_in_use(buffer), other.begin(), other.end()} {}
603 template <class U = value_type, constraints<std::enable_if_t<std::is_copy_constructible<U>::value>> =
nullptr>
672 auto operator=(enable_if_assignable_t<true, value_type&&> other)
noexcept(
711 auto operator=(enable_if_assignable_t<true, value_type const&> other)
noexcept(
714 this->assign(other.begin(), other.end());
732 class U = value_type,
743 this->assign(init_list.begin(), init_list.end());
781 typename external_vector_detail::external_storage_base<T>::storage_access_tag,
782 external_vector_detail::external_storage_base<T>
const*
const self
783 )
noexcept -> storage_base
const& {
784 return *
static_cast<storage_base const*
>(
static_cast<external_vector const*
>(self));
791 typename external_vector_detail::external_storage_base<T>::storage_access_tag,
792 external_vector_detail::external_storage_base<T>*
const self
793 )
noexcept -> storage_base& {
794 return *
static_cast<storage_base*
>(
static_cast<external_vector*
>(self));
Provides a backport of C++17's constexpr std::advance.
a container similar to std::vector<T> that uses an external buffer for storage
Definition external_vector.hpp:359
external_vector(span< byte > buffer, size_type count) noexcept(std::is_nothrow_default_constructible< value_type >::value)
construct an external_vector with default constructed values
Definition external_vector.hpp:501
external_vector(span< byte > buffer, std::initializer_list< value_type > init_list) noexcept(std::is_nothrow_copy_constructible< value_type >::value)
construct an external_vector from a list of values
Definition external_vector.hpp:604
auto operator=(enable_if_assignable_t< true, value_type const & > other) noexcept(std::is_nothrow_copy_constructible< value_type >::value &&std::is_nothrow_copy_assignable< value_type >::value) -> external_vector &
copy assignment operator
Definition external_vector.hpp:711
external_vector(span< byte > buffer, I first, I last) noexcept(denotes_nothrow_iterable_range_v< I > &&std::is_nothrow_constructible< value_type, typename std::iterator_traits< I >::reference >::value)
construct an external_vector from a range
Definition external_vector.hpp:475
external_vector(external_vector &&other) noexcept
construct an external_vector from another external_vector
Definition external_vector.hpp:626
friend auto swap(external_vector &lhs, external_vector &rhs) noexcept -> void
swaps all the elements between two external_vector values
Definition external_vector.hpp:769
auto swap(external_vector &other) noexcept -> void
swap contents with a different external_vector
Definition external_vector.hpp:752
static auto get_storage(typename external_vector_detail::external_storage_base< T >::storage_access_tag, external_vector_detail::external_storage_base< T > const *const self) noexcept -> storage_base const &
Internal function to get access to the storage for an external_vector.
Definition external_vector.hpp:780
external_vector(span< byte > buffer, size_type count, value_type const &value) noexcept(std::is_nothrow_copy_constructible< value_type >::value)
construct an external_vector with copies of a value
Definition external_vector.hpp:524
auto operator=(enable_if_assignable_t< false, value_type const & >) -> external_vector &=delete
deleted copy assignment operator
external_vector(external_vector const &)=delete
deleted copy constructor
~external_vector()=default
default destructor
auto operator=(enable_if_assignable_t< false, value_type && >) -> external_vector &=delete
deleted move assignment operator
static auto get_storage(typename external_vector_detail::external_storage_base< T >::storage_access_tag, external_vector_detail::external_storage_base< T > *const self) noexcept -> storage_base &
Internal function to get access to the storage for an external_vector.
Definition external_vector.hpp:790
external_vector(span< byte > buffer, external_vector const &other) noexcept(std::is_nothrow_copy_constructible< value_type >::value)
construct an external_vector from another external_vector
Definition external_vector.hpp:586
auto operator=(enable_if_assignable_t< true, value_type && > other) noexcept(std::is_nothrow_move_constructible< value_type >::value &&std::is_nothrow_move_assignable< value_type >::value) -> external_vector &
move assignment operator
Definition external_vector.hpp:672
auto operator=(std::initializer_list< value_type > init_list) noexcept(std::is_nothrow_copy_constructible< value_type >::value &&std::is_nothrow_copy_assignable< value_type >::value) -> external_vector &
assignment from a list of values
Definition external_vector.hpp:735
external_vector(span< byte > buffer, external_vector &&other) noexcept(std::is_nothrow_move_constructible< value_type >::value)
construct an external_vector from another external_vector
Definition external_vector.hpp:556
external_vector(span< byte > buffer) noexcept
construct an empty external_vector
Definition external_vector.hpp:438
external_vector() noexcept
construct an empty external_vector
Definition external_vector.hpp:454
An opt-in trait to indicate that a range type is a borrowed range.
Definition enable_borrowed_range.hpp:31
A backport of std::span for C++14. Provides a view over a contiguous range of elements of type T.
Definition span.hpp:315
Implementation of std::initializer_list for brace initialization.
Definition initializer_list.hpp:21
static constexpr value_type value
Definition integral_constant.hpp:34
Type trait to detect if a type is constructible from arguments of the specified types and is noexcept...
Definition is_constructible.hpp:84
provides a uniform interface to the properties of an iterator
Definition iterator_traits.hpp:90
#define ARENE_PRECONDITION(condition)
Check that a precondition holds, and terminate the application if it does not.
Definition contract.hpp:150
Implementation of a backport of std::forward_like.
Definition external_vector.hpp:803
static ARENE_MAYBE_UNUSED constexpr auto const & swap
A Customization Point Object implementation of the constexpr compatible std::swap from C++17.
Definition swap.hpp:130
Definition accumulate.hpp:37
constexpr std::integral_constant< std::size_t, N > array< T, N >::max_size
Definition array.hpp:537
constexpr bool is_random_access_iterator_v
Definition iterator_category_traits.hpp:332
typename static_if< B >::template then_else< T, F > conditional_t
lazily select between two types based on a boolean condition
Definition conditional.hpp:28
constexpr bool denotes_nothrow_iterable_range_v
Check if an iterator/sentinel pair form an iterable range that can be iterated without throwing.
constexpr std::integral_constant< typename inline_map< Key, Value, Capacity, Compare >::size_type, Capacity > inline_map< Key, Value, Capacity, Compare >::capacity
Definition map.hpp:2305
constexpr auto forward_like
A backport of C++23's std::forward_like, which allows forwarding a type with the same reference and c...
constexpr auto is_aligned(Integer value) -> bool
Check if an integer is a multiple of the specified power-of-two alignment.
Definition alignment.hpp:62
typename constraints_impl::constraints< EnableIfs... >::type constraints
Helper used to express template constraints in a consistent way using std::enable_if_t.
Definition constraints.hpp:51
Definition accumulate.hpp:36
::size_t size_t
A type suitable for holding the size of an object.
Definition cstddef.hpp:29
typename enable_if< Condition, Type >::type enable_if_t
Type alias to support constraining templates. Is an alias for the specified type if the Condition is ...
Definition enable_if.hpp:41
bool_constant< true > true_type
An alias for bool_constant<true>
Definition integral_constant.hpp:76
auto declval() noexcept -> T &&
A dummy function that is declared but not defined, with a user-specified return type,...
auto make_move_iterator(Iterator iter) -> std::move_iterator< Iterator >
Create a move_iterator with the specified underlying iterator.
Definition move_iterator.hpp:353
static constexpr auto const & ignore
An object to which anything can be assigned, which ignores the assigned value.
Definition ignore.hpp:72
constexpr auto forward(remove_reference_t< T > &value) noexcept -> T &&
Cast a value to an rvalue if T is an rvalue or rvalue reference, otherwise forward an lvalue referenc...
Definition forward.hpp:24