CMMTree v1.0
node.hpp
Go to the documentation of this file.
1#ifndef _node_hpp_
2#define _node_hpp_
3//---------------------------------------------
4#include<iostream>
5#include<vector>
6#include"utils.hpp"
7
8template <class X1, template<typename> class pred_type> class CMMTree;
9
19template <class T>
20typename std::vector<T>::const_iterator const& Null_iterator( T ) {
21 static std::vector<T> vec;
22 static typename std::vector<T>::const_iterator it = vec.begin();
23 return it;
24}
25
44template <class X1>
45class node{
46 public:
47 using data_addr = typename std::vector<X1>::const_iterator ;
48
49 private:
50 node<X1>* parent;
51 data_addr d_iterator;
52 std::vector< node<X1>* >* children;
53
54 #ifdef NODE_COUNTER
55 static unsigned n_count;
56 static inline std::mutex _mutex;
57 public:
58 static inline std::mutex &mutex() { return _mutex; }
59 #endif
60
61 public:
62
69 node(node<X1>* const& prev, const data_addr& pos ): parent(prev), d_iterator(pos), children(0) {
70 #ifdef NODE_COUNTER
71 n_count++;
72 #endif
73 }
74
78 node():parent(nullptr), d_iterator( Null_iterator( X1() ) ), children(nullptr){
79 #ifdef NODE_COUNTER
80 n_count++;
81 #endif
82 }
83
89 node(node<X1> const& ) = delete;
90
96 node<X1>& operator=(node<X1> const& ) = delete;
97
102 node(node<X1>&& other)noexcept
103 : parent( std::move(other.parent) ), d_iterator( std::move(other.d_iterator) ), children( std::move(other.children ) )
104 {}
105
111 node<X1>& operator=(node<X1>&& other)noexcept{
112 parent = std::move(other.parent); d_iterator = std::move(other.d_iterator); children = std::move(other.children );
113 }
114
118 ~node();
119
127 bool isNULLit(data_addr const& r)const {
128 return r == Null_iterator(X1()) ? true : false;
129 }
130
136 void add_new_node( data_addr const & pos );
137
143 size_t node_level()const;
144
150 data_addr const& data_address() const {return d_iterator;}
151
158 data_addr& data_address(){return d_iterator;}
159
166 void data_on_path(std::vector< data_addr > & vec) const;
167
173 node<X1>* const& parent_node_address()const { return parent; }
174
180 node<X1>const* this_node_address()const { return this; }
181 //node<X1>* this_node_address() { return this; }
182
188 size_t children_number()const { return children ? children->size() : 0 ; }
189
195 size_t siblings_number()const { return parent ? parent->children_number()-1 : 0; }
199 void change_children_capacity()const { if(parent) parent->children->shrink_to_fit(); }
200
206 node<X1>* const& operator[](size_t i)const {
207 if(!children) throw std::runtime_error("ERROR ! node<T>::operator[]()\nThe current node is a leaf - it has no any children.\n");
208 if(i>=children->size()) throw std::runtime_error("ERROR ! node<T>::operator[]()\nIndex out of range.\n");
209 return children->operator[](i);
210 }
211
217 std::vector< node<X1>* > const& childrenVec()const {return *children;}
218
224 std::vector< node<X1>* > & childrenVec() {return *children;}
225
231 void all_siblings_addresses(std::vector< const node<X1>* > & vec)const;
232
238 X1 const& node_value()const {
239 if( isNULLit(d_iterator) ) throw std::runtime_error("ERROR ! node<T>::node_value()\nYou can not use this function for root node\nwhen you have decided beforehand that root will not refer to any element of the input dataset.\n");
240 return *d_iterator;
241 }
242
246 static void n_counter_info(){
247 #ifdef NODE_COUNTER
248 SetColor(BLUE);
249 std::cout << "Number of nodes<" << typeid(X1).name() << "> = ";
250 SetColor(BBLUE);
251 std::cout << n_count << std::endl;
252 SetColor(DEFAULT);
253 #else
254 std::cout << "Information about the number of node objects is not available.\nUncomment the NODE_COUNTER directive to start object counting." << std::endl;
255 #endif
256 }
257
262 static unsigned n_Count(){
263 #ifdef NODE_COUNTER
264 return n_count;
265 #else
266 println("Information about the number of node objects is not available.\nUncomment the NODE_COUNTER directive to start object counting.");
267 return 0;
268 #endif
269 }
270
271 template <class T1, template<typename> class T2> friend class CMMTree;
272};
273
274//---------------------------------------------
275#endif
The Conditional Multiway Mapped Tree (CMMTree) class definition.
Definition cmmtree.hpp:21
The node class definition. This is a template of a class whose objects are used to build a tree of re...
Definition node.hpp:45
node< X1 > & operator=(node< X1 > const &)=delete
Copy assignment operator.
static unsigned n_Count()
The function returns a number of objects of type node<X1>.
Definition node.hpp:262
static void n_counter_info()
The function displays information about the total number of node<X1> objects.
Definition node.hpp:246
size_t node_level() const
This function returns the level of the CMMTree where a given node is located.
Definition node.cpp:51
node< X1 > *const & parent_node_address() const
This function returns the address of the parent node.
Definition node.hpp:173
typename std::vector< X1 >::const_iterator data_addr
Definition node.hpp:47
data_addr & data_address()
This function returns the const_iterator to a vector<X1> element (dataset element).
Definition node.hpp:158
node< X1 > & operator=(node< X1 > &&other) noexcept
Move assignment operator.
Definition node.hpp:111
void data_on_path(std::vector< data_addr > &vec) const
This function allows you to find a set of elements on a path - starting from the current node to the ...
Definition node.cpp:6
void change_children_capacity() const
A function that reduces the size of the vector of parent's children's addresses - from capacity() to ...
Definition node.hpp:199
node(node< X1 > *const &prev, const data_addr &pos)
This is the constructor designed to create the next node of the CMMTree.
Definition node.hpp:69
node(node< X1 > &&other) noexcept
Move constructor.
Definition node.hpp:102
node()
Dfefault constructor.
Definition node.hpp:78
node< X1 > *const & operator[](size_t i) const
The index operator for a vector of children of a given node.
Definition node.hpp:206
size_t children_number() const
The function returns the number of children of a given node.
Definition node.hpp:188
void all_siblings_addresses(std::vector< const node< X1 > * > &vec) const
This function allows you to obtain the addresses of all children of a parent.
Definition node.cpp:61
node(node< X1 > const &)=delete
Copy constructor.
void add_new_node(data_addr const &pos)
This function is used to create another CMMTree node.
Definition node.cpp:19
data_addr const & data_address() const
This function returns the const_iterator to a vector<X1> element (dataset element).
Definition node.hpp:150
std::vector< node< X1 > * > & childrenVec()
Function that allows direct access to a child vector.
Definition node.hpp:224
bool isNULLit(data_addr const &r) const
This function is used when there is a need to check whether it is possible to use the data to which r...
Definition node.hpp:127
~node()
Destructor.
Definition node.cpp:39
size_t siblings_number() const
The function returns the number of children of the parent of a given node.
Definition node.hpp:195
X1 const & node_value() const
The function allows you to retrieve (for reading) the value of an element of the input data set to wh...
Definition node.hpp:238
std::vector< node< X1 > * > const & childrenVec() const
Function that allows direct access to a child vector.
Definition node.hpp:217
node< X1 > const * this_node_address() const
The function returns the address of the node calling this function.
Definition node.hpp:180
std::vector< T >::const_iterator const & Null_iterator(T)
An iterator that is supposed to point to "nothing" of specified type ;).
Definition node.hpp:20
constexpr void println(auto const &a)
Definition utils.hpp:81