Wildvine Engine
Referencia Doxygen del codigo propio de Wildvine Engine.
Cargando...
Buscando...
Nada coincide
TMap.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
35namespace EU {
46 template<typename K, typename V>
47 class TMap
48 {
49 private:
50 struct Pair
51 {
52 K Key;
54
55 Pair() : Key(), Value() {}
56 Pair(const K& Key, const V& Value) : Key(Key), Value(Value) {}
57 };
58
60 size_t Capacity;
61 size_t Size;
62
68 void Resize(size_t NewCapacity)
69 {
70 Pair* NewData = new Pair[NewCapacity];
71 for (size_t i = 0; i < Size; ++i)
72 {
73 NewData[i] = Data[i];
74 }
75 delete[] Data;
76 Data = NewData;
77 Capacity = NewCapacity;
78 }
79
80 public:
85 : Data(nullptr), Capacity(0), Size(0)
86 {
87 }
88
93 {
94 delete[] Data;
95 }
96
103 void Add(const K& Key, const V& Value)
104 {
105 for (size_t i = 0; i < Size; ++i)
106 {
107 if (Data[i].Key == Key)
108 {
109 Data[i].Value = Value;
110 return;
111 }
112 }
113 if (Size == Capacity)
114 {
115 Resize(Capacity == 0 ? 1 : Capacity * 2);
116 }
117 Data[Size++] = Pair(Key, Value);
118 }
119
125 void Remove(const K& Key)
126 {
127 for (size_t i = 0; i < Size; ++i)
128 {
129 if (Data[i].Key == Key)
130 {
131 for (size_t j = i; j < Size - 1; ++j)
132 {
133 Data[j] = Data[j + 1];
134 }
135 --Size;
136 return;
137 }
138 }
139 std::cerr << "Key not found" << std::endl;
140 }
141
148 V& operator[](const K& Key)
149 {
150 for (size_t i = 0; i < Size; ++i)
151 {
152 if (Data[i].Key == Key)
153 {
154 return Data[i].Value;
155 }
156 }
157 std::cerr << "Key not found" << std::endl;
158 exit(1);
159 }
160
167 const V& operator[](const K& Key) const
168 {
169 for (size_t i = 0; i < Size; ++i)
170 {
171 if (Data[i].Key == Key)
172 {
173 return Data[i].Value;
174 }
175 }
176 std::cerr << "Key not found" << std::endl;
177 exit(1);
178 }
179
185 size_t Num() const
186 {
187 return Size;
188 }
189
195 size_t GetCapacity() const
196 {
197 return Capacity;
198 }
199 };
200
201 // EXAMPLE
202
203 /*
204 int main()
205 {
206 TMap<int, std::string> MyMap; ///< Crear una instancia de TMap para claves enteras y valores string.
207 MyMap.Add(1, "One"); ///< Añadir pares clave-valor al mapa.
208 MyMap.Add(2, "Two");
209 MyMap.Add(3, "Three");
210
211 MyMap.Remove(2); ///< Eliminar el par con clave 2.
212
213 std::cout << "Key 1: " << MyMap[1] << std::endl; ///< Acceder e imprimir el valor asociado con la clave 1.
214 std::cout << "Key 3: " << MyMap[3] << std::endl; ///< Acceder e imprimir el valor asociado con la clave 3.
215
216 std::cout << "Size: " << MyMap.Num() << ", Capacity: " << MyMap.GetCapacity() << std::endl; ///< Imprimir el tamaño y la capacidad del mapa.
217
218 return 0;
219 }
220 */
221}
222
TMap es una clase de mapa (diccionario) dinámica para almacenar pares clave-valor.
Definition TMap.h:48
~TMap()
Destructor que libera la memoria asignada al mapa.
Definition TMap.h:92
TMap()
Constructor por defecto que inicializa el mapa con capacidad y tamaño cero.
Definition TMap.h:84
size_t Size
Número de pares actualmente en el mapa.
Definition TMap.h:61
size_t Num() const
Devuelve el número de pares actualmente en el mapa.
Definition TMap.h:185
void Resize(size_t NewCapacity)
Redimensiona el mapa para tener una nueva capacidad.
Definition TMap.h:68
void Remove(const K &Key)
Elimina el par clave-valor en la posición especificada.
Definition TMap.h:125
const V & operator[](const K &Key) const
Versión constante de la sobrecarga del operador [] para acceder a valores por clave.
Definition TMap.h:167
size_t Capacity
Capacidad actual del mapa (número de pares que puede almacenar).
Definition TMap.h:60
Pair * Data
Puntero a la memoria donde se almacenan los pares clave-valor.
Definition TMap.h:59
void Add(const K &Key, const V &Value)
Añade un nuevo par clave-valor al mapa.
Definition TMap.h:103
V & operator[](const K &Key)
Sobrecarga del operador [] para acceder a valores por clave.
Definition TMap.h:148
size_t GetCapacity() const
Devuelve la capacidad actual del mapa.
Definition TMap.h:195
Definition Matrix2x2.h:35
Pair(const K &Key, const V &Value)
Definition TMap.h:56