Mountain  1.0.0
Simple C++ 2D Game Framework
list.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <functional>
4 #include <vector>
5 
6 #include "Mountain/core.hpp"
7 
10 
11 namespace Mountain
12 {
23  template <typename T>
24  class List
25  {
26  public:
27  using Iterator = typename std::vector<T>::iterator;
28  using ConstIterator = typename std::vector<T>::const_iterator;
29  using ReverseIterator = typename std::vector<T>::reverse_iterator;
30  using ConstReverseIterator = typename std::vector<T>::const_reverse_iterator;
31 
33  using Type = T;
34 
36  List() = default;
37 
41  explicit List(size_t size);
42 
47  explicit List(size_t size, const T& defaultValue);
48 
53  explicit List(size_t size, const T* values);
54 
59  template <size_t Size>
60  explicit List(const std::array<T, Size>& array);
61 
65  explicit List(const std::vector<T>& vector);
66 
70  explicit List(std::vector<T>&& vector);
71 
73  explicit List(Iterator b, Iterator e);
74 
78  List(const std::initializer_list<T>& values);
79 
81  ~List() = default;
82 
83  DEFAULT_COPY_MOVE_OPERATIONS(List)
84 
85 
86  void Resize(size_t size);
89 
91  void Clear();
92 
94  [[nodiscard]]
95  bool_t Empty() const;
96 
98  T& Add();
99 
103  void Add(const T& element);
104 
108  void Add(T&& element);
109 
114  void AddRange(const T* data, size_t count);
115 
119  void AddRange(const std::initializer_list<T>& values);
120 
124  void Fill(const T& value);
125 
129  void Fill(T&& value);
130 
135  template <class... Args>
136  void Emplace(Args&&... args);
137 
139  void PopBack();
140 
146  void Insert(size_t index);
147 
152  void Insert(const T& element, size_t index);
153 
158  void Insert(T&& element, size_t index);
159 
163  void Remove(const T& element);
164 
168  void RemoveAt(size_t index);
169 
173  void RemoveAt(ConstIterator iterator);
174 
179  void RemoveRange(size_t start, size_t end);
180 
185  [[nodiscard]]
186  bool_t Contains(const T& element) const;
187 
193  void Iterate(const std::function<void(T*)>& lambda);
194 
200  void Iterate(const std::function<void(const T*)>& lambda) const;
201 
208  [[nodiscard]]
209  bool_t Exists(const std::function<bool_t(const T*)>& lambda) const;
210 
217  [[nodiscard]]
218  T* Find(const std::function<bool_t(const T*)>& lambda);
219 
226  [[nodiscard]]
227  const T* Find(const std::function<bool_t(const T*)>& lambda) const;
228 
235  [[nodiscard]]
236  List<T*> FindAll(const std::function<bool_t(const T*)>& lambda);
237 
244  [[nodiscard]]
245  List<const T*> FindAll(const std::function<bool_t(const T*)>& lambda) const;
246 
251  [[nodiscard]]
252  size_t FindPosition(const std::function<bool_t(const T*)>& lambda) const;
253 
258  [[nodiscard]]
259  T* Find(const std::function<bool_t(const T*, size_t)>& lambda);
260 
261  void Sort(std::function<bool_t(const T& left, const T& right)> predicate = std::less());
262 
266  [[nodiscard]]
267  T* GetData();
268 
272  [[nodiscard]]
273  const T* GetData() const;
274 
278  [[nodiscard]]
279  size_t GetSize() const;
280 
284  [[nodiscard]]
285  size_t GetCapacity() const;
286 
293  [[nodiscard]]
294  typename std::vector<T>::reference operator[](size_t index);
295 
302  [[nodiscard]]
303  typename std::vector<T>::const_reference operator[](size_t index) const;
304 
305  [[nodiscard]]
306  const T& Front() const;
307 
308  [[nodiscard]]
309  T& Front();
310 
311  [[nodiscard]]
312  const T& Back() const;
313 
314  [[nodiscard]]
315  T& Back();
316 
317  [[nodiscard]]
318  Iterator Begin();
319 
320  [[nodiscard]]
321  Iterator End();
322 
323  [[nodiscard]]
324  ConstIterator CBegin() const;
325 
326  [[nodiscard]]
327  ConstIterator CEnd() const;
328 
329  [[nodiscard]]
330  ReverseIterator RBegin();
331 
332  [[nodiscard]]
333  ReverseIterator REnd();
334 
335  [[nodiscard]]
336  ConstReverseIterator CrBegin() const;
337 
338  [[nodiscard]]
339  ConstReverseIterator CrEnd() const;
340 
343  [[nodiscard]]
344  Iterator begin();
345 
348  [[nodiscard]]
349  ConstIterator begin() const;
350 
353  [[nodiscard]]
354  Iterator end();
355 
358  [[nodiscard]]
359  ConstIterator end() const;
360 
361  private:
362  std::vector<T> m_Vector;
363  };
364 }
365 
366 #include "Mountain/utils/list.inl"
size_t FindPosition(const std::function< bool_t(const T *)> &lambda) const
Tries to find an element that fulfills the requirements provided in a lambda.
bool_t Exists(const std::function< bool_t(const T *)> &lambda) const
Checks if an element exists that fulfills the requirements provided in a lambda.
void RemoveAt(size_t index)
Removes an element from the list at a given index.
bool_t Contains(const T &element) const
Checks if the list contains a specified element.
T & Add()
Adds a default element to the end of the list (calls the default constructor of T) ...
void Fill(const T &value)
Fills the list with a specified value.
size_t GetCapacity() const
Gets the capacity of the list.
A dynamic array implementation. Wrapper around the std::vector class.
Definition: list.hpp:24
void Remove(const T &element)
Removes an element from the list (only removes the first occurence it finds)
List()=default
Creates an empty list with a capacity of 0.
std::vector< T >::reference operator[](size_t index)
Gets an element of the list at a specified index.
T * Find(const std::function< bool_t(const T *)> &lambda)
Tries to find an element that fulfills the requirements provided in a lambda.
void PopBack()
Removes the last element of the list.
void AddRange(const T *data, size_t count)
Adds a range of elements to the end of the list.
void RemoveRange(size_t start, size_t end)
Removes a range of elements from the list.
void Iterate(const std::function< void(T *)> &lambda)
Allows iteration over the list with a lambda.
void Clear()
Clears the list.
bool_t Empty() const
Returns whether the list is empty. This is equivalent to doing GetSize() == 0.
void Emplace(Args &&... args)
Constructs a new element and adds it to the end of the list.
bool_t Type
The type of the List<T>, refers to T.
Definition: list.hpp:33
void Insert(size_t index)
Inserts an element at the given position using the default constructor.
void Resize(size_t size)
Resizes a specified amount of elements in the list.
size_t GetSize() const
Gets the size of the list.
List< T * > FindAll(const std::function< bool_t(const T *)> &lambda)
Tries to find all elements that fulfill the requirements provided in a lambda.
~List()=default
Destroys the list.
T * GetData()
Gets the underlying pointer to the list.
Contains all declarations of the Mountain Framework.
Definition: audio.hpp:22