source: XIOS/dev/dev_olga/src/extern/blitz/include/blitz/array/io.cc @ 1022

Last change on this file since 1022 was 1022, checked in by mhnguyen, 7 years ago
File size: 6.1 KB
Line 
1/***************************************************************************
2 * blitz/array/io.cc  Input/output of arrays.
3 *
4 * $Id$
5 *
6 * Copyright (C) 1997-2011 Todd Veldhuizen <tveldhui@acm.org>
7 *
8 * This file is a part of Blitz.
9 *
10 * Blitz is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License
12 * as published by the Free Software Foundation, either version 3
13 * of the License, or (at your option) any later version.
14 *
15 * Blitz is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 * GNU Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with Blitz.  If not, see <http://www.gnu.org/licenses/>.
22 *
23 * Suggestions:          blitz-devel@lists.sourceforge.net
24 * Bugs:                 blitz-support@lists.sourceforge.net   
25 *
26 * For more information, please see the Blitz++ Home Page:
27 *    https://sourceforge.net/projects/blitz/
28 *
29 ****************************************************************************/
30#ifndef BZ_ARRAYIO_CC
31#define BZ_ARRAYIO_CC
32
33#ifndef BZ_ARRAY_H
34 #error <blitz/array/io.cc> must be included via <blitz/array.h>
35#endif
36
37BZ_NAMESPACE(blitz)
38
39// NEEDS_WORK???
40// This version of operator<< is updated on August 2005
41// by Sergei Mingaleev <mingaleev@gmail.com>.
42// Also, the corresponding operator>> is updated.
43
44template<typename T_numtype>
45ostream& operator<<(ostream& os, const Array<T_numtype,1>& x)
46{
47  // Write the extent vector: e.g., (-4, 4)
48
49  os << "(" << x.lbound(0) << "," << x.ubound(0) << ")";
50  os << endl << "[ ";
51
52  for (int i1=x.lbound(0); i1<=x.ubound(0); i1++) {
53    os << x(i1) << " ";
54  }
55  os << "]" << endl;
56  return os;
57}
58
59template<typename T_numtype, int N_rank>
60ostream& operator<<(ostream& os, const Array<T_numtype,N_rank>& x)
61{
62  // Write the extent vector: this is separated by 'x's, e.g.
63  // (1, 10) x (-4, 4) x (-5, 5)
64
65  for (int i=0; i < N_rank; ++i) {
66    os << "(";
67    os << x.lbound(i); 
68    os << ",";
69    os << x.ubound(i); 
70    os << ")";
71    if (i != N_rank-1) os << " x ";
72  }
73  os << endl << "[ ";
74
75  switch (N_rank) {
76    case 2
77      for (int i1=x.lbound(0); i1<=x.ubound(0); i1++) {
78        for (int i2=x.lbound(1); i2<=x.ubound(1); i2++) {
79          os << x(i1,i2) << " ";
80        }
81        if (i1 != x.ubound(0)) os << endl << "  ";
82      }
83      break;
84    case 3:
85      for (int i1=x.lbound(0); i1<=x.ubound(0); i1++) {
86        for (int i2=x.lbound(1); i2<=x.ubound(1); i2++) {
87          for (int i3=x.lbound(2); i3<=x.ubound(2); i3++) {
88              os << x(i1,i2,i3) << " ";
89          }
90          if (i1 != x.ubound(0) || i2 != x.ubound(1)) os << endl << "  ";
91        }
92      }
93      break;
94    case 4:
95      for (int i1=x.lbound(0); i1<=x.ubound(0); i1++) {
96        for (int i2=x.lbound(1); i2<=x.ubound(1); i2++) {
97          for (int i3=x.lbound(2); i3<=x.ubound(2); i3++) {
98            for (int i4=x.lbound(3); i4<=x.ubound(3); i4++) {
99                os << x(i1,i2,i3,i4) << " ";
100            }
101            if (i1 != x.ubound(0) || i2 != x.ubound(1) || i3 != x.ubound(2)) 
102              os << endl << "  ";
103          }
104        }
105      }
106      break;
107    default:
108      cout << "Error: operator<< for " << N_rank
109           << "D Array is not supported!" << endl;
110      BZASSERT("ERROR!");
111      break;
112  };
113
114  os << "]" << endl;
115  return os;
116}
117
118/*
119 *  Input
120 */
121
122template<typename T_numtype, int N_rank>
123istream& operator>>(istream& is, Array<T_numtype,N_rank>& x)
124{
125  TinyVector<int,N_rank> lower_bounds, upper_bounds, extent;
126  char sep;
127
128  // Read the extent vector: this is separated by 'x's, e.g.
129  // (1, 10) x (-4, 4) x (-5, 5)
130
131  for (int i=0; i < N_rank; ++i) {
132    is >> sep;
133    BZPRECHECK(!is.bad(), "Premature end of input while scanning Array");
134    BZPRECHECK(sep == '(', "Format error while scanning input \
135Array \n -- expected '(' opening Array extents");
136
137    is >> lower_bounds(i); 
138    is >> sep; 
139    BZPRECHECK(sep == ',', "Format error while scanning input \
140Array \n -- expected ',' between Array extents");
141    is >> upper_bounds(i);
142
143    is >> sep; 
144    BZPRECHECK(sep == ')', "Format error while scanning input \
145Array \n -- expected ',' closing Array extents");
146
147    if (i != N_rank-1) {
148      is >> sep;
149      BZPRECHECK(sep == 'x', "Format error while scanning input \
150Array \n (expected 'x' between Array extents)");
151    }
152  }
153
154  is >> sep;
155  BZPRECHECK(sep == '[', "Format error while scanning input \
156Array \n (expected '[' before beginning of Array data)");
157
158  for (int i=0; i < N_rank; ++i)
159      extent(i) = upper_bounds(i) - lower_bounds(i) + 1;
160  x.resize(extent);
161  x.reindexSelf(lower_bounds);
162
163  switch (N_rank) {
164    case 1:
165      for (int i1=x.lbound(0); i1<=x.ubound(0); i1++) {
166        BZPRECHECK(!is.bad(), "Premature end of input while scanning Array");
167        is >> x(i1);
168      }
169      break;
170    case 2:
171      for (int i1=x.lbound(0); i1<=x.ubound(0); i1++) {
172        for (int i2=x.lbound(1); i2<=x.ubound(1); i2++) {
173          BZPRECHECK(!is.bad(), "Premature end of input while scanning Array");
174          is >> x(i1,i2);
175        }
176      }
177      break;
178    case 3:
179      for (int i1=x.lbound(0); i1<=x.ubound(0); i1++) {
180        for (int i2=x.lbound(1); i2<=x.ubound(1); i2++) {
181          for (int i3=x.lbound(2); i3<=x.ubound(2); i3++) {
182            BZPRECHECK(!is.bad(), "Premature end of input while scanning Array");
183            is >> x(i1,i2,i3);
184          }
185        }
186      }
187      break;
188    case 4:
189      for (int i1=x.lbound(0); i1<=x.ubound(0); i1++) {
190        for (int i2=x.lbound(1); i2<=x.ubound(1); i2++) {
191          for (int i3=x.lbound(2); i3<=x.ubound(2); i3++) {
192            for (int i4=x.lbound(3); i4<=x.ubound(3); i4++) {
193              BZPRECHECK(!is.bad(), "Premature end of input while scanning Array");
194              is >> x(i1,i2,i3,i4);
195            }
196          }
197        }
198      }
199      break;
200    default:
201      cout << "Error: read() for " << N_rank
202           << "D Array is not supported!" << endl;
203      BZASSERT("ERROR!");
204      break;
205  };
206
207  is >> sep;
208  BZPRECHECK(sep == ']', "Format error while scanning input \
209Array \n (expected ']' after end of Array data)");
210  return is;
211}
212
213BZ_NAMESPACE_END
214
215#endif // BZ_ARRAYIO_CC
Note: See TracBrowser for help on using the repository browser.