[mlpack-svn] r14028 - mlpack/trunk/src/mlpack/core/util
fastlab-svn at coffeetalk-1.cc.gatech.edu
fastlab-svn at coffeetalk-1.cc.gatech.edu
Thu Dec 20 17:38:12 EST 2012
Author: trironk3
Date: 2012-12-20 17:38:12 -0500 (Thu, 20 Dec 2012)
New Revision: 14028
Added:
mlpack/trunk/src/mlpack/core/util/string_util.cpp
mlpack/trunk/src/mlpack/core/util/string_util.hpp
Modified:
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
mlpack/trunk/src/mlpack/core/util/sfinae_utility.hpp
Log:
Implemented the ToString SFINAE pattern.
Modified: mlpack/trunk/src/mlpack/core/util/CMakeLists.txt
===================================================================
--- mlpack/trunk/src/mlpack/core/util/CMakeLists.txt 2012-12-20 22:19:16 UTC (rev 14027)
+++ mlpack/trunk/src/mlpack/core/util/CMakeLists.txt 2012-12-20 22:38:12 UTC (rev 14028)
@@ -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/trunk/src/mlpack/core/util/prefixedoutstream.hpp
===================================================================
--- mlpack/trunk/src/mlpack/core/util/prefixedoutstream.hpp 2012-12-20 22:19:16 UTC (rev 14027)
+++ mlpack/trunk/src/mlpack/core/util/prefixedoutstream.hpp 2012-12-20 22:38:12 UTC (rev 14028)
@@ -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 {
Modified: mlpack/trunk/src/mlpack/core/util/prefixedoutstream_impl.hpp
===================================================================
--- mlpack/trunk/src/mlpack/core/util/prefixedoutstream_impl.hpp 2012-12-20 22:19:16 UTC (rev 14027)
+++ mlpack/trunk/src/mlpack/core/util/prefixedoutstream_impl.hpp 2012-12-20 22:38:12 UTC (rev 14028)
@@ -56,7 +56,7 @@
>::type* = 0)
{
std::string result = s.ToString();
- BaseLogic<std::string&>(result);
+ BaseLogic<std::string>(result);
}
template<typename T>
Modified: mlpack/trunk/src/mlpack/core/util/sfinae_utility.hpp
===================================================================
--- mlpack/trunk/src/mlpack/core/util/sfinae_utility.hpp 2012-12-20 22:19:16 UTC (rev 14027)
+++ mlpack/trunk/src/mlpack/core/util/sfinae_utility.hpp 2012-12-20 22:38:12 UTC (rev 14028)
@@ -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
Added: mlpack/trunk/src/mlpack/core/util/string_util.cpp
===================================================================
--- mlpack/trunk/src/mlpack/core/util/string_util.cpp (rev 0)
+++ mlpack/trunk/src/mlpack/core/util/string_util.cpp 2012-12-20 22:38:12 UTC (rev 14028)
@@ -0,0 +1,33 @@
+/**
+ * @file string_util.cpp
+ *
+ * Defines methods useful for formatting output.
+ */
+
+#include "string_util.hpp"
+
+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)
+{
+ // 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;
+}
+} // namespace util
+} // namespace mlpack
Added: mlpack/trunk/src/mlpack/core/util/string_util.hpp
===================================================================
--- mlpack/trunk/src/mlpack/core/util/string_util.hpp (rev 0)
+++ mlpack/trunk/src/mlpack/core/util/string_util.hpp 2012-12-20 22:38:12 UTC (rev 14028)
@@ -0,0 +1,20 @@
+/**
+ * @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);
+
+}
+}
+#endif
More information about the mlpack-svn
mailing list