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 not data";
149 }
150 };
151
152# define INIT_CLASS_BINNODE(Name, height, Control_Data) \
153 template <typename Key> \
154 class Name : public Control_Data \
155 { \
156public: \
157 static const size_t MaxHeight = height; \
158 static Name * const NullPtr; \
159 typedef Key key_type; \
160 typedef Key Key_Type; \
161 \
162private: \
163 \
164 Key key = Key(); \
165 Name * lLink; \
166 Name * rLink; \
167 \
168public: \
169 \
170 Key & get_key() noexcept { return key; } \
171 const Key & get_key() const noexcept { return key; } \
172 Name *& getL() noexcept { return lLink; } \
173 Name *& getR() noexcept { return rLink; } \
174 const Name * getL() const noexcept { return lLink; } \
175 const Name * getR() const noexcept { return rLink; } \
176 Name(const Key& k) \
177 : key(k), lLink(NullPtr), rLink(NullPtr) \
178 { \
179 static_assert(std::is_copy_constructible<Key>::value, \
180 "No copy constructor for Key"); \
181 } \
182 Name(Key && k) noexcept \
183 : key(std::move(k)), lLink(NullPtr), rLink(NullPtr) \
184 { \
185 static_assert(std::is_move_constructible<Key>::value, \
186 "No move constructor for Key"); \
187 } \
188 Name(const Control_Data & control_data, const Key & k) \
189 : Control_Data(control_data), \
190 key(k), lLink(NullPtr), rLink(NullPtr) \
191 { \
192 /* Empty */ \
193 } \
194 Name(const Name & node) \
195 : Control_Data(node), \
196 key(node.key), lLink(NullPtr), rLink(NullPtr) \
197 { \
198 /* Empty */ \
199 } \
200 Name(Name && node) \
201 : Control_Data(std::move(static_cast<Control_Data &>(node))), \
202 key(std::move(node.key)), lLink(NullPtr), rLink(NullPtr) \
203 { \
204 /* Empty */ \
205 } \
206 Name(const Control_Data & control_data) noexcept : \
207 Control_Data(control_data), \
208 lLink(NullPtr), rLink(NullPtr) \
209 { \
210 /* Empty */ \
211 } \
212 Name() \
213 : lLink(NullPtr), rLink(NullPtr) \
214 { \
215 static_assert(std::is_default_constructible<Key>::value, \
216 "No default constructor for Key"); \
217 } \
218 void reset() noexcept \
219 { \
220 Control_Data::reset(); \
221 rLink = lLink = NullPtr; \
222 } \
223 static Name * key_to_node(Key & __key) noexcept \
224 { \
225 Name * node_zero = 0; \
226 size_t offset = (size_t) &(node_zero->key); \
227 unsigned long addr = (unsigned long)(&__key); \
228 return (Name*) (addr - offset); \
229 }
230
231
255# define DECLARE_BINNODE(Name, height, Control_Data) \
256 INIT_CLASS_BINNODE(Name, height, Control_Data) \
257}; \
258 template <typename Key> Name<Key> * const Name<Key>::NullPtr = nullptr; \
259 INIT_CLASS_BINNODE(Name##Vtl, height, Control_Data) \
260 virtual ~Name##Vtl() { /* empty */ } \
261}; \
262 template <typename Key> Name##Vtl<Key> * \
263 const Name##Vtl<Key>::NullPtr = nullptr
264
265
295# define DECLARE_BINNODE_SENTINEL(Name, height, Control_Data) \
296 INIT_CLASS_BINNODE(Name, height, Control_Data) \
297 Name(SentinelCtor) : \
298 Control_Data(sentinelCtor), lLink(NullPtr), rLink(NullPtr) {} \
299 static Name sentinel_node; \
300}; \
301 template <typename Key> \
302 Name<Key> Name<Key>::sentinel_node(sentinelCtor); \
303 template <typename Key> \
304 Name<Key> * const Name<Key>::NullPtr = &Name<Key>::sentinel_node; \
305 INIT_CLASS_BINNODE(Name##Vtl, height, Control_Data) \
306 virtual ~Name##Vtl() { /* empty */ } \
307private: \
308 Name##Vtl(SentinelCtor) : \
309 Control_Data(sentinelCtor), lLink(NullPtr), rLink(NullPtr) {} \
310 static Name##Vtl sentinel_node; \
311}; \
312 template <typename Key> \
313 Name##Vtl<Key> Name##Vtl<Key>::sentinel_node(sentinelCtor); \
314 template <typename Key> \
315 Name##Vtl<Key> * const Name##Vtl<Key>::NullPtr = \
316 &Name##Vtl<Key>::sentinel_node
317
321 template <class Node>
322 constexpr inline Node *&LLINK(Node *p) noexcept
323 {
324 return p->getL();
325 }
326
327 template <class Node>
328 constexpr inline const Node *LLINK(const Node *p) noexcept
329 {
330 return p->getL();
331 }
332
337 template <class Node>
338 constexpr inline Node *&RLINK(Node *p) noexcept
339 {
340 return p->getR();
341 }
342
343 template <class Node>
344 constexpr inline const Node *RLINK(const Node *p) noexcept
345 {
346 return p->getR();
347 }
348
353 template <class Node>
354 constexpr inline
355 typename Node::Key_Type &KEY(Node *p) noexcept
356 {
357 return p->get_key();
358 }
359
360 template <class Node>
361 constexpr inline
362 const typename Node::Key_Type &KEY(const Node *p) noexcept
363 {
364 return p->get_key();
365 }
366
367 template <class Node>
369 {
371 using key_type = typename Node::Key_Type;
372
373 static constexpr node_type * null() noexcept { return node_type::NullPtr; }
374
375 static constexpr node_type *& left(node_type * p) noexcept { return p->getL(); }
376 static constexpr node_type *& right(node_type * p) noexcept { return p->getR(); }
377
378 static const node_type * left(const node_type * p) noexcept { return p->getL(); }
379 static const node_type * right(const node_type * p) noexcept { return p->getR(); }
380
381 static key_type & key(node_type * p) noexcept { return p->get_key(); }
382 static const key_type & key(const node_type * p) noexcept { return p->get_key(); }
383 };
384
393} // end namespace Aleph
394# endif
Exception handling system with formatted messages for Aleph-w.
#define ah_domain_error()
Throws std::domain_error unconditionally.
Definition ah-errors.H:554
Debug assertion and warning utilities.
Core definitions, constants, and utility macros for Aleph-w.
SentinelCtor
Tag type for sentinel node construction.
Definition ahDefs.H:81
WeightedDigraph::Node Node
@ KEY
Definition btreepic.C:169
Node for binary search tree.
constexpr Node *& RLINK(Node *p) noexcept
Return the right tree of p.
#define DECLARE_BINNODE(Name, height, Control_Data)
Specify tree node for a binary tree.
constexpr Node *& LLINK(Node *p) noexcept
Return a pointer to left subtree.
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
DynList< T > maps(const C &c, Op op)
Classic map operation.
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