[mlpack] 20/40: Refactor to allow arbitrary types for Range.

Barak A. Pearlmutter barak+git at pearlmutter.net
Mon Feb 15 19:34:24 UTC 2016


This is an automated email from the git hooks/post-receive script.

bap pushed a commit to branch master
in repository mlpack.

commit 41a4cad35a2451368e06d142aefc921c82a61b11
Author: Ryan Curtin <ryan at ratml.org>
Date:   Mon Jan 25 11:51:15 2016 -0500

    Refactor to allow arbitrary types for Range.
    
    But for reverse compatibility, make RangeType<T> be math::Range, and upon 3.0.0
    we'll refactor so RangeType is Range.  (That will break reverse compatibility.)
---
 src/mlpack/core/math/range.hpp      | 64 +++++++++++++++++++-------------
 src/mlpack/core/math/range_impl.hpp | 73 +++++++++++++++++++++++--------------
 2 files changed, 85 insertions(+), 52 deletions(-)

diff --git a/src/mlpack/core/math/range.hpp b/src/mlpack/core/math/range.hpp
index c901f73..f396768 100644
--- a/src/mlpack/core/math/range.hpp
+++ b/src/mlpack/core/math/range.hpp
@@ -17,18 +17,31 @@
 namespace mlpack {
 namespace math {
 
+template<typename T>
+class RangeType;
+
+//! 3.0.0 TODO: break reverse-compatibility by changing RangeType to Range.
+typedef RangeType<double> Range;
+
 /**
  * Simple real-valued range.  It contains an upper and lower bound.
+ *
+ * Note that until mlpack 3.0.0, this class is named RangeType<> and for the
+ * specification where T is double, you can use math::Range.  As of mlpack
+ * 3.0.0, this class will be renamed math::Range<>.
+ *
+ * @tparam T type of element held by this range.
  */
-class Range
+template<typename T = double>
+class RangeType
 {
  private:
-  double lo; /// The lower bound.
-  double hi; /// The upper bound.
+  T lo; /// The lower bound.
+  T hi; /// The upper bound.
 
  public:
   /** Initialize to an empty set (where lo > hi). */
-  inline Range();
+  inline RangeType();
 
   /***
    * Initialize a range to enclose only the given point (lo = point, hi =
@@ -36,7 +49,7 @@ class Range
    *
    * @param point Point that this range will enclose.
    */
-  inline Range(const double point);
+  inline RangeType(const T point);
 
   /**
    * Initializes to specified range.
@@ -44,41 +57,41 @@ class Range
    * @param lo Lower bound of the range.
    * @param hi Upper bound of the range.
    */
-  inline Range(const double lo, const double hi);
+  inline RangeType(const T lo, const T hi);
 
   //! Get the lower bound.
-  inline double Lo() const { return lo; }
+  inline T Lo() const { return lo; }
   //! Modify the lower bound.
-  inline double& Lo() { return lo; }
+  inline T& Lo() { return lo; }
 
   //! Get the upper bound.
-  inline double Hi() const { return hi; }
+  inline T Hi() const { return hi; }
   //! Modify the upper bound.
-  inline double& Hi() { return hi; }
+  inline T& Hi() { return hi; }
 
   /**
    * Gets the span of the range (hi - lo).
    */
-  inline double Width() const;
+  inline T Width() const;
 
   /**
    * Gets the midpoint of this range.
    */
-  inline double Mid() const;
+  inline T Mid() const;
 
   /**
    * Expands this range to include another range.
    *
    * @param rhs Range to include.
    */
-  inline Range& operator|=(const Range& rhs);
+  inline RangeType& operator|=(const RangeType& rhs);
 
   /**
    * Expands this range to include another range.
    *
    * @param rhs Range to include.
    */
-  inline Range operator|(const Range& rhs) const;
+  inline RangeType operator|(const RangeType& rhs) const;
 
   /**
    * Shrinks this range to be the overlap with another range; this makes an
@@ -86,7 +99,7 @@ class Range
    *
    * @param rhs Other range.
    */
-  inline Range& operator&=(const Range& rhs);
+  inline RangeType& operator&=(const RangeType& rhs);
 
   /**
    * Shrinks this range to be the overlap with another range; this makes an
@@ -94,42 +107,43 @@ class Range
    *
    * @param rhs Other range.
    */
-  inline Range operator&(const Range& rhs) const;
+  inline RangeType operator&(const RangeType& rhs) const;
 
   /**
    * Scale the bounds by the given double.
    *
    * @param d Scaling factor.
    */
-  inline Range& operator*=(const double d);
+  inline RangeType& operator*=(const T d);
 
   /**
    * Scale the bounds by the given double.
    *
    * @param d Scaling factor.
    */
-  inline Range operator*(const double d) const;
+  inline RangeType operator*(const T d) const;
 
   /**
    * Scale the bounds by the given double.
    *
    * @param d Scaling factor.
    */
-  friend inline Range operator*(const double d, const Range& r); // Symmetric.
+  template<typename TT>
+  friend inline RangeType<TT> operator*(const TT d, const RangeType<TT>& r);
 
   /**
    * Compare with another range for strict equality.
    *
    * @param rhs Other range.
    */
-  inline bool operator==(const Range& rhs) const;
+  inline bool operator==(const RangeType& rhs) const;
 
   /**
    * Compare with another range for strict equality.
    *
    * @param rhs Other range.
    */
-  inline bool operator!=(const Range& rhs) const;
+  inline bool operator!=(const RangeType& rhs) const;
 
   /**
    * Compare with another range.  For Range objects x and y, x < y means that x
@@ -137,7 +151,7 @@ class Range
    *
    * @param rhs Other range.
    */
-  inline bool operator<(const Range& rhs) const;
+  inline bool operator<(const RangeType& rhs) const;
 
   /**
    * Compare with another range.  For Range objects x and y, x < y means that x
@@ -145,14 +159,14 @@ class Range
    *
    * @param rhs Other range.
    */
-  inline bool operator>(const Range& rhs) const;
+  inline bool operator>(const RangeType& rhs) const;
 
   /**
    * Determines if a point is contained within the range.
    *
    * @param d Point to check.
    */
-  inline bool Contains(const double d) const;
+  inline bool Contains(const T d) const;
 
   /**
    * Determines if another range overlaps with this one.
@@ -161,7 +175,7 @@ class Range
    *
    * @return true if ranges overlap at all.
    */
-  inline bool Contains(const Range& r) const;
+  inline bool Contains(const RangeType& r) const;
 
   /**
    * Serialize the range object.
diff --git a/src/mlpack/core/math/range_impl.hpp b/src/mlpack/core/math/range_impl.hpp
index 41897e4..a9fe360 100644
--- a/src/mlpack/core/math/range_impl.hpp
+++ b/src/mlpack/core/math/range_impl.hpp
@@ -23,25 +23,29 @@ namespace math {
 /**
  * Initialize the range to 0.
  */
-inline Range::Range() :
+template<typename T>
+inline RangeType<T>::RangeType() :
     lo(DBL_MAX), hi(-DBL_MAX) { /* nothing else to do */ }
 
 /**
  * Initialize a range to enclose only the given point.
  */
-inline Range::Range(const double point) :
+template<typename T>
+inline RangeType<T>::RangeType(const T point) :
     lo(point), hi(point) { /* nothing else to do */ }
 
 /**
  * Initializes the range to the specified values.
  */
-inline Range::Range(const double lo, const double hi) :
+template<typename T>
+inline RangeType<T>::RangeType(const T lo, const T hi) :
     lo(lo), hi(hi) { /* nothing else to do */ }
 
 /**
  * Gets the span of the range, hi - lo.  Returns 0 if the range is negative.
  */
-inline double Range::Width() const
+template<typename T>
+inline T RangeType<T>::Width() const
 {
   if (lo < hi)
     return (hi - lo);
@@ -52,7 +56,8 @@ inline double Range::Width() const
 /**
  * Gets the midpoint of this range.
  */
-inline double Range::Mid() const
+template<typename T>
+inline T RangeType<T>::Mid() const
 {
   return (hi + lo) / 2;
 }
@@ -60,7 +65,8 @@ inline double Range::Mid() const
 /**
  * Expands range to include the other range.
  */
-inline Range& Range::operator|=(const Range& rhs)
+template<typename T>
+inline RangeType<T>& RangeType<T>::operator|=(const RangeType<T>& rhs)
 {
   if (rhs.lo < lo)
     lo = rhs.lo;
@@ -70,17 +76,19 @@ inline Range& Range::operator|=(const Range& rhs)
   return *this;
 }
 
-inline Range Range::operator|(const Range& rhs) const
+template<typename T>
+inline RangeType<T> RangeType<T>::operator|(const RangeType<T>& rhs) const
 {
-  return Range((rhs.lo < lo) ? rhs.lo : lo,
-               (rhs.hi > hi) ? rhs.hi : hi);
+  return RangeType<T>((rhs.lo < lo) ? rhs.lo : lo,
+                      (rhs.hi > hi) ? rhs.hi : hi);
 }
 
 /**
  * Shrinks range to be the overlap with another range, becoming an empty
  * set if there is no overlap.
  */
-inline Range& Range::operator&=(const Range& rhs)
+template<typename T>
+inline RangeType<T>& RangeType<T>::operator&=(const RangeType<T>& rhs)
 {
   if (rhs.lo > lo)
     lo = rhs.lo;
@@ -90,16 +98,18 @@ inline Range& Range::operator&=(const Range& rhs)
   return *this;
 }
 
-inline Range Range::operator&(const Range& rhs) const
+template<typename T>
+inline RangeType<T> RangeType<T>::operator&(const RangeType<T>& rhs) const
 {
-  return Range((rhs.lo > lo) ? rhs.lo : lo,
-               (rhs.hi < hi) ? rhs.hi : hi);
+  return RangeType<T>((rhs.lo > lo) ? rhs.lo : lo,
+                      (rhs.hi < hi) ? rhs.hi : hi);
 }
 
 /**
  * Scale the bounds by the given double.
  */
-inline Range& Range::operator*=(const double d)
+template<typename T>
+inline RangeType<T>& RangeType<T>::operator*=(const T d)
 {
   lo *= d;
   hi *= d;
@@ -115,38 +125,42 @@ inline Range& Range::operator*=(const double d)
   return *this;
 }
 
-inline Range Range::operator*(const double d) const
+template<typename T>
+inline RangeType<T> RangeType<T>::operator*(const T d) const
 {
   double nlo = lo * d;
   double nhi = hi * d;
 
   if (nlo <= nhi)
-    return Range(nlo, nhi);
+    return RangeType<T>(nlo, nhi);
   else
-    return Range(nhi, nlo);
+    return RangeType<T>(nhi, nlo);
 }
 
 // Symmetric case.
-inline Range operator*(const double d, const Range& r)
+template<typename T>
+inline RangeType<T> operator*(const T d, const RangeType<T>& r)
 {
   double nlo = r.lo * d;
   double nhi = r.hi * d;
 
   if (nlo <= nhi)
-    return Range(nlo, nhi);
+    return RangeType<T>(nlo, nhi);
   else
-    return Range(nhi, nlo);
+    return RangeType<T>(nhi, nlo);
 }
 
 /**
  * Compare with another range for strict equality.
  */
-inline bool Range::operator==(const Range& rhs) const
+template<typename T>
+inline bool RangeType<T>::operator==(const RangeType<T>& rhs) const
 {
   return (lo == rhs.lo) && (hi == rhs.hi);
 }
 
-inline bool Range::operator!=(const Range& rhs) const
+template<typename T>
+inline bool RangeType<T>::operator!=(const RangeType<T>& rhs) const
 {
   return (lo != rhs.lo) || (hi != rhs.hi);
 }
@@ -155,12 +169,14 @@ inline bool Range::operator!=(const Range& rhs) const
  * Compare with another range.  For Range objects x and y, x < y means that x is
  * strictly less than y and does not overlap at all.
  */
-inline bool Range::operator<(const Range& rhs) const
+template<typename T>
+inline bool RangeType<T>::operator<(const RangeType<T>& rhs) const
 {
   return hi < rhs.lo;
 }
 
-inline bool Range::operator>(const Range& rhs) const
+template<typename T>
+inline bool RangeType<T>::operator>(const RangeType<T>& rhs) const
 {
   return lo > rhs.hi;
 }
@@ -168,7 +184,8 @@ inline bool Range::operator>(const Range& rhs) const
 /**
  * Determines if a point is contained within the range.
  */
-inline bool Range::Contains(const double d) const
+template<typename T>
+inline bool RangeType<T>::Contains(const T d) const
 {
   return d >= lo && d <= hi;
 }
@@ -176,14 +193,16 @@ inline bool Range::Contains(const double d) const
 /**
  * Determines if this range overlaps with another range.
  */
-inline bool Range::Contains(const Range& r) const
+template<typename T>
+inline bool RangeType<T>::Contains(const RangeType<T>& r) const
 {
   return lo <= r.hi && hi >= r.lo;
 }
 
 //! Serialize the range.
+template<typename T>
 template<typename Archive>
-void Range::Serialize(Archive& ar, const unsigned int /* version */)
+void RangeType<T>::Serialize(Archive& ar, const unsigned int /* version */)
 {
   ar & data::CreateNVP(hi, "hi");
   ar & data::CreateNVP(lo, "lo");

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/debian-science/packages/mlpack.git



More information about the debian-science-commits mailing list