[mlpack-git] master: Fix bug where query set gets modified. (8573522)
gitdub at big.cc.gt.atl.ga.us
gitdub at big.cc.gt.atl.ga.us
Wed Apr 22 16:32:29 EDT 2015
Repository : https://github.com/mlpack/mlpack
On branch : master
Link : https://github.com/mlpack/mlpack/compare/8f85309ae9be40e819b301b39c9a940aa28f3bb2...57d0567dddff01feea73b348f38cc040dc3cf8e3
>---------------------------------------------------------------
commit 85735226823018f3f4e09b71b87d353b115f2909
Author: ryan <ryan at ratml.org>
Date: Wed Apr 22 13:27:39 2015 -0400
Fix bug where query set gets modified.
>---------------------------------------------------------------
85735226823018f3f4e09b71b87d353b115f2909
.../methods/range_search/range_search_impl.hpp | 23 +++++++++++++---------
1 file changed, 14 insertions(+), 9 deletions(-)
diff --git a/src/mlpack/methods/range_search/range_search_impl.hpp b/src/mlpack/methods/range_search/range_search_impl.hpp
index ae443ac..e70446c 100644
--- a/src/mlpack/methods/range_search/range_search_impl.hpp
+++ b/src/mlpack/methods/range_search/range_search_impl.hpp
@@ -53,7 +53,7 @@ RangeSearch<MetricType, TreeType>::RangeSearch(
singleMode(!naive && singleMode), // Naive overrides single mode.
metric(metric)
{
- // Build the trees.
+ // Build the tree.
Timer::Start("range_search/tree_building");
// If in naive mode, then we do not need to build trees.
@@ -108,6 +108,17 @@ void RangeSearch<MetricType, TreeType>::Search(
// This will hold mappings for query points, if necessary.
std::vector<size_t> oldFromNewQueries;
+ // If we will be building a tree and it will modify the query set, make a copy
+ // of the dataset.
+ typename TreeType::Mat queryCopy;
+ const bool needsCopy = (!naive && !singleMode &&
+ tree::TreeTraits<TreeType>::RearrangesDataset);
+ if (needsCopy)
+ queryCopy = querySet;
+
+ const typename TreeType::Mat& querySetRef = (needsCopy) ? queryCopy :
+ querySet;
+
// If we have built the trees ourselves, then we will have to map all the
// indices back to their original indices when this computation is finished.
// To avoid extra copies, we will store the unmapped neighbors and distances
@@ -137,7 +148,7 @@ void RangeSearch<MetricType, TreeType>::Search(
// Create the helper object for the traversal.
typedef RangeSearchRules<MetricType, TreeType> RuleType;
- RuleType rules(referenceSet, querySet, range, *neighborPtr, *distancePtr,
+ RuleType rules(referenceSet, querySetRef, range, *neighborPtr, *distancePtr,
metric);
if (naive)
@@ -161,12 +172,6 @@ void RangeSearch<MetricType, TreeType>::Search(
// Build the query tree.
Timer::Stop("range_search/computing_neighbors");
Timer::Start("range_search/tree_building");
- typename TreeType::Mat queryCopy;
- if (tree::TreeTraits<TreeType>::RearrangesDataset)
- queryCopy = querySet;
-
- const typename TreeType::Mat& querySetRef =
- (tree::TreeTraits<TreeType>::RearrangesDataset) ? querySet : queryCopy;
TreeType* queryTree = BuildTree<TreeType>(
const_cast<typename TreeType::Mat&>(querySetRef), oldFromNewQueries);
Timer::Stop("range_search/tree_building");
@@ -377,7 +382,7 @@ void RangeSearch<MetricType, TreeType>::Search(
for (size_t i = 0; i < distances.size(); i++)
{
// Map distances (copy a column).
- size_t refMapping = oldFromNewReferences[i];
+ const size_t refMapping = oldFromNewReferences[i];
distances[refMapping] = (*distancePtr)[i];
// Copy each neighbor individually, because we need to map it.
More information about the mlpack-git
mailing list