R-Type
SparseArray.hpp
Go to the documentation of this file.
1 /*
2 ** EPITECH PROJECT, 2023
3 ** R-Type
4 ** File description:
5 ** SparseArray
6 */
7 
8 #ifndef SPARSEARRAY_HPP_
9 #define SPARSEARRAY_HPP_
10 #include <memory>
11 #include <optional>
12 #include <vector>
13 #include "Error.hpp"
14 
17 template <typename Component>
19 {
20  public:
22  using valueType = std::optional<Component>;
26  using constReferenceType = const valueType &;
28  using container = std::vector<valueType>;
29 
31  using sizeType = typename container::size_type;
32 
34  using iterator = typename container::iterator;
36  using constIterator = typename container::const_iterator;
37 
42  SparseArray(const SparseArray &sparseArray) : _data(sparseArray._data){};
43 
44  ~SparseArray() = default;
45 
49  SparseArray &operator=(const SparseArray &sparseArray)
50  {
51  _data = sparseArray._data;
52  return *this;
53  };
57  SparseArray &operator=(SparseArray &&sparseArray) noexcept
58  {
59  _data = sparseArray._data;
60  return *this;
61  };
62 
66  referenceType operator[](std::size_t idx) { return _data[idx]; };
70  constReferenceType operator[](std::size_t idx) const { return _data[idx]; };
71 
74  iterator begin() { return _data.begin(); };
77  constIterator begin() const { return _data.begin(); };
80  constIterator cbegin() const { return _data.cbegin(); };
81 
84  iterator end() { return _data.end(); };
87  constIterator end() const { return _data.end(); };
90  constIterator cend() const { return _data.cend(); };
91 
94  sizeType size() const { return _data.size(); };
95 
101  referenceType insert_at(sizeType pos, const Component &component)
102  {
103  if (_data.size() < pos + 1)
104  _data.resize(pos + 1);
105  _data[pos] = component;
106  return _data[pos];
107  };
113  referenceType insert_at(sizeType pos, Component &&component)
114  {
115  if (_data.size() < pos + 1)
116  _data.resize(pos + 1);
117  _data[pos] = component;
118  return _data[pos];
119  };
120 
125  {
126  auto &ret = insert_at(pos, Component());
127  return ret;
128  };
134  template <class... Params>
135  referenceType emplace_at(sizeType pos, Params &&...params)
136  {
137  auto &ret = insert_at(pos, Component());
138 
139  emplace_at(params...);
140  return ret;
141  };
142 
145  void erase(sizeType pos)
146  {
147  if (_data.size() > pos && _data[pos].has_value())
148  _data[pos].reset();
149  };
153  sizeType getIndex(const valueType &val) const
154  {
155  for (sizeType pos = 0; pos < _data.size(); pos++) {
156  if (std::addressof(_data[pos]) == std::addressof(val))
157  return pos;
158  }
160  };
161 
164  void resize(const std::size_t &count) { _data.resize(count); }
165 
166  private:
168  container _data;
169 };
170 
171 #endif /* !SPARSEARRAY_HPP_ */
ComponentNotInsertedError Class Error Error thrown when trying to access a component that is not regi...
Definition: Error.hpp:37
Array which can have empty indexes.
Definition: SparseArray.hpp:19
void erase(sizeType pos)
Removes the component at the specified position.
Definition: SparseArray.hpp:145
~SparseArray()=default
iterator begin()
Getter for the begin iterator.
Definition: SparseArray.hpp:74
constIterator begin() const
Getter for the begin iterator.
Definition: SparseArray.hpp:77
valueType & referenceType
Reference to valueType.
Definition: SparseArray.hpp:24
SparseArray & operator=(const SparseArray &sparseArray)
Overload for the copy assignement operator.
Definition: SparseArray.hpp:49
referenceType insert_at(sizeType pos, const Component &component)
Insert a copy of the component at the position specified. Will resize the array if the position is bi...
Definition: SparseArray.hpp:101
typename container::size_type sizeType
Type of the size of the container.
Definition: SparseArray.hpp:31
sizeType getIndex(const valueType &val) const
Getter for the index of the component given. The component needs to be in the array.
Definition: SparseArray.hpp:153
iterator end()
Getter for the end iterator.
Definition: SparseArray.hpp:84
SparseArray()
SparseArray's constructor.
Definition: SparseArray.hpp:39
typename container::iterator iterator
Type of the container's iterator.
Definition: SparseArray.hpp:34
referenceType insert_at(sizeType pos, Component &&component)
Insert the component at the position specified, the component will be moved in. Will resize the array...
Definition: SparseArray.hpp:113
constIterator cbegin() const
Getter for the begin iterator.
Definition: SparseArray.hpp:80
std::vector< valueType > container
Type of the container, which is a a vector storing a valueType.
Definition: SparseArray.hpp:28
constIterator end() const
Getter for the end iterator.
Definition: SparseArray.hpp:87
sizeType size() const
Getter for the array's size.
Definition: SparseArray.hpp:94
void resize(const std::size_t &count)
Resize the array.
Definition: SparseArray.hpp:164
std::optional< Component > valueType
Type of the array wrap in an optional.
Definition: SparseArray.hpp:22
typename container::const_iterator constIterator
Constant type of the container's iterator.
Definition: SparseArray.hpp:36
referenceType emplace_at(sizeType pos, Params &&...params)
Creates components at the specified positions.
Definition: SparseArray.hpp:135
referenceType operator[](std::size_t idx)
Overload for the bracket operator.
Definition: SparseArray.hpp:66
referenceType emplace_at(sizeType pos)
Creates the component at the specified position.
Definition: SparseArray.hpp:124
SparseArray(const SparseArray &sparseArray)
Copy constructor for the SparseArray.
Definition: SparseArray.hpp:42
const valueType & constReferenceType
Constant reference to valueType.
Definition: SparseArray.hpp:26
constReferenceType operator[](std::size_t idx) const
Overload for the bracket operator.
Definition: SparseArray.hpp:70
SparseArray & operator=(SparseArray &&sparseArray) noexcept
Overload for the move assignment operator.
Definition: SparseArray.hpp:57
constIterator cend() const
Getter for the end iterator.
Definition: SparseArray.hpp:90