AFF --- A container for numbers (array) by Friederich and Forbriger.
arrayexample.cc
Go to the documentation of this file.
1 
56 #define ARRAYEXAMPLE_VERSION \
57  "ARRAYEXAMPLE V1.1 Demonstrate array usage"
58 
59 // include iostream for output to terminal
60 #include <iostream>
61 // provide function sqrt()
62 #include <cmath>
63 
64 // provide aff::Array class template
65 #include <aff/array.h>
66 // provide aff::Shaper class template
67 #include <aff/shaper.h>
68 // provide function template aff::subarray
69 #include <aff/subarray.h>
70 // provide function template to produce a dump of array contents
71 #include <aff/dump.h>
72 
73 using std::cout;
74 using std::endl;
75 
76 /* ---------------------------------------------------------------------- */
77 
82 void section(const char* s, const char l='-')
83 {
84  cout << endl << s << endl;
85  const char* p=s;
86  while (*p) { cout << l; ++p; }
87  cout << endl;
88 }
89 
90 /* ---------------------------------------------------------------------- */
91 
103 template<class T>
104  std::ostream& operator<<(std::ostream& os, const aff::ConstArray<T>& a)
105 {
106  // size of array dimensions
107  int N=a.size(0);
108  int M=a.size(1);
109 
110  // report size of array
111  cout << "Elements (i,j) of " << N << "x" << M << " array:" << endl;
112 
113  // width of first column, where value of i will be printed
114  const int firstwidth=6;
115  // width of columns:
116  int columnwidth=(78-firstwidth)/M;
117 
118  // print column headings
119  cout.width(firstwidth);
120  cout.setf(std::ios_base::left, std::ios_base::adjustfield);
121  cout << "";
122  for (int j=a.first(1); j<=a.last(1); j++)
123  {
124  cout << "j=";
125  cout.width(columnwidth-2);
126  cout.setf(std::ios_base::left, std::ios_base::adjustfield);
127  cout << j;
128  }
129  cout << endl;
130 
131  // print contents of array
132  // outer loop: first index
133  for (int i=a.first(0); i<=a.last(0); i++)
134  {
135  cout << "i=";
136  cout.width(firstwidth-2);
137  cout.setf(std::ios_base::left, std::ios_base::adjustfield);
138  cout << i;
139  // inner loop: second index
140  for (int j=a.first(1); j<=a.last(1); j++)
141  {
142  cout.width(columnwidth);
143  cout << a(i,j);
144  }
145  cout << endl;
146  }
147  return os;
148 } // std::ostream& operator<<(std::ostream& os, const aff::ConstArray<T>& a)
149 
150 /*======================================================================*/
151 
153 int main()
154 {
155  cout << ARRAYEXAMPLE_VERSION << endl;
156 
157  // create a 3x4 array of type float
158  aff::Array<float> A(3,4);
159 
160  // print array (layout and contents) to terminal
161  section("Array A after being created:");
162  cout << A;
163 
164  // set all elements in array to 5.2
165  A=5.2;
166  section("Array A after all elements being set to 5.2:");
167  cout << A;
168 
169  /* ---------------------------------------------------------------------- */
170 
171  section("Demonstrate effect of shallow copy", '=');
172  // create a second array
174 
175  // let B be a copy of A
176  B=A;
177 
178  // B just references the same memory; thus changing values in A and B will
179  // effectively change the same array values
180  A(2,3)=10.7;
181  B(3,1)=-19.3;
182 
183  // both modifications are present in A
184  section("Array A after assignment to B and to A:");
185  cout << A;
186 
187  /* ---------------------------------------------------------------------- */
188 
189  section("Demonstrate effect of deep copy", '=');
190  // create a copy by actually copying all elements
191  B=A.copyout();
192 
193  // modify B
194  B(3,3)=102.3;
195  section("Array B after assignment to B:");
196  cout << B;
197  section("Array A after assignment to B:");
198  cout << A;
199 
200  /* ---------------------------------------------------------------------- */
201 
202  section("Demonstrate array usage with custom index range", '=');
203 
204  /* To create an array which does not use index range beginning with 1, use
205  * the function Shaper, which is available through aff/shaper.h
206  */
207  A=aff::Array<float>(aff::Shaper(-2,6)(12,16));
208  A=sqrt(2.);
209  section("Array A after being assigned to array of different shape:");
210  cout << A;
211 }
212 
213 /* ----- END OF arrayexample.cc ----- */
Array< T > copyout() const
create an identical copy (deep copy) of this array
Definition: array.h:559
int main()
test array functionality
void section(const char *s, const char l='-')
Definition: arrayexample.cc:82
external class to create subarrays (prototypes)
full template array class headers (prototypes)
debug helpers (prototypes)
Full multi-dimensional array functionality.This is the full array class template. It adds no addition...
Definition: array.h:151
Shaper class for Fortran layout.
Definition: shaper.h:66
rectangular Fortran array layout (prototypes)
#define ARRAYEXAMPLE_VERSION
Definition: arrayexample.cc:56