[mlpack-git] master: Move into mlpack namespaces. (a9452fc)

gitdub at mlpack.org gitdub at mlpack.org
Tue Oct 25 00:33:55 EDT 2016


Repository : https://github.com/mlpack/mlpack
On branch  : master
Link       : https://github.com/mlpack/mlpack/compare/31995784e651e1c17c988c79d9f53c9dbad620f8...81fce4edfc8bfb4c26b48ed388f559ec1cee26dd

>---------------------------------------------------------------

commit a9452fc85fb9ffa58408c6e400b68a83d9ac0c9e
Author: Ryan Curtin <ryan at ratml.org>
Date:   Tue Oct 25 13:33:55 2016 +0900

    Move into mlpack namespaces.


>---------------------------------------------------------------

a9452fc85fb9ffa58408c6e400b68a83d9ac0c9e
 src/mlpack/methods/approx_kfn/qdafn.hpp      | 10 ++++---
 src/mlpack/methods/approx_kfn/qdafn_impl.hpp | 42 ++++++++++++++--------------
 src/mlpack/tests/CMakeLists.txt              |  1 +
 src/mlpack/tests/qdafn_test.cpp              | 11 +++++---
 4 files changed, 35 insertions(+), 29 deletions(-)

diff --git a/src/mlpack/methods/approx_kfn/qdafn.hpp b/src/mlpack/methods/approx_kfn/qdafn.hpp
index 860acb8..694fbf9 100644
--- a/src/mlpack/methods/approx_kfn/qdafn.hpp
+++ b/src/mlpack/methods/approx_kfn/qdafn.hpp
@@ -16,12 +16,13 @@
  * }
  * @endcode
  */
-#ifndef QDAFN_HPP
-#define QDAFN_HPP
+#ifndef MLPACK_METHODS_APPROX_KFN_QDAFN_HPP
+#define MLPACK_METHODS_APPROX_KFN_QDAFN_HPP
 
 #include <mlpack/core.hpp>
 
-namespace qdafn {
+namespace mlpack {
+namespace neighbor {
 
 template<typename MatType = arma::mat>
 class QDAFN
@@ -77,7 +78,8 @@ class QDAFN
                       const double distance) const;
 };
 
-} // namespace qdafn
+} // namespace neighbor
+} // namespace mlpack
 
 // Include implementation.
 #include "qdafn_impl.hpp"
diff --git a/src/mlpack/methods/approx_kfn/qdafn_impl.hpp b/src/mlpack/methods/approx_kfn/qdafn_impl.hpp
index 47a698c..bf462da 100644
--- a/src/mlpack/methods/approx_kfn/qdafn_impl.hpp
+++ b/src/mlpack/methods/approx_kfn/qdafn_impl.hpp
@@ -4,8 +4,8 @@
  *
  * Implementation of QDAFN class methods.
  */
-#ifndef QDAFN_IMPL_HPP
-#define QDAFN_IMPL_HPP
+#ifndef MLPACK_METHODS_APPROX_KFN_QDAFN_IMPL_HPP
+#define MLPACK_METHODS_APPROX_KFN_QDAFN_IMPL_HPP
 
 // In case it hasn't been included yet.
 #include "qdafn.hpp"
@@ -13,7 +13,8 @@
 #include <queue>
 #include <mlpack/methods/neighbor_search/sort_policies/furthest_neighbor_sort.hpp>
 
-namespace qdafn {
+namespace mlpack {
+namespace neighbor {
 
 // Constructor.
 template<typename MatType>
@@ -86,6 +87,10 @@ void QDAFN<MatType>::Search(const MatType& querySet,
     arma::Col<size_t> tableLocations = arma::zeros<arma::Col<size_t>>(l);
 
     // Now that the queue is initialized, iterate over m elements.
+    std::vector<std::pair<double, size_t>> v(k, std::make_pair(-1.0,
+        size_t(-1)));
+    std::priority_queue<std::pair<double, size_t>>
+        resultsQueue(std::less<std::pair<double, size_t>>(), std::move(v));
     for (size_t i = 0; i < m; ++i)
     {
       std::pair<size_t, double> p = queue.top();
@@ -99,26 +104,12 @@ void QDAFN<MatType>::Search(const MatType& querySet,
           querySet.col(q), referenceSet.col(referenceIndex));
 
       // Is this neighbor good enough to insert into the results?
-      arma::vec queryDist = distances.unsafe_col(q);
-      arma::Col<size_t> queryIndices = neighbors.unsafe_col(q);
-      const size_t insertPosition =
-          mlpack::neighbor::FurthestNeighborSort::SortDistance(queryDist,
-          queryIndices, dist);
-      bool found = false;
-      for (size_t j = 0; j < neighbors.n_rows; ++j)
+      if (dist > resultsQueue.top().first)
       {
-        if (neighbors(j, q) == referenceIndex)
-        {
-          found = true;
-          break;
-        }
+        resultsQueue.pop();
+        resultsQueue.push(std::make_pair(dist, referenceIndex));
       }
 
-      // SortDistance() returns (size_t() - 1) if we shouldn't add it.
-      if (insertPosition != (size_t() - 1) && !found)
-        InsertNeighbor(distances, neighbors, q, insertPosition, referenceIndex,
-            dist);
-
       // Now (line 14) get the next element and insert into the queue.  Do this
       // by adjusting the previous value.  Don't insert anything if we are at
       // the end of the search, though.
@@ -132,6 +123,14 @@ void QDAFN<MatType>::Search(const MatType& querySet,
         queue.push(std::make_pair(val, p.second));
       }
     }
+
+    // Extract the results.
+    for (size_t j = 1; j <= k; ++j)
+    {
+      neighbors(k - j, q) = resultsQueue.top().second;
+      distances(k - j, q) = resultsQueue.top().first;
+      resultsQueue.pop();
+    }
   }
 }
 
@@ -160,6 +159,7 @@ void QDAFN<MatType>::InsertNeighbor(arma::mat& distances,
   neighbors(pos, queryIndex) = neighbor;
 }
 
-} // namespace qdafn
+} // namespace neighbor
+} // namespace mlpack
 
 #endif
diff --git a/src/mlpack/tests/CMakeLists.txt b/src/mlpack/tests/CMakeLists.txt
index a93f7bf..9f6965b 100644
--- a/src/mlpack/tests/CMakeLists.txt
+++ b/src/mlpack/tests/CMakeLists.txt
@@ -60,6 +60,7 @@ add_executable(mlpack_test
   nystroem_method_test.cpp
   pca_test.cpp
   perceptron_test.cpp
+  qdafn_test.cpp
   quic_svd_test.cpp
   radical_test.cpp
   randomized_svd_test.cpp
diff --git a/src/mlpack/tests/qdafn_test.cpp b/src/mlpack/tests/qdafn_test.cpp
index ee10610..ea64b52 100644
--- a/src/mlpack/tests/qdafn_test.cpp
+++ b/src/mlpack/tests/qdafn_test.cpp
@@ -4,20 +4,21 @@
  *
  * Test the QDAFN functionality.
  */
-#define BOOST_TEST_MODULE QDAFNTest
-
 #include <boost/test/unit_test.hpp>
+#include "test_tools.hpp"
+#include "serialization.hpp"
 
 #include <mlpack/core.hpp>
-#include "qdafn.hpp"
+#include <mlpack/methods/approx_kfn/qdafn.hpp>
 #include <mlpack/methods/neighbor_search/neighbor_search.hpp>
 
 using namespace std;
 using namespace arma;
 using namespace mlpack;
-using namespace qdafn;
 using namespace mlpack::neighbor;
 
+BOOST_AUTO_TEST_SUITE(QDAFNTest);
+
 /**
  * With one reference point, make sure that is the one that is returned.
  */
@@ -100,3 +101,5 @@ BOOST_AUTO_TEST_CASE(QDAFNUniformSet)
 
   BOOST_REQUIRE_GE(successes, 700);
 }
+
+BOOST_AUTO_TEST_SUITE_END();




More information about the mlpack-git mailing list