Commit 1f30cd30 authored by Administrator's avatar Administrator Committed by Florian Uhlig
Browse files

Move code from an external project to directory external

The code was copied at some time from an external project into the lx
directory. Since it fails the license header check the code was moved
to the externals directory. Also a second copy of the headers were deleted.
parent 52caa620
/** \file
* Defines the allocator interface as used by the KDTree class.
*
* \author Martin F. Krafft <libkdtree@pobox.madduck.net>
*/
#ifndef INCLUDE_KDTREE_ALLOCATOR_HPP
#define INCLUDE_KDTREE_ALLOCATOR_HPP
#include <cstddef>
#include "node.hpp"
namespace KDTree
{
template<typename _Tp, typename _Alloc>
class _Alloc_base {
public:
typedef _Node<_Tp> _Node_;
typedef typename _Node_::_Base_ptr _Base_ptr;
typedef _Alloc allocator_type;
_Alloc_base(allocator_type const& __A) : _M_node_allocator(__A) {}
allocator_type get_allocator() const { return _M_node_allocator; }
class NoLeakAlloc {
_Alloc_base* base;
_Node_* new_node;
public:
NoLeakAlloc(_Alloc_base* b) : base(b), new_node(base->_M_allocate_node()) {}
_Node_* get() { return new_node; }
void disconnect() { new_node = NULL; }
~NoLeakAlloc()
{
if (new_node) base->_M_deallocate_node(new_node);
}
};
protected:
allocator_type _M_node_allocator;
_Node_* _M_allocate_node() { return _M_node_allocator.allocate(1); }
void _M_deallocate_node(_Node_* const __P) { return _M_node_allocator.deallocate(__P, 1); }
void _M_construct_node(_Node_* __p, _Tp const __V = _Tp(), _Base_ptr const __PARENT = NULL,
_Base_ptr const __LEFT = NULL, _Base_ptr const __RIGHT = NULL)
{
new (__p) _Node_(__V, __PARENT, __LEFT, __RIGHT);
}
void _M_destroy_node(_Node_* __p) { _M_node_allocator.destroy(__p); }
};
} // namespace KDTree
#endif // include guard
/* COPYRIGHT --
*
* This file is part of libkdtree++, a C++ template KD-Tree sorting container.
* libkdtree++ is (c) 2004-2007 Martin F. Krafft <libkdtree@pobox.madduck.net>
* and Sylvain Bougerel <sylvain.bougerel.devel@gmail.com> distributed under the
* terms of the Artistic License 2.0. See the ./COPYING file in the source tree
* root for more information.
*
* THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES
* OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
*/
/** \file
* Defines the various functors and interfaces used for KDTree.
*
* \author Martin F. Krafft <libkdtree@pobox.madduck.net>
* \author Sylvain Bougerel <sylvain.bougerel.devel@gmail.com>
*/
#ifndef INCLUDE_KDTREE_ACCESSOR_HPP
#define INCLUDE_KDTREE_ACCESSOR_HPP
#include <cstddef>
namespace KDTree
{
template<typename _Val>
struct _Bracket_accessor {
typedef typename _Val::value_type result_type;
result_type operator()(_Val const& V, size_t const N) const { return V[N]; }
};
template<typename _Tp>
struct always_true {
bool operator()(const _Tp&) const { return true; }
};
template<typename _Tp, typename _Dist>
struct squared_difference {
typedef _Dist distance_type;
distance_type operator()(const _Tp& __a, const _Tp& __b) const
{
distance_type d = __a - __b;
return d * d;
}
};
template<typename _Tp, typename _Dist>
struct squared_difference_counted {
typedef _Dist distance_type;
squared_difference_counted() : _M_count(0) {}
void reset() { _M_count = 0; }
long& count() const { return _M_count; }
distance_type operator()(const _Tp& __a, const _Tp& __b) const
{
distance_type d = __a - __b;
++_M_count;
return d * d;
}
private:
mutable long _M_count;
};
} // namespace KDTree
#endif // include guard
/* COPYRIGHT --
*
* This file is part of libkdtree++, a C++ template KD-Tree sorting container.
* libkdtree++ is (c) 2004-2007 Martin F. Krafft <libkdtree@pobox.madduck.net>
* and Sylvain Bougerel <sylvain.bougerel.devel@gmail.com> distributed under the
* terms of the Artistic License 2.0. See the ./COPYING file in the source tree
* root for more information.
*
* THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES
* OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
*/
/** \file
* Defines interfaces for iterators as used by the KDTree class.
*
* \author Martin F. Krafft <libkdtree@pobox.madduck.net>
*/
#ifndef INCLUDE_KDTREE_ITERATOR_HPP
#define INCLUDE_KDTREE_ITERATOR_HPP
#include <iterator>
#include "node.hpp"
namespace KDTree
{
template<typename _Val, typename _Ref, typename _Ptr>
class _Iterator;
template<typename _Val, typename _Ref, typename _Ptr>
inline bool operator==(_Iterator<_Val, _Ref, _Ptr> const&, _Iterator<_Val, _Ref, _Ptr> const&);
template<typename _Val>
inline bool operator==(_Iterator<_Val, const _Val&, const _Val*> const&, _Iterator<_Val, _Val&, _Val*> const&);
template<typename _Val>
inline bool operator==(_Iterator<_Val, _Val&, _Val*> const&, _Iterator<_Val, const _Val&, const _Val*> const&);
template<typename _Val, typename _Ref, typename _Ptr>
inline bool operator!=(_Iterator<_Val, _Ref, _Ptr> const&, _Iterator<_Val, _Ref, _Ptr> const&);
template<typename _Val>
inline bool operator!=(_Iterator<_Val, const _Val&, const _Val*> const&, _Iterator<_Val, _Val&, _Val*> const&);
template<typename _Val>
inline bool operator!=(_Iterator<_Val, _Val&, _Val*> const&, _Iterator<_Val, const _Val&, const _Val*> const&);
class _Base_iterator {
protected:
typedef _Node_base::_Base_const_ptr _Base_const_ptr;
_Base_const_ptr _M_node;
inline _Base_iterator(_Base_const_ptr const __N = NULL) : _M_node(__N) {}
inline _Base_iterator(_Base_iterator const& __THAT) : _M_node(__THAT._M_node) {}
inline void _M_increment()
{
if (_M_node->_M_right) {
_M_node = _M_node->_M_right;
while (_M_node->_M_left)
_M_node = _M_node->_M_left;
}
else {
_Base_const_ptr __p = _M_node->_M_parent;
while (__p && _M_node == __p->_M_right) {
_M_node = __p;
__p = _M_node->_M_parent;
}
if (__p) // (__p) provide undetermined behavior on end()++ rather
// than a seg fault, similar to standard iterator.
_M_node = __p;
}
}
inline void _M_decrement()
{
if (!_M_node->_M_parent) // clearly identify the header node
{
_M_node = _M_node->_M_right;
}
else if (_M_node->_M_left) {
_Base_const_ptr x = _M_node->_M_left;
while (x->_M_right)
x = x->_M_right;
_M_node = x;
}
else {
_Base_const_ptr __p = _M_node->_M_parent;
while (__p && _M_node == __p->_M_left) // see below
{
_M_node = __p;
__p = _M_node->_M_parent;
}
if (__p) // (__p) provide undetermined behavior on rend()++ rather
// than a seg fault, similar to standard iterator.
_M_node = __p;
}
}
template<size_t const __K, typename _Val, typename _Acc, typename _Dist, typename _Cmp, typename _Alloc>
friend class KDTree;
};
template<typename _Val, typename _Ref, typename _Ptr>
class _Iterator : protected _Base_iterator {
public:
typedef _Val value_type;
typedef _Ref reference;
typedef _Ptr pointer;
typedef _Iterator<_Val, _Val&, _Val*> iterator;
typedef _Iterator<_Val, _Val const&, _Val const*> const_iterator;
typedef _Iterator<_Val, _Ref, _Ptr> _Self;
typedef _Node<_Val> const* _Link_const_type;
typedef std::bidirectional_iterator_tag iterator_category;
typedef ptrdiff_t difference_type;
inline _Iterator() : _Base_iterator() {}
inline _Iterator(_Link_const_type const __N) : _Base_iterator(__N) {}
inline _Iterator(iterator const& __THAT) : _Base_iterator(__THAT) {}
_Link_const_type get_raw_node() const { return _Link_const_type(_M_node); }
reference operator*() const { return _Link_const_type(_M_node)->_M_value; }
pointer operator->() const { return &(operator*()); }
_Self operator++()
{
_M_increment();
return *this;
}
_Self operator++(int)
{
_Self ret = *this;
_M_increment();
return ret;
}
_Self& operator--()
{
_M_decrement();
return *this;
}
_Self operator--(int)
{
_Self ret = *this;
_M_decrement();
return ret;
}
friend bool operator==<>(_Iterator<_Val, _Ref, _Ptr> const&, _Iterator<_Val, _Ref, _Ptr> const&);
friend bool operator==<>(_Iterator<_Val, const _Val&, const _Val*> const&, _Iterator<_Val, _Val&, _Val*> const&);
friend bool operator==<>(_Iterator<_Val, _Val&, _Val*> const&, _Iterator<_Val, const _Val&, const _Val*> const&);
friend bool operator!=<>(_Iterator<_Val, _Ref, _Ptr> const&, _Iterator<_Val, _Ref, _Ptr> const&);
friend bool operator!=<>(_Iterator<_Val, const _Val&, const _Val*> const&, _Iterator<_Val, _Val&, _Val*> const&);
friend bool operator!=<>(_Iterator<_Val, _Val&, _Val*> const&, _Iterator<_Val, const _Val&, const _Val*> const&);
};
template<typename _Val, typename _Ref, typename _Ptr>
inline bool operator==(_Iterator<_Val, _Ref, _Ptr> const& __X, _Iterator<_Val, _Ref, _Ptr> const& __Y)
{
return __X._M_node == __Y._M_node;
}
template<typename _Val>
inline bool operator==(_Iterator<_Val, const _Val&, const _Val*> const& __X, _Iterator<_Val, _Val&, _Val*> const& __Y)
{
return __X._M_node == __Y._M_node;
}
template<typename _Val>
inline bool operator==(_Iterator<_Val, _Val&, _Val*> const& __X, _Iterator<_Val, const _Val&, const _Val*> const& __Y)
{
return __X._M_node == __Y._M_node;
}
template<typename _Val, typename _Ref, typename _Ptr>
inline bool operator!=(_Iterator<_Val, _Ref, _Ptr> const& __X, _Iterator<_Val, _Ref, _Ptr> const& __Y)
{
return __X._M_node != __Y._M_node;
}
template<typename _Val>
inline bool operator!=(_Iterator<_Val, const _Val&, const _Val*> const& __X, _Iterator<_Val, _Val&, _Val*> const& __Y)
{
return __X._M_node != __Y._M_node;
}
template<typename _Val>
inline bool operator!=(_Iterator<_Val, _Val&, _Val*> const& __X, _Iterator<_Val, const _Val&, const _Val*> const& __Y)
{
return __X._M_node != __Y._M_node;
}
} // namespace KDTree
#endif // include guard
/* COPYRIGHT --
*
* This file is part of libkdtree++, a C++ template KD-Tree sorting container.
* libkdtree++ is (c) 2004-2007 Martin F. Krafft <libkdtree@pobox.madduck.net>
* and Sylvain Bougerel <sylvain.bougerel.devel@gmail.com> distributed under the
* terms of the Artistic License 2.0. See the ./COPYING file in the source tree
* root for more information.
*
* THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES
* OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
*/
This diff is collapsed.
/** \file
* Defines interfaces for nodes as used by the KDTree class.
*
* \author Martin F. Krafft <libkdtree@pobox.madduck.net>
*/
#ifndef INCLUDE_KDTREE_NODE_HPP
#define INCLUDE_KDTREE_NODE_HPP
#ifdef KDTREE_DEFINE_OSTREAM_OPERATORS
#include <ostream>
#endif
#include <cstddef>
#include <cmath>
namespace KDTree
{
struct _Node_base {
typedef _Node_base* _Base_ptr;
typedef _Node_base const* _Base_const_ptr;
_Base_ptr _M_parent;
_Base_ptr _M_left;
_Base_ptr _M_right;
_Node_base(_Base_ptr const __PARENT = NULL, _Base_ptr const __LEFT = NULL, _Base_ptr const __RIGHT = NULL)
: _M_parent(__PARENT)
, _M_left(__LEFT)
, _M_right(__RIGHT)
{
}
static _Base_ptr _S_minimum(_Base_ptr __x)
{
while (__x->_M_left)
__x = __x->_M_left;
return __x;
}
static _Base_ptr _S_maximum(_Base_ptr __x)
{
while (__x->_M_right)
__x = __x->_M_right;
return __x;
}
};
template<typename _Val>
struct _Node : public _Node_base {
using _Node_base::_Base_ptr;
typedef _Node* _Link_type;
_Val _M_value;
_Node(_Val const& __VALUE = _Val(), _Base_ptr const __PARENT = NULL, _Base_ptr const __LEFT = NULL,
_Base_ptr const __RIGHT = NULL)
: _Node_base(__PARENT, __LEFT, __RIGHT)
, _M_value(__VALUE)
{
}
#ifdef KDTREE_DEFINE_OSTREAM_OPERATORS
template<typename Char, typename Traits>
friend std::basic_ostream<Char, Traits>& operator<<(typename std::basic_ostream<Char, Traits>& out,
_Node_base const& node)
{
out << &node;
out << " parent: " << node._M_parent;
out << "; left: " << node._M_left;
out << "; right: " << node._M_right;
return out;
}
template<typename Char, typename Traits>
friend std::basic_ostream<Char, Traits>& operator<<(typename std::basic_ostream<Char, Traits>& out,
_Node<_Val> const& node)
{
out << &node;
out << ' ' << node._M_value;
out << "; parent: " << node._M_parent;
out << "; left: " << node._M_left;
out << "; right: " << node._M_right;
return out;
}
#endif
};
template<typename _Val, typename _Acc, typename _Cmp>
class _Node_compare {
public:
_Node_compare(size_t const __DIM, _Acc const& acc, _Cmp const& cmp) : _M_DIM(__DIM), _M_acc(acc), _M_cmp(cmp) {}
bool operator()(_Val const& __A, _Val const& __B) const { return _M_cmp(_M_acc(__A, _M_DIM), _M_acc(__B, _M_DIM)); }
private:
size_t _M_DIM; // don't make this const so that an assignment operator can be auto-generated
_Acc _M_acc;
_Cmp _M_cmp;
};
/*! Compare two values on the same dimension using a comparison functor _Cmp
and an accessor _Acc.
The comparison functor and the accessor are references to the template
parameters of the KDTree.
*/
template<typename _ValA, typename _ValB, typename _Cmp, typename _Acc>
inline bool _S_node_compare(const size_t __dim, const _Cmp& __cmp, const _Acc& __acc, const _ValA& __a,
const _ValB& __b)
{
return __cmp(__acc(__a, __dim), __acc(__b, __dim));
}
/*! Compute the distance between two values for one dimension only.
The distance functor and the accessor are references to the template
parameters of the KDTree.
*/
template<typename _ValA, typename _ValB, typename _Dist, typename _Acc>
inline typename _Dist::distance_type _S_node_distance(const size_t __dim, const _Dist& __dist, const _Acc& __acc,
const _ValA& __a, const _ValB& __b)
{
return __dist(__acc(__a, __dim), __acc(__b, __dim));
}
/*! Compute the distance between two values and accumulate the result for all
dimensions.
The distance functor and the accessor are references to the template
parameters of the KDTree.
*/
template<typename _ValA, typename _ValB, typename _Dist, typename _Acc>
inline typename _Dist::distance_type _S_accumulate_node_distance(const size_t __dim, const _Dist& __dist,
const _Acc& __acc, const _ValA& __a,
const _ValB& __b)
{
typename _Dist::distance_type d = 0;
for (size_t i = 0; i < __dim; ++i)
d += __dist(__acc(__a, i), __acc(__b, i));
return d;
}
/*! Descend on the left or the right of the node according to the comparison
between the node's value and the value.
\note it's the caller responsibility to check if node is NULL.
*/
template<typename _Val, typename _Cmp, typename _Acc>
inline _Node_base* _S_node_descend(const size_t __dim, const _Cmp& __cmp, const _Acc& __acc, const _Val& __val,
const _Node_base* __node)
{
if (_S_node_compare(__dim, __cmp, __acc, __val, static_cast<const _Node<_Val>*>(__node)->_M_value))
return __node->_M_left;
else
return __node->_M_right;
}
/*! Find the nearest node to __val from __node
If many nodes are equidistant to __val, the node with the lowest memory
address is returned.
\return the nearest node of __end node if no nearest node was found for the
given arguments.
*/
template<class SearchVal, typename _Val, typename _Cmp, typename _Acc, typename _Dist, typename _Predicate>
inline std::pair<const _Node<_Val>*, std::pair<size_t, typename _Dist::distance_type>>
_S_node_nearest(const size_t __k, size_t __dim, SearchVal const& __val, const _Node<_Val>* __node,
const _Node_base* __end, const _Node<_Val>* __best, typename _Dist::distance_type __max,
const _Cmp& __cmp, const _Acc& __acc, const _Dist& __dist, _Predicate __p)
{
const _Node_base* pcur = __node;
const _Node_base* cur = _S_node_descend(__dim % __k, __cmp, __acc, __val, __node);
size_t cur_dim = __dim + 1;
// find the smallest __max distance in direct descent
while (cur) {
if (__p(static_cast<const _Node<_Val>*>(cur)->_M_value)) {
typename _Dist::distance_type d = 0;
for (size_t i = 0; i != __k; ++i)
d += _S_node_distance(i, __dist, __acc, __val, static_cast<const _Node<_Val>*>(cur)->_M_value);
d = sqrt(d);
if (d <= __max)
// ("bad candidate notes")
// Changed: removed this test: || ( d == __max && cur < __best ))
// Can't do this optimisation without checking that the current 'best' is not the root AND is not a valid candidate...
// This is because find_nearest() etc will call this function with the best set to _M_root EVEN IF _M_root is not a valid answer (eg too far away or doesn't pass the predicate test)
{
__best = static_cast<const _Node<_Val>*>(cur);
__max = d;
__dim = cur_dim;
}
}
pcur = cur;
cur = _S_node_descend(cur_dim % __k, __cmp, __acc, __val, cur);
++cur_dim;
}
// Swap cur to prev, only prev is a valid node.
cur = pcur;
--cur_dim;
pcur = NULL;
// Probe all node's children not visited yet (siblings of the visited nodes).
const _Node_base* probe = cur;
const _Node_base* pprobe = probe;
const _Node_base* near_node;
const _Node_base* far_node;
size_t probe_dim = cur_dim;
if (_S_node_compare(probe_dim % __k, __cmp, __acc, __val, static_cast<const _Node<_Val>*>(probe)->_M_value))
near_node = probe->_M_right;
else
near_node = probe->_M_left;
if (near_node
// only visit node's children if node's plane intersect hypersphere
&& (sqrt(
_S_node_distance(probe_dim % __k, __dist, __acc, __val, static_cast<const _Node<_Val>*>(probe)->_M_value))
<= __max)) {