[arrayfire] 220/408: Fixes to print functions

Ghislain Vaillant ghisvail-guest at moszumanska.debian.org
Mon Sep 21 19:12:02 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 07ffda726133e885e5ac2cc1a3564374064842d2
Author: Shehzan Mohammed <shehzan at arrayfire.com>
Date:   Mon Aug 10 15:23:36 2015 -0400

    Fixes to print functions
    
    * Print the name of the array in the print functions instead of cpp. Since
    af_print_array API cannot be changed, added a new function called
    af_print_array_c that takes in the expression as input. af_print_array now
    prints "No Name Array" for the name of the array.
    * The cpp print function calls af_print_array_c now. This function sould
    replace af_print_array in v4.x
    * Now allow print to use either cout or a stream. This will allow writing to
    string streams
---
 include/af/util.h    | 20 +++++++-----
 src/api/c/print.cpp  | 88 +++++++++++++++++++++++++++++++++++-----------------
 src/api/cpp/util.cpp |  5 ++-
 3 files changed, 74 insertions(+), 39 deletions(-)

diff --git a/include/af/util.h b/include/af/util.h
index b280246..2c7b7ac 100644
--- a/include/af/util.h
+++ b/include/af/util.h
@@ -16,13 +16,6 @@ namespace af
     class array;
 
     /**
-        \defgroup print_func_print print
-
-        \brief Print the array to screen
-
-        \ingroup arrayfire_func
-    */
-    /**
         \param[in] exp is an expression, generally the name of the array
         \param[in] arr is the input array
 
@@ -97,6 +90,17 @@ extern "C" {
     AFAPI af_err af_print_array(af_array arr);
 
     /**
+        \param[in] exp is the expression or name of the array
+        \param[in] arr is the input array
+
+        \returns error codes
+
+        \ingroup print_func_print
+    */
+    AFAPI af_err af_print_array_c(const char *exp, const af_array arr);
+
+    /**
+        \param[in] exp is the expression or name of the array
         \param[in] arr is the input array
         \param[in] precision precision for the display
 
@@ -104,7 +108,7 @@ extern "C" {
 
         \ingroup print_func_print
     */
-    AFAPI af_err af_print_array_p(af_array arr, const int precision);
+    AFAPI af_err af_print_array_p(const char *exp, const af_array arr, const int precision);
 
     // 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 1b9357b..e91a4ed 100644
--- a/src/api/c/print.cpp
+++ b/src/api/c/print.cpp
@@ -53,8 +53,14 @@ static void printer(ostream &out, const T* ptr, const ArrayInfo &info, unsigned
 }
 
 template<typename T>
-static void print(af_array arr, const int precision)
+static void print(const char *exp, af_array arr, const int precision, std::ostream &os = std::cout)
 {
+    if(exp == NULL) {
+        os << "No Name Array" << std::endl;
+    } else {
+        os << exp << std::endl;
+    }
+
     const ArrayInfo info = getInfo(arr);
     vector<T> data(info.elements());
 
@@ -66,17 +72,17 @@ static void print(af_array arr, const int precision)
     const ArrayInfo infoT = getInfo(arrT);
     AF_CHECK(af_release_array(arrT));
 
-    std::ios_base::fmtflags backup = std::cout.flags();
+    std::ios_base::fmtflags backup = os.flags();
 
-    std::cout << "[" << info.dims() << "]\n";
+    os << "[" << info.dims() << "]\n";
 #ifndef NDEBUG
-    std::cout <<"   Offsets: ["<<info.offsets()<<"]"<<std::endl;
-    std::cout <<"   Strides: ["<<info.strides()<<"]"<<std::endl;
+    os <<"   Offsets: [" << info.offsets() << "]" << std::endl;
+    os <<"   Strides: [" << info.strides() << "]" << std::endl;
 #endif
 
-    printer(std::cout, &data.front(), infoT, infoT.ndims() - 1, precision);
+    printer(os, &data.front(), infoT, infoT.ndims() - 1, precision);
 
-    std::cout.flags(backup);
+    os.flags(backup);
 }
 
 af_err af_print_array(af_array arr)
@@ -86,16 +92,41 @@ af_err af_print_array(af_array arr)
         af_dtype type = info.getType();
         switch(type)
         {
-        case f32:   print<float>   (arr, 4);   break;
-        case c32:   print<cfloat>  (arr, 4);   break;
-        case f64:   print<double>  (arr, 4);   break;
-        case c64:   print<cdouble> (arr, 4);   break;
-        case b8:    print<char>    (arr, 4);   break;
-        case s32:   print<int>     (arr, 4);   break;
-        case u32:   print<unsigned>(arr, 4);   break;
-        case u8:    print<uchar>   (arr, 4);   break;
-        case s64:   print<intl>    (arr, 4);   break;
-        case u64:   print<uintl>   (arr, 4);   break;
+        case f32:   print<float>   (NULL, arr, 4);   break;
+        case c32:   print<cfloat>  (NULL, arr, 4);   break;
+        case f64:   print<double>  (NULL, arr, 4);   break;
+        case c64:   print<cdouble> (NULL, arr, 4);   break;
+        case b8:    print<char>    (NULL, arr, 4);   break;
+        case s32:   print<int>     (NULL, arr, 4);   break;
+        case u32:   print<unsigned>(NULL, arr, 4);   break;
+        case u8:    print<uchar>   (NULL, arr, 4);   break;
+        case s64:   print<intl>    (NULL, arr, 4);   break;
+        case u64:   print<uintl>   (NULL, arr, 4);   break;
+        default:    TYPE_ERROR(1, type);
+        }
+    }
+    CATCHALL;
+    return AF_SUCCESS;
+}
+
+af_err af_print_array_c(const char *exp, const af_array arr)
+{
+    try {
+        ARG_ASSERT(0, exp != NULL);
+        ArrayInfo info = getInfo(arr);
+        af_dtype type = info.getType();
+        switch(type)
+        {
+        case f32:   print<float>   (exp, arr, 4);   break;
+        case c32:   print<cfloat>  (exp, arr, 4);   break;
+        case f64:   print<double>  (exp, arr, 4);   break;
+        case c64:   print<cdouble> (exp, arr, 4);   break;
+        case b8:    print<char>    (exp, arr, 4);   break;
+        case s32:   print<int>     (exp, arr, 4);   break;
+        case u32:   print<unsigned>(exp, arr, 4);   break;
+        case u8:    print<uchar>   (exp, arr, 4);   break;
+        case s64:   print<intl>    (exp, arr, 4);   break;
+        case u64:   print<uintl>   (exp, arr, 4);   break;
         default:    TYPE_ERROR(1, type);
         }
     }
@@ -103,23 +134,24 @@ af_err af_print_array(af_array arr)
     return AF_SUCCESS;
 }
 
-af_err af_print_array_p(af_array arr, const int precision)
+af_err af_print_array_p(const char *exp, const af_array arr, const int precision)
 {
     try {
+        ARG_ASSERT(0, exp != NULL);
         ArrayInfo info = getInfo(arr);
         af_dtype type = info.getType();
         switch(type)
         {
-        case f32:   print<float>   (arr, precision);   break;
-        case c32:   print<cfloat>  (arr, precision);   break;
-        case f64:   print<double>  (arr, precision);   break;
-        case c64:   print<cdouble> (arr, precision);   break;
-        case b8:    print<char>    (arr, precision);   break;
-        case s32:   print<int>     (arr, precision);   break;
-        case u32:   print<unsigned>(arr, precision);   break;
-        case u8:    print<uchar>   (arr, precision);   break;
-        case s64:   print<intl>    (arr, precision);   break;
-        case u64:   print<uintl>   (arr, precision);   break;
+        case f32:   print<float   >(exp, arr, precision);   break;
+        case c32:   print<cfloat  >(exp, arr, precision);   break;
+        case f64:   print<double  >(exp, arr, precision);   break;
+        case c64:   print<cdouble >(exp, arr, precision);   break;
+        case b8:    print<char    >(exp, arr, precision);   break;
+        case s32:   print<int     >(exp, arr, precision);   break;
+        case u32:   print<unsigned>(exp, arr, precision);   break;
+        case u8:    print<uchar   >(exp, arr, precision);   break;
+        case s64:   print<intl    >(exp, arr, precision);   break;
+        case u64:   print<uintl   >(exp, arr, precision);   break;
         default:    TYPE_ERROR(1, type);
         }
     }
diff --git a/src/api/cpp/util.cpp b/src/api/cpp/util.cpp
index f4e9ff6..5ce7aa4 100644
--- a/src/api/cpp/util.cpp
+++ b/src/api/cpp/util.cpp
@@ -18,15 +18,14 @@ namespace af
 {
     void print(const char *exp, const array &arr)
     {
-        printf("%s ", exp);
-        AF_THROW(af_print_array(arr.get()));
+        AF_THROW(af_print_array_c(exp, arr.get()));
         return;
     }
 
     void print(const char *exp, const array &arr, const int precision)
     {
         printf("%s ", exp);
-        AF_THROW(af_print_array_p(arr.get(), precision));
+        AF_THROW(af_print_array_p(exp, arr.get(), precision));
         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