mlpack
hilbert_r_tree_auxiliary_information_impl.hpp
Go to the documentation of this file.
1 
13 #ifndef MLPACK_CORE_TREE_RECTANGLE_TREE_HR_TREE_AUXILIARY_INFO_IMPL_HPP
14 #define MLPACK_CORE_TREE_RECTANGLE_TREE_HR_TREE_AUXILIARY_INFO_IMPL_HPP
15 
17 
18 namespace mlpack {
19 namespace tree {
20 
21 template<typename TreeType,
22  template<typename> class HilbertValueType>
25 { }
26 
27 template<typename TreeType,
28  template<typename> class HilbertValueType>
30 HilbertRTreeAuxiliaryInformation(const TreeType* node) :
31  hilbertValue(node)
32 { }
33 
34 template<typename TreeType,
35  template<typename> class HilbertValueType>
39  TreeType* tree,
40  bool deepCopy) :
41  hilbertValue(other.HilbertValue(), tree, deepCopy)
42 { }
43 
44 template<typename TreeType,
45  template<typename> class HilbertValueType>
48  hilbertValue(std::move(other.hilbertValue))
49 { }
50 
51 template<typename TreeType,
52  template<typename> class HilbertValueType>
56 {
57  hilbertValue = other.hilbertValue;
58  return *this;
59 }
60 
61 template<typename TreeType,
62  template<typename> class HilbertValueType>
64 HandlePointInsertion(TreeType* node, const size_t point)
65 {
66  if (node->IsLeaf())
67  {
68  // Get the position at which the point should be inserted, and then update
69  // the largest Hilbert value of the node.
70  size_t pos = hilbertValue.InsertPoint(node, node->Dataset().col(point));
71 
72  // Move points.
73  for (size_t i = node->NumPoints(); i > pos; i--)
74  node->Point(i) = node->Point(i - 1);
75 
76  // Insert the point.
77  node->Point(pos) = point;
78  node->Count()++;
79  }
80  else
81  {
82  // Calculate the Hilbert value.
83  hilbertValue.InsertPoint(node, node->Dataset().col(point));
84  }
85 
86  return true;
87 }
88 
89 template<typename TreeType,
90  template<typename> class HilbertValueType>
92 HandleNodeInsertion(TreeType* node, TreeType* nodeToInsert, bool insertionLevel)
93 {
94  if (insertionLevel)
95  {
96  size_t pos;
97 
98  // Find the best position for the node being inserted.
99  // The node should be inserted according to its Hilbert value.
100  for (pos = 0; pos < node->NumChildren(); pos++)
101  if (HilbertValueType<ElemType>::CompareValues(
102  node->Child(pos).AuxiliaryInfo().HilbertValue(),
103  nodeToInsert->AuxiliaryInfo().HilbertValue()) < 0)
104  break;
105 
106  // Move nodes.
107  for (size_t i = node->NumChildren(); i > pos; i--)
108  node->children[i] = node->children[i - 1];
109 
110  // Insert the node.
111  node->children[pos] = nodeToInsert;
112  nodeToInsert->Parent() = node;
113 
114  // Update the largest Hilbert value.
115  hilbertValue.InsertNode(nodeToInsert);
116  }
117  else
118  hilbertValue.InsertNode(nodeToInsert); // Update the largest Hilbert value.
119 
120  return true;
121 }
122 
123 template<typename TreeType,
124  template<typename> class HilbertValueType>
126 HandlePointDeletion(TreeType* node, const size_t localIndex)
127 {
128  // Update the largest Hilbert value.
129  hilbertValue.DeletePoint(node, localIndex);
130 
131  for (size_t i = localIndex + 1; localIndex < node->NumPoints(); ++i)
132  node->Point(i - 1) = node->Point(i);
133 
134  node->NumPoints()--;
135  return true;
136 }
137 
138 template<typename TreeType,
139  template<typename> class HilbertValueType>
141 HandleNodeRemoval(TreeType* node, const size_t nodeIndex)
142 {
143  // Update the largest Hilbert value.
144  hilbertValue.RemoveNode(node, nodeIndex);
145 
146  for (size_t i = nodeIndex + 1; nodeIndex < node->NumChildren(); ++i)
147  node->children[i - 1] = node->children[i];
148 
149  node->NumChildren()--;
150  return true;
151 }
152 
153 template<typename TreeType,
154  template<typename> class HilbertValueType>
156 UpdateAuxiliaryInfo(TreeType* node)
157 {
158  if (node->IsLeaf()) // Should already be updated
159  return true;
160 
161  TreeType& child = node->Child(node->NumChildren() - 1);
162  if (hilbertValue.CompareWith(child.AuxiliaryInfo().HilbertValue()) < 0)
163  {
164  hilbertValue = child.AuxiliaryInfo().HilbertValue();
165  return true;
166  }
167  return false;
168 }
169 
170 template<typename TreeType,
171  template<typename> class HilbertValueType>
174 {
175  hilbertValue.NullifyData();
176 }
177 
178 template<typename TreeType,
179  template<typename> class HilbertValueType>
180 template<typename Archive>
182  Archive& ar,
183  const uint32_t /* version */)
184 {
185  ar(CEREAL_NVP(hilbertValue));
186 }
187 
188 
189 } // namespace tree
190 } // namespace mlpack
191 
192 #endif // MLPACK_CORE_TREE_RECTANGLE_TREE_HR_TREE_AUXILIARY_INFO_IMPL_HPP
bool HandleNodeInsertion(TreeType *node, TreeType *nodeToInsert, bool insertionLevel)
The Hilbert R tree requires to insert nodes according to their Hilbert value.
Definition: hilbert_r_tree_auxiliary_information_impl.hpp:92
Linear algebra utility functions, generally performed on matrices or vectors.
Definition: cv.hpp:1
Definition: hilbert_r_tree_auxiliary_information.hpp:22
bool UpdateAuxiliaryInfo(TreeType *node)
Update the auxiliary information in the node.
Definition: hilbert_r_tree_auxiliary_information_impl.hpp:156
Definition: pointer_wrapper.hpp:23
bool HandlePointInsertion(TreeType *node, const size_t point)
The Hilbert R tree requires to insert points according to their Hilbert value.
Definition: hilbert_r_tree_auxiliary_information_impl.hpp:64
bool HandleNodeRemoval(TreeType *node, const size_t nodeIndex)
The Hilbert R tree requires all nodes to be arranged according to their Hilbert value.
Definition: hilbert_r_tree_auxiliary_information_impl.hpp:141
void serialize(Archive &ar, const uint32_t)
Serialize the information.
Definition: hilbert_r_tree_auxiliary_information_impl.hpp:181
const HilbertValueType< ElemType > & HilbertValue() const
Return the largest Hilbert value of a point covered by the node.
Definition: hilbert_r_tree_auxiliary_information.hpp:133
void NullifyData()
Clear memory.
Definition: hilbert_r_tree_auxiliary_information_impl.hpp:173
bool HandlePointDeletion(TreeType *node, const size_t localIndex)
The Hilbert R tree requires all points to be arranged according to their Hilbert value.
Definition: hilbert_r_tree_auxiliary_information_impl.hpp:126
HilbertRTreeAuxiliaryInformation & operator=(const HilbertRTreeAuxiliaryInformation &other)
Copy the auxiliary information.
Definition: hilbert_r_tree_auxiliary_information_impl.hpp:54
HilbertRTreeAuxiliaryInformation()
Default constructor.
Definition: hilbert_r_tree_auxiliary_information_impl.hpp:24