99 class KDTree :
protected _Alloc_base<_Val, _Alloc>
102 typedef _Alloc_base<_Val, _Alloc> _Base;
103 typedef typename _Base::allocator_type allocator_type;
115 typedef _Val value_type;
116 typedef value_type* pointer;
117 typedef value_type
const* const_pointer;
118 typedef value_type& reference;
119 typedef value_type
const& const_reference;
120 typedef typename _Acc::result_type subvalue_type;
121 typedef typename _Dist::distance_type distance_type;
122 typedef size_t size_type;
123 typedef std::ptrdiff_t difference_type;
125 KDTree(_Acc
const& __acc = _Acc(), _Dist
const& __dist = _Dist(),
126 _Cmp
const& __cmp = _Cmp(),
const allocator_type& __a = allocator_type())
127 : _Base(__a), _M_header(),
128 _M_count(0), _M_acc(__acc), _M_cmp(__cmp), _M_dist(__dist)
130 _M_empty_initialise();
133 KDTree(
const KDTree& __x)
134 : _Base(__x.get_allocator()), _M_header(), _M_count(0),
135 _M_acc(__x._M_acc), _M_cmp(__x._M_cmp), _M_dist(__x._M_dist)
137 _M_empty_initialise();
146 std::vector<value_type> temp;
147 temp.reserve(__x.size());
148 std::copy(__x.begin(),__x.end(),std::back_inserter(temp));
149 _M_optimise(temp.begin(), temp.end(), 0);
152 template<
typename _InputIterator>
153 KDTree(_InputIterator __first, _InputIterator __last,
154 _Acc
const& acc = _Acc(), _Dist
const& __dist = _Dist(),
155 _Cmp
const& __cmp = _Cmp(),
const allocator_type& __a = allocator_type())
156 : _Base(__a), _M_header(), _M_count(0),
157 _M_acc(acc), _M_cmp(__cmp), _M_dist(__dist)
159 _M_empty_initialise();
168 std::vector<value_type> temp;
169 temp.reserve(std::distance(__first,__last));
170 std::copy(__first,__last,std::back_inserter(temp));
171 _M_optimise(temp.begin(), temp.end(), 0);
191 void efficient_replace_and_optimise( std::vector<value_type> & writable_vector )
194 _M_optimise(writable_vector.begin(), writable_vector.end(), 0);
200 operator=(
const KDTree& __x)
205 _M_dist = __x._M_dist;
215 std::vector<value_type> temp;
216 temp.reserve(__x.size());
217 std::copy(__x.begin(),__x.end(),std::back_inserter(temp));
218 efficient_replace_and_optimise(temp);
229 get_allocator()
const
231 return _Base::get_allocator();
243 return size_type(-1);
249 return this->size() == 0;
255 _M_erase_subtree(_M_get_root());
256 _M_set_leftmost(&_M_header);
257 _M_set_rightmost(&_M_header);
295 typedef _Iterator<_Val, const_reference, const_pointer> const_iterator;
297 typedef const_iterator iterator;
298 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
299 typedef std::reverse_iterator<iterator> reverse_iterator;
304 const_iterator begin()
const {
return const_iterator(_M_get_leftmost()); }
305 const_iterator end()
const {
return const_iterator(
static_cast<_Link_const_type
>(&_M_header)); }
306 const_reverse_iterator rbegin()
const {
return const_reverse_iterator(end()); }
307 const_reverse_iterator rend()
const {
return const_reverse_iterator(begin()); }
310 insert(iterator , const_reference __V)
312 return this->insert(__V);
316 insert(const_reference __V)
320 _Link_type __n = _M_new_node(__V, &_M_header);
323 _M_set_leftmost(__n);
324 _M_set_rightmost(__n);
325 return iterator(__n);
327 return _M_insert(_M_get_root(), __V, 0);
330 template <
class _InputIterator>
331 void insert(_InputIterator __first, _InputIterator __last) {
332 for (; __first != __last; ++__first)
333 this->insert(*__first);
337 insert(iterator __pos, size_type __n,
const value_type& __x)
339 for (; __n > 0; --__n)
340 this->insert(__pos, __x);
343 template<
typename _InputIterator>
345 insert(iterator __pos, _InputIterator __first, _InputIterator __last) {
346 for (; __first != __last; ++__first)
347 this->insert(__pos, *__first);
361 erase(const_reference __V) {
362 const_iterator b = this->
find(__V);
367 erase_exact(const_reference __V) {
368 this->erase(this->find_exact(__V));
373 erase(const_iterator
const& __IT)
375 assert(__IT != this->end());
376 _Link_const_type target = __IT.get_raw_node();
377 _Link_const_type n = target;
379 while ((n = _S_parent(n)) != &_M_header)
381 _M_erase(
const_cast<_Link_type
>(target), level );
382 _M_delete_node(
const_cast<_Link_type
>(target) );
406 template <
class SearchVal>
408 find(SearchVal
const& __V)
const
410 if (!_M_get_root())
return this->end();
411 return _M_find(_M_get_root(), __V, 0);
428 template <
class SearchVal>
430 find_exact(SearchVal
const& __V)
const
432 if (!_M_get_root())
return this->end();
433 return _M_find_exact(_M_get_root(), __V, 0);
437 count_within_range(const_reference __V, subvalue_type
const __R)
const
439 if (!_M_get_root())
return 0;
440 _Region_ __region(__V, __R, _M_acc, _M_cmp);
441 return this->count_within_range(__region);
445 count_within_range(_Region_
const& __REGION)
const
447 if (!_M_get_root())
return 0;
449 _Region_ __bounds(__REGION);
450 return _M_count_within_range(_M_get_root(),
451 __REGION, __bounds, 0);
454 template <
typename SearchVal,
class Visitor>
456 visit_within_range(SearchVal
const& V, subvalue_type
const R, Visitor visitor)
const
458 if (!_M_get_root())
return visitor;
459 _Region_ region(V, R, _M_acc, _M_cmp);
460 return this->visit_within_range(region, visitor);
463 template <
class Visitor>
465 visit_within_range(_Region_
const& REGION, Visitor visitor)
const
469 _Region_ bounds(REGION);
470 return _M_visit_within_range(visitor, _M_get_root(), REGION, bounds, 0);
476 find_within_range_iterative(const_reference __a, const_reference __b)
478 return const_iterator(begin());
481 template <
typename SearchVal,
typename _OutputIterator>
483 find_within_range(SearchVal
const& val, subvalue_type
const range,
484 _OutputIterator out)
const
486 if (!_M_get_root())
return out;
487 _Region_ region(val, range, _M_acc, _M_cmp);
488 return this->find_within_range(region, out);
491 template <
typename _OutputIterator>
493 find_within_range(_Region_
const& region,
494 _OutputIterator out)
const
498 _Region_ bounds(region);
499 out = _M_find_within_range(out, _M_get_root(),
505 template <
class SearchVal>
506 std::pair<const_iterator, distance_type>
507 find_nearest (SearchVal
const& __val)
const
511 std::pair<const _Node<_Val>*,
512 std::pair<size_type, typename _Acc::result_type> >
522 _M_get_root()->_M_value,
529 return std::pair<const_iterator, distance_type>
530 (best.first, best.second.second);
532 return std::pair<const_iterator, distance_type>(end(), 0);
535 template <
class SearchVal>
536 std::pair<const_iterator, distance_type>
537 find_nearest (SearchVal
const& __val, distance_type __max)
const
541 bool root_is_candidate =
false;
545 (__K, _M_dist, _M_acc, _M_get_root()->_M_value, __val));
546 if (root_dist <= __max)
548 root_is_candidate =
true;
552 std::pair<const _Node<_Val>*,
553 std::pair<size_type, typename _Acc::result_type> >
555 node, __max, _M_cmp, _M_acc, _M_dist,
558 if (root_is_candidate || best.first != _M_get_root())
559 return std::pair<const_iterator, distance_type>
560 (best.first, best.second.second);
562 return std::pair<const_iterator, distance_type>(end(), __max);
565 template <
class SearchVal,
class _Predicate>
566 std::pair<const_iterator, distance_type>
567 find_nearest_if (SearchVal
const& __val, distance_type __max,
568 _Predicate __p)
const
572 bool root_is_candidate =
false;
574 if (__p(_M_get_root()->_M_value))
578 (__K, _M_dist, _M_acc, _M_get_root()->_M_value, __val));
579 if (root_dist <= __max)
581 root_is_candidate =
true;
586 std::pair<const _Node<_Val>*,
587 std::pair<size_type, typename _Acc::result_type> >
589 node, __max, _M_cmp, _M_acc, _M_dist, __p);
591 if (root_is_candidate || best.first != _M_get_root())
592 return std::pair<const_iterator, distance_type>
593 (best.first, best.second.second);
595 return std::pair<const_iterator, distance_type>(end(), __max);
601 std::vector<value_type> __v(this->begin(),this->end());
603 _M_optimise(__v.begin(), __v.end(), 0);
614 _M_check_node(_M_get_root(),0);
619 void _M_check_children( _Link_const_type child, _Link_const_type parent, size_type
const level,
bool to_the_left )
624 _Node_compare_ compare(level % __K, _M_acc, _M_cmp);
628 assert(!to_the_left || !compare(parent->_M_value,child->_M_value));
629 assert(to_the_left || !compare(child->_M_value,parent->_M_value));
631 _M_check_children(_S_left(child),parent,level,to_the_left);
632 _M_check_children(_S_right(child),parent,level,to_the_left);
636 void _M_check_node( _Link_const_type node, size_type
const level )
642 _M_check_children( _S_left(node), node, level,
true );
644 _M_check_children( _S_right(node), node, level,
false );
646 _M_check_node( _S_left(node), level+1 );
647 _M_check_node( _S_right(node), level+1 );
651 void _M_empty_initialise()
653 _M_set_leftmost(&_M_header);
654 _M_set_rightmost(&_M_header);
655 _M_header._M_parent = NULL;
660 _M_insert_left(_Link_type __N, const_reference __V)
662 _S_set_left(__N, _M_new_node(__V)); ++_M_count;
663 _S_set_parent( _S_left(__N), __N );
664 if (__N == _M_get_leftmost())
665 _M_set_leftmost( _S_left(__N) );
666 return iterator(_S_left(__N));
670 _M_insert_right(_Link_type __N, const_reference __V)
672 _S_set_right(__N, _M_new_node(__V)); ++_M_count;
673 _S_set_parent( _S_right(__N), __N );
674 if (__N == _M_get_rightmost())
675 _M_set_rightmost( _S_right(__N) );
676 return iterator(_S_right(__N));
680 _M_insert(_Link_type __N, const_reference __V,
683 if (_Node_compare_(__L % __K, _M_acc, _M_cmp)(__V, __N->_M_value))
686 return _M_insert_left(__N, __V);
687 return _M_insert(_S_left(__N), __V, __L+1);
691 if (!_S_right(__N) || __N == _M_get_rightmost())
692 return _M_insert_right(__N, __V);
693 return _M_insert(_S_right(__N), __V, __L+1);
698 _M_erase(_Link_type dead_dad, size_type
const level)
701 _Link_type step_dad = _M_get_erase_replacement(dead_dad, level);
704 if (dead_dad == _M_get_root())
705 _M_set_root(step_dad);
706 else if (_S_left(_S_parent(dead_dad)) == dead_dad)
707 _S_set_left(_S_parent(dead_dad), step_dad);
709 _S_set_right(_S_parent(dead_dad), step_dad);
714 if (dead_dad == _M_get_leftmost())
715 _M_set_leftmost( (step_dad ? step_dad : _S_parent(dead_dad)) );
716 if (dead_dad == _M_get_rightmost())
717 _M_set_rightmost( (step_dad ? step_dad : _S_parent(dead_dad)) );
722 _S_set_parent(step_dad, _S_parent(dead_dad));
725 if (_S_left(dead_dad))
726 _S_set_parent(_S_left(dead_dad), step_dad);
727 if (_S_right(dead_dad))
728 _S_set_parent(_S_right(dead_dad), step_dad);
731 _S_set_left(step_dad, _S_left(dead_dad));
732 _S_set_right(step_dad, _S_right(dead_dad));
741 _M_get_erase_replacement(_Link_type node, size_type
const level)
744 if (_S_is_leaf(node))
747 std::pair<_Link_type,size_type> candidate;
750 candidate = _M_get_j_min( std::pair<_Link_type,size_type>(_S_right(node),level), level+1);
752 else if ((!_S_right(node)))
753 candidate = _M_get_j_max( std::pair<_Link_type,size_type>(_S_left(node),level), level+1);
763 _Node_compare_ compare(level % __K, _M_acc, _M_cmp);
766 if (compare(_S_right(node)->_M_value, _S_left(node)->_M_value))
768 candidate = _M_get_j_min(std::pair<_Link_type,size_type>(_S_right(node),level), level+1);
770 candidate = _M_get_j_max( std::pair<_Link_type,size_type>(_S_left(node),level), level+1);
776 _Link_type parent = _S_parent(candidate.first);
777 if (_S_left(parent) == candidate.first)
778 _S_set_left(parent, _M_erase(candidate.first, candidate.second));
780 _S_set_right(parent, _M_erase(candidate.first, candidate.second));
782 return candidate.first;
787 std::pair<_Link_type,size_type>
788 _M_get_j_min( std::pair<_Link_type,size_type>
const node, size_type
const level)
790 typedef std::pair<_Link_type,size_type> Result;
791 if (_S_is_leaf(node.first))
792 return Result(node.first,level);
794 _Node_compare_ compare(node.second % __K, _M_acc, _M_cmp);
795 Result candidate = node;
796 if (_S_left(node.first))
798 Result left = _M_get_j_min(Result(_S_left(node.first), node.second), level+1);
799 if (compare(left.first->_M_value, candidate.first->_M_value))
802 if (_S_right(node.first))
804 Result right = _M_get_j_min( Result(_S_right(node.first),node.second), level+1);
805 if (compare(right.first->_M_value, candidate.first->_M_value))
808 if (candidate.first == node.first)
809 return Result(candidate.first,level);
816 std::pair<_Link_type,size_type>
817 _M_get_j_max( std::pair<_Link_type,size_type>
const node, size_type
const level)
819 typedef std::pair<_Link_type,size_type> Result;
821 if (_S_is_leaf(node.first))
822 return Result(node.first,level);
824 _Node_compare_ compare(node.second % __K, _M_acc, _M_cmp);
825 Result candidate = node;
826 if (_S_left(node.first))
828 Result left = _M_get_j_max( Result(_S_left(node.first),node.second), level+1);
829 if (compare(candidate.first->_M_value, left.first->_M_value))
832 if (_S_right(node.first))
834 Result right = _M_get_j_max(Result(_S_right(node.first),node.second), level+1);
835 if (compare(candidate.first->_M_value, right.first->_M_value))
839 if (candidate.first == node.first)
840 return Result(candidate.first,level);
847 _M_erase_subtree(_Link_type __n)
851 _M_erase_subtree(_S_right(__n));
852 _Link_type __t = _S_left(__n);
859 _M_find(_Link_const_type node, const_reference value, size_type
const level)
const
865 const_iterator found = this->end();
867 _Node_compare_ compare(level % __K, _M_acc, _M_cmp);
868 if (!compare(node->_M_value,value))
871 if (_M_matches_node(node, value, level))
872 return const_iterator(node);
874 found = _M_find(_S_left(node), value, level+1);
876 if ( _S_right(node) && found == this->end() && !compare(value,node->_M_value))
877 found = _M_find(_S_right(node), value, level+1);
882 _M_find_exact(_Link_const_type node, const_reference value, size_type
const level)
const
888 const_iterator found = this->end();
890 _Node_compare_ compare(level % __K, _M_acc, _M_cmp);
891 if (!compare(node->_M_value,value))
894 if (value == *const_iterator(node))
895 return const_iterator(node);
897 found = _M_find_exact(_S_left(node), value, level+1);
901 if ( _S_right(node) && found == this->end() && !compare(value,node->_M_value))
902 found = _M_find_exact(_S_right(node), value, level+1);
907 _M_matches_node_in_d(_Link_const_type __N, const_reference __V,
908 size_type
const __L)
const
910 _Node_compare_ compare(__L % __K, _M_acc, _M_cmp);
911 return !(compare(__N->_M_value, __V) || compare(__V, __N->_M_value));
915 _M_matches_node_in_other_ds(_Link_const_type __N, const_reference __V,
916 size_type
const __L = 0)
const
919 while ((__i = (__i + 1) % __K) != __L % __K)
920 if (!_M_matches_node_in_d(__N, __V, __i))
return false;
925 _M_matches_node(_Link_const_type __N, const_reference __V,
926 size_type __L = 0)
const
928 return _M_matches_node_in_d(__N, __V, __L)
929 && _M_matches_node_in_other_ds(__N, __V, __L);
933 _M_count_within_range(_Link_const_type __N, _Region_
const& __REGION,
934 _Region_
const& __BOUNDS,
935 size_type
const __L)
const
938 if (__REGION.encloses(_S_value(__N)))
944 _Region_ __bounds(__BOUNDS);
945 __bounds.set_high_bound(_S_value(__N), __L);
946 if (__REGION.intersects_with(__bounds))
947 count += _M_count_within_range(_S_left(__N),
948 __REGION, __bounds, __L+1);
952 _Region_ __bounds(__BOUNDS);
953 __bounds.set_low_bound(_S_value(__N), __L);
954 if (__REGION.intersects_with(__bounds))
955 count += _M_count_within_range(_S_right(__N),
956 __REGION, __bounds, __L+1);
963 template <
class Visitor>
965 _M_visit_within_range(Visitor visitor,
966 _Link_const_type N, _Region_
const& REGION,
967 _Region_
const& BOUNDS,
968 size_type
const L)
const
970 if (REGION.encloses(_S_value(N)))
972 visitor(_S_value(N));
976 _Region_ bounds(BOUNDS);
977 bounds.set_high_bound(_S_value(N), L);
978 if (REGION.intersects_with(bounds))
979 visitor = _M_visit_within_range(visitor, _S_left(N),
980 REGION, bounds, L+1);
984 _Region_ bounds(BOUNDS);
985 bounds.set_low_bound(_S_value(N), L);
986 if (REGION.intersects_with(bounds))
987 visitor = _M_visit_within_range(visitor, _S_right(N),
988 REGION, bounds, L+1);
996 template <
typename _OutputIterator>
998 _M_find_within_range(_OutputIterator out,
999 _Link_const_type __N, _Region_
const& __REGION,
1000 _Region_
const& __BOUNDS,
1001 size_type
const __L)
const
1003 if (__REGION.encloses(_S_value(__N)))
1005 *out++ = _S_value(__N);
1009 _Region_ __bounds(__BOUNDS);
1010 __bounds.set_high_bound(_S_value(__N), __L);
1011 if (__REGION.intersects_with(__bounds))
1012 out = _M_find_within_range(out, _S_left(__N),
1013 __REGION, __bounds, __L+1);
1017 _Region_ __bounds(__BOUNDS);
1018 __bounds.set_low_bound(_S_value(__N), __L);
1019 if (__REGION.intersects_with(__bounds))
1020 out = _M_find_within_range(out, _S_right(__N),
1021 __REGION, __bounds, __L+1);
1028 template <
typename _Iter>
1030 _M_optimise(_Iter
const& __A, _Iter
const& __B,
1031 size_type
const __L)
1033 if (__A == __B)
return;
1034 _Node_compare_ compare(__L % __K, _M_acc, _M_cmp);
1035 _Iter __m = __A + (__B - __A) / 2;
1036 std::nth_element(__A, __m, __B, compare);
1038 if (__m != __A) _M_optimise(__A, __m, __L+1);
1039 if (++__m != __B) _M_optimise(__m, __B, __L+1);
1045 return const_cast<_Link_const_type
>(_M_root);
1054 void _M_set_root(_Link_type n)
1060 _M_get_leftmost()
const
1062 return static_cast<_Link_type
>(_M_header._M_left);
1068 _M_header._M_left = a;
1072 _M_get_rightmost()
const
1074 return static_cast<_Link_type
>( _M_header._M_right );
1080 _M_header._M_right = a;
1084 _S_parent(_Base_ptr N)
1086 return static_cast<_Link_type
>( N->_M_parent );
1089 static _Link_const_type
1090 _S_parent(_Base_const_ptr N)
1092 return static_cast<_Link_const_type
>( N->_M_parent );
1096 _S_set_parent(_Base_ptr N, _Base_ptr p)
1102 _S_set_left(_Base_ptr N, _Base_ptr l)
1108 _S_left(_Base_ptr N)
1110 return static_cast<_Link_type
>( N->_M_left );
1113 static _Link_const_type
1114 _S_left(_Base_const_ptr N)
1116 return static_cast<_Link_const_type
>( N->_M_left );
1120 _S_set_right(_Base_ptr N, _Base_ptr r)
1126 _S_right(_Base_ptr N)
1128 return static_cast<_Link_type
>( N->_M_right );
1131 static _Link_const_type
1132 _S_right(_Base_const_ptr N)
1134 return static_cast<_Link_const_type
>( N->_M_right );
1138 _S_is_leaf(_Base_const_ptr N)
1140 return !_S_left(N) && !_S_right(N);
1143 static const_reference
1144 _S_value(_Link_const_type N)
1149 static const_reference
1150 _S_value(_Base_const_ptr N)
1152 return static_cast<_Link_const_type
>(N)->_M_value;
1155 static _Link_const_type
1156 _S_minimum(_Link_const_type __X)
1158 return static_cast<_Link_const_type
> ( _Node_base::_S_minimum(__X) );
1161 static _Link_const_type
1162 _S_maximum(_Link_const_type __X)
1164 return static_cast<_Link_const_type
>( _Node_base::_S_maximum(__X) );
1169 _M_new_node(const_reference __V,
1170 _Base_ptr
const __PARENT = NULL,
1171 _Base_ptr
const __LEFT = NULL,
1172 _Base_ptr
const __RIGHT = NULL)
1174 typename _Base::NoLeakAlloc noleak(
this);
1175 _Link_type new_node = noleak.get();
1176 this->_M_construct_node(new_node, __V, __PARENT, __LEFT, __RIGHT);
1177 noleak.disconnect();
1192 _M_delete_node(_Link_type __p)
1194 this->_M_destroy_node(__p);
1195 this->_M_deallocate_node(__p);
1205#ifdef KDTREE_DEFINE_OSTREAM_OPERATORS
1206 friend std::ostream&
1207 operator<<(std::ostream& o,
1210 o <<
"meta node: " << tree._M_header << std::endl;
1211 o <<
"root node: " << tree._M_root << std::endl;
1214 return o <<
"[empty " << __K <<
"d-tree " << &tree <<
"]";
1216 o <<
"nodes total: " << tree.size() << std::endl;
1217 o <<
"dimensions: " << __K << std::endl;
1222 std::stack<_Link_const_type> s;
1223 s.push(tree._M_get_root());
1227 _Link_const_type n = s.top();
1229 o << *n << std::endl;
1230 if (_Tree::_S_left(n)) s.push(_Tree::_S_left(n));
1231 if (_Tree::_S_right(n)) s.push(_Tree::_S_right(n));