Aleph-w  1.9
General library for algorithms and data structures
tpl_dynSlist.H
1 
2 /* Aleph-w
3 
4  / \ | | ___ _ __ | |__ __ __
5  / _ \ | |/ _ \ '_ \| '_ \ ____\ \ /\ / / Data structures & Algorithms
6  / ___ \| | __/ |_) | | | |_____\ V V / version 1.9b
7  /_/ \_\_|\___| .__/|_| |_| \_/\_/ https://github.com/lrleon/Aleph-w
8  |_|
9 
10  This file is part of Aleph-w library
11 
12  Copyright (c) 2002-2018 Leandro Rabindranath Leon & Alejandro Mujica
13 
14  This program is free software: you can redistribute it and/or modify
15  it under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  This program is distributed in the hope that it will be useful, but
20  WITHOUT ANY WARRANTY; without even the implied warranty of
21  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22  General Public License for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with this program. If not, see <https://www.gnu.org/licenses/>.
26 */
27 
28 # ifndef TPL_DYNSLIST_H
29 # define TPL_DYNSLIST_H
30 
31 # include <tpl_slist.H>
32 
33 using namespace Aleph;
34 
35 namespace Aleph {
36 
45  template <typename T>
46  class DynSlist : public Slist<T>
47  {
48  size_t num_items;
49  int current_pos;
50  Snode<T> * current_node;
51  typename Slist<T>::Node * get_previous_to_pos(const int & pos)
52  {
53  if (pos > num_items)
54  throw std::out_of_range ("position out of range");
55 
56  if (pos < current_pos) // hay que retroceder?
57  { // Si, reinicie posición actual
58  current_pos = 0;
59  current_node = this;
60  }
61  while (current_pos < pos) // avanzar hasta nodo predecesor a pos
62  {
63  current_node = current_node->get_next();
64  ++current_pos;
65  }
66  return current_node;
67  }
68 
69  public:
70 
72  DynSlist() : num_items(0), current_pos(0), current_node(this)
73  {
74  // Empty
75  }
76 
92  T & operator [] (const size_t & i)
93  {
94  return get_previous_to_pos(i)->get_next()->get_data();
95  }
96 
98  size_t size() const { return num_items; }
99 
111  void insert(const int & pos, const T & data)
112  { // apartar nodo para nuevo elemento
113  typename Slist<T>::Node * node = new typename Slist<T>::Node (data);
114  typename Slist<T>::Node * prev = get_previous_to_pos(pos);
115  prev->insert_next(node);
116  ++num_items;
117  }
118 
124  void remove(const int & pos)
125  { // obtener nodo predecesor al nuevo elemento
126  typename Slist<T>::Node * prev = get_previous_to_pos(pos);
127  typename Slist<T>::Node * node_to_delete = prev->remove_next();
128  delete node_to_delete;
129  --num_items;
130  }
131 
133  virtual ~DynSlist()
134  { // eliminar nodo por nodo hasta que la lista devenga vacía
135  while (not this->is_empty())
136  delete this->remove_first_ne(); // remove_first de clase Slink
137  }
138 
144  class Iterator : public Slist<T>::Iterator
145  {
146  public:
150  typedef T Item_Type;
151 
153  Iterator(DynSlist & list) : Slist<T>::Iterator(list) { /* Empty */ }
154 
157  };
158  };
159 
160 } // end namespace Aleph
161 
162 # endif // TPL_DYNSLIST_H
163 
size_t size() const
Retorna la cantidad de elementos que tiene la lista.
Definition: tpl_dynSlist.H:98
Iterator(DynSlist &list)
Constructor.
Definition: tpl_dynSlist.H:153
Snode * remove_next()
Definition: tpl_snode.H:74
Node * get_curr()
Definition: tpl_slist.H:139
Definition: tpl_snode.H:48
void insert(const int &pos, const T &data)
Definition: tpl_dynSlist.H:111
Definition: tpl_dynSlist.H:46
Definition: tpl_dynSlist.H:144
DynSlist()
Constructor.
Definition: tpl_dynSlist.H:72
Definition: tpl_slist.H:46
Snode *& get_next()
Retorna el nodo siguiente a this.
Definition: tpl_snode.H:77
Definition: ah-comb.H:35
T & get_data()
Retorna una referencia al dato contenido en el nodo.
Definition: tpl_snode.H:57
T Item_Type
El tipo de elemento que retorna get_curr().
Definition: tpl_dynSlist.H:150
T & get_curr()
retorna una referencia al elemento actual.
Definition: tpl_dynSlist.H:156
virtual ~DynSlist()
Destructor.
Definition: tpl_dynSlist.H:133
T & operator[](const size_t &i)
Definition: tpl_dynSlist.H:92
Definition: List.H:49
Slist< T > Set_Type
El tipo de conjunto sobre el cual se itera.
Definition: tpl_dynSlist.H:148

Leandro Rabindranath León