Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
tpl_binNode.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
44# ifndef TPL_BINNODE_H
45# define TPL_BINNODE_H
46
47# include <iostream>
48# include <stdexcept>
49# include <type_traits>
50# include <utility>
51# include <ahDefs.H>
52# include <ahAssert.H>
53 # include <ah-errors.H>
54
131namespace Aleph
132{
134 {
135 Empty_Node() = default;
136
137 explicit Empty_Node(SentinelCtor) noexcept
138 { /* empty */
139 }
140
141 static void reset() noexcept
142 { /* empty */
143 }
144
146 {
147 ah_domain_error() << "Empty_Node has no data";
148# if defined(__GNUC__) || defined(__clang__)
150# elif defined(_MSC_VER)
151 __assume(0);
152# endif
153 }
154 };
155
156# define INIT_CLASS_BINNODE(Name, height, Control_Data) \
157 template <typename Key> \
158 class Name : public Control_Data \
159 { \
160public: \
161 static const size_t MaxHeight = height; \
162 static Name * const NullPtr; \
163 typedef Key key_type; \
164 typedef Key Key_Type; \
165 \
166private: \
167 \
168 Key key = Key(); \
169 Name * lLink; \
170 Name * rLink; \
171 \
172public: \
173 \
174 Key & get_key() noexcept { return key; } \
175 const Key & get_key() const noexcept { return key; } \
176 Name *& getL() noexcept { return lLink; } \
177 Name *& getR() noexcept { return rLink; } \
178 const Name * getL() const noexcept { return lLink; } \
179 const Name * getR() const noexcept { return rLink; } \
180 Name(const Key& k) \
181 : key(k), lLink(NullPtr), rLink(NullPtr) \
182 { \
183 static_assert(std::is_copy_constructible<Key>::value, \
184 "No copy constructor for Key"); \
185 } \
186 Name(Key && k) noexcept \
187 : key(std::move(k)), lLink(NullPtr), rLink(NullPtr) \
188 { \
189 static_assert(std::is_move_constructible<Key>::value, \
190 "No move constructor for Key"); \
191 } \
192 Name(const Control_Data & control_data, const Key & k) \
193 : Control_Data(control_data), \
194 key(k), lLink(NullPtr), rLink(NullPtr) \
195 { \
196 /* Empty */ \
197 } \
198 Name(const Name & node) \
199 : Control_Data(node), \
200 key(node.key), lLink(NullPtr), rLink(NullPtr) \
201 { \
202 /* Empty */ \
203 } \
204 Name(Name && node) \
205 : Control_Data(std::move(static_cast<Control_Data &>(node))), \
206 key(std::move(node.key)), lLink(NullPtr), rLink(NullPtr) \
207 { \
208 /* Empty */ \
209 } \
210 Name(const Control_Data & control_data) noexcept : \
211 Control_Data(control_data), \
212 lLink(NullPtr), rLink(NullPtr) \
213 { \
214 /* Empty */ \
215 } \
216 Name() \
217 : lLink(NullPtr), rLink(NullPtr) \
218 { \
219 static_assert(std::is_default_constructible<Key>::value, \
220 "No default constructor for Key"); \
221 } \
222 void reset() noexcept \
223 { \
224 Control_Data::reset(); \
225 rLink = lLink = NullPtr; \
226 } \
227 static Name * key_to_node(Key & __key) noexcept \
228 { \
229 const size_t offset = __builtin_offsetof(Name, key); \
230 char * addr = reinterpret_cast<char *>(&__key); \
231 return reinterpret_cast<Name *>(addr - offset); \
232 }
233
234
258# define DECLARE_BINNODE(Name, height, Control_Data) \
259 INIT_CLASS_BINNODE(Name, height, Control_Data) \
260}; \
261 template <typename Key> Name<Key> * const Name<Key>::NullPtr = nullptr; \
262 INIT_CLASS_BINNODE(Name##Vtl, height, Control_Data) \
263 virtual ~Name##Vtl() { /* empty */ } \
264}; \
265 template <typename Key> Name##Vtl<Key> * \
266 const Name##Vtl<Key>::NullPtr = nullptr
267
268
298# define DECLARE_BINNODE_SENTINEL(Name, height, Control_Data) \
299 INIT_CLASS_BINNODE(Name, height, Control_Data) \
300 Name(SentinelCtor) : \
301 Control_Data(sentinelCtor), lLink(NullPtr), rLink(NullPtr) {} \
302 static Name sentinel_node; \
303}; \
304 template <typename Key> \
305 Name<Key> Name<Key>::sentinel_node(sentinelCtor); \
306 template <typename Key> \
307 Name<Key> * const Name<Key>::NullPtr = &Name<Key>::sentinel_node; \
308 INIT_CLASS_BINNODE(Name##Vtl, height, Control_Data) \
309 virtual ~Name##Vtl() { /* empty */ } \
310private: \
311 Name##Vtl(SentinelCtor) : \
312 Control_Data(sentinelCtor), lLink(NullPtr), rLink(NullPtr) {} \
313 static Name##Vtl sentinel_node; \
314}; \
315 template <typename Key> \
316 Name##Vtl<Key> Name##Vtl<Key>::sentinel_node(sentinelCtor); \
317 template <typename Key> \
318 Name##Vtl<Key> * const Name##Vtl<Key>::NullPtr = \
319 &Name##Vtl<Key>::sentinel_node
320
324 template <class Node>
325 constexpr inline Node *&LLINK(Node *p) noexcept
326 {
327 return p->getL();
328 }
329
330 template <class Node>
331 constexpr inline const Node *LLINK(const Node *p) noexcept
332 {
333 return p->getL();
334 }
335
340 template <class Node>
341 constexpr inline Node *&RLINK(Node *p) noexcept
342 {
343 return p->getR();
344 }
345
346 template <class Node>
347 constexpr inline const Node *RLINK(const Node *p) noexcept
348 {
349 return p->getR();
350 }
351
356 template <class Node>
357 constexpr inline
358 typename Node::Key_Type &KEY(Node *p) noexcept
359 {
360 return p->get_key();
361 }
362
363 template <class Node>
364 constexpr inline
365 const typename Node::Key_Type &KEY(const Node *p) noexcept
366 {
367 return p->get_key();
368 }
369
370 template <class Node>
372 {
374 using key_type = typename Node::Key_Type;
375
376 static constexpr node_type * null() noexcept { return node_type::NullPtr; }
377
378 static constexpr node_type *& left(node_type * p) noexcept { return p->getL(); }
379 static constexpr node_type *& right(node_type * p) noexcept { return p->getR(); }
380
381 static const node_type * left(const node_type * p) noexcept { return p->getL(); }
382 static const node_type * right(const node_type * p) noexcept { return p->getR(); }
383
384 static key_type & key(node_type * p) noexcept { return p->get_key(); }
385 static const key_type & key(const node_type * p) noexcept { return p->get_key(); }
386 };
387
396} // end namespace Aleph
397# endif
Exception handling system with formatted messages for Aleph-w.
#define ah_domain_error()
Throws std::domain_error unconditionally.
Definition ah-errors.H:559
Debug assertion and warning utilities.
Core definitions, constants, and utility macros for Aleph-w.
SentinelCtor
Tag type for sentinel node construction.
Definition ahDefs.H:83
WeightedDigraph::Node Node
@ KEY
Definition btreepic.C:169
Node for binary search tree.
size_t blossom_maximum_cardinality_matching(const GT &g, DynDlist< typename GT::Arc * > &matching, SA sa=SA())
Alias of compute_maximum_cardinality_general_matching().
Definition Blossom.H:466
#define DECLARE_BINNODE(Name, height, Control_Data)
Specify tree node for a binary tree.
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
Empty_Node()=default
static void reset() noexcept
Empty_Node(SentinelCtor) noexcept
static Empty_Node & get_data()
static constexpr node_type *& left(node_type *p) noexcept
static const node_type * left(const node_type *p) noexcept
static constexpr node_type *& right(node_type *p) noexcept
static key_type & key(node_type *p) noexcept
static constexpr node_type * null() noexcept
static const key_type & key(const node_type *p) noexcept
typename Node::Key_Type key_type
static const node_type * right(const node_type *p) noexcept
#define RLINK(i, n)
#define LLINK(i, n)