Sierra Toolkit Version of the Day
FArrayPrint.hpp
00001 #ifndef STK_UTIL_UTIL_FArrayPrint_h
00002 #define STK_UTIL_UTIL_FArrayPrint_h
00003 
00004 #include <ostream>
00005 #include <iomanip>
00006 
00007 #include <stk_util/diag/FArray.hpp>
00008 
00009 
00010 namespace sierra {
00011 
00012 namespace {
00013 
00014 template<unsigned N> struct ArrayVerbosePrint ;
00015 
00016 template<>
00017 struct ArrayVerbosePrint<0>
00018 {
00019   template<typename T>
00020   static std::ostream &dump(std::ostream &os, const unsigned * const, const T *, const unsigned * const)
00021   {
00022     return os;
00023   }
00024 };
00025 
00026 template<>
00027 struct ArrayVerbosePrint<1>
00028 {
00029   template<typename T>
00030   static std::ostream &dump(std::ostream &os, const unsigned * const dim,
00031                             const T *a_ptr, const unsigned * const a_inc)
00032   {
00033     const unsigned inc = *a_inc ;
00034     const T * ptr = a_ptr;
00035     const T * const end = ptr + *dim * inc ;
00036     os << "(";
00037     while ( ptr != end ) {
00038       if (ptr != a_ptr)
00039         os << " ";
00040       os << *ptr;
00041       ptr += inc ;
00042     }
00043     os << ")";
00044 
00045     return os;
00046   }
00047 };
00048 
00049 template<>
00050 struct ArrayVerbosePrint<2>
00051 {
00052 public:
00053   template<typename T>
00054   static std::ostream &dump(std::ostream &os, const unsigned * const dim,
00055                             const T *a_ptr, const unsigned * const a_inc)
00056   {
00057     if (dim[0] < 8) { // less than 8 columns wide
00058       const unsigned inc = a_inc[1] ;
00059       const T * r_ptr = a_ptr;
00060       const T * const end = r_ptr + dim[1] * inc ;
00061       os << "+-\n";
00062       while ( r_ptr != end ) {
00063         {
00064           const unsigned inner_inc = *a_inc ;
00065           const T * c_ptr = r_ptr;
00066           const T * const inner_end = c_ptr + dim[0] * inner_inc ;
00067           os << "| ";
00068           while ( c_ptr != inner_end ) {
00069             if (c_ptr != r_ptr)
00070               os << " ";
00071             os << *c_ptr;
00072             c_ptr += inner_inc ;
00073           }
00074           os << "\n";
00075         }
00076           
00077         r_ptr += inc ;
00078       }
00079       os << "+-\n";
00080     }
00081     else {
00082       const unsigned inc = a_inc[1] ;
00083       const T * ptr = a_ptr;
00084       const T * const end = ptr + dim[1] * inc ;
00085       while ( ptr != end ) {
00086         ArrayVerbosePrint<1>::dump(os, dim, ptr, a_inc );
00087         os << std::endl;
00088         ptr += inc ;
00089       }
00090     }
00091 
00092     return os;
00093   }
00094 };
00095 
00096 template<>
00097 struct ArrayVerbosePrint<3>
00098 {
00099 public:
00100   template<typename T>
00101   static std::ostream &dump(std::ostream &os, const unsigned * const dim,
00102                             const T *a_ptr, const unsigned * const a_inc)
00103   {
00104 //       if (a_inc[3] < 50) {
00105 //  const unsigned ia = a_inc[2] ;
00106 //  unsigned index = 0;
00107 //  const T * ptr = a_ptr;
00108 //  const T * const end = ptr + a_inc[3];
00109 //  while ( ptr != end ) {
00110 //    if (ptr != a_ptr)
00111 //      os << ",";
00112 //    os << "(";
00113 //    ArrayVerbosePrint<2>::dump(os, dim, ptr, a_inc );
00114 //    os << ")";
00115 //    ptr += ia ;
00116 //    ++index;
00117 //  }
00118 //       }
00119 //       else {
00120     const unsigned ia = a_inc[2] ;
00121     unsigned index = 0;
00122     const T * const a_end = a_ptr + a_inc[3];
00123     while ( a_end != a_ptr ) {
00124       os << "(";
00125       for (unsigned i = 0; i < 2; ++i)
00126         os << "0:" << dim[i] - 1 << ", ";
00127       os << index << ")" << std::endl;
00128       ArrayVerbosePrint<2>::dump(os, dim, a_ptr, a_inc );
00129       os << std::endl << std::endl;
00130       a_ptr += ia ;
00131       ++index;
00132     }
00133 //       }
00134 
00135     return os;
00136   }
00137 };
00138 
00139 template<unsigned N>
00140 struct ArrayVerbosePrint
00141 {
00142 public:
00143   template<typename T>
00144   static std::ostream &dump(std::ostream &os, const unsigned * const dim,
00145                             const T *a_ptr, const unsigned * const a_inc)
00146   {
00147     const unsigned ia = a_inc[N - 1] ;
00148     unsigned index = 0;
00149     const T * const a_end = a_ptr + a_inc[N];
00150     while ( a_end != a_ptr ) {
00151       os << "(";
00152       for (unsigned i = 0; i < N - 1; ++i)
00153         os << "0:" << dim[i] - 1 << ", ";
00154       os << index << ")" << std::endl;
00155       ArrayVerbosePrint<N - 1>::dump(os, dim, a_ptr, a_inc );
00156       os << std::endl << std::endl;
00157       a_ptr += ia ;
00158       ++index;
00159     }
00160 
00161     return os;
00162   }
00163 };
00164 
00165 } // namespace <unnamed>
00166 
00167 template< class ElementType,
00168     class Tag0,
00169     class Tag1,
00170     class Tag2,
00171     class Tag3,
00172     class Tag4,
00173     class Tag5,
00174     class Tag6,
00175     class Tag7 >
00176 std::ostream &
00177 operator<<(
00178   std::ostream &        os,
00179   const sierra::Array<ElementType, Tag0, Tag1, Tag2, Tag3, Tag4, Tag5, Tag6, Tag7 > &array)
00180 {
00181   typedef sierra::Array<ElementType, Tag0, Tag1, Tag2, Tag3, Tag4, Tag5, Tag6, Tag7 > X;
00182 
00183   ArrayVerbosePrint<X::NumDim>::dump(os, array.dimension(), array.ptr(), array.stride());
00184   os << std::endl;
00185 
00186   return os;
00187 }
00188 
00189 
00190 template< class ElementType,
00191     class Tag0,
00192     class Tag1,
00193     class Tag2,
00194     class Tag3,
00195     class Tag4,
00196     class Tag5,
00197     class Tag6,
00198     class Tag7 >
00199 std::ostream &
00200 operator<<(
00201   std::ostream &        os,
00202   const sierra::ArrayContainer<ElementType, Tag0, Tag1, Tag2, Tag3, Tag4, Tag5, Tag6, Tag7 > &array)
00203 {
00204   typedef sierra::ArrayContainer<ElementType, Tag0, Tag1, Tag2, Tag3, Tag4, Tag5, Tag6, Tag7 > X;
00205 
00206   ArrayVerbosePrint<X::NumDim>::dump(os, array.dimension(), array.ptr(), array.stride());
00207   os << std::endl;
00208 
00209   return os;
00210 }
00211 
00212 
00213 template< class ElementType, int Dimension>
00214 std::ostream &
00215 operator<<(
00216   std::ostream &        os,
00217   const sierra::FArray<ElementType, Dimension> &array)
00218 {
00219   typedef sierra::FArray<ElementType, Dimension> X;
00220 
00221   ArrayVerbosePrint<X::NumDim>::dump(os, array.dimension(), array.ptr(), array.stride());
00222   os << std::endl;
00223 
00224   return os;
00225 }
00226 
00227 
00228 template< class ElementType, int Dimension>
00229 std::ostream &
00230 operator<<(
00231   std::ostream &        os,
00232   const sierra::FArrayContainer<ElementType, Dimension> &array)
00233 {
00234   typedef sierra::FArrayContainer<ElementType, Dimension> X;
00235 
00236   ArrayVerbosePrint<X::NumDim>::dump(os, array.dimension(), array.ptr(), array.stride());
00237   os << std::endl;
00238 
00239   return os;
00240 }
00241 
00242 } // namespace sierra
00243 
00244 #endif // STK_UTIL_UTIL_FArrayPrint_h
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines