di 0.1.0
Loading...
Searching...
No Matches
vector.h
Go to the documentation of this file.
1#pragma once
2
15#include "di/platform/prelude.h"
16#include "di/types/prelude.h"
18#include "di/util/exchange.h"
22
23namespace di::container {
24template<typename T, concepts::Allocator Alloc>
25class Vector : public MutableVectorInterface<Vector<T, Alloc>, T> {
26public:
27 using Value = T;
28 using ConstValue = T const;
29 using Allocator = Alloc;
30
31 constexpr Vector() = default;
32 constexpr Vector(Vector const&) = delete;
33 constexpr Vector(Vector&& other)
34 : m_data(util::exchange(other.m_data, nullptr))
35 , m_size(util::exchange(other.m_size, 0))
36 , m_capacity(util::exchange(other.m_capacity, 0))
37 , m_allocator(util::move(other.m_allocator)) {}
38
39 constexpr Vector(std::initializer_list<T> init)
41 {
42 this->append_container(init);
43 }
44
45 constexpr ~Vector() { deallocate(); }
46
47 constexpr auto operator=(Vector const&) -> Vector& = delete;
48 constexpr auto operator=(Vector&& other) -> Vector& {
49 deallocate();
50 this->m_data = util::exchange(other.m_data, nullptr);
51 this->m_size = util::exchange(other.m_size, 0);
52 this->m_capacity = util::exchange(other.m_capacity, 0);
53 this->m_allocator = util::move(other.m_allocator);
54 return *this;
55 }
56
57 constexpr auto span() -> Span<Value> { return { m_data, m_size }; }
58 constexpr auto span() const -> Span<ConstValue> { return { m_data, m_size }; }
59
60 constexpr auto capacity() const -> usize { return m_capacity; }
61 constexpr auto max_size() const -> usize { return static_cast<usize>(-1); }
62
64 DI_ASSERT(capacity() == 0U);
65
66 return as_fallible(di::allocate_many<T>(m_allocator, n)) % [&](AllocationResult<T> result) {
67 auto [data, new_capacity] = result;
68 m_data = data;
69 m_capacity = new_capacity;
71 }
72 constexpr void assume_size(usize size) { m_size = size; }
73
74 constexpr auto grow_capacity(usize min_capacity) const -> usize {
75 constexpr auto smallest_allowed_capacity = 32zu;
76 if (m_capacity >= min_capacity) {
77 return min_capacity;
78 }
79 return di::max({ min_capacity, 2 * m_capacity, smallest_allowed_capacity });
80 }
81
82 constexpr auto allocator() -> Alloc& { return m_allocator; }
83 constexpr auto allocator() const -> Alloc const& { return m_allocator; }
84
85private:
86 constexpr void deallocate() {
87 this->clear();
88 if (m_data) {
89 di::deallocate_many<T>(m_allocator, m_data, m_capacity);
90 }
91 }
92
93 T* m_data { nullptr };
94 usize m_size { 0 };
95 usize m_capacity { 0 };
96 [[no_unique_address]] Alloc m_allocator {};
97};
98
99template<concepts::InputContainer Con, typename T = meta::ContainerValue<Con>>
101}
102
103namespace di {
105}
#define DI_ASSERT(...)
Definition assert_bool.h:7
Value const ConstValue
Definition vector.h:28
Definition mutable_vector_interface.h:30
constexpr void clear()
Definition mutable_vector_interface.h:57
constexpr auto append_container(Con &&container)
Definition mutable_vector_interface.h:79
Definition vector.h:25
constexpr auto grow_capacity(usize min_capacity) const -> usize
Definition vector.h:74
constexpr auto capacity() const -> usize
Definition vector.h:60
constexpr auto operator=(Vector &&other) -> Vector &
Definition vector.h:48
constexpr auto allocator() const -> Alloc const &
Definition vector.h:83
constexpr Vector(Vector &&other)
Definition vector.h:33
Alloc Allocator
Definition vector.h:29
constexpr Vector(std::initializer_list< T > init)
Definition vector.h:39
constexpr void assume_size(usize size)
Definition vector.h:72
constexpr auto span() const -> Span< ConstValue >
Definition vector.h:58
constexpr ~Vector()
Definition vector.h:45
constexpr Vector(Vector const &)=delete
T Value
Definition vector.h:27
constexpr auto allocator() -> Alloc &
Definition vector.h:82
T const ConstValue
Definition vector.h:28
constexpr auto operator=(Vector const &) -> Vector &=delete
constexpr Vector()=default
constexpr auto max_size() const -> usize
Definition vector.h:61
constexpr auto span() -> Span< Value >
Definition vector.h:57
constexpr auto reserve_from_nothing(usize n) -> meta::AllocatorResult< Alloc >
Definition vector.h:63
Definition span_forward_declaration.h:10
Definition allocator.h:20
Definition sequence.h:12
constexpr auto deallocate
Definition deallocate.h:23
constexpr auto move
Definition move.h:38
constexpr auto size
Definition size.h:62
constexpr auto data
Definition data.h:51
meta::LikeExpected< decltype(di::allocate(util::declval< Alloc & >(), 0, 0)), T > AllocatorResult
Definition allocator.h:25
size_t usize
Definition integers.h:33
di::meta::Decay< decltype(T)> Tag
Definition tag_invoke.h:28
Definition vocab.h:96
constexpr auto exchange(T &object, U &&new_value) -> T
Definition exchange.h:8
Definition any_storable.h:9
constexpr tag_invoke_detail::TagInvokeFn tag_invoke
Definition tag_invoke.h:22
constexpr auto exchange(T &object, U &&new_value) -> T
Definition exchange.h:8
constexpr auto as_fallible
Definition as_fallible.h:26
constexpr auto try_infallible
Definition try_infallible.h:31
constexpr auto deallocate_many
Definition deallocate_many.h:28
constexpr auto allocate_many
Definition allocate_many.h:48
constexpr auto max
Definition max.h:49
Definition allocation_result.h:7
Definition in_place_template.h:5