1
1
/***************************************************************************
2
2
* blitz/array/io.cc Input/output of arrays.
4
* Copyright (C) 1997-2001 Todd Veldhuizen <tveldhui@oonumerics.org>
6
* This program is free software; you can redistribute it and/or
7
* modify it under the terms of the GNU General Public License
8
* as published by the Free Software Foundation; either version 2
6
* Copyright (C) 1997-2011 Todd Veldhuizen <tveldhui@acm.org>
8
* This file is a part of Blitz.
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
9
13
* of the License, or (at your option) any later version.
11
* This program is distributed in the hope that it will be useful,
15
* Blitz is distributed in the hope that it will be useful,
12
16
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
17
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU General Public License for more details.
18
* GNU Lesser General Public License for more details.
16
* Suggestions: blitz-dev@oonumerics.org
17
* Bugs: blitz-bugs@oonumerics.org
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/>.
23
* Suggestions: blitz-devel@lists.sourceforge.net
24
* Bugs: blitz-support@lists.sourceforge.net
19
26
* For more information, please see the Blitz++ Home Page:
20
* http://oonumerics.org/blitz/
27
* https://sourceforge.net/projects/blitz/
22
29
****************************************************************************/
23
30
#ifndef BZ_ARRAYIO_CC
30
37
BZ_NAMESPACE(blitz)
40
// This version of operator<< is updated on August 2005
41
// by Sergei Mingaleev <mingaleev@gmail.com>.
42
// Also, the corresponding operator>> is updated.
32
44
template<typename T_numtype>
33
45
ostream& operator<<(ostream& os, const Array<T_numtype,1>& x)
35
os << x.extent(firstRank) << endl;
37
for (int i=x.lbound(firstRank); i <= x.ubound(firstRank); ++i)
39
os << setw(9) << x(i) << " ";
40
if (!((i+1-x.lbound(firstRank))%7))
47
template<typename T_numtype>
48
ostream& operator<<(ostream& os, const Array<T_numtype,2>& x)
50
os << x.rows() << " x " << x.columns() << endl;
52
for (int i=x.lbound(firstRank); i <= x.ubound(firstRank); ++i)
54
for (int j=x.lbound(secondRank); j <= x.ubound(secondRank); ++j)
56
os << setw(9) << x(i,j) << " ";
57
if (!((j+1-x.lbound(secondRank)) % 7))
61
if (i != x.ubound(firstRank))
47
// Write the extent vector: e.g., (-4, 4)
49
os << "(" << x.lbound(0) << "," << x.ubound(0) << ")";
52
for (int i1=x.lbound(0); i1<=x.ubound(0); i1++) {
70
59
template<typename T_numtype, int N_rank>
71
60
ostream& operator<<(ostream& os, const Array<T_numtype,N_rank>& x)
73
for (int i=0; i < N_rank; ++i)
82
_bz_typename Array<T_numtype, N_rank>::const_iterator iter = x.begin();
83
_bz_typename Array<T_numtype, N_rank>::const_iterator end = x.end();
87
os << setw(9) << (*iter) << " ";
90
// See if we need a linefeed
62
// Write the extent vector: this is separated by 'x's, e.g.
63
// (1, 10) x (-4, 4) x (-5, 5)
65
for (int i=0; i < N_rank; ++i) {
71
if (i != N_rank-1) os << " x ";
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) << " ";
81
if (i1 != x.ubound(0)) os << endl << " ";
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) << " ";
90
if (i1 != x.ubound(0) || i2 != x.ubound(1)) os << endl << " ";
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) << " ";
101
if (i1 != x.ubound(0) || i2 != x.ubound(1) || i3 != x.ubound(2))
108
cout << "Error: operator<< for " << N_rank
109
<< "D Array is not supported!" << endl;
104
122
template<typename T_numtype, int N_rank>
105
123
istream& operator>>(istream& is, Array<T_numtype,N_rank>& x)
107
TinyVector<int,N_rank> extent;
110
// Read the extent vector: this is separated by 'x's, e.g.
113
for (int i=0; i < N_rank; ++i)
117
BZPRECHECK(!is.bad(), "Premature end of input while scanning array");
122
BZPRECHECK(sep == 'x', "Format error while scanning input array"
123
<< endl << " (expected 'x' between array extents)");
128
BZPRECHECK(sep == '[', "Format error while scanning input array"
129
<< endl << " (expected '[' before beginning of array data)");
133
_bz_typename Array<T_numtype,N_rank>::iterator iter = x.begin();
134
_bz_typename Array<T_numtype,N_rank>::iterator end = x.end();
136
while (iter != end) {
137
BZPRECHECK(!is.bad(), "Premature end of input while scanning array");
144
BZPRECHECK(sep == ']', "Format error while scanning input array"
145
<< endl << " (expected ']' after end of array data)");
125
TinyVector<int,N_rank> lower_bounds, upper_bounds, extent;
128
// Read the extent vector: this is separated by 'x's, e.g.
129
// (1, 10) x (-4, 4) x (-5, 5)
131
for (int i=0; i < N_rank; ++i) {
133
BZPRECHECK(!is.bad(), "Premature end of input while scanning Array");
134
BZPRECHECK(sep == '(', "Format error while scanning input \
135
Array \n -- expected '(' opening Array extents");
137
is >> lower_bounds(i);
139
BZPRECHECK(sep == ',', "Format error while scanning input \
140
Array \n -- expected ',' between Array extents");
141
is >> upper_bounds(i);
144
BZPRECHECK(sep == ')', "Format error while scanning input \
145
Array \n -- expected ',' closing Array extents");
149
BZPRECHECK(sep == 'x', "Format error while scanning input \
150
Array \n (expected 'x' between Array extents)");
155
BZPRECHECK(sep == '[', "Format error while scanning input \
156
Array \n (expected '[' before beginning of Array data)");
158
for (int i=0; i < N_rank; ++i)
159
extent(i) = upper_bounds(i) - lower_bounds(i) + 1;
161
x.reindexSelf(lower_bounds);
165
for (int i1=x.lbound(0); i1<=x.ubound(0); i1++) {
166
BZPRECHECK(!is.bad(), "Premature end of input while scanning Array");
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");
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");
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);
201
cout << "Error: read() for " << N_rank
202
<< "D Array is not supported!" << endl;
208
BZPRECHECK(sep == ']', "Format error while scanning input \
209
Array \n (expected ']' after end of Array data)");