Iros
 
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
30 constexpr Vector() = default;
31 constexpr Vector(Vector const&) = delete;
32 constexpr Vector(Vector&& other)
33 : m_data(util::exchange(other.m_data, nullptr))
34 , m_size(util::exchange(other.m_size, 0))
35 , m_capacity(util::exchange(other.m_capacity, 0))
36 , m_allocator(util::move(other.m_allocator)) {}
37
38 constexpr Vector(std::initializer_list<T> init)
40 {
41 this->append_container(init);
42 }
43
44 constexpr ~Vector() { deallocate(); }
45
46 constexpr auto operator=(Vector const&) -> Vector& = delete;
47 constexpr auto operator=(Vector&& other) -> Vector& {
48 deallocate();
49 this->m_data = util::exchange(other.m_data, nullptr);
50 this->m_size = util::exchange(other.m_size, 0);
51 this->m_capacity = util::exchange(other.m_capacity, 0);
52 this->m_allocator = util::move(other.m_allocator);
53 return *this;
54 }
55
56 constexpr auto span() -> Span<Value> { return { m_data, m_size }; }
57 constexpr auto span() const -> Span<ConstValue> { return { m_data, m_size }; }
58
59 constexpr auto capacity() const -> usize { return m_capacity; }
60 constexpr auto max_size() const -> usize { return static_cast<usize>(-1); }
61
63 DI_ASSERT(capacity() == 0U);
64
65 return as_fallible(di::allocate_many<T>(m_allocator, n)) % [&](AllocationResult<T> result) {
66 auto [data, new_capacity] = result;
67 m_data = data;
68 m_capacity = new_capacity;
70 }
71 constexpr void assume_size(usize size) { m_size = size; }
72
73 constexpr auto grow_capacity(usize min_capacity) const -> usize {
74 constexpr auto smallest_allowed_capacity = 32zu;
75 if (m_capacity >= min_capacity) {
76 return min_capacity;
77 }
78 return di::max({ min_capacity, 2 * m_capacity, smallest_allowed_capacity });
79 }
80
81 constexpr auto allocator() -> Alloc& { return m_allocator; }
82 constexpr auto allocator() const -> Alloc const& { return m_allocator; }
83
84private:
85 constexpr void deallocate() {
86 this->clear();
87 if (m_data) {
88 di::deallocate_many<T>(m_allocator, m_data, m_capacity);
89 }
90 }
91
92 T* m_data { nullptr };
93 usize m_size { 0 };
94 usize m_capacity { 0 };
95 [[no_unique_address]] Alloc m_allocator {};
96};
97
98template<concepts::InputContainer Con, typename T = meta::ContainerValue<Con>>
100}
101
102namespace di {
104}
#define DI_ASSERT(...)
Definition assert_bool.h:7
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_forward_declaration.h:8
constexpr auto grow_capacity(usize min_capacity) const -> usize
Definition vector.h:73
constexpr auto capacity() const -> usize
Definition vector.h:59
constexpr auto operator=(Vector &&other) -> Vector &
Definition vector.h:47
constexpr auto allocator() const -> Alloc const &
Definition vector.h:82
constexpr Vector(Vector &&other)
Definition vector.h:32
constexpr Vector(std::initializer_list< T > init)
Definition vector.h:38
constexpr void assume_size(usize size)
Definition vector.h:71
constexpr auto span() const -> Span< ConstValue >
Definition vector.h:57
constexpr ~Vector()
Definition vector.h:44
constexpr Vector(Vector const &)=delete
T Value
Definition vector.h:27
constexpr auto allocator() -> Alloc &
Definition vector.h:81
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:60
constexpr auto span() -> Span< Value >
Definition vector.h:56
constexpr auto reserve_from_nothing(usize n) -> meta::AllocatorResult< Alloc >
Definition vector.h:62
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:54
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 zstring_parser.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:47
Definition allocation_result.h:7
Definition in_place_template.h:5