Aleph-w 7.10.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{
133 namespace bin_node_detail
134 {
144 template <typename Key>
145 inline constexpr bool nothrow_default_key_v =
146 std::is_nothrow_default_constructible_v<Key>;
147
149 template <typename First, typename Second>
150 inline constexpr bool nothrow_default_key_v<std::pair<First, Second>> =
151 nothrow_default_key_v<First> and nothrow_default_key_v<Second>;
152
156 template <class Node>
157 using key_of_t = std::remove_cvref_t<decltype(std::declval<Node &>().get_key())>;
158
168 template <class Node>
169 inline constexpr bool nothrow_from_key_of_v =
170 noexcept(std::declval<Node &>().get_key()) and
171 std::is_nothrow_constructible_v<Node, key_of_t<Node> &&>;
172
180 template <class Node>
181 inline constexpr bool nothrow_header_from_v =
182 std::is_nothrow_default_constructible_v<Node> or nothrow_from_key_of_v<Node>;
183
216 template <class Node>
217 [[nodiscard]] Node header_from([[maybe_unused]] Node & other)
218 noexcept(nothrow_header_from_v<Node>)
219 {
220 if constexpr (std::is_nothrow_default_constructible_v<Node> or
221 not nothrow_from_key_of_v<Node>)
222 return Node();
223 else
224 return Node(std::move(other.get_key()));
225 }
226
241 template <class Compare>
242 [[nodiscard]] constexpr decltype(auto) compare_to_take(Compare & cmp) noexcept
243 {
244 if constexpr (std::is_copy_constructible_v<Compare>)
245 return static_cast<const Compare &>(cmp);
246 else if constexpr (std::is_constructible_v<Compare, Compare &>)
247 return static_cast<Compare &>(cmp);
248 else
249 return std::move(cmp);
250 }
251
256 template <class Compare>
257 inline constexpr bool nothrow_compare_taken_v =
258 std::is_nothrow_constructible_v<Compare,
259 decltype(compare_to_take(std::declval<Compare &>()))>;
260 } // namespace bin_node_detail
261
263 {
264 Empty_Node() = default;
265
266 explicit Empty_Node(SentinelCtor) noexcept
267 { /* empty */
268 }
269
270 static void reset() noexcept
271 { /* empty */
272 }
273
275 {
276 ah_domain_error() << "Empty_Node has no data";
277# if defined(__GNUC__) || defined(__clang__)
278 __builtin_unreachable();
279# elif defined(_MSC_VER)
280 __assume(0);
281# endif
282 }
283 };
284
285# define INIT_CLASS_BINNODE(Name, height, Control_Data) \
286 template <typename Key> \
287 class Name : public Control_Data \
288 { \
289public: \
290 static const size_t MaxHeight = height; \
291 static Name * const NullPtr; \
292 typedef Key key_type; \
293 typedef Key Key_Type; \
294 \
295private: \
296 \
297 Key key = Key(); \
298 Name * lLink; \
299 Name * rLink; \
300 \
301public: \
302 \
303 Key & get_key() noexcept { return key; } \
304 const Key & get_key() const noexcept { return key; } \
305 Name *& getL() noexcept { return lLink; } \
306 Name *& getR() noexcept { return rLink; } \
307 const Name * getL() const noexcept { return lLink; } \
308 const Name * getR() const noexcept { return rLink; } \
309 Name(const Key& k) \
310 : key(k), lLink(NullPtr), rLink(NullPtr) \
311 { \
312 static_assert(std::is_copy_constructible<Key>::value, \
313 "No copy constructor for Key"); \
314 } \
315 /* noexcept as the move of Key and the default construction of the \
316 base: a key without a move constructor is copied, and what that \
317 copy, or a base given to the macro, throws must reach the caller */ \
318 Name(Key && k) \
319 noexcept(std::is_nothrow_move_constructible_v<Key> and \
320 std::is_nothrow_default_constructible_v<Control_Data>) \
321 : key(std::move(k)), lLink(NullPtr), rLink(NullPtr) \
322 { \
323 static_assert(std::is_move_constructible<Key>::value, \
324 "No move constructor for Key"); \
325 } \
326 Name(const Control_Data & control_data, const Key & k) \
327 : Control_Data(control_data), \
328 key(k), lLink(NullPtr), rLink(NullPtr) \
329 { \
330 /* Empty */ \
331 } \
332 Name(const Name & node) \
333 : Control_Data(node), \
334 key(node.key), lLink(NullPtr), rLink(NullPtr) \
335 { \
336 /* Empty */ \
337 } \
338 Name(Name && node) \
339 : Control_Data(std::move(static_cast<Control_Data &>(node))), \
340 key(std::move(node.key)), lLink(NullPtr), rLink(NullPtr) \
341 { \
342 /* Empty */ \
343 } \
344 /* noexcept as the default construction of Key, which this builds, \
345 and the copy of the base, which a base given to the macro may \
346 make throw */ \
347 Name(const Control_Data & control_data) \
348 noexcept(Aleph::bin_node_detail::nothrow_default_key_v<Key> and \
349 std::is_nothrow_copy_constructible_v<Control_Data>) : \
350 Control_Data(control_data), \
351 lLink(NullPtr), rLink(NullPtr) \
352 { \
353 /* Empty */ \
354 } \
355 /* noexcept as the default construction of Key, which this builds, \
356 and that of the base. That of a std::pair is seen in its members */ \
357 Name() \
358 noexcept(Aleph::bin_node_detail::nothrow_default_key_v<Key> and \
359 std::is_nothrow_default_constructible_v<Control_Data>) \
360 : lLink(NullPtr), rLink(NullPtr) \
361 { \
362 static_assert(std::is_default_constructible<Key>::value, \
363 "No default constructor for Key"); \
364 } \
365 void reset() noexcept \
366 { \
367 Control_Data::reset(); \
368 rLink = lLink = NullPtr; \
369 } \
370 static Name * key_to_node(Key & __key) noexcept \
371 { \
372 const size_t offset = __builtin_offsetof(Name, key); \
373 char * addr = reinterpret_cast<char *>(&__key); \
374 return reinterpret_cast<Name *>(addr - offset); \
375 }
376
377
401# define DECLARE_BINNODE(Name, height, Control_Data) \
402 INIT_CLASS_BINNODE(Name, height, Control_Data) \
403}; \
404 template <typename Key> Name<Key> * const Name<Key>::NullPtr = nullptr; \
405 INIT_CLASS_BINNODE(Name##Vtl, height, Control_Data) \
406 virtual ~Name##Vtl() { /* empty */ } \
407}; \
408 template <typename Key> Name##Vtl<Key> * \
409 const Name##Vtl<Key>::NullPtr = nullptr
410
411
449# define DECLARE_BINNODE_SENTINEL(Name, height, Control_Data) \
450 INIT_CLASS_BINNODE(Name, height, Control_Data) \
451 Name(SentinelCtor) : \
452 Control_Data(sentinelCtor), lLink(NullPtr), rLink(NullPtr) {} \
453 static Name sentinel_node; \
454}; \
455 template <typename Key> \
456 Name<Key> Name<Key>::sentinel_node(sentinelCtor); \
457 template <typename Key> \
458 Name<Key> * const Name<Key>::NullPtr = &Name<Key>::sentinel_node; \
459 INIT_CLASS_BINNODE(Name##Vtl, height, Control_Data) \
460 virtual ~Name##Vtl() { /* empty */ } \
461 Name##Vtl(SentinelCtor) : \
462 Control_Data(sentinelCtor), lLink(NullPtr), rLink(NullPtr) {} \
463 static Name##Vtl sentinel_node; \
464}; \
465 template <typename Key> \
466 Name##Vtl<Key> Name##Vtl<Key>::sentinel_node(sentinelCtor); \
467 template <typename Key> \
468 Name##Vtl<Key> * const Name##Vtl<Key>::NullPtr = \
469 &Name##Vtl<Key>::sentinel_node
474 template <class Node>
475 constexpr inline Node *&LLINK(Node *p) noexcept
477 return p->getL();
478 }
479
480 template <class Node>
481 constexpr inline const Node *LLINK(const Node *p) noexcept
482 {
483 return p->getL();
484 }
485
490 template <class Node>
491 constexpr inline Node *&RLINK(Node *p) noexcept
493 return p->getR();
494 }
495
496 template <class Node>
497 constexpr inline const Node *RLINK(const Node *p) noexcept
498 {
499 return p->getR();
500 }
501
506 template <class Node>
507 constexpr inline
508 typename Node::Key_Type &KEY(Node *p) noexcept
509 {
510 return p->get_key();
511 }
512
513 template <class Node>
514 constexpr inline
515 const typename Node::Key_Type &KEY(const Node *p) noexcept
517 return p->get_key();
520 template <class Node>
522 {
524 using key_type = typename Node::Key_Type;
525
526 static constexpr node_type * null() noexcept { return node_type::NullPtr; }
528 static constexpr node_type *& left(node_type * p) noexcept { return p->getL(); }
529 static constexpr node_type *& right(node_type * p) noexcept { return p->getR(); }
531 static const node_type * left(const node_type * p) noexcept { return p->getL(); }
532 static const node_type * right(const node_type * p) noexcept { return p->getR(); }
533
534 static key_type & key(node_type * p) noexcept { return p->get_key(); }
535 static const key_type & key(const node_type * p) noexcept { return p->get_key(); }
536 };
537
545 DECLARE_BINNODE(BinNode, 2048, Empty_Node);
546} // end namespace Aleph
547# endif
Exception handling system with formatted messages for Aleph-w.
#define ah_domain_error()
Throws std::domain_error unconditionally.
Definition ah-errors.H:702
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
Basic binary node with no extra control data.
int cmp(const __gmp_expr< T, U > &expr1, const __gmp_expr< V, W > &expr2)
Definition gmpfrxx.h:4196
#define DECLARE_BINNODE(Name, height, Control_Data)
Specify tree node for a binary tree.
constexpr decltype(auto) compare_to_take(Compare &cmp) noexcept
What a tree that takes the nodes of another takes of its comparison criteria.
constexpr bool nothrow_header_from_v
Whether header_from() builds a header node without throwing.
constexpr bool nothrow_from_key_of_v
Whether a node is built without throwing from the key of another node, which is moved.
constexpr bool nothrow_default_key_v
Whether building a Key by default throws nothing.
std::remove_cvref_t< decltype(std::declval< Node & >().get_key())> key_of_t
The type of the key of a node, as its get_key() gives it.
constexpr bool nothrow_compare_taken_v
Whether a tree takes without throwing the comparison criteria that compare_to_take() gives it.
Node header_from(Node &other) noexcept(nothrow_header_from_v< Node >)
Builds a header node of a tree that takes the nodes of another tree.
Main namespace for Aleph-w library functions.
and
Check uniqueness with explicit hash + equality functors.
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 constexpr node_type *& right(node_type *p) noexcept
static key_type & key(node_type *p) noexcept
static constexpr node_type * null() noexcept
typename Node::Key_Type key_type
#define RLINK(i, n)
#define LLINK(i, n)