Wildvine Engine
Referencia Doxygen del codigo propio de Wildvine Engine.
Cargando...
Buscando...
Nada coincide
TSet.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#pragma once
35
36namespace EU {
46 template<typename T>
47 class TSet
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:
76 : Data(nullptr), Capacity(0), Size(0)
77 {
78 }
79
84 {
85 delete[] Data;
86 }
87
93 void Add(const T& Element)
94 {
95 if (Contains(Element))
96 {
97 return;
98 }
99 if (Size == Capacity)
100 {
101 Resize(Capacity == 0 ? 1 : Capacity * 2);
102 }
103 Data[Size++] = Element;
104 }
105
111 void Remove(const T& Element)
112 {
113 for (size_t i = 0; i < Size; ++i)
114 {
115 if (Data[i] == Element)
116 {
117 for (size_t j = i; j < Size - 1; ++j)
118 {
119 Data[j] = Data[j + 1];
120 }
121 --Size;
122 return;
123 }
124 }
125 std::cerr << "Element not found" << std::endl;
126 }
127
135 bool Contains(const T& Element) const
136 {
137 for (size_t i = 0; i < Size; ++i)
138 {
139 if (Data[i] == Element)
140 {
141 return true;
142 }
143 }
144 return false;
145 }
146
152 size_t Num() const
153 {
154 return Size;
155 }
156
162 size_t GetCapacity() const
163 {
164 return Capacity;
165 }
166 };
167
168 // Example
169
170 /*
171 int main()
172 {
173 TSet<int> MySet; ///< Crear una instancia de TSet para elementos enteros.
174 MySet.Add(1); ///< Añadir elementos al conjunto.
175 MySet.Add(2);
176 MySet.Add(3);
177
178 MySet.Remove(2); ///< Eliminar el elemento 2 del conjunto.
179
180 std::cout << "Contains 1: " << MySet.Contains(1) << std::endl; ///< Verificar e imprimir si el conjunto contiene el elemento 1.
181 std::cout << "Contains 2: " << MySet.Contains(2) << std::endl; ///< Verificar e imprimir si el conjunto contiene el elemento 2.
182
183 std::cout << "Size: " << MySet.Num() << ", Capacity: " << MySet.GetCapacity() << std::endl; ///< Imprimir el tamaño y la capacidad del conjunto.
184
185 return 0;
186 }
187 */
188}
189
TSet es una clase de conjunto dinámica para almacenar elementos únicos.
Definition TSet.h:48
size_t Num() const
Devuelve el número de elementos actualmente en el conjunto.
Definition TSet.h:152
~TSet()
Destructor que libera la memoria asignada al conjunto.
Definition TSet.h:83
void Add(const T &Element)
Añade un nuevo elemento al conjunto.
Definition TSet.h:93
size_t Size
Número de elementos actualmente en el conjunto.
Definition TSet.h:52
size_t Capacity
Capacidad actual del conjunto (número de elementos que puede almacenar).
Definition TSet.h:51
TSet()
Constructor por defecto que inicializa el conjunto con capacidad y tamaño cero.
Definition TSet.h:75
T * Data
Puntero a la memoria donde se almacenan los elementos.
Definition TSet.h:50
void Remove(const T &Element)
Elimina el elemento especificado del conjunto.
Definition TSet.h:111
void Resize(size_t NewCapacity)
Redimensiona el conjunto para tener una nueva capacidad.
Definition TSet.h:59
size_t GetCapacity() const
Devuelve la capacidad actual del conjunto.
Definition TSet.h:162
bool Contains(const T &Element) const
Verifica si el conjunto contiene el elemento especificado.
Definition TSet.h:135
Definition Matrix2x2.h:35