Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
tpl_slist.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
82# ifndef TPL_SLIST_H
83# define TPL_SLIST_H
84
85# include <ahDefs.H>
86# include <tpl_snode.H>
87# include <ah-errors.H>
88
89using namespace Aleph;
90
91namespace Aleph {
92
101 template <typename T> class Slist : public Snode<T>
102 {
103
104 public:
105
106 typedef Snode<T> Node;
107
109 Slist() = default;
110
115 void insert_first(Node * node)
116 {
117 assert(node not_eq nullptr);
118 assert(node->is_empty());
119
120 this->insert_next(node);
121 }
122
125 {
126 return this->remove_next();
127 }
128
135 {
136 ah_underflow_error_if(this->is_empty()) << "list is empty";
137
138 return this->remove_next();
139 }
140
142 Node * get_first()
143 {
144 ah_underflow_error_if(this->is_empty()) << "list is empty";
145
146 return this->get_next();
147 }
148
150 const Node * get_first() const
151 {
152 ah_underflow_error_if(this->is_empty()) << "list is empty";
153
154 return reinterpret_cast<const Node*>(this->Slink::get_next());
155 }
156
158 Node * get_first_ne() noexcept { return this->get_next(); }
159
162 {
163 return reinterpret_cast<const Node*>(this->Slink::get_next());
164 }
165
174 {
175
176 private:
177
180
181 public:
186
194 {
195 }
196
198 bool has_curr() const noexcept { return current != list; }
199
206 {
207 ah_overflow_error_if(not this->has_curr()) << "Slist::Iterator overflow";
208
209 return current;
210 }
215 void next()
216
217 {
218 ah_overflow_error_if(not this->has_curr()) << "Slist::Iterator overflow";
219
220 current = current->get_next();
221 }
224 {
226 }
227
234 {
235 if (current == node)
236 return *this;
237
238 current = node;
239 return *this;
240 }
241 };
242 };
243
244} // end namespace Aleph
245
246# endif /* TPL_SLIST_H */
247
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_overflow_error_if(C)
Throws std::overflow_error if condition holds.
Definition ah-errors.H:463
Core definitions, constants, and utility macros for Aleph-w.
constexpr bool is_empty() const noexcept
Return true if list is empty.
Definition htlist.H:523
Iterator over singly linked nodes.
Definition tpl_slist.H:174
Iterator & operator=(Node *node)
Assign the iterator to point to node.
Definition tpl_slist.H:233
Snode< T > * Item_Type
Type of the element returned by get_curr().
Definition tpl_slist.H:185
Node * get_curr()
Return the current node.
Definition tpl_slist.H:205
Iterator(Slist &_list) noexcept
Construct an iterator over list.
Definition tpl_slist.H:191
Snode< T > Set_Type
Type of the set being iterated.
Definition tpl_slist.H:183
void reset_first() noexcept
Reset the iterator to the first node of the list.
Definition tpl_slist.H:223
void next()
Advance the iterator to the next node.
Definition tpl_slist.H:215
bool has_curr() const noexcept
Return true if the iterator currently points to a node.
Definition tpl_slist.H:198
Singly linked list of nodes that store values of type T.
Definition tpl_slist.H:102
Node * remove_first_ne() noexcept
Remove and return the first node without checking emptiness.
Definition tpl_slist.H:124
Snode< T > Node
Definition tpl_slist.H:106
Node * get_first_ne() noexcept
Return the first node without checking emptiness.
Definition tpl_slist.H:158
Node * get_first()
Return a pointer to the first node; throw if the list is empty.
Definition tpl_slist.H:142
void insert_first(Node *node)
Insert node right after this sentinel (at the beginning).
Definition tpl_slist.H:115
const Node * get_first() const
Return a pointer to the first node (const overload).
Definition tpl_slist.H:150
const Node * get_first_ne() const noexcept
Return the first node without checking emptiness (const overload).
Definition tpl_slist.H:161
Node * remove_first()
Remove the first node of the list.
Definition tpl_slist.H:134
Slist()=default
Default constructor.
Singly linked node that stores data of type T.
Definition tpl_snode.H:66
Snode *& get_next()
Return the next node after this.
Definition tpl_snode.H:91
Snode * remove_next()
Remove the node right after this and return it.
Definition tpl_snode.H:88
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
DynList< T > maps(const C &c, Op op)
Classic map operation.
Typed singly linked node.