My Project
SparseArray.hpp
1 /*
2 ** EPITECH PROJECT, 2022
3 ** r-type
4 ** File description:
5 ** SparseArray
6 */
7 
8 #ifndef SPARSEARRAY_HPP_
9  #define SPARSEARRAY_HPP_
10 
11  #include <iostream>
12  #include <optional>
13  #include <vector>
14  #include <memory>
15 
21 template <typename Component>
22 class Sparse_array {
23  public:
24  using value_type = std::optional<Component>;
25  using reference_type = value_type &;
26  using const_reference_type = value_type const &;
27  using container_t = std::vector<value_type>;
28  using iterator = typename container_t::iterator;
29  using const_iterator = typename container_t::const_iterator;
30  using size_type = typename container_t::size_type;
31 
32  public:
36  Sparse_array() = default;
37 
43  explicit Sparse_array(Sparse_array const &sp_arr)
44  {
45  for (std::size_t i = 0; i < sp_arr.size(); i++) {
46  _data.push_back(std::nullopt);
47  }
48  for (std::size_t i = 0; i < sp_arr.size(); i++) {
49  _data[i] = sp_arr._data[i];
50  }
51  };
52 
58  explicit Sparse_array(Sparse_array &&sp_arr) noexcept : _data(std::move(sp_arr._data)) {};
59 
65  explicit Sparse_array(std::size_t size)
66  {
67  for (std::size_t i = 0; i < size; i++) {
68  _data.push_back(std::nullopt);
69  }
70  };
71 
75  virtual ~Sparse_array() = default;
76 
84  {
85  std::swap(_data, sp_arr._data);
86  return *this;
87  };
88 
95  Sparse_array &operator=(Sparse_array &&sp_arr) noexcept
96  {
97  _data = std::move(sp_arr._data);
98  return *this;
99  };
100 
107  reference_type operator[](size_t idx)
108  {
109  return _data[idx];
110  };
111 
118  const_reference_type operator[](size_t idx) const
119  {
120  return _data[idx];
121  };
122 
128  iterator begin()
129  {
130  return _data.begin();
131  };
132 
138  const_iterator begin() const
139  {
140  return _data.begin();
141  };
142 
148  const_iterator cbegin() const
149  {
150  return _data.cbegin();
151  };
152 
158  iterator end()
159  {
160  return _data.end();
161  };
162 
168  const_iterator end() const
169  {
170  return _data.end();
171  };
172 
178  const_iterator cend() const
179  {
180  return _data.cend();
181  };
182 
188  size_type size() const
189  {
190  return _data.size();
191  };
192 
198  void extend(size_t sizeToExtend)
199  {
200  for (size_t x = 0; x < sizeToExtend; x += 1)
201  _data.push_back(std::nullopt);
202  }
203 
211  reference_type insert_at(size_type pos, Component const &component)
212  {
213  if (pos > _data.size())
214  throw std::invalid_argument("Invalid pos in insert_at (SparseArray)");
215  if (pos < _data.size()) {
216  auto tmp = _data.get_allocator();
217 
218  std::allocator_traits<decltype(tmp)>::destroy(tmp, std::addressof(_data[pos]));
219  _data[pos] = component;
220  return _data[pos];
221  }
222 
223  _data.push_back(component);
224  return _data.back();
225  };
226 
234  reference_type insert_at(size_type pos, Component &&component)
235  {
236  if (pos > _data.size())
237  throw std::invalid_argument("Invalid position in sparse array");
238  if (pos < _data.size()) {
239  auto tmp = _data.get_allocator();
240 
241  std::allocator_traits<decltype(tmp)>::destroy(tmp, std::addressof(_data[pos]));
242  _data[pos] = std::move(component);
243  return _data[pos];
244  }
245 
246  _data.push_back(std::move(component));
247  return _data.back();
248  };
249 
258  template <class... Params>
259  reference_type emplace_at(size_type pos, Params &&...params)
260  {
261  if (pos > _data.size())
262  throw std::invalid_argument("Invalid pos in sparse array");
263  auto tmp = _data.get_allocator();
264  std::allocator_traits<decltype(tmp)>::destroy(tmp, std::addressof(_data[pos]));
265  std::allocator_traits<decltype(tmp)>::construct(tmp, std::addressof(_data[pos]), std::forward(params)...);
266  return _data[pos];
267  };
268 
274  void erase(size_type pos)
275  {
276  _data[pos] = std::nullopt;
277  };
278 
285  size_type get_index(value_type const &val_type) const
286  {
287  auto tmp = std::addressof(val_type);
288 
289  for (std::size_t i = 0; i < _data.size(); i++) {
290  if (tmp == std::addressof(_data[i]))
291  return i;
292  }
293  throw std::invalid_argument("Component doesn't exist (SparseArray)");
294  };
295 
296  private:
297  container_t _data;
298 };
299 
300 #endif /* !SPARSEARRAY_HPP_ */
Sparse_array
Class that handle Element of type Component like a vector which can be holed.
Definition: SparseArray.hpp:22
Sparse_array::end
iterator end()
Overloads the operator ebd()
Definition: SparseArray.hpp:158
Sparse_array::operator=
Sparse_array & operator=(Sparse_array &&sp_arr) noexcept
Overload the operator =.
Definition: SparseArray.hpp:95
Sparse_array::Sparse_array
Sparse_array(Sparse_array &&sp_arr) noexcept
Construct a new Sparse_array object.
Definition: SparseArray.hpp:58
Sparse_array::emplace_at
reference_type emplace_at(size_type pos, Params &&...params)
Overloads the operator emplace_at()
Definition: SparseArray.hpp:259
Sparse_array::operator[]
const_reference_type operator[](size_t idx) const
Overloads the operator [] const.
Definition: SparseArray.hpp:118
Sparse_array::size
size_type size() const
Overloads the operator size()
Definition: SparseArray.hpp:188
Sparse_array::operator[]
reference_type operator[](size_t idx)
Overloads the operator [].
Definition: SparseArray.hpp:107
Component
Namespace for all components.
Definition: CConnection.hpp:14
Sparse_array::get_index
size_type get_index(value_type const &val_type) const
Get the index object.
Definition: SparseArray.hpp:285
component
Namespace for all components.
Definition: CDamage.hpp:14
Sparse_array::extend
void extend(size_t sizeToExtend)
Overload the operator extend()
Definition: SparseArray.hpp:198
Sparse_array::begin
iterator begin()
Overloads the operator begin()
Definition: SparseArray.hpp:128
Sparse_array::cbegin
const_iterator cbegin() const
Overloads the operator cbegin() const.
Definition: SparseArray.hpp:148
Sparse_array::Sparse_array
Sparse_array(std::size_t size)
Construct a new Sparse_array object.
Definition: SparseArray.hpp:65
Sparse_array::erase
void erase(size_type pos)
Overloads the operator erase()
Definition: SparseArray.hpp:274
Sparse_array::cend
const_iterator cend() const
Overloads the operator cend() const.
Definition: SparseArray.hpp:178
Sparse_array::insert_at
reference_type insert_at(size_type pos, Component &&component)
Overloads the operator insert_at()
Definition: SparseArray.hpp:234
Sparse_array::operator=
Sparse_array & operator=(Sparse_array const &sp_arr)
Overload the operator =.
Definition: SparseArray.hpp:83
Sparse_array::~Sparse_array
virtual ~Sparse_array()=default
Destroy the Sparse_array object.
Sparse_array::Sparse_array
Sparse_array()=default
Construct a new Sparse_array object.
Sparse_array::Sparse_array
Sparse_array(Sparse_array const &sp_arr)
Construct a new Sparse_array object.
Definition: SparseArray.hpp:43
Sparse_array::insert_at
reference_type insert_at(size_type pos, Component const &component)
Overloads the operator insert_at()
Definition: SparseArray.hpp:211
Sparse_array::begin
const_iterator begin() const
Overloads the operator begin() const.
Definition: SparseArray.hpp:138
Sparse_array::end
const_iterator end() const
Overloads the operator end() const.
Definition: SparseArray.hpp:168