6 # ifndef ALEPH_VECTOR_H
7 # define ALEPH_VECTOR_H
9 # include <tpl_dynArray.H>
10 # include <ahIterator.H>
11 # include <ah_stdc++_utils.H>
78 static const int Invalid_Position = -1;
87 : dyn_array_ptr(&vec.array), current_position(pos)
92 void set_pos(
const size_t &)
94 current_position = this->num_elem;
97 int get_position()
const {
return current_position; }
99 DynArray<T> * get_dyn_array_ptr() {
return dyn_array_ptr; }
103 if (i >= dyn_array_ptr->size())
106 return dyn_array_ptr->access(i);
125 iterator() : dyn_array_ptr(NULL), current_position(Invalid_Position)
132 : dyn_array_ptr(itor.dyn_array_ptr),
133 current_position(itor.current_position)
144 dyn_array_ptr = itor.dyn_array_ptr;
145 current_position = itor.current_position;
153 current_position = index;
155 return access(index);
162 access(current_position) = key;
170 return dyn_array_ptr->access(current_position);
176 return & dyn_array_ptr->access(current_position);
216 current_position += n;
223 current_position -= n;
231 return current_position < itor.current_position;
238 return current_position <= itor.current_position;
245 return current_position > itor.current_position;
252 return current_position >= itor.current_position;
259 return current_position == itor.current_position;
266 return current_position != itor.current_position;
273 return current_position - itor.current_position;
282 ret_val.current_position += n;
289 return &array == dyn_array_ptr;
292 bool verify(
const iterator & it)
const
294 return dyn_array_ptr == it.dyn_array_ptr;
310 array.reserve(0, num_elem - 1);
315 template <
typename Itor>
explicit
316 vector (Itor beg,
const Itor &
end) : array(0), num_elem(0)
318 Aleph::verify_iterators(beg, end);
321 array[num_elem++] = *beg++;
323 I(num_elem == array.size());
329 array.reserve(0, num_elem - 1);
332 array.access(i) = value;
341 bool empty()
const {
return num_elem == 0; }
352 if (num < array.size())
355 array.reserve(array.size(), array.size() + num);
361 if (num < array.size())
374 if (num < array.size())
382 for (; num_elem < num; num_elem++)
383 array.access(num_elem) = value;
393 num_elem = c.num_elem;
401 if (num > array.size())
402 array.reserve(0, num - 1);
407 array.access(i) = value;
412 template <
typename Itor>
415 Aleph::verify_iterators(beg, end);
419 array[num_elem++] = *beg++;
426 std::swap(num_elem, c.num_elem);
446 return array.access(idx);
452 return array.access(idx);
459 return array.access(0);
466 return array.access(num_elem - 1);
473 return iterator (*
this);
480 return iterator (*
this, num_elem);
485 void open_gap(
const size_t & position,
const size_type & gap_len = 1)
487 const size_type old_size = array.size();
488 array.reserve(old_size, old_size + gap_len - 1);
490 if (position >= old_size)
493 const size_t final_pos = position + gap_len;
494 for (
int i = array.size() - 1; i > final_pos; i--)
495 array.access(i) = array.access(i - gap_len);
501 iterator
insert(
const iterator & pos,
const T & value)
503 Aleph::verify_container_and_iterator(array, pos);
505 open_gap(pos.get_position());
507 array.access(pos.get_position()) = value;
518 Aleph::verify_container_and_iterator(array, pos);
520 open_gap(pos.get_position(), len);
522 for (
int i = 0; i < len; i++, pos++, num_elem++)
523 array.access(i) = value;
528 template <
class Itor>
529 void insert(
const iterator & pos, Itor beg,
const Itor &
end)
531 Aleph::verify_container_and_iterators(array, pos, beg, end);
535 open_gap(pos.get_position(), gap_len);
539 for (
int i = pos.get_position(); gap_len > 0; i++, gap_len--)
540 array.access(i) = *beg++;
546 array[num_elem] = value;
552 void close_gap(
const size_t & position,
const size_type & len = 1)
554 for (
int i = position; i < num_elem - len; i++)
555 array.access(i) = array.access(i + len);
563 iterator
erase (
const iterator & pos)
565 Aleph::verify_container_and_iterator(array, pos);
567 close_gap(pos.get_position());
569 return iterator(*
this, pos.get_position());
575 iterator
erase (
const iterator & beg,
const iterator &
end)
577 Aleph::verify_container_and_iterators(array, beg, end);
579 const size_t gap_last =
580 end.get_position() <= num_elem ? end.get_position() : num_elem;
581 const size_t gap_start = beg.get_position();
582 const size_t gap_len = gap_last - gap_start;
584 if (gap_start > gap_last)
585 return iterator(*
this, num_elem);
587 close_gap(gap_start, gap_len);
589 return iterator(*
this, gap_start);
605 if (num_elem != r.num_elem)
608 const size_t len = std::min( num_elem, r.num_elem);
610 for (
size_t i = 0; i < len; i++)
611 if (array.access(i) != r.array.access(i))
620 return not (*
this == r);
630 const bool l_smaller = num_elem < r.num_elem;
632 const size_t len = l_smaller ? num_elem : r.num_elem;
634 for (
size_t i = 0; i < len; i++)
635 if (array.access(i) < r.array.access(i))
637 else if (r.array.access(i) < array.access(i))
655 return not (r > *
this);
663 return not (*
this < r);
669 template <
typename T>
678 # endif // ALEPH_VECTOR_H
iterator operator+=(const size_type &n)
Avanza el iterador n posiciones hacia delante.
Definition: Vector.H:214
vector< T >::reference reference
Tipo referencia a un elemento dentro del vector.
Definition: Vector.H:122
iterator insert(const iterator &pos, const T &value)
Inserta value en la posición dada por el iterador pos.
Definition: Vector.H:501
vector(const vector &c)
Instancia un vector copia de v.
Definition: Vector.H:302
iterator erase(const iterator &pos)
Borra el elemento de la posición pos.
Definition: Vector.H:563
size_type max_size() const
Retorna la máxima dimensión que puede tener el vector.
Definition: Vector.H:344
T value_type
El tipo de dato que almacena el vector.
Definition: Vector.H:51
bool operator>(const iterator &itor) const
Definition: Vector.H:243
bool operator!=(const vector &r) const
Retorna true si this es distinto que el vector r.
Definition: Vector.H:618
void pop_back()
Elimina el elemento situado al final del vector.
Definition: Vector.H:593
iterator operator++()
Definition: Vector.H:181
bool operator<=(const iterator &itor) const
Definition: Vector.H:236
const size_type & size() const
Retorna el número de elementos que tiene el vector.
Definition: Vector.H:338
vector::value_type & reference
Tipo referencia a una entrada del vector.
Definition: Vector.H:54
bool operator==(const vector &r) const
Definition: Vector.H:600
void clear()
Elimina todos los elementos del vector.
Definition: Vector.H:596
T & operator*()
Retorna una referencia al elemento actual del iterador.
Definition: Vector.H:168
void assign(const size_type &num, const T &value)
Asigna al vector num entradas con valor value.
Definition: Vector.H:399
iterator operator+(const int &n) const
Definition: Vector.H:278
iterator operator--()
Definition: Vector.H:198
bool empty() const
Retorna true si el vector está vacío.
Definition: Vector.H:341
void assign(Itor beg, const Itor &end)
Definition: Vector.H:413
size_t size_type
Tipo numérico utilizados para tamaños e índices del vector.
Definition: Vector.H:60
T & operator[](const size_type &index)
Acceso por posición; es equivalente al mismo acceso en el vector.
Definition: Vector.H:151
Definition: ahIterator.H:8
bool operator<(const iterator &itor) const
Definition: Vector.H:229
vector()
Instancia un vector vacío.
Definition: Vector.H:299
T * operator->()
Dereferencia el elemento actual del iterador.
Definition: Vector.H:174
iterator()
Instancia un iterador sin contenedor asociado.
Definition: Vector.H:125
int difference_type
Definition: Vector.H:116
vector< T >::value_type value_type
El tipo de dato que almacena el vector.
Definition: Vector.H:112
void resize(const size_type &num)
Reajusta la capacidad del vector a num entradas.
Definition: Vector.H:359
void resize(const size_type &num, const T &value)
Definition: Vector.H:372
void insert(iterator pos, const size_type &len, const T &value)
Definition: Vector.H:516
bool operator>=(const vector &r) const
Definition: Vector.H:661
vector(Itor beg, const Itor &end)
Definition: Vector.H:316
bool operator<=(const vector &r) const
Definition: Vector.H:653
void reserve(const size_type &num)
Aumenta la capacidad del vector en num entradas.
Definition: Vector.H:350
reference back() const
Definition: Vector.H:464
iterator erase(const iterator &beg, const iterator &end)
Definition: Vector.H:575
iterator begin() const
Definition: Vector.H:471
iterator operator-=(const size_type &n)
Retrocede el iterador n posiciones hacia atrás.
Definition: Vector.H:221
bool operator==(const iterator &itor) const
Definition: Vector.H:257
iterator(const iterator &itor)
Instancia un iterador copia de otro iterador.
Definition: Vector.H:131
size_type capacity() const
Retorna la capacidad (dimension actual) del vector.
Definition: Vector.H:347
Definition: tpl_dynArray.H:70
int operator-(const iterator &itor) const
Definition: Vector.H:271
reference front() const
Definition: Vector.H:457
iterator & operator=(const iterator &itor)
Asigna un iterador.
Definition: Vector.H:139
reference at(const size_type &idx)
Retorna una referencia al elemento idx del vector.
Definition: Vector.H:431
bool operator<(const vector &r) const
Definition: Vector.H:625
void swap(vector &c)
Definition: Vector.H:424
vector(const size_type &num, const T &value)
Instancia un nuevo vector de num elementos de valor value.
Definition: Vector.H:327
vector(const size_type &num)
Instancia un vector con capacidad inicial de num.
Definition: Vector.H:308
void insert(const iterator &pos, Itor beg, const Itor &end)
Definition: Vector.H:529
bool operator>(const vector &r) const
Definition: Vector.H:645
void push_back(const T &value)
Inserta value al final del vector.
Definition: Vector.H:544
const vector::value_type & const_reference
Tipo referencia constante a una entrada del vector.
Definition: Vector.H:57
reference operator[](const size_type &idx)
Accede a la entrada idx.
Definition: Vector.H:444
iterator end() const
Definition: Vector.H:478
bool operator!=(const iterator &itor) const
Definition: Vector.H:264
const_reference at(const size_type &idx) const
Definition: Vector.H:438
vector & operator=(const vector &c)
Asigna un vector.
Definition: Vector.H:387
vector< T >::value_type * pointer
Tipo puntero a entrada dentro del vector.
Definition: Vector.H:119
bool operator>=(const iterator &itor) const
Definition: Vector.H:250