Aleph-w  1.9
General library for algorithms and data structures
tpl_binNode.H
1 
2 /* Aleph-w
3 
4  / \ | | ___ _ __ | |__ __ __
5  / _ \ | |/ _ \ '_ \| '_ \ ____\ \ /\ / / Data structures & Algorithms
6  / ___ \| | __/ |_) | | | |_____\ V V / version 1.9b
7  /_/ \_\_|\___| .__/|_| |_| \_/\_/ https://github.com/lrleon/Aleph-w
8  |_|
9 
10  This file is part of Aleph-w library
11 
12  Copyright (c) 2002-2018 Leandro Rabindranath Leon & Alejandro Mujica
13 
14  This program is free software: you can redistribute it and/or modify
15  it under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  This program is distributed in the hope that it will be useful, but
20  WITHOUT ANY WARRANTY; without even the implied warranty of
21  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22  General Public License for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with this program. If not, see <https://www.gnu.org/licenses/>.
26 */
27 # ifndef TPL_BINNODE_H
28 # define TPL_BINNODE_H
29 
30 # include <iostream>
31 # include <ahDefs.H>
32 # include <ahAssert.H>
33 
110 namespace Aleph {
111 
112 
113 struct Empty_Node
114 {
115  Empty_Node() noexcept { /* empty */ }
116 
117  Empty_Node(SentinelCtor) noexcept { /* empty */ }
118 
119  void reset() noexcept { /* empty */ }
120 
121  Empty_Node & get_data()
122  {
123  throw std::domain_error("Empty_Node has not data");
124  }
125 };
126 
127 # define INIT_CLASS_BINNODE(Name, height, Control_Data) \
128  template <typename Key> \
129  class Name : public Control_Data \
130  { \
131  public: \
132  static const size_t MaxHeight = height; \
133  static Name * const NullPtr; \
134  typedef Key key_type; \
135  typedef Key Key_Type; \
136  \
137  private: \
138  \
139  Key key = Key(); \
140  Name * lLink; \
141  Name * rLink; \
142  \
143  public: \
144  \
145  Key & get_key() noexcept { return key; } \
146  const Key & get_key() const noexcept { return key; } \
147  Name *& getL() noexcept { return lLink; } \
148  Name *& getR() noexcept { return rLink; } \
149  const Name * getL() const noexcept { return lLink; } \
150  const Name * getR() const noexcept { return rLink; } \
151  Name(const Key& k) noexcept(noexcept(Key(key))) \
152  : key(k), lLink(NullPtr), rLink(NullPtr) \
153  { \
154  static_assert(std::is_copy_constructible<Key>::value, \
155  "No copy constructor for Key"); \
156  } \
157  Name(Key && k) noexcept(noexcept(std::swap(key, k))) \
158  : key(std::move(k)), lLink(NullPtr), rLink(NullPtr) \
159  { \
160  static_assert(std::is_move_constructible<Key>::value, \
161  "No move constructor for Key"); \
162  } \
163  Name(const Control_Data & control_data, const Key & k) \
164  noexcept(noexcept(Key(k))) \
165  : Control_Data(control_data), \
166  key(k), lLink(NullPtr), rLink(NullPtr) \
167  { \
168  /* Empty */ \
169  } \
170  Name(const Name & node) noexcept(noexcept(Key(node.key))) \
171  : Control_Data(node), \
172  key(node.key), lLink(NullPtr), rLink(NullPtr) \
173  { \
174  /* Empty */ \
175  } \
176  Name(Name && node) \
177  noexcept(noexcept(std::forward<Key>(node).key)) \
178  : Control_Data(std::forward<Control_Data>(node)), \
179  key(std::forward<Key>(node).key), lLink(NullPtr), rLink(NullPtr) \
180  { \
181  /* Empty */ \
182  } \
183  Name(const Control_Data & control_data) noexcept : \
184  Control_Data(control_data), \
185  lLink(NullPtr), rLink(NullPtr) \
186  { \
187  /* Empty */ \
188  } \
189  Name() noexcept(std::is_nothrow_constructible<Key>::value) \
190  : lLink(NullPtr), rLink(NullPtr) \
191  { \
192  static_assert(std::is_default_constructible<Key>::value, \
193  "No default constructor for Key"); \
194  } \
195  void reset() noexcept \
196  { \
197  Control_Data::reset(); \
198  rLink = lLink = NullPtr; \
199  } \
200  static Name * key_to_node(Key & __key) noexcept \
201  { \
202  Name * node_zero = 0; \
203  size_t offset = (size_t) &(node_zero->key); \
204  unsigned long addr = (unsigned long)(&__key); \
205  return (Name*) (addr - offset); \
206  }
207 
208 
232 # define DECLARE_BINNODE(Name, height, Control_Data) \
233  INIT_CLASS_BINNODE(Name, height, Control_Data) \
234 }; \
235  template <typename Key> Name<Key> * const Name<Key>::NullPtr = nullptr; \
236  INIT_CLASS_BINNODE(Name##Vtl, height, Control_Data) \
237  virtual ~Name##Vtl() { /* empty */ } \
238  }; \
239  template <typename Key> Name##Vtl<Key> * \
240  const Name##Vtl<Key>::NullPtr = nullptr
241 
242 
272 # define DECLARE_BINNODE_SENTINEL(Name, height, Control_Data) \
273  INIT_CLASS_BINNODE(Name, height, Control_Data) \
274  Name(SentinelCtor) : \
275  Control_Data(sentinelCtor), lLink(NullPtr), rLink(NullPtr) {} \
276  static Name sentinel_node; \
277 }; \
278  template <typename Key> \
279  Name<Key> Name<Key>::sentinel_node(sentinelCtor); \
280  template <typename Key> \
281  Name<Key> * const Name<Key>::NullPtr = &Name<Key>::sentinel_node; \
282  INIT_CLASS_BINNODE(Name##Vtl, height, Control_Data) \
283  virtual ~Name##Vtl() { /* empty */ } \
284 private: \
285  Name##Vtl(SentinelCtor) : \
286  Control_Data(sentinelCtor), lLink(NullPtr), rLink(NullPtr) {} \
287  static Name##Vtl sentinel_node; \
288  }; \
289  template <typename Key> \
290  Name##Vtl<Key> Name##Vtl<Key>::sentinel_node(sentinelCtor); \
291  template <typename Key> \
292  Name##Vtl<Key> * const Name##Vtl<Key>::NullPtr = \
293  &Name##Vtl<Key>::sentinel_node
294 
298 template <class Node> inline Node *& LLINK(Node * p) noexcept
299 {
300  return p->getL();
301 }
302 
307 template <class Node> inline Node *& RLINK(Node * p) noexcept
308 {
309  return p->getR();
310 }
311 
312 
317  template <class Node> inline
318 typename Node::Key_Type & KEY(Node * p) noexcept { return p->get_key(); }
319 
326 DECLARE_BINNODE(BinNode, 2048, Empty_Node);
327 
328 
329 } // end namespace Aleph
330 # endif
Node::Key_Type & KEY(Node *p) noexcept
Definition: tpl_binNode.H:318
Definition: ah-comb.H:35
Node *& LLINK(Node *p) noexcept
Definition: tpl_binNode.H:298
Node for binary search tree.
#define DECLARE_BINNODE(Name, height, Control_Data)
Definition: tpl_binNode.H:232
Node *& RLINK(Node *p) noexcept
Definition: tpl_binNode.H:307

Leandro Rabindranath León