19 #ifdef SPATIAL_ENABLE_ASSERT
20 # ifndef SPATIAL_ASSERT_HPP_ONCE
21 # define SPATIAL_ASSERT_HPP_ONCE
50 inline void assert_fail
51 (
const char* msg,
const char* filename,
unsigned int line)
throw()
55 std::cerr << std::endl
56 <<
"Assertion failed (" << filename <<
":" << line
57 <<
"): '" << msg <<
"'" << std::endl;
67 template <
typename Key,
typename Value>
struct Kdtree_link;
68 template <
typename Key,
typename Value>
struct Relaxed_kdtree_link;
69 template <
typename Link>
struct Node;
70 template <
typename Rank,
typename Key,
typename Value,
typename Compare,
73 template <
typename Rank,
typename Key,
typename Value,
typename Compare,
74 typename Balancing,
typename Alloc>
77 template <
typename Value>
78 inline const typename Kdtree_link<Value, Value>::key_type&
79 const_key(
const Node<Kdtree_link<Value, Value> >* node);
80 template <
typename Key,
typename Value>
81 inline const typename Kdtree_link<Key, Value>::key_type&
82 const_key(
const Node<Kdtree_link<Key, Value> >* node);
83 template <
typename Key,
typename Value>
84 inline const Kdtree_link<Key, Value>*
85 const_link(
const Node<Kdtree_link<Key, Value> >* node);
87 template <
typename Value>
88 inline const typename Relaxed_kdtree_link<Value, Value>::key_type&
89 const_key(
const Node<Relaxed_kdtree_link<Value, Value> >* node);
90 template <
typename Key,
typename Value>
91 inline const typename Relaxed_kdtree_link<Key, Value>::key_type&
92 const_key(
const Node<Relaxed_kdtree_link<Key, Value> >* node);
93 template <
typename Key,
typename Value>
94 inline const Relaxed_kdtree_link<Key, Value>*
95 const_link(
const Node<Relaxed_kdtree_link<Key, Value> >* node);
108 template <
typename Compare,
typename Key,
typename Value>
111 assert_invariant_node
113 const details::Node<details::Kdtree_link<Key, Value> >* node)
116 const details::Node<details::Kdtree_link<Key, Value> >
117 *left = node->left, *right = node->right;
118 while (!
header(node->parent))
120 if (node->parent->left == node)
122 if (!cmp((depth - 1) % rank,
const_key(node),
128 if (cmp((depth - 1) % rank,
const_key(node),
136 ((!left || assert_invariant_node(cmp, rank, next_depth, left))
137 && (!right || assert_invariant_node(cmp, rank, next_depth, right)));
140 template <
typename Compare,
typename Key,
typename Value>
142 assert_invariant_node
144 const details::Node<details::Relaxed_kdtree_link<Key, Value> >* node)
147 const details::Node<details::Relaxed_kdtree_link<Key, Value> >
148 *left = node->left, *right = node->right;
149 while (!
header(node->parent))
151 if (node->parent->left == node)
153 if (cmp((depth - 1) % rank,
const_key(node->parent),
159 if (cmp((depth - 1) % rank,
const_key(node),
167 ((!left || assert_invariant_node(cmp, rank, next_depth, left))
168 && (!right || assert_invariant_node(cmp, rank, next_depth, right)));
177 template <
typename Container>
179 assert_invariant(
const Container& container)
181 typename Container::const_iterator::node_ptr node
182 = container.end().node->parent;
183 if (container.size() == 0 && node != node->parent)
186 return assert_invariant_node
187 (container.key_comp(), container.dimension(), 0, node);
199 template <
typename Compare,
typename Key,
typename Value>
204 const details::Node<details::Kdtree_link<Key, Value> >* node,
207 for (std::size_t i = 0; i <
depth; ++i) o <<
".";
208 if (
header(node->parent)) o <<
"T";
209 else if (node->parent->left == node) o <<
"L";
210 else if (node->parent->right == node) o <<
"R";
212 const details::Node<details::Kdtree_link<Key, Value> >
214 std::size_t test_depth =
depth;
215 while (!
header(test->parent))
217 if (test->parent->left == test)
219 if (!cmp((test_depth - 1) % rank,
const_key(test),
225 if (cmp((test_depth - 1) % rank,
const_key(test),
232 o <<
"<node:" << node <<
">{parent:" << node->parent
233 <<
" left:" << node->left <<
" right:" << node->right
238 assert_inspect_node(cmp, rank, o, node->left, depth + 1);
240 assert_inspect_node(cmp, rank, o, node->right, depth + 1);
244 template <
typename Compare,
typename Key,
typename Value>
248 const details::Node<details::Relaxed_kdtree_link<Key, Value> >* node,
252 assert_inspect_node(cmp, rank, o, node->left, depth + 1);
253 for (std::size_t i = 0; i <
depth; ++i) o <<
".";
254 if (
header(node->parent)) o <<
"T";
255 else if (node->parent->left == node) o <<
"L";
256 else if (node->parent->right == node) o <<
"R";
258 const details::Node<details::Relaxed_kdtree_link<Key, Value> >
260 std::size_t test_depth =
depth;
261 while (!
header(test->parent))
263 if (test->parent->left == test)
265 if (cmp((test_depth - 1) % rank,
const_key(test->parent),
271 if (cmp((test_depth - 1) % rank,
const_key(test),
278 o <<
"<node:" << node <<
">{parent:" << node->parent
279 <<
" left:" << node->left <<
" right:" << node->right
285 assert_inspect_node(cmp, rank, o, node->right, depth + 1);
311 template <
typename Rank,
typename Key,
typename Value,
typename Compare,
314 inline void assert_inspect
315 (
const char* msg,
const char* filename,
unsigned int line,
316 const details::Kdtree<Rank, Key, Value, Compare, Alloc>& tree)
throw()
320 std::cerr << std::endl
321 <<
"Assertion failed (" << filename <<
":" << line
322 <<
"): '" << msg <<
"' does not satisfy invariant"
324 std::cerr <<
"<Kdtree:" << &tree <<
">{" << std::endl;
325 std::cerr <<
"header:<node:" << tree.end().node
326 <<
">{parent:" << tree.end().node->parent
327 <<
" left:" << tree.end().node->left
328 <<
" right:" << tree.end().node->right
330 std::cerr <<
"leftmost:" << tree.begin().node
331 <<
" size:" << tree.size()
332 <<
" items:[" << std::endl;
333 if (tree.end().node->parent != tree.end().node)
334 assert_inspect_node(tree.key_comp(), tree.dimension(), std::cerr,
335 tree.end().node->parent, 0);
336 std::cerr <<
"]}" << std::endl;
342 template <
typename Rank,
typename Key,
typename Value,
typename Compare,
343 typename Balancing,
typename Alloc>
344 inline void assert_inspect
345 (
const char* msg,
const char* filename,
unsigned int line,
346 const details::Relaxed_kdtree<Rank, Key, Value, Compare, Balancing, Alloc>&
351 std::cerr << std::endl
352 <<
"Assertion failed (" << filename <<
":" << line
353 <<
"): '" << msg <<
"' does not satisfy invariant"
355 std::cerr <<
"<Relaxed_kdtree:" << &tree <<
">{" << std::endl;
356 std::cerr <<
"header:<node:" << tree.end().node
357 <<
">{parent:" << tree.end().node->parent
358 <<
" left:" << tree.end().node->left
359 <<
" right:" << tree.end().node->right
361 std::cerr <<
"leftmost:" << tree.begin().node
362 <<
" size:" << tree.size()
363 <<
" items:[" << std::endl;
364 if (tree.end().node->parent != tree.end().node)
365 assert_inspect_node(tree.key_comp(), tree.dimension(), std::cerr,
366 tree.end().node->parent, 0);
367 std::cerr <<
"]}" << std::endl;
378 #ifdef SPATIAL_ASSERT_CHECK
379 # undef SPATIAL_ASSERT_CHECK
382 #ifndef SPATIAL_ENABLE_ASSERT
403 # define SPATIAL_ASSERT_CHECK(expr)
426 # define SPATIAL_ASSERT_INVARIANT(container)
428 # define SPATIAL_ASSERT_CHECK(expr) \
430 ? static_cast<void>(0) \
431 : ::spatial::assert::assert_fail(#expr, __FILE__, __LINE__))
432 # define SPATIAL_ASSERT_INVARIANT(container) \
433 ((::spatial::assert::assert_invariant(container)) \
434 ? static_cast<void>(0) \
435 : ::spatial::assert::assert_inspect(#container, __FILE__, __LINE__, container))
const Kdtree_link< Value, Value >::key_type & const_key(const Node< Kdtree_link< Value, Value > > *node)
This function converts a pointer on a node into a key for a Kdtree_link type.
bool header(const Node< Link > *x)
Check if node is a header node.
std::size_t depth(const Iterator &iterator)
Returns the depth of a node's iterator.
std::size_t dimension_type
Defines the type for the dimension as being a size.
const Kdtree_link< Key, Value > * const_link(const Node< Kdtree_link< Key, Value > > *node)
This function converts a pointer on a node into a link for a Kdtree_link type.
The main namespace used in the library.