Aleph-w  1.9
General library for algorithms and data structures
tpl_slist.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_SLIST_H
29 # define TPL_SLIST_H
30 
31 # include <ahDefs.H>
32 # include <tpl_snode.H>
33 
34 using namespace Aleph;
35 
36 namespace Aleph {
37 
46  template <typename T> class Slist : public Snode<T>
47  {
48 
49  public:
50 
51  typedef Snode<T> Node;
52 
54  Slist() { /* empty */ }
55 
63  void insert_first(Node * node)
64  {
65  assert(node not_eq nullptr);
66  assert(node->is_empty());
67 
68  this->insert_next(node);
69  }
70 
71  Node * remove_first_ne() noexcept
72  {
73  return this->remove_next();
74  }
75 
83  Node * remove_first()
84  {
85  if (this->is_empty())
86  throw std::underflow_error ("list is empty");
87 
88  return this->remove_next();
89  }
90 
92  Node * get_first()
93  {
94  if (this->is_empty())
95  throw std::underflow_error ("list is empty");
96 
97  return this->get_next();
98  }
99 
108  class Iterator
109  {
110 
111  private:
112 
113  Slist * list;
114  Node * current;
115 
116  public:
120  typedef Snode<T> * Item_Type;
121 
128  Iterator(Slist & _list) : list(&_list), current(list->get_first()) {}
129 
131  bool has_curr() const { return current != list; }
132 
139  Node * get_curr()
140  {
141  if (not this->has_curr())
142  throw std::overflow_error ("");
143 
144  return current;
145  }
156  void next()
157 
158  {
159  if (not this->has_curr())
160  throw std::overflow_error ("");
161 
162  current = current->get_next();
163  }
165  void reset_first() { current = list->get_next(); }
166  Iterator & operator = (Node * node)
167  {
168  if (this == node)
169  return *this;
170 
171  current = node;
172  return *this;
173  }
174  };
175  };
176 
177 } // end namespace Aleph
178 
179 # endif /* TPL_SLIST_H */
180 
Definition: tpl_slist.H:108
Snode * remove_next()
Definition: tpl_snode.H:74
Node * get_curr()
Definition: tpl_slist.H:139
Node * get_first()
Retorna el primer nodo de la lista.
Definition: tpl_slist.H:92
Definition: tpl_snode.H:48
Iterator(Slist &_list)
Definition: tpl_slist.H:128
Snode< T > * Item_Type
El tipo de elemento que retorna get_curr().
Definition: tpl_slist.H:120
void reset_first()
Coloca el iterador en el primer elemento de la lista.
Definition: tpl_slist.H:165
Definition: tpl_slist.H:46
Snode *& get_next()
Retorna el nodo siguiente a this.
Definition: tpl_snode.H:77
void insert_first(Node *node)
Definition: tpl_slist.H:63
Slist()
Constructor vacío.
Definition: tpl_slist.H:54
Definition: ah-comb.H:35
bool has_curr() const
Retorna true si el iterador tiene nodo actual.
Definition: tpl_slist.H:131
Snode< T > Set_Type
El tipo de conjunto sobre el cual se itera.
Definition: tpl_slist.H:118
Node * remove_first()
Definition: tpl_slist.H:83
Definition: List.H:49
void next()
Definition: tpl_slist.H:156

Leandro Rabindranath León