/usr/include/kdtree++/node.hpp is in libkdtree++-dev 0.7.1+git20101123-3build1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 | /** \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, typename NodeType>
inline
const NodeType*
_S_node_descend (const size_t __dim,
const _Cmp& __cmp, const _Acc& __acc,
const _Val& __val, const NodeType* __node)
{
if (_S_node_compare(__dim, __cmp, __acc, __val, __node->_M_value))
return static_cast<const NodeType *>(__node->_M_left);
return static_cast<const NodeType *>(__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 NodeType, typename _Cmp,
typename _Acc, typename _Dist,
typename _Predicate>
inline
std::pair<const NodeType*,
std::pair<size_t, typename _Dist::distance_type> >
_S_node_nearest (const size_t __k, size_t __dim, SearchVal const& __val,
const NodeType* __node, const _Node_base* __end,
const NodeType* __best, typename _Dist::distance_type __max,
const _Cmp& __cmp, const _Acc& __acc, const _Dist& __dist,
_Predicate __p)
{
typedef const NodeType* NodePtr;
NodePtr pcur = __node;
NodePtr 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(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, cur->_M_value);
d = std::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 = 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).
NodePtr probe = cur;
NodePtr pprobe = probe;
NodePtr near_node;
NodePtr far_node;
size_t probe_dim = cur_dim;
if (_S_node_compare(probe_dim % __k, __cmp, __acc, __val, probe->_M_value))
near_node = static_cast<NodePtr>(probe->_M_right);
else
near_node = static_cast<NodePtr>(probe->_M_left);
if (near_node
// only visit node's children if node's plane intersect hypersphere
&& (std::sqrt(_S_node_distance(probe_dim % __k, __dist, __acc, __val, probe->_M_value)) <= __max))
{
probe = near_node;
++probe_dim;
}
while (cur != __end)
{
while (probe != cur)
{
if (_S_node_compare(probe_dim % __k, __cmp, __acc, __val, probe->_M_value))
{
near_node = static_cast<NodePtr>(probe->_M_left);
far_node = static_cast<NodePtr>(probe->_M_right);
}
else
{
near_node = static_cast<NodePtr>(probe->_M_right);
far_node = static_cast<NodePtr>(probe->_M_left);
}
if (pprobe == probe->_M_parent) // going downward ...
{
if (__p(probe->_M_value))
{
typename _Dist::distance_type d = 0;
for (size_t i=0; i < __k; ++i)
d += _S_node_distance(i, __dist, __acc, __val, probe->_M_value);
d = std::sqrt(d);
if (d <= __max) // CHANGED, see the above notes ("bad candidate notes")
{
__best = probe;
__max = d;
__dim = probe_dim;
}
}
pprobe = probe;
if (near_node)
{
probe = near_node;
++probe_dim;
}
else if (far_node &&
// only visit node's children if node's plane intersect hypersphere
std::sqrt(_S_node_distance(probe_dim % __k, __dist, __acc, __val, probe->_M_value)) <= __max)
{
probe = far_node;
++probe_dim;
}
else
{
probe = static_cast<NodePtr>(probe->_M_parent);
--probe_dim;
}
}
else // ... and going upward.
{
if (pprobe == near_node && far_node
// only visit node's children if node's plane intersect hypersphere
&& std::sqrt(_S_node_distance(probe_dim % __k, __dist, __acc, __val, probe->_M_value)) <= __max)
{
pprobe = probe;
probe = far_node;
++probe_dim;
}
else
{
pprobe = probe;
probe = static_cast<NodePtr>(probe->_M_parent);
--probe_dim;
}
}
}
pcur = cur;
cur = static_cast<NodePtr>(cur->_M_parent);
--cur_dim;
pprobe = cur;
probe = cur;
probe_dim = cur_dim;
if (cur != __end)
{
if (pcur == cur->_M_left)
near_node = static_cast<NodePtr>(cur->_M_right);
else
near_node = static_cast<NodePtr>(cur->_M_left);
if (near_node
// only visit node's children if node's plane intersect hypersphere
&& (std::sqrt(_S_node_distance(cur_dim % __k, __dist, __acc, __val, cur->_M_value)) <= __max))
{
probe = near_node;
++probe_dim;
}
}
}
return std::pair<NodePtr,
std::pair<size_t, typename _Dist::distance_type> >
(__best, std::pair<size_t, typename _Dist::distance_type>
(__dim, __max));
}
} // 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.
*/
|