1 # ifndef TPL_MEMARRAY_H
2 # define TPL_MEMARRAY_H
24 size_t contract_threshold;
29 contract_threshold = dim / 4;
33 bool expand(
const size_t first = 0)
38 const size_t newsz = dim << 1;
39 T * new_ptr =
new T [newsz];
40 for (
int i = 0; i < dim; ++i)
41 std::swap(ptr[(i + first) % dim ], new_ptr[i]);
45 contract_threshold = dim/4;
51 bool contract(
const size_t first = 0)
53 if (n > contract_threshold)
56 const size_t newsz = dim >> 1;
58 T * new_ptr =
new T [newsz];
60 for (
int i = 0; i < newsz; ++i)
61 std::swap(ptr[(first + i) % dim], new_ptr[i]);
63 for (
int i = newsz; i < dim; ++i)
69 contract_threshold = dim/4;
75 static size_t init_dim(
const size_t dim)
77 if (dim == 0 or ((dim > 0) and (dim & (dim - 1)) == 0))
80 return 1 << (int) (log2(dim) + 1);
86 const size_t &
capacity()
const {
return dim; }
88 const size_t & size()
const {
return n; }
90 bool is_empty()
const {
return n == 0; }
105 : ptr(NULL), dim(init_dim(__dim)), n(0)
121 std::swap(ptr, a.ptr);
122 std::swap(dim, a.dim);
124 std::swap(contract_threshold, a.contract_threshold);
128 : ptr(NULL), dim(a.dim), n(a.n)
131 for (
int i = 0; i < dim; ++i)
136 : ptr(NULL), dim(0), n(0), contract_threshold(0)
146 T * newptr =
new T [a.n];
147 for (
int i = 0; i < a.n; ++i)
148 newptr[i] = a.ptr[i];
178 T & ret = ptr[n++] = item;
186 T & ret = ptr[n++] = std::move(item);
190 T &
put(
const size_t i,
const T & item)
193 throw std::range_error(
"index out of range");
195 return ptr[i] = item;
198 T &
put(
const size_t i, T && item)
201 throw std::range_error(
"index out of range");
203 return ptr[i] = std::move(item);
218 throw std::underflow_error(
"Deleted more entries than capacity");
221 T ret = std::move(ptr[n]);
228 T & last() {
return ptr[n - 1]; }
230 T & first() {
return ptr[0]; }
232 T & access(
const size_t i)
237 const T & last()
const {
return ptr[n - 1]; }
239 const T & first()
const {
return ptr[0]; }
241 const T & access(
const size_t i)
const
255 throw std::range_error(
"access out of range");
260 T & operator () (
const size_t i)
266 const T & operator () (
const size_t i)
const
272 template <
class Operation>
273 bool traverse(Operation & operation)
275 for (
int i = 0; i < n; i++)
276 if (not operation(ptr[i]))
282 template <
class Operation>
283 bool traverse(Operation & operation)
const
285 return const_cast<MemArray*
>(
this)->traverse(operation);
288 template <
class Operation>
289 bool traverse(Operation && operation = Operation())
const
291 return traverse<Operation>(operation);
294 template <
class Operation>
295 bool traverse(Operation && operation = Operation())
297 return traverse<Operation>(operation);
300 Functional_Methods(T);
306 # endif // TPL_MEMARRAY_H
Definition: tpl_memArray.H:17
const size_t & capacity() const
Retorna la capacidad del arreglo.
Definition: tpl_memArray.H:86
void putn(const size_t more)
inserta entradas para el arreglo.
Definition: tpl_memArray.H:207
T & put(const T &item)
Coloca item al final de arreglo. Se expande si se alcanza el final.
Definition: tpl_memArray.H:174
MemArray(size_t __dim=8)
Definition: tpl_memArray.H:104
T & operator[](const size_t i)
acceso a la i-ésima entrada válida
Definition: tpl_memArray.H:247