[mlpack-svn] r13454 - in mlpack/trunk/src/mlpack/core: dists util
fastlab-svn at coffeetalk-1.cc.gatech.edu
fastlab-svn at coffeetalk-1.cc.gatech.edu
Fri Aug 24 12:24:25 EDT 2012
Author: trironk3
Date: 2012-08-24 12:24:24 -0400 (Fri, 24 Aug 2012)
New Revision: 13454
Added:
mlpack/trunk/src/mlpack/core/util/sfinae_utility.hpp
Modified:
mlpack/trunk/src/mlpack/core/dists/discrete_distribution.cpp
mlpack/trunk/src/mlpack/core/dists/discrete_distribution.hpp
mlpack/trunk/src/mlpack/core/util/CMakeLists.txt
mlpack/trunk/src/mlpack/core/util/prefixedoutstream.hpp
mlpack/trunk/src/mlpack/core/util/prefixedoutstream_impl.hpp
Log:
#164: Any objects passed into a prefixedoutstream that contain a std::string ToString() const method will write the results of the ToString method instead of simply being passed directly into the prefixedoutstream.
Still need to document and organize.
Modified: mlpack/trunk/src/mlpack/core/dists/discrete_distribution.cpp
===================================================================
--- mlpack/trunk/src/mlpack/core/dists/discrete_distribution.cpp 2012-08-23 19:37:54 UTC (rev 13453)
+++ mlpack/trunk/src/mlpack/core/dists/discrete_distribution.cpp 2012-08-24 16:24:24 UTC (rev 13454)
@@ -79,3 +79,12 @@
else
probabilities.fill(1 / probabilities.n_elem); // Force normalization.
}
+
+std::string DiscreteDistribution::ToString() const
+{
+ std::ostringstream convert;
+ convert << "Distribution" << this << std::endl;
+ convert << probabilities;
+ return convert.str();
+}
+
Modified: mlpack/trunk/src/mlpack/core/dists/discrete_distribution.hpp
===================================================================
--- mlpack/trunk/src/mlpack/core/dists/discrete_distribution.hpp 2012-08-23 19:37:54 UTC (rev 13453)
+++ mlpack/trunk/src/mlpack/core/dists/discrete_distribution.hpp 2012-08-24 16:24:24 UTC (rev 13454)
@@ -128,6 +128,8 @@
const arma::vec& Probabilities() const { return probabilities; }
//! Modify the vector of probabilities.
arma::vec& Probabilities() { return probabilities; }
+
+ std::string ToString() const;
private:
arma::vec probabilities;
Modified: mlpack/trunk/src/mlpack/core/util/CMakeLists.txt
===================================================================
--- mlpack/trunk/src/mlpack/core/util/CMakeLists.txt 2012-08-23 19:37:54 UTC (rev 13453)
+++ mlpack/trunk/src/mlpack/core/util/CMakeLists.txt 2012-08-24 16:24:24 UTC (rev 13454)
@@ -20,6 +20,7 @@
save_restore_utility.hpp
save_restore_utility.cpp
save_restore_utility_impl.hpp
+ sfinae_utility.hpp
timers.hpp
timers.cpp
)
Modified: mlpack/trunk/src/mlpack/core/util/prefixedoutstream.hpp
===================================================================
--- mlpack/trunk/src/mlpack/core/util/prefixedoutstream.hpp 2012-08-23 19:37:54 UTC (rev 13453)
+++ mlpack/trunk/src/mlpack/core/util/prefixedoutstream.hpp 2012-08-24 16:24:24 UTC (rev 13454)
@@ -14,7 +14,11 @@
#include <streambuf>
#include <boost/lexical_cast.hpp>
+#include <boost/utility/enable_if.hpp>
+#include <boost/type_traits.hpp>
+#include <mlpack/core/util/sfinae_utility.hpp>
+
namespace mlpack {
namespace io {
@@ -111,6 +115,47 @@
bool ignoreInput;
private:
+ HAS_MEM_FUNC(ToString, HasToString)
+
+ //! This handles forwarding all primitive types transparently
+ template<typename T>
+ void CallBaseLogic(T s,
+ typename boost::disable_if<
+ boost::is_class<T>
+ >::type* = 0)
+ {
+ BaseLogic<T>(s);
+ }
+
+ // Forward all objects that do not implement a ToString() method transparently
+ template<typename T>
+ void CallBaseLogic(T s,
+ typename boost::enable_if<
+ boost::is_class<T>
+ >::type* = 0,
+ typename boost::disable_if<
+ HasToString<T, std::string(T::*)() const>
+ >::type* = 0)
+ {
+ BaseLogic<T>(s);
+ }
+
+ // Call ToString() on all objects that implement ToString() before forwarding
+ template<typename T>
+ void CallBaseLogic(T s,
+ typename boost::enable_if<
+ boost::is_class<T>
+ >::type* = 0,
+ typename boost::enable_if<
+ HasToString<T, std::string(T::*)() const>
+ >::type* = 0)
+ {
+ std::string result = s.ToString();
+ BaseLogic<std::string&>(result);
+ }
+
+
+
/**
* @brief Conducts the base logic required in all the operator << overloads.
* Mostly just a good idea to reduce copy-pasta.
Modified: mlpack/trunk/src/mlpack/core/util/prefixedoutstream_impl.hpp
===================================================================
--- mlpack/trunk/src/mlpack/core/util/prefixedoutstream_impl.hpp 2012-08-23 19:37:54 UTC (rev 13453)
+++ mlpack/trunk/src/mlpack/core/util/prefixedoutstream_impl.hpp 2012-08-24 16:24:24 UTC (rev 13454)
@@ -15,8 +15,7 @@
template<typename T>
PrefixedOutStream& PrefixedOutStream::operator<<(T s)
{
- BaseLogic<T>(s);
-
+ CallBaseLogic<T>(s);
return *this;
}
@@ -32,7 +31,7 @@
PrefixIfNeeded();
std::ostringstream convert;
- convert << val;
+ convert << val;
if(convert.fail())
{
Added: mlpack/trunk/src/mlpack/core/util/sfinae_utility.hpp
===================================================================
--- mlpack/trunk/src/mlpack/core/util/sfinae_utility.hpp (rev 0)
+++ mlpack/trunk/src/mlpack/core/util/sfinae_utility.hpp 2012-08-24 16:24:24 UTC (rev 13454)
@@ -0,0 +1,30 @@
+/**
+ * @file sfinae_utility.hpp
+ * @author Trironk Kiatkungwanglai
+ *
+ * Utilities for the SFINAE Paradigm.
+ *
+ * Taken from http://stackoverflow.com/a/6324863/391618
+ *
+ */
+#ifndef __MLPACK_CORE_SFINAE_UTILITY
+#define __MLPACK_CORE_SFINAE_UTILITY
+
+#include <boost/utility/enable_if.hpp>
+#include <boost/type_traits.hpp>
+
+// Taken from: http://stackoverflow.com/a/264088/391618
+
+#define HAS_MEM_FUNC(func, name) \
+ template<typename T, typename sig> \
+ struct name { \
+ typedef char yes; \
+ typedef long no; \
+ template <typename U, U> struct type_check; \
+ template <typename _1> static yes &chk(type_check<sig, &_1::func> *); \
+ template <typename > static no &chk(...); \
+ static bool const value = sizeof(chk<T>(0)) == sizeof(yes); \
+ };
+
+
+#endif
More information about the mlpack-svn
mailing list