Sierra Toolkit  Version of the Day
FArrayPrint.hpp
1 #ifndef STK_UTIL_UTIL_FArrayPrint_h
2 #define STK_UTIL_UTIL_FArrayPrint_h
3 
4 #include <ostream>
5 #include <iomanip>
6 
8 
9 
10 namespace sierra {
11 
12 namespace {
13 
14 template<unsigned N> struct ArrayVerbosePrint ;
15 
16 template<>
17 struct ArrayVerbosePrint<0>
18 {
19  template<typename T>
20  static std::ostream &dump(std::ostream &os, const unsigned * const, const T *, const unsigned * const)
21  {
22  return os;
23  }
24 };
25 
26 template<>
27 struct ArrayVerbosePrint<1>
28 {
29  template<typename T>
30  static std::ostream &dump(std::ostream &os, const unsigned * const dim,
31  const T *a_ptr, const unsigned * const a_inc)
32  {
33  const unsigned inc = *a_inc ;
34  const T * ptr = a_ptr;
35  const T * const end = ptr + *dim * inc ;
36  os << "(";
37  while ( ptr != end ) {
38  if (ptr != a_ptr)
39  os << " ";
40  os << *ptr;
41  ptr += inc ;
42  }
43  os << ")";
44 
45  return os;
46  }
47 };
48 
49 template<>
50 struct ArrayVerbosePrint<2>
51 {
52 public:
53  template<typename T>
54  static std::ostream &dump(std::ostream &os, const unsigned * const dim,
55  const T *a_ptr, const unsigned * const a_inc)
56  {
57  if (dim[0] < 8) { // less than 8 columns wide
58  const unsigned inc = a_inc[1] ;
59  const T * r_ptr = a_ptr;
60  const T * const end = r_ptr + dim[1] * inc ;
61  os << "+-\n";
62  while ( r_ptr != end ) {
63  {
64  const unsigned inner_inc = *a_inc ;
65  const T * c_ptr = r_ptr;
66  const T * const inner_end = c_ptr + dim[0] * inner_inc ;
67  os << "| ";
68  while ( c_ptr != inner_end ) {
69  if (c_ptr != r_ptr)
70  os << " ";
71  os << *c_ptr;
72  c_ptr += inner_inc ;
73  }
74  os << "\n";
75  }
76 
77  r_ptr += inc ;
78  }
79  os << "+-\n";
80  }
81  else {
82  const unsigned inc = a_inc[1] ;
83  const T * ptr = a_ptr;
84  const T * const end = ptr + dim[1] * inc ;
85  while ( ptr != end ) {
86  ArrayVerbosePrint<1>::dump(os, dim, ptr, a_inc );
87  os << std::endl;
88  ptr += inc ;
89  }
90  }
91 
92  return os;
93  }
94 };
95 
96 template<>
97 struct ArrayVerbosePrint<3>
98 {
99 public:
100  template<typename T>
101  static std::ostream &dump(std::ostream &os, const unsigned * const dim,
102  const T *a_ptr, const unsigned * const a_inc)
103  {
104 // if (a_inc[3] < 50) {
105 // const unsigned ia = a_inc[2] ;
106 // unsigned index = 0;
107 // const T * ptr = a_ptr;
108 // const T * const end = ptr + a_inc[3];
109 // while ( ptr != end ) {
110 // if (ptr != a_ptr)
111 // os << ",";
112 // os << "(";
113 // ArrayVerbosePrint<2>::dump(os, dim, ptr, a_inc );
114 // os << ")";
115 // ptr += ia ;
116 // ++index;
117 // }
118 // }
119 // else {
120  const unsigned ia = a_inc[2] ;
121  unsigned index = 0;
122  const T * const a_end = a_ptr + a_inc[3];
123  while ( a_end != a_ptr ) {
124  os << "(";
125  for (unsigned i = 0; i < 2; ++i)
126  os << "0:" << dim[i] - 1 << ", ";
127  os << index << ")" << std::endl;
128  ArrayVerbosePrint<2>::dump(os, dim, a_ptr, a_inc );
129  os << std::endl << std::endl;
130  a_ptr += ia ;
131  ++index;
132  }
133 // }
134 
135  return os;
136  }
137 };
138 
139 template<unsigned N>
140 struct ArrayVerbosePrint
141 {
142 public:
143  template<typename T>
144  static std::ostream &dump(std::ostream &os, const unsigned * const dim,
145  const T *a_ptr, const unsigned * const a_inc)
146  {
147  const unsigned ia = a_inc[N - 1] ;
148  unsigned index = 0;
149  const T * const a_end = a_ptr + a_inc[N];
150  while ( a_end != a_ptr ) {
151  os << "(";
152  for (unsigned i = 0; i < N - 1; ++i)
153  os << "0:" << dim[i] - 1 << ", ";
154  os << index << ")" << std::endl;
155  ArrayVerbosePrint<N - 1>::dump(os, dim, a_ptr, a_inc );
156  os << std::endl << std::endl;
157  a_ptr += ia ;
158  ++index;
159  }
160 
161  return os;
162  }
163 };
164 
165 } // namespace <unnamed>
166 
167 template< class ElementType,
168  class Tag0,
169  class Tag1,
170  class Tag2,
171  class Tag3,
172  class Tag4,
173  class Tag5,
174  class Tag6,
175  class Tag7 >
176 std::ostream &
177 operator<<(
178  std::ostream & os,
180 {
182 
183  ArrayVerbosePrint<X::NumDim>::dump(os, array.dimension(), array.ptr(), array.stride());
184  os << std::endl;
185 
186  return os;
187 }
188 
189 
190 template< class ElementType,
191  class Tag0,
192  class Tag1,
193  class Tag2,
194  class Tag3,
195  class Tag4,
196  class Tag5,
197  class Tag6,
198  class Tag7 >
199 std::ostream &
200 operator<<(
201  std::ostream & os,
203 {
205 
206  ArrayVerbosePrint<X::NumDim>::dump(os, array.dimension(), array.ptr(), array.stride());
207  os << std::endl;
208 
209  return os;
210 }
211 
212 
213 template< class ElementType, int Dimension>
214 std::ostream &
215 operator<<(
216  std::ostream & os,
218 {
220 
221  ArrayVerbosePrint<X::NumDim>::dump(os, array.dimension(), array.ptr(), array.stride());
222  os << std::endl;
223 
224  return os;
225 }
226 
227 
228 template< class ElementType, int Dimension>
229 std::ostream &
230 operator<<(
231  std::ostream & os,
233 {
235 
236  ArrayVerbosePrint<X::NumDim>::dump(os, array.dimension(), array.ptr(), array.stride());
237  os << std::endl;
238 
239  return os;
240 }
241 
242 } // namespace sierra
243 
244 #endif // STK_UTIL_UTIL_FArrayPrint_h
Definition: Env.cpp:53
c_ptr_< T > c_ptr(const T *t)
Definition: Writer.hpp:675
Extend Array with deep copy assignment and resize operations.
Definition: Array.hpp:101
std::ostream & operator<<(std::ostream &s, const Bucket &k)
Print the part names for which this bucket is a subset.
Definition: Bucket.cpp:239
Multidimensional array of contiguous memory. The memory is not owned by the array, but container access semantics are enforced, i.e. const Array<> elements cannot be assigned to.
Definition: FArray.hpp:78
Extend FArray with deep copy assignment and resize operations.
Definition: FArray.hpp:86
Writer & dump(Writer &dout, const std::vector< T > &t)
Template dump prints the object contained within a std::vector object to the diagnostic writer...
Definition: WriterExt.hpp:115
const unsigned * dimension() const
Definition: Array.hpp:177
Multidimensional array view of contiguous memory.
Definition: Array.hpp:82