[mlpack-svn] r14231 - mlpack/branches/mlpack-1.x/src/mlpack/core/util

fastlab-svn at coffeetalk-1.cc.gatech.edu fastlab-svn at coffeetalk-1.cc.gatech.edu
Fri Feb 8 15:24:50 EST 2013


Author: rcurtin
Date: 2013-02-08 15:24:49 -0500 (Fri, 08 Feb 2013)
New Revision: 14231

Added:
   mlpack/branches/mlpack-1.x/src/mlpack/core/util/string_util.cpp
   mlpack/branches/mlpack-1.x/src/mlpack/core/util/string_util.hpp
Modified:
   mlpack/branches/mlpack-1.x/src/mlpack/core/util/
   mlpack/branches/mlpack-1.x/src/mlpack/core/util/CMakeLists.txt
   mlpack/branches/mlpack-1.x/src/mlpack/core/util/nulloutstream.hpp
   mlpack/branches/mlpack-1.x/src/mlpack/core/util/prefixedoutstream.cpp
   mlpack/branches/mlpack-1.x/src/mlpack/core/util/prefixedoutstream.hpp
   mlpack/branches/mlpack-1.x/src/mlpack/core/util/prefixedoutstream_impl.hpp
   mlpack/branches/mlpack-1.x/src/mlpack/core/util/sfinae_utility.hpp
Log:
Pull changes in for string output.



Property changes on: mlpack/branches/mlpack-1.x/src/mlpack/core/util
___________________________________________________________________
Added: svn:mergeinfo
   + /mlpack/trunk/src/mlpack/core/util:13981-14230

Modified: mlpack/branches/mlpack-1.x/src/mlpack/core/util/CMakeLists.txt
===================================================================
--- mlpack/branches/mlpack-1.x/src/mlpack/core/util/CMakeLists.txt	2013-02-08 20:23:35 UTC (rev 14230)
+++ mlpack/branches/mlpack-1.x/src/mlpack/core/util/CMakeLists.txt	2013-02-08 20:24:49 UTC (rev 14231)
@@ -19,6 +19,8 @@
   save_restore_utility.cpp
   save_restore_utility_impl.hpp
   sfinae_utility.hpp
+  string_util.hpp
+  string_util.cpp
   timers.hpp
   timers.cpp
 )

Modified: mlpack/branches/mlpack-1.x/src/mlpack/core/util/nulloutstream.hpp
===================================================================
--- mlpack/branches/mlpack-1.x/src/mlpack/core/util/nulloutstream.hpp	2013-02-08 20:23:35 UTC (rev 14230)
+++ mlpack/branches/mlpack-1.x/src/mlpack/core/util/nulloutstream.hpp	2013-02-08 20:24:49 UTC (rev 14231)
@@ -78,7 +78,7 @@
 
   //! Does nothing.
   template<typename T>
-  NullOutStream& operator<<(T s)
+  NullOutStream& operator<<(T& s)
   { (void) s; return *this; }
 };
 

Modified: mlpack/branches/mlpack-1.x/src/mlpack/core/util/prefixedoutstream.cpp
===================================================================
--- mlpack/branches/mlpack-1.x/src/mlpack/core/util/prefixedoutstream.cpp	2013-02-08 20:23:35 UTC (rev 14230)
+++ mlpack/branches/mlpack-1.x/src/mlpack/core/util/prefixedoutstream.cpp	2013-02-08 20:24:49 UTC (rev 14231)
@@ -96,7 +96,7 @@
 
 PrefixedOutStream& PrefixedOutStream::operator<<(std::string& str)
 {
-  BaseLogic<std::string&>(str);
+  BaseLogic<std::string>(str);
   return *this;
 }
 

Modified: mlpack/branches/mlpack-1.x/src/mlpack/core/util/prefixedoutstream.hpp
===================================================================
--- mlpack/branches/mlpack-1.x/src/mlpack/core/util/prefixedoutstream.hpp	2013-02-08 20:23:35 UTC (rev 14230)
+++ mlpack/branches/mlpack-1.x/src/mlpack/core/util/prefixedoutstream.hpp	2013-02-08 20:24:49 UTC (rev 14231)
@@ -18,6 +18,7 @@
 #include <boost/type_traits.hpp>
 
 #include <mlpack/core/util/sfinae_utility.hpp>
+#include <mlpack/core/util/string_util.hpp>
 
 namespace mlpack {
 namespace util {
@@ -106,7 +107,7 @@
 
   //! Write anything else to the stream.
   template<typename T>
-  PrefixedOutStream& operator<<(T s);
+  PrefixedOutStream& operator<<(const T& s);
 
   //! The output stream that all data is to be sent too; example: std::cout.
   std::ostream& destination;
@@ -119,14 +120,14 @@
 
   //! This handles forwarding all primitive types transparently
   template<typename T>
-  void CallBaseLogic(T s,
+  void CallBaseLogic(const T& s,
       typename boost::disable_if<
           boost::is_class<T>
       >::type*);
 
   //! Forward all objects that do not implement a ToString() method
   template<typename T>
-  void CallBaseLogic(T s,
+  void CallBaseLogic(const T& s,
       typename boost::enable_if<
           boost::is_class<T>
       >::type*,
@@ -136,7 +137,7 @@
 
   //! Call ToString() on all objects that implement ToString() before forwarding
   template<typename T>
-  void CallBaseLogic(T s,
+  void CallBaseLogic(const T& s,
       typename boost::enable_if<
           boost::is_class<T>
       >::type*,
@@ -152,7 +153,7 @@
    * @param val The The data to be output.
    */
   template<typename T>
-  void BaseLogic(T val);
+  void BaseLogic(const T& val);
 
   /**
    * Output the prefix, but only if we need to and if we are allowed to.

Modified: mlpack/branches/mlpack-1.x/src/mlpack/core/util/prefixedoutstream_impl.hpp
===================================================================
--- mlpack/branches/mlpack-1.x/src/mlpack/core/util/prefixedoutstream_impl.hpp	2013-02-08 20:23:35 UTC (rev 14230)
+++ mlpack/branches/mlpack-1.x/src/mlpack/core/util/prefixedoutstream_impl.hpp	2013-02-08 20:24:49 UTC (rev 14231)
@@ -16,7 +16,7 @@
 namespace util {
 
 template<typename T>
-PrefixedOutStream& PrefixedOutStream::operator<<(T s)
+PrefixedOutStream& PrefixedOutStream::operator<<(const T& s)
 {
   CallBaseLogic<T>(s);
   return *this;
@@ -24,7 +24,7 @@
 
 //! This handles forwarding all primitive types transparently
 template<typename T>
-void PrefixedOutStream::CallBaseLogic(T s,
+void PrefixedOutStream::CallBaseLogic(const T& s,
     typename boost::disable_if<
         boost::is_class<T>
     >::type* = 0)
@@ -34,7 +34,7 @@
 
 // Forward all objects that do not implement a ToString() method transparently
 template<typename T>
-void PrefixedOutStream::CallBaseLogic(T s,
+void PrefixedOutStream::CallBaseLogic(const T& s,
     typename boost::enable_if<
         boost::is_class<T>
     >::type* = 0,
@@ -47,7 +47,7 @@
 
 // Call ToString() on all objects that implement ToString() before forwarding
 template<typename T>
-void PrefixedOutStream::CallBaseLogic(T s,
+void PrefixedOutStream::CallBaseLogic(const T& s,
     typename boost::enable_if<
         boost::is_class<T>
     >::type* = 0,
@@ -56,11 +56,11 @@
     >::type* = 0)
 {
   std::string result = s.ToString();
-  BaseLogic<std::string&>(result);
+  BaseLogic<std::string>(result);
 }
 
 template<typename T>
-void PrefixedOutStream::BaseLogic(T val)
+void PrefixedOutStream::BaseLogic(const T& val)
 {
   // We will use this to track whether or not we need to terminate at the end of
   // this call (only for streams which terminate after a newline).

Modified: mlpack/branches/mlpack-1.x/src/mlpack/core/util/sfinae_utility.hpp
===================================================================
--- mlpack/branches/mlpack-1.x/src/mlpack/core/util/sfinae_utility.hpp	2013-02-08 20:23:35 UTC (rev 14230)
+++ mlpack/branches/mlpack-1.x/src/mlpack/core/util/sfinae_utility.hpp	2013-02-08 20:24:49 UTC (rev 14231)
@@ -19,10 +19,10 @@
  * This macro generates a template struct that is useful for enabling/disabling
  * a method if the template class passed in contains a member function matching
  * a given signature with a specified name.
- * 
+ *
  * The generated struct should be used in conjunction with boost::disable_if and
  * boost::enable_if. Here is an example usage:
- * 
+ *
  * For general references, see:
  * http://stackoverflow.com/a/264088/391618
  *
@@ -32,15 +32,15 @@
  * @param NAME the name of the struct to construct. For example: HasToString
  * @param FUNC the name of the function to check for. For example: ToString
  */
-#define HAS_MEM_FUNC(FUNC, NAME)                                                   \
-template<typename T, typename sig>                                                 \
-struct NAME {                                                                      \
-  typedef char yes[1];                                                             \
-  typedef char no [2];                                                             \
-  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);                      \
+#define HAS_MEM_FUNC(FUNC, NAME)                                               \
+template<typename T, typename sig>                                             \
+struct NAME {                                                                  \
+  typedef char yes[1];                                                         \
+  typedef char no [2];                                                         \
+  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

Copied: mlpack/branches/mlpack-1.x/src/mlpack/core/util/string_util.cpp (from rev 14230, mlpack/trunk/src/mlpack/core/util/string_util.cpp)
===================================================================
--- mlpack/branches/mlpack-1.x/src/mlpack/core/util/string_util.cpp	                        (rev 0)
+++ mlpack/branches/mlpack-1.x/src/mlpack/core/util/string_util.cpp	2013-02-08 20:24:49 UTC (rev 14231)
@@ -0,0 +1,30 @@
+/**
+ * @file string_util.cpp
+ *
+ * Defines methods useful for formatting output.
+ */
+#include "string_util.hpp"
+
+using namespace mlpack;
+using namespace mlpack::util;
+
+//! A utility function that replaces all all newlines with a number of spaces
+//! depending on the indentation level.
+std::string mlpack::util::Indent(std::string input)
+{
+  // Tab the first line.
+  input.insert(0, 1, ' ');
+  input.insert(0, 1, ' ');
+
+  // Get the character sequence to replace all newline characters.
+  std::string tabbedNewline("\n  ");
+
+  // Replace all newline characters with the precomputed character sequence.
+  size_t start_pos = 0;
+  while((start_pos = input.find("\n", start_pos)) != std::string::npos) {
+      input.replace(start_pos, 1, tabbedNewline);
+      start_pos += tabbedNewline.length();
+  }
+
+  return input;
+}

Copied: mlpack/branches/mlpack-1.x/src/mlpack/core/util/string_util.hpp (from rev 14230, mlpack/trunk/src/mlpack/core/util/string_util.hpp)
===================================================================
--- mlpack/branches/mlpack-1.x/src/mlpack/core/util/string_util.hpp	                        (rev 0)
+++ mlpack/branches/mlpack-1.x/src/mlpack/core/util/string_util.hpp	2013-02-08 20:24:49 UTC (rev 14231)
@@ -0,0 +1,21 @@
+/**
+ * @file string_util.hpp
+ *
+ * Declares methods that are useful for writing formatting output.
+ */
+#ifndef __MLPACK_CORE_STRING_UTIL_HPP
+#define __MLPACK_CORE_STRING_UTIL_HPP
+
+#include <string>
+
+namespace mlpack {
+namespace util {
+
+//! A utility function that replaces all all newlines with a number of spaces
+//! depending on the indentation level.
+std::string Indent(std::string input);
+
+}; // namespace util
+}; // namespace mlpack
+
+#endif




More information about the mlpack-svn mailing list