13 #ifndef SPATIAL_ORDERED_HPP
14 #define SPATIAL_ORDERED_HPP
16 #include "../traits.hpp"
17 #include "spatial_bidirectional.hpp"
18 #include "spatial_rank.hpp"
19 #include "spatial_except.hpp"
34 <typename container_traits<Ct>::mode_type,
35 typename container_traits<Ct>::rank_type>
90 {
return increment_ordered(*
this); }
97 increment_ordered(*
this);
104 {
return decrement_ordered(*
this); }
111 decrement_ordered(*
this);
131 template<
typename Ct>
134 <typename container_traits<Ct>::mode_type,
135 typename container_traits<Ct>::rank_type>
185 :
Base(container.rank(), ptr, dim),
_cmp(container.key_comp())
196 {
return increment_ordered(*
this); }
203 increment_ordered(*
this);
210 {
return decrement_ordered(*
this); }
217 decrement_ordered(*
this);
254 template <
typename Container>
280 template <
typename Container>
303 template <
typename Container>
326 template <
typename Container>
350 template <
typename Container>
351 inline ordered_iterator<Container>
356 (container, container.
dimension() - 1, container.end().
node);
373 template <
typename Container>
374 inline ordered_iterator<const Container>
379 (container, container.
dimension() - 1, container.end().
node);
382 template <
typename Container>
383 inline ordered_iterator<const Container>
405 template <
typename Container>
406 inline ordered_iterator<Container>
409 if (container.empty())
return ordered_end(container);
411 it(container, 0, container.end().node->parent);
427 template <
typename Container>
428 inline ordered_iterator<const Container>
431 if (container.empty())
return ordered_end(container);
433 it(container, 0, container.end().node->parent);
437 template <
typename Container>
438 inline ordered_iterator<const Container>
445 template <
typename Cmp,
typename Rank,
typename Key>
448 const Key& a,
const Key& b)
452 if (cmp(d, a, b))
return true;
453 if (cmp(d, b, a))
return false;
460 template <
typename Container>
470 SPATIAL_ASSERT_CHECK(iter.
node != 0);
472 SPATIAL_ASSERT_CHECK(iter.
node_dim < rank());
477 node_ptr l_node, r_node; l_node = r_node = iter.
node;
479 bool left_ended =
false, right_ended =
false;
484 if (l_node->left != 0
488 l_node = l_node->left;
490 while (l_node->right != 0
492 || best == 0 || !cmp(l_dim,
const_key(best),
494 { l_node = l_node->right; l_dim =
incr_dim(rank, l_dim); }
500 { best = l_node; best_dim = l_dim; }
504 node_ptr p = l_node->parent;
505 while (!
header(p) && p->left == l_node)
520 { best = l_node; best_dim = l_dim; }
522 else left_ended =
true;
527 if (r_node->right != 0
532 r_node = r_node->right;
534 while (r_node->left != 0
538 { r_node = r_node->left; r_dim =
incr_dim(rank, r_dim); }
544 { best = r_node; best_dim = r_dim; }
549 node_ptr p = r_node->parent;
550 while (!
header(p) && p->right == r_node)
565 { best = r_node; best_dim = r_dim; }
568 else right_ended =
true;
574 if (++set_dim == rank())
break;
577 l_node = r_node = iter.
node;
584 SPATIAL_ASSERT_CHECK(r_dim == (rank() - 1));
585 SPATIAL_ASSERT_CHECK(iter.
node_dim < rank());
586 SPATIAL_ASSERT_CHECK(iter.
node != 0);
592 template <
typename Container>
602 SPATIAL_ASSERT_CHECK(iter.
node != 0);
604 SPATIAL_ASSERT_CHECK(iter.
node_dim < rank());
609 node_ptr l_node, r_node; l_node = r_node = iter.
node;
611 bool left_ended =
false, right_ended =
false;
616 if (l_node->left != 0
621 l_node = l_node->left;
623 while (l_node->right != 0
628 { l_node = l_node->right; l_dim =
incr_dim(rank, l_dim); }
634 { best = l_node; best_dim = l_dim; }
638 node_ptr p = l_node->parent;
639 while (!
header(p) && p->left == l_node)
654 { best = l_node; best_dim = l_dim; }
656 else left_ended =
true;
661 if (r_node->right != 0
666 r_node = r_node->right;
673 { r_node = r_node->left; r_dim =
incr_dim(rank, r_dim); }
679 { best = r_node; best_dim = r_dim; }
684 node_ptr p = r_node->parent;
685 while (!
header(p) && p->right == r_node)
700 { best = r_node; best_dim = r_dim; }
703 else right_ended =
true;
709 if (++set_dim == rank())
break;
712 l_node = r_node = iter.
node;
719 SPATIAL_ASSERT_CHECK(r_dim == (rank() - 1));
720 SPATIAL_ASSERT_CHECK(iter.
node_dim < rank());
721 SPATIAL_ASSERT_CHECK(iter.
node != 0);
725 template <
typename Container>
737 template <
typename Container>
747 SPATIAL_ASSERT_CHECK(iter.
node != 0);
748 SPATIAL_ASSERT_CHECK(iter.
node_dim < rank());
759 node_ptr l_node, r_node; l_node = r_node = iter.
node;
761 bool left_ended =
false, right_ended =
false;
766 if (l_node->left != 0
771 l_node = l_node->left;
777 { l_node = l_node->right; l_dim =
incr_dim(rank, l_dim); }
783 { best = l_node; best_dim = l_dim; }
787 node_ptr p = l_node->parent;
788 while (!
header(p) && p->left == l_node)
803 { best = l_node; best_dim = l_dim; }
805 else left_ended =
true;
810 if (r_node->right != 0
814 r_node = r_node->right;
820 { r_node = r_node->left; r_dim =
incr_dim(rank, r_dim); }
826 { best = r_node; best_dim = r_dim; }
831 node_ptr p = r_node->parent;
832 while (!
header(p) && p->right == r_node)
847 { best = r_node; best_dim = r_dim; }
850 else right_ended =
true;
856 if (++set_dim == rank())
break;
859 l_node = r_node = iter.
node;
866 SPATIAL_ASSERT_CHECK(r_dim == (rank() - 1));
867 SPATIAL_ASSERT_CHECK(iter.
node_dim < rank());
868 SPATIAL_ASSERT_CHECK(iter.
node != 0);
875 template <
typename Container>
885 SPATIAL_ASSERT_CHECK(iter.
node != 0);
886 SPATIAL_ASSERT_CHECK(iter.
node_dim < rank());
897 node_ptr l_node, r_node; l_node = r_node = iter.
node;
899 bool left_ended =
false, right_ended =
false;
904 if (l_node->left != 0
910 l_node = l_node->left;
916 { l_node = l_node->right; l_dim =
incr_dim(rank, l_dim); }
922 { best = l_node; best_dim = l_dim; }
926 node_ptr p = l_node->parent;
927 while (!
header(p) && p->left == l_node)
942 { best = l_node; best_dim = l_dim; }
944 else left_ended =
true;
953 r_node = r_node->right;
960 { r_node = r_node->left; r_dim =
incr_dim(rank, r_dim); }
966 { best = r_node; best_dim = r_dim; }
971 node_ptr p = r_node->parent;
972 while (!
header(p) && p->right == r_node)
987 { best = r_node; best_dim = r_dim; }
990 else right_ended =
true;
996 if (++set_dim == rank())
break;
999 l_node = r_node = iter.
node;
1006 SPATIAL_ASSERT_CHECK(r_dim == (rank() - 1));
1007 SPATIAL_ASSERT_CHECK(iter.
node_dim < rank());
1008 SPATIAL_ASSERT_CHECK(iter.
node != 0);
1012 template <
typename Container>
1023 template <
typename Container>
1032 SPATIAL_ASSERT_CHECK(iter.
node_dim < rank());
1034 SPATIAL_ASSERT_CHECK(iter.
node != 0);
1035 node_ptr end = iter.
node->parent;
1037 while (iter.
node->left != 0)
1042 node_ptr best = iter.
node;
1046 node_ptr node = iter.
node;
1050 if (node->right != 0
1051 && (node_dim > set_dim
1055 node_dim =
incr_dim(rank, node_dim);
1056 while (node->left != 0)
1057 { node = node->left; node_dim =
incr_dim(rank, node_dim); }
1060 { best = node; best_dim = node_dim; }
1064 node_ptr p = node->parent;
1065 while (p != end && p->right == node)
1068 node_dim =
decr_dim(rank, node_dim);
1072 node_dim =
decr_dim(rank, node_dim);
1076 { best = node; best_dim = node_dim; }
1078 }
while (node != end);
1079 }
while (++set_dim < rank());
1081 SPATIAL_ASSERT_CHECK(best_dim < rank());
1082 SPATIAL_ASSERT_CHECK(best != 0);
1083 SPATIAL_ASSERT_CHECK(!
header(best));
1089 template<
typename Container>
1099 SPATIAL_ASSERT_CHECK(iter.
node != 0);
1100 SPATIAL_ASSERT_CHECK(iter.
node_dim < rank());
1102 node_ptr end = iter.
node->parent;
1104 while (iter.
node->right != 0)
1109 node_ptr best = iter.
node;
1113 node_ptr node = iter.
node;
1118 && (node_dim > set_dim
1122 node_dim =
incr_dim(rank, node_dim);
1123 while (node->right != 0)
1124 { node = node->right; node_dim =
incr_dim(rank, node_dim); }
1127 { best = node; best_dim = node_dim; }
1131 node_ptr p = node->parent;
1132 while (p != end && p->left == node)
1135 node_dim =
decr_dim(rank, node_dim);
1139 node_dim =
decr_dim(rank, node_dim);
1143 { best = node; best_dim = node_dim; }
1145 }
while (node != end);
1146 }
while (++set_dim < rank());
1148 SPATIAL_ASSERT_CHECK(iter.
node_dim < rank());
1149 SPATIAL_ASSERT_CHECK(iter.
node != 0);
1156 template<
typename Container>
1166 SPATIAL_ASSERT_CHECK(iter.
node != 0);
1167 SPATIAL_ASSERT_CHECK(iter.
node_dim < rank());
1169 node_ptr end = iter.
node->parent;
1171 while (iter.
node->right != 0)
1176 node_ptr best = iter.
node;
1180 node_ptr node = iter.
node;
1185 if (node->left != 0 && node_dim > set_dim)
1188 node_dim =
incr_dim(rank, node_dim);
1189 while (node->right != 0)
1190 { node = node->right; node_dim =
incr_dim(rank, node_dim); }
1193 { best = node; best_dim = node_dim; }
1197 node_ptr p = node->parent;
1198 while (p != end && p->left == node)
1201 node_dim =
decr_dim(rank, node_dim);
1205 node_dim =
decr_dim(rank, node_dim);
1209 { best = node; best_dim = node_dim; }
1211 }
while (node != end);
1212 }
while (++set_dim < rank());
1214 SPATIAL_ASSERT_CHECK(iter.
node_dim < rank());
1215 SPATIAL_ASSERT_CHECK(iter.
node != 0);
1220 template <
typename Container>
1231 #endif // SPATIAL_ORDERED_HPP
ordered_iterator(const ordered_iterator< Ct > &iter)
Convertion of mutable iterator into a constant iterator is permitted.
ordered_iterator()
Build an uninitialized iterator.
All elements returned by this iterator are ordered from the smallest to the largest value of their ke...
const rank_type & rank() const
Return the current Rank type used by the iterator.
ordered_iterator< const Container > ordered_cbegin(const Container &container)
Finds the value in container for which its key has the smallest coordinate over the dimension ordered...
Link::invariant_category invariant_category(const Node< Link > *)
For a given node, this function returns the invariant category of the node.
bool order_ref(const Cmp &cmp, const Rank &rank, const Key &a, const Key &b)
node_ptr node
The pointer to the current node.
void check_dimension(dimension_type rank, dimension_type dimension)
Checks that dimension is not greater or equal to rank.
Tp::key_compare key_compare
Comparison functor used to compare two instances of key_type.
ordered_iterator()
Uninitialized iterator.
key_compare key_comp() const
Return the key_comparator used by the iterator.
dimension_type dimension() const
Return the number of dimensions stored by the Rank of the iterator.
ordered_iterator(Ct &container, dimension_type dim, typename container_traits< Ct >::mode_type::node_ptr ptr)
When the information of the dimension for the current node being pointed to by the iterator is known...
ordered_iterator< Ct > operator++(int)
Increments the iterator but returns the value of the iterator before the increment.
ordered_iterator< Container > ordered_begin(Container &container)
Finds the value in container for which its key has the smallest coordinate over the dimension ordered...
ordered_iterator< const Ct > & operator--()
Decrements the iterator and returns the decremented value.
ordered_iterator< Container > & maximum_ordered(ordered_iterator< Container > &iter)
Move the iterator given in parameter to the maximum value along the iterator's ordered dimension but ...
All elements returned by this iterator are ordered from the smallest to the largest value of their ke...
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.
ordered_iterator< Container > & minimum_ordered(ordered_iterator< Container > &iter)
Move the iterator given in parameter to the minimum value along the iterator's ordered dimension but ...
dimension_type node_dim
The dimension of the current node.
bool header(const Node< Link > *x)
Check if node is a header node.
container_traits< Ct >::key_compare key_compare
Alias for the key_compare type used by the iterator.
ordered_iterator< Container > & decrement_ordered(ordered_iterator< Container > &iter)
Move the pointer given in parameter to the previous element in the ordered iteration of values along ...
ordered_iterator< Ct > & operator++()
Increments the iterator and returns the incremented value.
ordered_iterator< Container > ordered_end(Container &container)
Finds the past-the-end position in container for this constant iterator.
ordered_iterator< const Ct > & operator++()
Increments the iterator and returns the incremented value.
The traits type for all containers in the spatial namespace.
container_traits< Ct >::mode_type::node_ptr node_ptr
The type for the node pointed to by the iterator.
Tp::mode_type mode_type
The Link Mode type that is associated with the container.
details::Const_bidirectional_iterator< typename container_traits< Ct >::mode_type, typename container_traits< Ct >::rank_type > Base
std::size_t dimension_type
Defines the type for the dimension as being a size.
ordered_iterator(const Ct &container, typename container_traits< Ct >::const_iterator iter)
The standard way to build this iterator: specify a ordered dimension, an iterator on a container...
Tp::rank_type rank_type
The type used to represent the rank of the container.
ordered_iterator< Ct > & operator--()
Decrements the iterator and returns the decremented value.
ordered_iterator< const Ct > operator++(int)
Increments the iterator but returns the value of the iterator before the increment.
The category of invariants for a k-d tree node: strict or relaxed.
ordered_iterator(Ct &container, typename container_traits< Ct >::iterator iter)
The standard way to build this iterator: specify a ordered dimension, an iterator on a container...
key_compare key_comp() const
Return the key_comparator used by the iterator.
The main namespace used in the library.
dimension_type decr_dim(Rank rank, dimension_type node_dim)
Decrement dimension node_dim, given rank.
ordered_iterator< const Container > ordered_cend(const Container &container)
Finds the past-the-end position in container for this constant iterator.
A common template for constant bidirectional iterators that work on identical modes of linking...
key_compare _cmp
The related data for the iterator.
ordered_iterator< const Ct > operator--(int)
Decrements the iterator but returns the value of the iterator before the decrement.
A common template for bidirectional iterators that work on identical modes of linking.
container_traits< Ct >::key_compare key_compare
ordered_iterator< Ct > operator--(int)
Decrements the iterator but returns the value of the iterator before the decrement.
details::Bidirectional_iterator< typename container_traits< Ct >::mode_type, typename container_traits< Ct >::rank_type > Base
ordered_iterator< Container > & increment_ordered(ordered_iterator< Container > &iter)
Move the pointer given in parameter to the next element in the ordered iteration of values along the ...
key_compare _cmp
The related data for the iterator.
dimension_type incr_dim(Rank rank, dimension_type node_dim)
Increment dimension node_dim, given rank.