Wildvine Engine
Referencia Doxygen del codigo propio de Wildvine Engine.
Cargando...
Buscando...
Nada coincide
TArray.h
Ir a la documentación de este archivo.
1
6/*
7 * MIT License
8 *
9 * Copyright (c) 2024 Roberto Charreton
10 *
11 * Permission is hereby granted, free of charge, to any person obtaining a copy
12 * of this software and associated documentation files (the "Software"), to deal
13 * in the Software without restriction, including without limitation the rights
14 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 * copies of the Software, and to permit persons to whom the Software is
16 * furnished to do so, subject to the following conditions:
17 *
18 * The above copyright notice and this permission notice shall be included in all
19 * copies or substantial portions of the Software.
20 *
21 * In addition, any project or software that uses this library or class must include
22 * the following acknowledgment in the credits:
23 *
24 * "This project uses software developed by Roberto Charreton and Attribute Overload."
25 *
26 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
27 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
28 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
29 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
30 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
31 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
32 * SOFTWARE.
33*/
34
35#pragma once
36namespace EU {
46 template<typename T>
47 class TArray
48 {
49 private:
50 T* Data;
51 size_t Capacity;
52 size_t Size;
53
59 void Resize(size_t NewCapacity)
60 {
61 T* NewData = new T[NewCapacity];
62 for (size_t i = 0; i < Size; ++i)
63 {
64 NewData[i] = Data[i];
65 }
66 delete[] Data;
67 Data = NewData;
68 Capacity = NewCapacity;
69 }
70
71 public:
75 TArray() : Data(nullptr), Capacity(0), Size(0) {}
76
81 delete[] Data;
82 }
83
89 void Add(const T& Element)
90 {
91 if (Size == Capacity)
92 {
93 Resize(Capacity == 0 ? 1 : Capacity * 2);
94 }
95 Data[Size++] = Element;
96 }
97
103 void RemoveAt(size_t Index)
104 {
105 if (Index >= Size)
106 {
107 std::cerr << "Index out of range" << std::endl;
108 return;
109 }
110 for (size_t i = Index; i < Size - 1; ++i)
111 {
112 Data[i] = Data[i + 1];
113 }
114 --Size;
115 }
116
123 T& operator[](size_t Index)
124 {
125 if (Index >= Size)
126 {
127 std::cerr << "Index out of range" << std::endl;
128 exit(1);
129 }
130 return Data[Index];
131 }
132
139 const T& operator[](size_t Index) const
140 {
141 if (Index >= Size)
142 {
143 std::cerr << "Index out of range" << std::endl;
144 exit(1);
145 }
146 return Data[Index];
147 }
148
154 size_t Num() const
155 {
156 return Size;
157 }
158
164 size_t GetCapacity() const
165 {
166 return Capacity;
167 }
168 };
169
170 // EXAMPLE
171
172 /*
173 int main() {
174
175 // TArray Example
176 TArray<int> MyArray;
177 MyArray.Add(1);
178 MyArray.Add(2);
179 MyArray.Add(3);
180 MyArray.Add(4);
181 MyArray.Add(5);
182
183 MyArray.Add(6);
184 MyArray.RemoveAt(2);
185
186 for (size_t i = 0; i < MyArray.Num(); ++i)
187 {
188 std::cout << MyArray[i] << " ";
189 }
190 std::cout << std::endl;
191
192 std::cout << "Size: " << MyArray.Num() << ", Capacity: " << MyArray.GetCapacity() << std::endl;
193
194 return 0;
195 }
196 */
197}
198
TArray es una clase de array dinámica para almacenar elementos de tipo T.
Definition TArray.h:48
size_t Size
Número de elementos actualmente en el array.
Definition TArray.h:52
TArray()
Constructor por defecto que inicializa el array con capacidad y tamaño cero.
Definition TArray.h:75
void RemoveAt(size_t Index)
Elimina el elemento en la posición especificada.
Definition TArray.h:103
const T & operator[](size_t Index) const
Versión constante de la sobrecarga del operador [] para acceder a elementos por índice.
Definition TArray.h:139
~TArray()
Destructor que libera la memoria asignada al array.
Definition TArray.h:80
size_t GetCapacity() const
Devuelve la capacidad actual del array.
Definition TArray.h:164
size_t Capacity
Capacidad actual del array (número de elementos que puede almacenar).
Definition TArray.h:51
T & operator[](size_t Index)
Sobrecarga del operador [] para acceder a elementos por índice.
Definition TArray.h:123
T * Data
Puntero a la memoria donde se almacenan los elementos del array.
Definition TArray.h:50
void Add(const T &Element)
Añade un nuevo elemento al final del array.
Definition TArray.h:89
void Resize(size_t NewCapacity)
Redimensiona el array para tener una nueva capacidad.
Definition TArray.h:59
size_t Num() const
Devuelve el número de elementos actualmente en el array.
Definition TArray.h:154
Definition Matrix2x2.h:35