@@ -157,6 +157,39 @@ inline typename std::enable_if<!has_assign<Container>::value, void>::type
157
157
for (size_t i = 0 ; i < nElements; i++) c[i] = value;
158
158
}
159
159
160
+
161
+ /* * operator "<" for std::sort() */
162
+ struct IndexDist_Sorter
163
+ {
164
+ /* * PairType will be typically: ResultItem<IndexType,DistanceType> */
165
+ template <typename PairType>
166
+ bool operator ()(const PairType& p1, const PairType& p2) const
167
+ {
168
+ return p1.second < p2.second ;
169
+ }
170
+ };
171
+
172
+ /* *
173
+ * Each result element in RadiusResultSet. Note that distances and indices
174
+ * are named `first` and `second` to keep backward-compatibility with the
175
+ * `std::pair<>` type used in the past. In contrast, this structure is ensured
176
+ * to be `std::is_standard_layout` so it can be used in wrappers to other
177
+ * languages.
178
+ * See: https://github.com/jlblancoc/nanoflann/issues/166
179
+ */
180
+ template <typename IndexType = size_t , typename DistanceType = double >
181
+ struct ResultItem
182
+ {
183
+ ResultItem () = default ;
184
+ ResultItem (const IndexType index, const DistanceType distance)
185
+ : first(index), second(distance)
186
+ {
187
+ }
188
+
189
+ IndexType first; // !< Index of the sample in the dataset
190
+ DistanceType second; // !< Distance from sample to query point
191
+ };
192
+
160
193
/* * @addtogroup result_sets_grp Result set classes
161
194
* @{ */
162
195
@@ -237,6 +270,11 @@ class KNNResultSet
237
270
}
238
271
239
272
DistanceType worstDist () const { return dists[capacity - 1 ]; }
273
+
274
+ void sort ()
275
+ {
276
+ // already sorted
277
+ }
240
278
};
241
279
242
280
/* * Result set for RKNN searches (N-closest neighbors with a maximum radius) */
@@ -321,38 +359,11 @@ class RKNNResultSet
321
359
}
322
360
323
361
DistanceType worstDist () const { return dists[capacity - 1 ]; }
324
- };
325
362
326
- /* * operator "<" for std::sort() */
327
- struct IndexDist_Sorter
328
- {
329
- /* * PairType will be typically: ResultItem<IndexType,DistanceType> */
330
- template <typename PairType>
331
- bool operator ()(const PairType& p1, const PairType& p2) const
332
- {
333
- return p1.second < p2.second ;
334
- }
335
- };
336
-
337
- /* *
338
- * Each result element in RadiusResultSet. Note that distances and indices
339
- * are named `first` and `second` to keep backward-compatibility with the
340
- * `std::pair<>` type used in the past. In contrast, this structure is ensured
341
- * to be `std::is_standard_layout` so it can be used in wrappers to other
342
- * languages.
343
- * See: https://github.com/jlblancoc/nanoflann/issues/166
344
- */
345
- template <typename IndexType = size_t , typename DistanceType = double >
346
- struct ResultItem
347
- {
348
- ResultItem () = default ;
349
- ResultItem (const IndexType index, const DistanceType distance)
350
- : first(index), second(distance)
363
+ void sort ()
351
364
{
365
+ // already sorted
352
366
}
353
-
354
- IndexType first; // !< Index of the sample in the dataset
355
- DistanceType second; // !< Distance from sample to query point
356
367
};
357
368
358
369
/* *
@@ -413,6 +424,12 @@ class RadiusResultSet
413
424
m_indices_dists.begin (), m_indices_dists.end (), IndexDist_Sorter ());
414
425
return *it;
415
426
}
427
+
428
+ void sort ()
429
+ {
430
+ std::sort (
431
+ m_indices_dists.begin (), m_indices_dists.end (), IndexDist_Sorter ());
432
+ }
416
433
};
417
434
418
435
/* * @} */
@@ -980,7 +997,7 @@ struct array_or_vector<-1, T>
980
997
*/
981
998
template <
982
999
class Derived , typename Distance, class DatasetAdaptor , int32_t DIM = -1 ,
983
- typename IndexType = uint32_t >
1000
+ typename index_t = uint32_t >
984
1001
class KDTreeBaseClass
985
1002
{
986
1003
public:
@@ -995,6 +1012,7 @@ class KDTreeBaseClass
995
1012
996
1013
using ElementType = typename Distance::ElementType;
997
1014
using DistanceType = typename Distance::DistanceType;
1015
+ using IndexType = index_t ;
998
1016
999
1017
/* *
1000
1018
* Array of indices to vectors in the dataset_.
@@ -1239,10 +1257,7 @@ class KDTreeBaseClass
1239
1257
std::ref (right_bbox), std::ref (thread_count),
1240
1258
std::ref (mutex));
1241
1259
}
1242
- else
1243
- {
1244
- --thread_count;
1245
- }
1260
+ else { --thread_count; }
1246
1261
1247
1262
BoundingBox left_bbox (bbox);
1248
1263
left_bbox[cutfeat].high = cutval;
@@ -1495,17 +1510,17 @@ class KDTreeBaseClass
1495
1510
*/
1496
1511
template <
1497
1512
typename Distance, class DatasetAdaptor , int32_t DIM = -1 ,
1498
- typename IndexType = uint32_t >
1513
+ typename index_t = uint32_t >
1499
1514
class KDTreeSingleIndexAdaptor
1500
1515
: public KDTreeBaseClass<
1501
- KDTreeSingleIndexAdaptor<Distance, DatasetAdaptor, DIM, IndexType >,
1502
- Distance, DatasetAdaptor, DIM, IndexType >
1516
+ KDTreeSingleIndexAdaptor<Distance, DatasetAdaptor, DIM, index_t >,
1517
+ Distance, DatasetAdaptor, DIM, index_t >
1503
1518
{
1504
1519
public:
1505
1520
/* * Deleted copy constructor*/
1506
1521
explicit KDTreeSingleIndexAdaptor (
1507
1522
const KDTreeSingleIndexAdaptor<
1508
- Distance, DatasetAdaptor, DIM, IndexType >&) = delete;
1523
+ Distance, DatasetAdaptor, DIM, index_t >&) = delete;
1509
1524
1510
1525
/* * The data source used by this index */
1511
1526
const DatasetAdaptor& dataset_;
@@ -1516,15 +1531,16 @@ class KDTreeSingleIndexAdaptor
1516
1531
1517
1532
using Base = typename nanoflann::KDTreeBaseClass<
1518
1533
nanoflann::KDTreeSingleIndexAdaptor<
1519
- Distance, DatasetAdaptor, DIM, IndexType >,
1520
- Distance, DatasetAdaptor, DIM, IndexType >;
1534
+ Distance, DatasetAdaptor, DIM, index_t >,
1535
+ Distance, DatasetAdaptor, DIM, index_t >;
1521
1536
1522
1537
using Offset = typename Base::Offset;
1523
1538
using Size = typename Base::Size ;
1524
1539
using Dimension = typename Base::Dimension;
1525
1540
1526
1541
using ElementType = typename Base::ElementType;
1527
1542
using DistanceType = typename Base::DistanceType;
1543
+ using IndexType = typename Base::IndexType;
1528
1544
1529
1545
using Node = typename Base::Node;
1530
1546
using NodePtr = Node*;
@@ -1677,6 +1693,9 @@ class KDTreeSingleIndexAdaptor
1677
1693
assign (dists, (DIM > 0 ? DIM : Base::dim_), zero);
1678
1694
DistanceType dist = this ->computeInitialDistances (*this , vec, dists);
1679
1695
searchLevel (result, vec, Base::root_node_, dist, dists, epsError);
1696
+
1697
+ if (searchParams.sorted ) result.sort ();
1698
+
1680
1699
return result.full ();
1681
1700
}
1682
1701
@@ -1733,9 +1752,6 @@ class KDTreeSingleIndexAdaptor
1733
1752
radius, IndicesDists);
1734
1753
const Size nFound =
1735
1754
radiusSearchCustomCallback (query_point, resultSet, searchParams);
1736
- if (searchParams.sorted )
1737
- std::sort (
1738
- IndicesDists.begin (), IndicesDists.end (), IndexDist_Sorter ());
1739
1755
return nFound;
1740
1756
}
1741
1757
@@ -1848,7 +1864,7 @@ class KDTreeSingleIndexAdaptor
1848
1864
{
1849
1865
const IndexType accessor = Base::vAcc_[i]; // reorder... : i;
1850
1866
DistanceType dist = distance_.evalMetric (
1851
- vec, accessor, (DIM > 0 ? DIM : Base::dim_));
1867
+ vec, accessor, (DIM > 0 ? DIM : Base::dim_));
1852
1868
if (dist < worst_dist)
1853
1869
{
1854
1870
if (!result_set.addPoint (dist, Base::vAcc_[i]))
@@ -2202,9 +2218,6 @@ class KDTreeSingleIndexDynamicAdaptor_
2202
2218
radius, IndicesDists);
2203
2219
const size_t nFound =
2204
2220
radiusSearchCustomCallback (query_point, resultSet, searchParams);
2205
- if (searchParams.sorted )
2206
- std::sort (
2207
- IndicesDists.begin (), IndicesDists.end (), IndexDist_Sorter ());
2208
2221
return nFound;
2209
2222
}
2210
2223
0 commit comments