Expression Templates Library (ETL)
aligned_vector.hpp
Go to the documentation of this file.
1 //=======================================================================
2 // Copyright (c) 2014-2023 Baptiste Wicht
3 // Distributed under the terms of the MIT License.
4 // (See accompanying file LICENSE or copy at
5 // http://opensource.org/licenses/MIT)
6 //=======================================================================
7 
13 #pragma once
14 
15 #include "cpp_utils/aligned_vector.hpp"
16 
17 namespace etl {
18 
25 template <typename T, typename Alloc>
26 struct simple_vector {
27  using value_type = T;
28 
29  simple_vector() = default;
30 
31  simple_vector(const simple_vector& rhs) : size(rhs.size) {
32  resize_impl(rhs.size, false);
33 
34  for (size_t i = 0; i < size; ++i) {
35  _data[i] = rhs._data[i];
36  }
37  }
38 
39  simple_vector(simple_vector&& rhs) : size(rhs.size) {
40  _data = rhs._data;
41  rhs._data = nullptr;
42  }
43 
44  simple_vector& operator=(const simple_vector& rhs) {
45  if (this != &rhs) {
46  resize_impl(rhs.size, false);
47 
48  size = rhs.size;
49 
50  for (size_t i = 0; i < size; ++i) {
51  _data[i] = rhs._data[i];
52  }
53  }
54 
55  return *this;
56  }
57 
58  simple_vector& operator=(simple_vector&& rhs) {
59  if (this != &rhs) {
60  release();
61 
62  size = rhs.size;
63 
64  _data = rhs._data;
65  rhs._data = nullptr;
66  }
67 
68  return *this;
69  }
70 
71  ~simple_vector() {
72  release();
73  }
74 
75  void resize(size_t n) {
76  resize_impl(n, true);
77  }
78 
79  T& operator[](size_t i) {
80  return _data[i];
81  }
82 
83  const T& operator[](size_t i) const {
84  return _data[i];
85  }
86 
87  T* data() {
88  return _data;
89  }
90 
91  const T* data() const {
92  return _data;
93  }
94 
95 private:
96  void release() {
97  if (_data) {
98  //In case of non-trivial type, we need to call the destructors
99  if constexpr (!std::is_trivially_default_constructible_v<T>) {
100  for (size_t i = 0; i < size; ++i) {
101  _data[i].~T();
102  }
103  }
104 
105  allocator.deallocate(_data, size);
106 
107  _data = nullptr;
108  }
109  }
110 
111  void resize_impl(size_t n, bool copy = true) {
112  auto* new_data = allocator.allocate(n);
113 
114  // Call all the constructors if necessary
115  if constexpr (!std::is_trivially_default_constructible_v<T>) {
116  new (new_data) T[n]();
117  }
118 
119  // Initialize to the default values
120  if constexpr (std::is_trivially_default_constructible_v<T>) {
121  std::fill_n(new_data, n, T());
122  }
123 
124  if (copy && _data) {
125  for (size_t i = 0; i < size && i < n; ++i) {
126  new_data[i] = _data[i];
127  }
128  }
129 
130  release();
131 
132  _data = new_data;
133  size = n;
134  }
135 
136  T* _data = nullptr;
137  size_t size = 0;
138  Alloc allocator;
139 };
140 
141 template <typename T, std::size_t A>
143  using type = std::vector<T, cpp::aligned_allocator<T, A>>;
144 };
145 
146 template <std::size_t A>
147 struct aligned_vector_impl<bool, A> {
149 };
150 
151 template <typename T, std::size_t A>
152 using aligned_vector = typename aligned_vector_impl<T, A>::type;
153 
154 } //end of namespace etl
Root namespace for the ETL library.
Definition: adapter.hpp:15
A simple std::vector to work with bool.
Definition: aligned_vector.hpp:26
Definition: aligned_vector.hpp:142