SleakEngine 0.1.0
C++23 multi-backend game engine
Loading...
Searching...
No Matches
List.hpp
Go to the documentation of this file.
1#ifndef _LIST_H_
2#define _LIST_H_
3
5#include <algorithm> // For std::swap
6#include <functional>
7#include <memory>
8
9namespace Sleak {
10/**
11 * @class List
12 * @brief Implements a dynamic array-like list for storing and managing a
13 * collection of elements.
14 *
15 * The List class provides a flexible and efficient way to store and access
16 * elements in a linear order. It is suitable for scenarios where you need to
17 * dynamically resize the collection or access elements by index.
18 * @ingroup utility
19 */
20template <typename T>
21class List {
22 private:
23 T* data = nullptr;
24 size_t size = 0;
25 size_t capacity = 0;
26
27 public:
28 List() noexcept = default;
29
30 /// Adopts an existing buffer directly; List takes ownership.
31 List(T* Data, size_t Size) : data(Data), size(Size), capacity(Size) {}
32
33 /// Copies other's elements into an exact-size buffer; empty allocates none.
34 List(const List& other) {
35 if (other.size == 0) return;
36 std::unique_ptr<T[]> copy(new T[other.size]);
37 std::copy(other.data, other.data + other.size, copy.get());
38 data = copy.release();
39 size = other.size;
40 capacity = other.size;
41 }
42
43 // Move constructor
44 List(List&& other) noexcept
45 : data(other.data), size(other.size), capacity(other.capacity) {
46 other.data = nullptr;
47 other.size = 0;
48 other.capacity = 0;
49 }
50
51 /// Replaces contents with a copy of other; strong exception guarantee.
52 List& operator=(const List& other) {
53 if (this != &other) {
54 List copy(other);
55 swap(copy);
56 }
57 return *this;
58 }
59
60 // Move assignment
61 List& operator=(List&& other) noexcept {
62 if (this != &other) {
63 delete[] data;
64 data = other.data;
65 size = other.size;
66 capacity = other.capacity;
67 other.data = nullptr;
68 other.size = 0;
69 other.capacity = 0;
70 }
71 return *this;
72 }
73
74 List(std::initializer_list<T> initList) {
75 for (const T& value : initList) {
76 add(value);
77 }
78 }
79
80 /// Frees the backing array.
81 ~List() { delete[] data; }
82
83 // Element addition
84 void add(const T& value) {
85 if (size == capacity) resize();
86 data[size++] = value;
87 }
88
89 void add(T&& value) {
90 if (size == capacity) resize();
91 data[size++] = std::move(value);
92 }
93
94 void add(std::initializer_list<T> AddList) {
95 for (auto&& item : AddList) {
96 add(std::move(item));
97 }
98 }
99
100 // Element access
101 T& operator[](size_t index) {
102 if (index >= size) throw Sleak::IndexOutOfBoundsException();
103 return data[index];
104 }
105
106 const T& operator[](size_t index) const {
107 if (index >= size) throw Sleak::IndexOutOfBoundsException();
108 return data[index];
109 }
110
111 T& at(size_t index) {
112 if (index >= size) throw Sleak::IndexOutOfBoundsException();
113 return data[index];
114 }
115
116 const T& at(size_t index) const {
117 if (index >= size) throw Sleak::IndexOutOfBoundsException();
118 return data[index];
119 }
120
121 // Capacity
122 size_t GetSize() const { return size; }
123 size_t GetByteSize() const { return size * sizeof(T); }
124 size_t GetCapacity() const { return capacity; }
125 bool empty() const { return size == 0; }
126
127 // Data access
128 T* GetData() { return data; }
129 const T* GetData() const { return data; }
130 void* GetRawData() { return static_cast<void*>(data); }
131 const void* GetRawData() const { return static_cast<const void*>(data); }
132
133 // Iterators
134 T* begin() { return data; }
135 const T* begin() const { return data; }
136 T* end() { return data + size; }
137 const T* end() const { return data + size; }
138
139 // Modifiers
140 void clear() {
141 // Reset each live slot to a default-constructed T so that any
142 // resources the elements own (e.g. RefPtr refcounts) are released.
143 // Setting size=0 alone leaks everything until the backing array
144 // is reallocated or destroyed.
145 for (size_t i = 0; i < size; ++i) {
146 data[i] = T();
147 }
148 size = 0;
149 }
150
151 void release() {
152 delete[] data;
153 data = nullptr;
154 size = 0;
155 capacity = 0;
156 }
157
158 void resize() {
159 size_t newCapacity = (capacity == 0) ? 1 : capacity * 2;
160 resize(newCapacity);
161 }
162
163 void resize(size_t newCapacity) {
164 if (newCapacity <= capacity) return;
165
166 T* newData = new T[newCapacity];
167 for (size_t i = 0; i < size; ++i) {
168 newData[i] = std::move(data[i]);
169 }
170 delete[] data;
171 data = newData;
172 capacity = newCapacity;
173 }
174
175 void insert(size_t index, const T& value) {
176 if (index > size) throw Sleak::IndexOutOfBoundsException();
177 if (size == capacity) resize();
178
179 for (size_t i = size; i > index; --i) {
180 data[i] = data[i - 1];
181 }
182 data[index] = value;
183 ++size;
184 }
185
186 void erase(size_t index) {
187 if (index >= size) throw Sleak::IndexOutOfBoundsException();
188
189 for (size_t i = index; i < size - 1; ++i) {
190 data[i] = std::move(data[i + 1]);
191 }
192 // Release the now-unused tail slot; otherwise it keeps owning
193 // the last element (RefPtrs pin their pointees forever).
194 data[size - 1] = T();
195 --size;
196 }
197
198 // Operations
199 /// Sorts in place; compare must be a strict weak ordering.
200 template <typename Compare>
201 void sort(Compare compare) {
202 std::sort(data, data + size, compare);
203 }
204
205 void reverse() {
206 for (size_t i = 0; i < size / 2; ++i) {
207 std::swap(data[i], data[size - 1 - i]);
208 }
209 }
210
211 void swap(List& other) noexcept {
212 std::swap(data, other.data);
213 std::swap(size, other.size);
214 std::swap(capacity, other.capacity);
215 }
216
217 // Search
218 T* find(std::function<bool(const T&)> predicate) {
219 for (size_t i = 0; i < size; ++i) {
220 if (predicate(data[i])) return &data[i];
221 }
222 return nullptr;
223 }
224
225 const T* find(std::function<bool(const T&)> predicate) const {
226 for (size_t i = 0; i < size; ++i) {
227 if (predicate(data[i])) return &data[i];
228 }
229 return nullptr;
230 }
231
232 int32_t indexOf(const T& value) {
233 for(int i = 0; i < size; i++)
234 if(data[i] == value)
235 return i;
236
237 return -1;
238 }
239};
240} // namespace Sleak
241
242#endif // _LIST_H_
void * GetRawData()
Definition List.hpp:130
T * end()
Definition List.hpp:136
void clear()
Definition List.hpp:140
void reverse()
Definition List.hpp:205
const void * GetRawData() const
Definition List.hpp:131
T * GetData()
Definition List.hpp:128
const T * GetData() const
Definition List.hpp:129
const T * end() const
Definition List.hpp:137
void add(std::initializer_list< T > AddList)
Definition List.hpp:94
const T * begin() const
Definition List.hpp:135
void sort(Compare compare)
Sorts in place; compare must be a strict weak ordering.
Definition List.hpp:201
T * find(std::function< bool(const T &)> predicate)
Definition List.hpp:218
void add(const IndexType &value)
Definition List.hpp:84
T * begin()
Definition List.hpp:134
size_t GetByteSize() const
Definition List.hpp:123
List(const List &other)
Copies other's elements into an exact-size buffer; empty allocates none.
Definition List.hpp:34
~List()
Frees the backing array.
Definition List.hpp:81
bool empty() const
Definition List.hpp:125
const T & at(size_t index) const
Definition List.hpp:116
void release()
Definition List.hpp:151
size_t GetSize() const
Definition List.hpp:122
void resize(size_t newCapacity)
Definition List.hpp:163
T & operator[](size_t index)
Definition List.hpp:101
List(List &&other) noexcept
Definition List.hpp:44
List() noexcept=default
List & operator=(List &&other) noexcept
Definition List.hpp:61
void erase(size_t index)
Definition List.hpp:186
List & operator=(const List &other)
Replaces contents with a copy of other; strong exception guarantee.
Definition List.hpp:52
const T * find(std::function< bool(const T &)> predicate) const
Definition List.hpp:225
T & at(size_t index)
Definition List.hpp:111
void swap(List &other) noexcept
Definition List.hpp:211
const T & operator[](size_t index) const
Definition List.hpp:106
void insert(size_t index, const T &value)
Definition List.hpp:175
void add(T &&value)
Definition List.hpp:89
List(std::initializer_list< T > initList)
Definition List.hpp:74
size_t GetCapacity() const
Definition List.hpp:124
int32_t indexOf(const T &value)
Definition List.hpp:232
Root namespace for everything the engine exposes.
Definition Camera.hpp:10