Aleph-w  1.9
General library for algorithms and data structures
slink_nc.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 # ifndef SLINK_NC_H
28 # define SLINK_NC_H
29 
30 # include <aleph.H>
31 
32 namespace Aleph
33 {
42  class Slink_Nc
43  {
44  protected:
45  Slink_Nc * next;
46 
47  public:
49  Slink_Nc() : next(nullptr) { /* empty */ }
50 
52  Slink_Nc(const Slink_Nc &) : next(nullptr) { /* empty */ }
53 
54  ~Slink_Nc() { /* empty */ }
55 
57  Slink_Nc & operator = (const Slink_Nc & link)
58  {
59  if (&link == this)
60  return *this;
61 
62  if (not is_empty())
63  throw std::invalid_argument("link is not empty");
64 
65  next = nullptr;
66  }
67 
69  void reset()
70  {
71  assert(this not_eq nullptr);
72  next = nullptr;
73  }
74 
76  bool is_empty() const
77  {
78  assert(this not_eq nullptr);
79  return next == nullptr;
80  }
81 
84  {
85  assert(this not_eq nullptr);
86  return next;
87  }
88 
96  void insert(Slink_Nc * p)
97  {
98  assert(this not_eq nullptr);
99  assert(p not_eq nullptr);
100  assert(p->is_empty());
101  p->next = next;
102  next = p;
103  }
104 
114  {
115  assert(this not_eq nullptr);
116  Slink_Nc * ret_val = next;
117  next = ret_val->next;
118  ret_val->reset();
119  return ret_val;
120  }
121 
122  class Iterator
123  {
124  Slink_Nc * head;
125  Slink_Nc * curr;
126 
127  public:
128 
129  Iterator() : head(nullptr), curr(nullptr) { /* Empty */ }
130 
131  Iterator(Slink_Nc * head_ptr) : head(head_ptr), curr(head->get_next())
132  {
133  // empty
134  }
135 
136  Iterator(Slink_Nc & _head) : head(&_head), curr(head->get_next())
137  {
138  // empty
139  }
140 
141  Iterator(Slink_Nc * head_ptr, Slink_Nc * curr_ptr)
142  : head(head_ptr), curr(curr_ptr)
143  {
144  // empty
145  }
146 
147  void reset_first()
148  {
149  assert(curr != nullptr and head != nullptr);
150  curr = head->get_next();
151  }
152 
153  void set(Slink_Nc * new_curr)
154  {
155  assert(curr != nullptr and head != nullptr);
156  curr = new_curr;
157  }
158 
159  void reset(Slink_Nc * new_head)
160  {
161  head = new_head;
162  curr = head->get_next();;
163  }
164 
165  bool has_curr() const
166  {
167  assert(head != nullptr);
168  return curr != nullptr;
169  }
170 
172  Slink_Nc * get_curr_ne() noexcept
173  {
174  assert(curr != nullptr and head != nullptr);
175  return curr;
176  }
177 
178  Slink_Nc * get_curr()
179  {
180  if (not has_curr())
181  throw std::overflow_error("Not element in list");
182  return get_curr_ne();
183  }
184 
186  bool is_in_first() const { return curr == head->next; }
187 
189  void next_ne() noexcept
190  {
191  curr = curr->get_next();
192  }
193 
194  void next() throw(std::exception, std::overflow_error)
195  {
196  if (not has_curr())
197  throw std::overflow_error("Not next element in list");
198  next_ne();
199  }
200 
202  bool operator == (const Iterator & it) const { return curr == it.curr; }
203 
205  bool operator != (const Iterator & it) const { return curr != it.curr; }
206 
207  bool verify(Slink_Nc * l) const { return head == l; }
208 
209  bool verify(const Iterator & it) const { return head == it.head; }
210  };
211 
212  };
213 }
214 
215 # endif
216 
Definition: ah-comb.H:35

Leandro Rabindranath León