Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
tpl_dyn_slist_nc.H
Go to the documentation of this file.
1
2/*
3 Aleph_w
4
5 Data structures & Algorithms
6 version 2.0.0b
7 https://github.com/lrleon/Aleph-w
8
9 This file is part of Aleph-w library
10
11 Copyright (c) 2002-2026 Leandro Rabindranath Leon
12
13 Permission is hereby granted, free of charge, to any person obtaining a copy
14 of this software and associated documentation files (the "Software"), to deal
15 in the Software without restriction, including without limitation the rights
16 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17 copies of the Software, and to permit persons to whom the Software is
18 furnished to do so, subject to the following conditions:
19
20 The above copyright notice and this permission notice shall be included in all
21 copies or substantial portions of the Software.
22
23 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 SOFTWARE.
30*/
31
32
39# ifndef TPL_DYN_SLIST_NC_H
40# define TPL_DYN_SLIST_NC_H
41
42# include <tpl_snode_nc.H>
43# include <ah-errors.H>
44
45namespace Aleph
46{
69 template <typename T> class Dyn_Slist_Nc : public Snode_Nc <T>
70 {
71 public:
73
74 private:
76 size_t num_items;
77
78 public:
79
84 void empty()
85 {
86 while (not this->is_empty())
87 delete Node::remove_next();
88 this->reset();
89 head = this;
90 num_items = 0;
91 }
92
98 void clear() noexcept { empty(); }
99
109 T & insert(const T & data)
110 {
111 Node * p = new Node(data);
112 Node::insert(p);
113
114 if (num_items++ == 0)
115 head = p;
116
117 return p->get_data();
118 }
119
129 T & append(const T & data)
130 {
131 assert(head not_eq nullptr);
132 Node * p = new Node(data);
133 head->insert(p);
134
135 head = p;
136 ++num_items;
137 return p->get_data();
138 }
139
145 {
146 ah_underflow_error_if(this->is_empty()) << "List is empty";
147
148 return this->get_next()->get_data();
149 }
150
155 const T & get_first() const
156 {
157 ah_underflow_error_if(this->is_empty()) << "List is empty";
158
159 return this->get_next()->get_data();
160 }
161
167 {
168 ah_underflow_error_if(this->is_empty()) << "List is empty";
169
170 return head->get_data();
171 }
172
177 const T & get_last() const
178 {
179 ah_underflow_error_if(this->is_empty()) << "List is empty";
180
181 return head->get_data();
182 }
183
190 {
191 ah_underflow_error_if(this->is_empty()) << "List is empty";
192
193 Node * p = Node::remove_next();
194 T ret_val = p->get_data();
195 delete p;
196 if (--num_items == 0)
197 head = this;
198 return ret_val;
199 }
200
202 T & put(const T & item) { return append(item); }
203
205 T get() { return remove_first(); }
206
208 T & rear() { return get_last(); }
209
214 const T & rear() const { return get_last(); }
215
217 T & front() { return get_first(); }
218
223 const T & front() const { return get_first(); }
224
226 T & push(const T & item) { return insert(item); }
227
229 T pop() { return remove_first(); }
230
232 T & top() { return get_first(); }
233
238 const T & top() const { return get_first(); }
239
240
242 const size_t & size() const { return num_items; }
243
246 {
247 public:
249 {
250 // empty
251 }
252
254 {
255 return Node::Iterator::get_curr()->get_data();
256 }
257
258 T & get_curr() const
259 {
260 return Node::Iterator::get_curr()->get_data();
261 }
262
263 };
264
267 {
268 // empty
269 }
270
273 {
274 for (Iterator it(const_cast <Dyn_Slist_Nc &>(l));
275 it.has_curr(); it.next_ne())
276 append(it.get_curr());
277 }
278
281 {
282 empty();
283 }
284
294 {
295 if (this == &list)
296 return *this;
297
298 empty();
299
300 assert(this->is_empty());
301
302 for (Iterator it(const_cast<Dyn_Slist_Nc &>(list));
303 it.has_curr(); it.next_ne())
304 append(it.get_curr());
305
306 return *this;
307 }
308
313 T & operator [] (const size_t & n)
314 {
315 ah_range_error_if(n >= num_items) << "Index " << n << " out of range";
316
317 Iterator it(*this);
318 for (size_t i = 0; i < n; ++i, it.next_ne());
319
320 return it.get_curr();
321 }
322
327 const T & operator [] (const size_t & n) const
328 {
329 ah_range_error_if(n >= num_items) << "Index " << n << " out of range";
330
331 Iterator it(const_cast<Dyn_Slist_Nc&>(*this));
332 for (size_t i = 0; i < n; ++i, it.next_ne());
333
334 return it.get_curr();
335 }
336 };
337}
338
339# endif
Exception handling system with formatted messages for Aleph-w.
#define ah_underflow_error_if(C)
Throws std::underflow_error if condition holds.
Definition ah-errors.H:368
#define ah_range_error_if(C)
Throws std::range_error if condition holds.
Definition ah-errors.H:207
Iterator for Dyn_Slist_Nc.
Iterator(Dyn_Slist_Nc< T > &list)
T & get_curr_n() const noexcept
Dynamic list of elements of type T.
void clear() noexcept
Alias for empty().
T & rear()
If this is a queue, returns the youngest element.
T remove_first()
Removes the first element of the list and returns a copy of it.
T pop()
If this is a stack, removes the top element.
T & operator[](const size_t &n)
Access element at index n.
Dyn_Slist_Nc(const Dyn_Slist_Nc &l)
Copy constructor.
T & front()
If this is a queue, returns the oldest element.
T & get_first()
Returns a reference to the first element of the list.
T & insert(const T &data)
Insert an element at the beginning of the list.
T & put(const T &item)
If this is a queue, inserts the item item.
T & top()
If this is a stack, returns the top element.
const T & front() const
Returns a reference to the oldest element (const version).
T & push(const T &item)
If this is a stack, inserts the item item.
const T & top() const
Returns the top element (const version).
const size_t & size() const
Returns the number of elements in the list.
T & get_last()
Returns a reference to the last element of the list.
T get()
If this is a queue, extracts the oldest element.
Dyn_Slist_Nc()
Default constructor.
T & append(const T &data)
Append an element at the end of the list.
void empty()
Empties the container.
Dyn_Slist_Nc & operator=(const Dyn_Slist_Nc &list)
Assignment operator with lvalue semantics.
const T & get_last() const
Returns a reference to the last element of the list (const version).
const T & get_first() const
Returns a reference to the first element of the list (const version).
const T & rear() const
Returns a reference to the youngest element (const version).
Snode_Nc * remove_next()
Removes the node following this.
Snode_Nc *& get_next()
Returns the node following this.
T & get_data()
Returns a reference to the data contained in the node.
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
Divide_Conquer_DP_Result< Cost > divide_and_conquer_partition_dp(const size_t groups, const size_t n, Transition_Cost_Fn transition_cost, const Cost inf=dp_optimization_detail::default_inf< Cost >())
Optimize partition DP using divide-and-conquer optimization.
std::decay_t< typename HeadC::Item_Type > T
Definition ah-zip.H:105
Typed singly linked node (no check).
DynList< int > l