[mlpack-git] master: Add a BoundTraits<> class for template metaprogramming. (acbf832)

gitdub at big.cc.gt.atl.ga.us gitdub at big.cc.gt.atl.ga.us
Mon May 4 11:13:38 EDT 2015


Repository : https://github.com/mlpack/mlpack

On branch  : master
Link       : https://github.com/mlpack/mlpack/compare/744b3268b46dbd04fc42b343d992bceda121bc11...b9d571606ded7e6261682ce4eddca40aa3015cc3

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

commit acbf832a796a6f6bfe6f7a67a6189696b22602e1
Author: Ryan Curtin <ryan at ratml.org>
Date:   Mon May 4 10:20:35 2015 -0400

    Add a BoundTraits<> class for template metaprogramming.


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

acbf832a796a6f6bfe6f7a67a6189696b22602e1
 src/mlpack/core/tree/CMakeLists.txt   |  1 +
 src/mlpack/core/tree/ballbound.hpp    | 12 ++++++++++--
 src/mlpack/core/tree/bound_traits.hpp | 32 ++++++++++++++++++++++++++++++++
 src/mlpack/core/tree/bounds.hpp       |  1 +
 src/mlpack/core/tree/hrectbound.hpp   | 13 +++++++++++--
 5 files changed, 55 insertions(+), 4 deletions(-)

diff --git a/src/mlpack/core/tree/CMakeLists.txt b/src/mlpack/core/tree/CMakeLists.txt
index 5491dd8..7000b3f 100644
--- a/src/mlpack/core/tree/CMakeLists.txt
+++ b/src/mlpack/core/tree/CMakeLists.txt
@@ -15,6 +15,7 @@ set(SOURCES
   binary_space_tree/single_tree_traverser_impl.hpp
   binary_space_tree/traits.hpp
   bounds.hpp
+  bound_traits.hpp
   cosine_tree/cosine_tree.hpp
   cosine_tree/cosine_tree.cpp
   cover_tree/cover_tree.hpp
diff --git a/src/mlpack/core/tree/ballbound.hpp b/src/mlpack/core/tree/ballbound.hpp
index 4a83461..b755d44 100644
--- a/src/mlpack/core/tree/ballbound.hpp
+++ b/src/mlpack/core/tree/ballbound.hpp
@@ -10,6 +10,7 @@
 
 #include <mlpack/core.hpp>
 #include <mlpack/core/metrics/lmetric.hpp>
+#include "bound_traits.hpp"
 
 namespace mlpack {
 namespace bound {
@@ -184,11 +185,18 @@ class BallBound
    * Returns a string representation of this object.
    */
   std::string ToString() const;
+};
 
+//! A specialization of BoundTraits for this bound type.
+template<typename VecType, typename TMetricType>
+struct BoundTraits<BallBound<VecType, TMetricType>>
+{
+  //! These bounds are potentially loose in some dimensions.
+  const static bool HasTightBounds = false;
 };
 
-}; // namespace bound
-}; // namespace mlpack
+} // namespace bound
+} // namespace mlpack
 
 #include "ballbound_impl.hpp"
 
diff --git a/src/mlpack/core/tree/bound_traits.hpp b/src/mlpack/core/tree/bound_traits.hpp
new file mode 100644
index 0000000..a02facd
--- /dev/null
+++ b/src/mlpack/core/tree/bound_traits.hpp
@@ -0,0 +1,32 @@
+/**
+ * @file bound_traits.hpp
+ * @author Ryan Curtin
+ *
+ * A class for template metaprogramming traits for bounds.
+ */
+#ifndef __MLPACK_CORE_TREE_BOUND_TRAITS_HPP
+#define __MLPACK_CORE_TREE_BOUND_TRAITS_HPP
+
+namespace mlpack {
+namespace bound {
+
+/**
+ * A class to obtain compile-time traits about BoundType classes.  If you are
+ * writing your own BoundType class, you should make a template specialization
+ * in order to set the values correctly.
+ *
+ * @see TreeTraits, KernelTraits
+ */
+template<typename BoundType>
+struct BoundTraits
+{
+  //! If true, then the bounds for each dimension are tight.  If false, then the
+  //! bounds for each dimension may be looser than the range of all points held
+  //! in the bound.  This defaults to false.
+  static const bool HasTightBounds = false;
+};
+
+} // namespace bound
+} // namespace mlpack
+
+#endif
diff --git a/src/mlpack/core/tree/bounds.hpp b/src/mlpack/core/tree/bounds.hpp
index bd1de8c..784f202 100644
--- a/src/mlpack/core/tree/bounds.hpp
+++ b/src/mlpack/core/tree/bounds.hpp
@@ -10,6 +10,7 @@
 #include <mlpack/core/math/range.hpp>
 #include <mlpack/core/metrics/lmetric.hpp>
 
+#include "bound_traits.hpp"
 #include "hrectbound.hpp"
 #include "ballbound.hpp"
 
diff --git a/src/mlpack/core/tree/hrectbound.hpp b/src/mlpack/core/tree/hrectbound.hpp
index 4d81338..d4e5351 100644
--- a/src/mlpack/core/tree/hrectbound.hpp
+++ b/src/mlpack/core/tree/hrectbound.hpp
@@ -12,6 +12,7 @@
 #include <mlpack/core.hpp>
 #include <mlpack/core/math/range.hpp>
 #include <mlpack/core/metrics/lmetric.hpp>
+#include "bound_traits.hpp"
 
 namespace mlpack {
 namespace bound {
@@ -183,8 +184,16 @@ class HRectBound
   double minWidth;
 };
 
-}; // namespace bound
-}; // namespace mlpack
+// A specialization of BoundTraits for this class.
+template<int Power, bool TakeRoot>
+struct BoundTraits<HRectBound<Power, TakeRoot>>
+{
+  //! These bounds are always tight for each dimension.
+  const static bool HasTightBounds = true;
+};
+
+} // namespace bound
+} // namespace mlpack
 
 #include "hrectbound_impl.hpp"
 



More information about the mlpack-git mailing list