tetl 0.1.0
Embedded Template Library
Loading...
Searching...
No Matches
vector.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: BSL-1.0
2// SPDX-FileCopyrightText: Copyright (C) 2024 Tobias Hienzsch
3
4#ifndef TETL_VECTOR_VECTOR_HPP
5#define TETL_VECTOR_VECTOR_HPP
6
7#include <etl/_cstddef/ptrdiff_t.hpp>
8#include <etl/_cstddef/size_t.hpp>
9#include <etl/_iterator/next.hpp>
10#include <etl/_memory/allocator_traits.hpp>
11#include <etl/_memory/ranges_destroy.hpp>
12#include <etl/_memory/uninitialized_fill.hpp>
13#include <etl/_utility/exchange.hpp>
14#include <etl/_utility/move.hpp>
15#include <etl/_utility/swap.hpp>
16
17namespace etl {
18
19/// \headerfile etl/vector.hpp
20/// \ingroup vector
21template <typename T, typename Allocator>
22struct vector {
23 using value_type = T;
24 using allocator_type = Allocator;
25 using size_type = etl::size_t;
26 using difference_type = etl::ptrdiff_t;
27 using pointer = T*;
28 using const_pointer = T const*;
29 using reference = T&;
30 using const_reference = T const&;
31 using iterator = T*;
32 using const_iterator = T const*;
33
34 constexpr vector() = default;
35
36 explicit constexpr vector(Allocator alloc)
37 : _alloc{etl::move(alloc)}
38 {
39 }
40
41 constexpr vector(etl::size_t n, T const& value, Allocator alloc = Allocator())
42 : _alloc{etl::move(alloc)}
43 {
44 allocate_and_fill(n, value);
45 }
46
47 explicit constexpr vector(etl::size_t n, Allocator alloc = Allocator())
48 : vector{n, T(), etl::move(alloc)}
49 {
50 }
51
52 constexpr vector(vector const& o) = delete;
53 constexpr auto operator=(vector const& o) -> vector& = delete;
54
55 constexpr vector(vector&& other) noexcept
56 : _ptr{etl::exchange(other._ptr, nullptr)}
57 , _size{etl::exchange(other._size, etl::size_t(0))}
58 , _capacity{etl::exchange(other._capacity, etl::size_t(0))}
59 , _alloc{etl::exchange(other._alloc, Allocator{})}
60 {
61 }
62
63 constexpr auto operator=(vector&& other) noexcept -> vector&
64 {
65 _ptr = etl::exchange(other._ptr, nullptr);
66 _size = etl::exchange(other._size, etl::size_t(0));
67 _capacity = etl::exchange(other._capacity, etl::size_t(0));
68 _alloc = etl::exchange(other._alloc, Allocator{});
69 return *this;
70 }
71
72 constexpr ~vector() noexcept
73 {
74 clear();
75 if (_ptr != nullptr) {
76 deallocate();
77 }
78 }
79
80 [[nodiscard]] constexpr auto data() -> T*
81 {
82 return _ptr;
83 }
84
85 [[nodiscard]] constexpr auto data() const -> T const*
86 {
87 return _ptr;
88 }
89
90 [[nodiscard]] constexpr auto begin() -> T*
91 {
92 return _ptr;
93 }
94
95 [[nodiscard]] constexpr auto begin() const -> T const*
96 {
97 return _ptr;
98 }
99
100 [[nodiscard]] constexpr auto end() -> T*
101 {
102 return etl::next(_ptr, etl::ptrdiff_t(size()));
103 }
104
105 [[nodiscard]] constexpr auto end() const -> T const*
106 {
107 return etl::next(_ptr, etl::ptrdiff_t(size()));
108 }
109
110 [[nodiscard]] constexpr auto empty() -> bool
111 {
112 return size() == 0;
113 }
114
115 [[nodiscard]] constexpr auto empty() const -> bool
116 {
117 return size() == 0;
118 }
119
120 [[nodiscard]] constexpr auto size() -> etl::size_t
121 {
122 return _size;
123 }
124
125 [[nodiscard]] constexpr auto size() const -> etl::size_t
126 {
127 return _size;
128 }
129
130 [[nodiscard]] constexpr auto capacity() -> etl::size_t
131 {
132 return _capacity;
133 }
134
135 [[nodiscard]] constexpr auto capacity() const -> etl::size_t
136 {
137 return _capacity;
138 }
139
140 constexpr auto clear() noexcept -> void
141 {
142 etl::ranges::destroy(*this);
143 _size = 0;
144 }
145
146 friend constexpr auto swap(vector& lhs, vector& rhs) -> void
147 {
148 etl::swap(lhs._ptr, rhs._ptr);
149 etl::swap(lhs._size, rhs._size);
150 etl::swap(lhs._capacity, rhs._capacity);
151 swap(lhs._alloc, rhs._alloc);
152 }
153
154private:
155 constexpr auto allocate_and_fill(etl::size_t n, T const& value) -> void
156 {
157 _ptr = etl::allocator_traits<Allocator>::allocate(_alloc, n);
158 _size = n;
159 _capacity = n;
160 etl::uninitialized_fill(begin(), end(), value);
161 }
162
163 constexpr auto deallocate() -> void
164 {
165 etl::allocator_traits<Allocator>::deallocate(_alloc, _ptr, capacity());
166 }
167
168 T* _ptr{nullptr};
169 etl::size_t _size{0};
170 etl::size_t _capacity{0};
171 TETL_NO_UNIQUE_ADDRESS Allocator _alloc;
172};
173
174} // namespace etl
175
176#endif // TETL_VECTOR_VECTOR_HPP
Definition ranges_in_fun_result.hpp:12
Definition adjacent_find.hpp:9
Definition allocator_traits.hpp:88
Definition vector.hpp:22
constexpr auto begin() const -> T const *
Definition vector.hpp:95
constexpr auto capacity() -> etl::size_t
Definition vector.hpp:130
constexpr vector(etl::size_t n, T const &value, Allocator alloc=Allocator())
Definition vector.hpp:41
constexpr auto size() -> etl::size_t
Definition vector.hpp:120
constexpr auto end() -> T *
Definition vector.hpp:100
constexpr vector(vector const &o)=delete
constexpr vector()=default
constexpr auto size() const -> etl::size_t
Definition vector.hpp:125
constexpr auto empty() const -> bool
Definition vector.hpp:115
constexpr auto operator=(vector const &o) -> vector &=delete
constexpr auto clear() noexcept -> void
Definition vector.hpp:140
constexpr auto data() -> T *
Definition vector.hpp:80
constexpr vector(vector &&other) noexcept
Definition vector.hpp:55
constexpr auto data() const -> T const *
Definition vector.hpp:85
constexpr auto begin() -> T *
Definition vector.hpp:90
constexpr auto operator=(vector &&other) noexcept -> vector &
Definition vector.hpp:63
constexpr auto end() const -> T const *
Definition vector.hpp:105
constexpr auto empty() -> bool
Definition vector.hpp:110
constexpr vector(Allocator alloc)
Definition vector.hpp:36
constexpr ~vector() noexcept
Definition vector.hpp:72
constexpr vector(etl::size_t n, Allocator alloc=Allocator())
Definition vector.hpp:47
constexpr auto capacity() const -> etl::size_t
Definition vector.hpp:135
friend constexpr auto swap(vector &lhs, vector &rhs) -> void
Definition vector.hpp:146