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
3#ifndef TETL_VECTOR_VECTOR_HPP
4#define TETL_VECTOR_VECTOR_HPP
5
13#include <etl/_utility/move.hpp>
14#include <etl/_utility/swap.hpp>
15
16namespace etl {
17
20template <typename T, typename Allocator>
21struct vector {
22 using value_type = T;
23 using allocator_type = Allocator;
26 using pointer = T*;
27 using const_pointer = T const*;
28 using reference = T&;
29 using const_reference = T const&;
30 using iterator = T*;
31 using const_iterator = T const*;
32
33 constexpr vector() = default;
34
35 explicit constexpr vector(Allocator alloc)
36 : _alloc{etl::move(alloc)}
37 {
38 }
39
40 constexpr vector(etl::size_t n, T const& value, Allocator alloc = Allocator())
41 : _alloc{etl::move(alloc)}
42 {
43 allocate_and_fill(n, value);
44 }
45
46 explicit constexpr vector(etl::size_t n, Allocator alloc = Allocator())
47 : vector{n, T(), etl::move(alloc)}
48 {
49 }
50
51 constexpr vector(vector const& o) = delete;
52 constexpr auto operator=(vector const& o) -> vector& = delete;
53
54 constexpr vector(vector&& other) noexcept
55 : _ptr{etl::exchange(other._ptr, nullptr)}
56 , _size{etl::exchange(other._size, etl::size_t(0))}
57 , _capacity{etl::exchange(other._capacity, etl::size_t(0))}
58 , _alloc{etl::exchange(other._alloc, Allocator{})}
59 {
60 }
61
62 constexpr auto operator=(vector&& other) noexcept -> vector&
63 {
64 _ptr = etl::exchange(other._ptr, nullptr);
65 _size = etl::exchange(other._size, etl::size_t(0));
66 _capacity = etl::exchange(other._capacity, etl::size_t(0));
67 _alloc = etl::exchange(other._alloc, Allocator{});
68 return *this;
69 }
70
71 constexpr ~vector() noexcept
72 {
73 clear();
74 if (_ptr != nullptr) {
75 deallocate();
76 }
77 }
78
79 [[nodiscard]] constexpr auto data() -> T* { return _ptr; }
80
81 [[nodiscard]] constexpr auto data() const -> T const* { return _ptr; }
82
83 [[nodiscard]] constexpr auto begin() -> T* { return _ptr; }
84
85 [[nodiscard]] constexpr auto begin() const -> T const* { return _ptr; }
86
87 [[nodiscard]] constexpr auto end() -> T* { return etl::next(_ptr, etl::ptrdiff_t(size())); }
88
89 [[nodiscard]] constexpr auto end() const -> T const* { return etl::next(_ptr, etl::ptrdiff_t(size())); }
90
91 [[nodiscard]] constexpr auto empty() -> bool { return size() == 0; }
92
93 [[nodiscard]] constexpr auto empty() const -> bool { return size() == 0; }
94
95 [[nodiscard]] constexpr auto size() -> etl::size_t { return _size; }
96
97 [[nodiscard]] constexpr auto size() const -> etl::size_t { return _size; }
98
99 [[nodiscard]] constexpr auto capacity() -> etl::size_t { return _capacity; }
100
101 [[nodiscard]] constexpr auto capacity() const -> etl::size_t { return _capacity; }
102
103 constexpr auto clear() noexcept -> void
104 {
106 _size = 0;
107 }
108
109 friend constexpr auto swap(vector& lhs, vector& rhs) -> void
110 {
111 etl::swap(lhs._ptr, rhs._ptr);
112 etl::swap(lhs._size, rhs._size);
113 etl::swap(lhs._capacity, rhs._capacity);
114 swap(lhs._alloc, rhs._alloc);
115 }
116
117private:
118 constexpr auto allocate_and_fill(etl::size_t n, T const& value) -> void
119 {
121 _size = n;
122 _capacity = n;
123 etl::uninitialized_fill(begin(), end(), value);
124 }
125
126 constexpr auto deallocate() -> void { etl::allocator_traits<Allocator>::deallocate(_alloc, _ptr, capacity()); }
127
128 T* _ptr{nullptr};
129 etl::size_t _size{0};
130 etl::size_t _capacity{0};
131 TETL_NO_UNIQUE_ADDRESS Allocator _alloc;
132};
133
134} // namespace etl
135
136#endif // TETL_VECTOR_VECTOR_HPP
#define TETL_NO_UNIQUE_ADDRESS
Definition attributes.hpp:41
constexpr auto move(InputIt first, InputIt last, OutputIt destination) -> OutputIt
Moves the elements in the range [first, last), to another range beginning at destination,...
Definition move.hpp:26
constexpr auto next(InputIt it, typename iterator_traits< InputIt >::difference_type n=1) -> InputIt
Return the nth successor of iterator it.
Definition next.hpp:14
constexpr struct etl::ranges::destroy_fn destroy
Definition adjacent_find.hpp:8
constexpr auto exchange(T &obj, U &&newValue) noexcept(etl::is_nothrow_move_constructible_v< T > and etl::is_nothrow_assignable_v< T &, U >) -> T
Replaces the value of obj with new_value and returns the old value of obj.
Definition exchange.hpp:16
TETL_BUILTIN_PTRDIFF ptrdiff_t
etl::ptrdiff_t is the signed integer type of the result of subtracting two pointers.
Definition ptrdiff_t.hpp:14
constexpr auto uninitialized_fill(ForwardIt first, ForwardIt last, T const &value) -> void
Definition uninitialized_fill.hpp:13
auto swap(inplace_function< R(Args...), Capacity, Alignment > &lhs, inplace_function< R(Args...), Capacity, Alignment > &rhs) noexcept -> void
Overloads the etl::swap algorithm for etl::inplace_function. Exchanges the state of lhs with that of ...
Definition inplace_function.hpp:249
TETL_BUILTIN_SIZET size_t
etl::size_t is the unsigned integer type of the result of the sizeof operator.
Definition size_t.hpp:14
static constexpr auto allocate(Alloc &a, size_type n)
Definition allocator_traits.hpp:97
static constexpr void deallocate(Alloc &a, pointer p, size_type n)
Definition allocator_traits.hpp:99
constexpr auto begin() const -> T const *
Definition vector.hpp:85
constexpr auto capacity() -> etl::size_t
Definition vector.hpp:99
T & reference
Definition vector.hpp:28
Allocator allocator_type
Definition vector.hpp:23
etl::ptrdiff_t difference_type
Definition vector.hpp:25
constexpr vector(etl::size_t n, T const &value, Allocator alloc=Allocator())
Definition vector.hpp:40
constexpr auto size() -> etl::size_t
Definition vector.hpp:95
constexpr auto end() -> T *
Definition vector.hpp:87
constexpr vector(vector const &o)=delete
etl::size_t size_type
Definition vector.hpp:24
constexpr vector()=default
constexpr auto size() const -> etl::size_t
Definition vector.hpp:97
T * iterator
Definition vector.hpp:30
T const * const_iterator
Definition vector.hpp:31
constexpr auto empty() const -> bool
Definition vector.hpp:93
constexpr auto operator=(vector const &o) -> vector &=delete
constexpr auto clear() noexcept -> void
Definition vector.hpp:103
constexpr auto data() -> T *
Definition vector.hpp:79
constexpr vector(vector &&other) noexcept
Definition vector.hpp:54
constexpr auto data() const -> T const *
Definition vector.hpp:81
T * pointer
Definition vector.hpp:26
constexpr auto begin() -> T *
Definition vector.hpp:83
constexpr auto operator=(vector &&other) noexcept -> vector &
Definition vector.hpp:62
constexpr auto end() const -> T const *
Definition vector.hpp:89
constexpr auto empty() -> bool
Definition vector.hpp:91
constexpr vector(Allocator alloc)
Definition vector.hpp:35
T const * const_pointer
Definition vector.hpp:27
constexpr ~vector() noexcept
Definition vector.hpp:71
constexpr vector(etl::size_t n, Allocator alloc=Allocator())
Definition vector.hpp:46
T const & const_reference
Definition vector.hpp:29
T value_type
Definition vector.hpp:22
constexpr auto capacity() const -> etl::size_t
Definition vector.hpp:101
friend constexpr auto swap(vector &lhs, vector &rhs) -> void
Definition vector.hpp:109