[arrayfire] 258/408: FEAT added to string function

Ghislain Vaillant ghisvail-guest at moszumanska.debian.org
Mon Sep 21 19:12:09 UTC 2015


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

ghisvail-guest pushed a commit to branch debian/sid
in repository arrayfire.

commit ac92ddaea5a53006fddc80fd20292f4eebccc055
Author: Shehzan Mohammed <shehzan at arrayfire.com>
Date:   Thu Aug 13 17:53:07 2015 -0400

    FEAT added to string function
---
 docs/details/util.dox | 11 +++++++++++
 include/af/util.h     | 28 ++++++++++++++++++++++++++++
 src/api/c/print.cpp   | 46 +++++++++++++++++++++++++++++++++++++++++++---
 src/api/cpp/util.cpp  |  8 +++++++-
 4 files changed, 89 insertions(+), 4 deletions(-)

diff --git a/docs/details/util.dox b/docs/details/util.dox
index 5f70a4d..92ade02 100644
--- a/docs/details/util.dox
+++ b/docs/details/util.dox
@@ -11,6 +11,17 @@ Print Array and dimensions to screen
 
 =======================================================================
 
+\defgroup print_func_tostring toString
+
+\brief Print the array to a string instead of the screen
+
+This function is similar to af::print, except that it prints to a string
+rather than to screen.
+
+\ingroup arrayfire_func
+
+=======================================================================
+
 \defgroup stream_func_read readArray
 
 \brief Load an array from a file
diff --git a/include/af/util.h b/include/af/util.h
index bd655f7..2bbe155 100644
--- a/include/af/util.h
+++ b/include/af/util.h
@@ -82,6 +82,20 @@ namespace af
     */
     AFAPI int readArrayCheck(const char *filename, const char *key);
 
+    /**
+        \param[out] output is the pointer to the c-string that will hold the data. The memory for
+        output is allocated by the function. The user is responsible for deleting the memory.
+        \param[in] exp is an expression, generally the name of the array
+        \param[in] arr is the input array
+        \param[in] precision is the precision length for display
+        \param[in] transpose determines whether or not to transpose the array before storing it in
+        the string
+
+        \ingroup print_func_tostring
+    */
+    AFAPI void toString(char **output, const char *exp, const array &arr,
+                        const int precision = 4, const bool transpose = true);
+
     // Purpose of Addition: "How to add Function" documentation
     AFAPI array exampleFunction(const array& in, const af_someenum_t param);
 }
@@ -196,6 +210,20 @@ extern "C" {
     */
     AFAPI af_err af_read_array_key_check(int *index, const char *filename, const char* key);
 
+    /**
+        \param[out] output is the pointer to the c-string that will hold the data. The memory for
+        output is allocated by the function. The user is responsible for deleting the memory.
+        \param[in] exp is an expression, generally the name of the array
+        \param[in] arr is the input array
+        \param[in] precision is the precision length for display
+        \param[in] transpose determines whether or not to transpose the array before storing it in
+        the string
+
+        \ingroup print_func_tostring
+    */
+    AFAPI af_err af_array_to_string(char **output, const char *exp, const af_array arr,
+                                    const int precision, const bool transpose);
+
     // Purpose of Addition: "How to add Function" documentation
     AFAPI af_err af_example_function(af_array* out, const af_array in, const af_someenum_t param);
 
diff --git a/src/api/c/print.cpp b/src/api/c/print.cpp
index d820613..eb6dd05 100644
--- a/src/api/c/print.cpp
+++ b/src/api/c/print.cpp
@@ -10,6 +10,8 @@
 #include <iostream>
 #include <iomanip>
 #include <vector>
+#include <string>
+#include <sstream>
 #include <af/array.h>
 #include <af/data.h>
 #include <copy.hpp>
@@ -53,7 +55,7 @@ static void printer(ostream &out, const T* ptr, const ArrayInfo &info, unsigned
 }
 
 template<typename T>
-static void print(const char *exp, af_array arr, const int precision, std::ostream &os = std::cout)
+static void print(const char *exp, af_array arr, const int precision, std::ostream &os = std::cout, bool transpose = true)
 {
     if(exp == NULL) {
         os << "No Name Array" << std::endl;
@@ -65,12 +67,19 @@ static void print(const char *exp, af_array arr, const int precision, std::ostre
     vector<T> data(info.elements());
 
     af_array arrT;
-    AF_CHECK(af_reorder(&arrT, arr, 1, 0, 2, 3));
+    if(transpose) {
+        AF_CHECK(af_reorder(&arrT, arr, 1, 0, 2, 3));
+    } else {
+        arrT = arr;
+    }
 
     //FIXME: Use alternative function to avoid copies if possible
     AF_CHECK(af_get_data_ptr(&data.front(), arrT));
     const ArrayInfo infoT = getInfo(arrT);
-    AF_CHECK(af_release_array(arrT));
+
+    if(transpose) {
+        AF_CHECK(af_release_array(arrT));
+    }
 
     std::ios_base::fmtflags backup = os.flags();
 
@@ -133,3 +142,34 @@ af_err af_print_array_gen(const char *exp, const af_array arr, const int precisi
     CATCHALL;
     return AF_SUCCESS;
 }
+
+af_err af_array_to_string(char **output, const char *exp, const af_array arr,
+                          const int precision, bool transpose)
+{
+    try {
+        ARG_ASSERT(0, exp != NULL);
+        ArrayInfo info = getInfo(arr);
+        af_dtype type = info.getType();
+        std::stringstream ss;
+        switch(type)
+        {
+        case f32:   print<float   >(exp, arr, precision, ss, transpose);   break;
+        case c32:   print<cfloat  >(exp, arr, precision, ss, transpose);   break;
+        case f64:   print<double  >(exp, arr, precision, ss, transpose);   break;
+        case c64:   print<cdouble >(exp, arr, precision, ss, transpose);   break;
+        case b8:    print<char    >(exp, arr, precision, ss, transpose);   break;
+        case s32:   print<int     >(exp, arr, precision, ss, transpose);   break;
+        case u32:   print<unsigned>(exp, arr, precision, ss, transpose);   break;
+        case u8:    print<uchar   >(exp, arr, precision, ss, transpose);   break;
+        case s64:   print<intl    >(exp, arr, precision, ss, transpose);   break;
+        case u64:   print<uintl   >(exp, arr, precision, ss, transpose);   break;
+        default:    TYPE_ERROR(1, type);
+        }
+        std::string str = ss.str();
+        *output = new char[str.size() + 1];
+        std::copy(str.begin(), str.end(), *output);
+        (*output)[str.size()] = '\0'; // don't forget the terminating 0
+    }
+    CATCHALL;
+    return AF_SUCCESS;
+}
diff --git a/src/api/cpp/util.cpp b/src/api/cpp/util.cpp
index 5cc48b7..a99b856 100644
--- a/src/api/cpp/util.cpp
+++ b/src/api/cpp/util.cpp
@@ -24,7 +24,6 @@ namespace af
 
     void print(const char *exp, const array &arr, const int precision)
     {
-        printf("%s ", exp);
         AF_THROW(af_print_array_gen(exp, arr.get(), precision));
         return;
     }
@@ -56,4 +55,11 @@ namespace af
         AF_THROW(af_read_array_key_check(&out, filename, key));
         return out;
     }
+
+    void toString(char **output, const char *exp, const array &arr, const int precision, const bool transpose)
+    {
+        AF_THROW(af_array_to_string(output, exp, arr.get(), precision, transpose));
+        return;
+    }
+
 }

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



More information about the debian-science-commits mailing list