-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwrapperVector.hpp
More file actions
166 lines (144 loc) · 3.56 KB
/
wrapperVector.hpp
File metadata and controls
166 lines (144 loc) · 3.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
//
// Created by gvalenzuela on 3/20/25.
//
#ifndef WRAPPERVECTOR_HPP
#define WRAPPERVECTOR_HPP
#include <algorithm>
#include <cstddef>
#include <iostream>
#include <ostream>
#include <utility>
auto constexpr INIT_CAPACITY {100}; //< Capacidad inicial del vector
/**
* @brief: Clase que hace una implementación custom del vector STL usando
* arrays.
* @tparam TData: Tipo de dato que se almacenará en el vector. Este tipo de dato
* es genérico.
*/
template<typename TData>
class wrapperVector
{
private:
TData* m_data; //< Puntero a los datos
size_t m_size; //< Cantidad de elementos en el vector
size_t m_capacity; //< Capacidad del vector
/**
* @brief: Cuando no haya espacio para mas elementos, debemos aumentar la
* capacidad del vector.
*/
void resize()
{
auto newCapacity = (m_capacity == 0) ? INIT_CAPACITY : m_capacity * 2;
auto newData = new TData[newCapacity];
for (size_t i = 0; i < m_size; i++)
{
newData[i] = std::move(m_data[i]);
}
delete[] m_data;
m_data = newData;
m_capacity = newCapacity; // <-- Faltaba esta linea
}
public:
/**
* @brief: Constructor por defecto
*/
wrapperVector()
: m_data {nullptr}
, m_size {0}
, m_capacity {0}
{
}
/**
* @brief: Destructor
*/
~wrapperVector()
{
delete[] m_data;
}
/**
* @brief: Agregar un elemento al final del vector
*/
void push_back(const TData& data)
{
if (m_size == m_capacity)
{
resize();
}
m_data[m_size++] = data;
}
/**
* @brief: Agregar un elemento al final del vector. Usamos el operador de
* mover, para evitar copias.
*/
void push_back(TData&& data)
{
if (m_size == m_capacity)
{
resize();
}
m_data[m_size++] = std::move(data);
}
/**
* @brief: Obtener el tamaño del vector
* @note: [[nodiscard]] es un atributo que indica que el valor de retorno de la función no debe ser ignorado.
*/
[[nodiscard]] size_t size() const
{
return m_size;
}
/**
* @brief: Obtener la capacidad del vector
* @note: [[nodiscard]] es un atributo que indica que el valor de retorno de la función no debe ser ignorado.
*/
[[nodiscard]] size_t capacity() const
{
return m_capacity;
}
/**
* @brief: Obtener un elemento del vector
*/
TData& operator[](size_t index)
{
if (index >= m_size)
{
std::cout << "Indice fuera de limites" << std::endl;
}
return m_data[index];
}
/**
* @brief: Obtener un elemento del vector
*/
const TData& at(size_t index) const
{
if (index >= m_size)
{
std::cout << "Indice fuera de limites" << std::endl;
}
return m_data[index];
}
/**
* @brief: Realiza un swap de los datos de dos vectores
* @note: noexcept es una especificación que indica que la función no lanzará excepciones.
*/
void swap(wrapperVector& other) noexcept
{
std::swap(m_data, other.m_data);
std::swap(m_size, other.m_size);
std::swap(m_capacity, other.m_capacity);
}
/**
* @brief: Iterador básico (primer elemento)
*/
TData* begin()
{
return m_data;
}
/**
* @brief: Iterador básico (último elemento)
*/
TData* end()
{
return m_data + m_size;
}
};
#endif // WRAPPERVECTOR_HPP