1 | /*************************************************************************** |
---|
2 | * blitz/array/io.cc Input/output of arrays. |
---|
3 | * |
---|
4 | * Copyright (C) 1997-2001 Todd Veldhuizen <tveldhui@oonumerics.org> |
---|
5 | * |
---|
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 |
---|
9 | * of the License, or (at your option) any later version. |
---|
10 | * |
---|
11 | * This program is distributed in the hope that it will be useful, |
---|
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
14 | * GNU General Public License for more details. |
---|
15 | * |
---|
16 | * Suggestions: blitz-dev@oonumerics.org |
---|
17 | * Bugs: blitz-bugs@oonumerics.org |
---|
18 | * |
---|
19 | * For more information, please see the Blitz++ Home Page: |
---|
20 | * http://oonumerics.org/blitz/ |
---|
21 | * |
---|
22 | ****************************************************************************/ |
---|
23 | #ifndef BZ_ARRAYIO_CC |
---|
24 | #define BZ_ARRAYIO_CC |
---|
25 | |
---|
26 | #ifndef BZ_ARRAY_H |
---|
27 | #error <blitz/array/io.cc> must be included via <blitz/array.h> |
---|
28 | #endif |
---|
29 | |
---|
30 | BZ_NAMESPACE(blitz) |
---|
31 | |
---|
32 | template<typename T_numtype> |
---|
33 | ostream& operator<<(ostream& os, const Array<T_numtype,1>& x) |
---|
34 | { |
---|
35 | os << x.extent(firstRank) << endl; |
---|
36 | os << " [ "; |
---|
37 | for (int i=x.lbound(firstRank); i <= x.ubound(firstRank); ++i) |
---|
38 | { |
---|
39 | os << setw(9) << x(i) << " "; |
---|
40 | if (!((i+1-x.lbound(firstRank))%7)) |
---|
41 | os << endl << " "; |
---|
42 | } |
---|
43 | os << " ]"; |
---|
44 | return os; |
---|
45 | } |
---|
46 | |
---|
47 | template<typename T_numtype> |
---|
48 | ostream& operator<<(ostream& os, const Array<T_numtype,2>& x) |
---|
49 | { |
---|
50 | os << x.rows() << " x " << x.columns() << endl; |
---|
51 | os << "[ "; |
---|
52 | for (int i=x.lbound(firstRank); i <= x.ubound(firstRank); ++i) |
---|
53 | { |
---|
54 | for (int j=x.lbound(secondRank); j <= x.ubound(secondRank); ++j) |
---|
55 | { |
---|
56 | os << setw(9) << x(i,j) << " "; |
---|
57 | if (!((j+1-x.lbound(secondRank)) % 7)) |
---|
58 | os << endl << " "; |
---|
59 | } |
---|
60 | |
---|
61 | if (i != x.ubound(firstRank)) |
---|
62 | os << endl << " "; |
---|
63 | } |
---|
64 | |
---|
65 | os << "]" << endl; |
---|
66 | |
---|
67 | return os; |
---|
68 | } |
---|
69 | |
---|
70 | template<typename T_numtype, int N_rank> |
---|
71 | ostream& operator<<(ostream& os, const Array<T_numtype,N_rank>& x) |
---|
72 | { |
---|
73 | for (int i=0; i < N_rank; ++i) |
---|
74 | { |
---|
75 | os << x.extent(i); |
---|
76 | if (i != N_rank - 1) |
---|
77 | os << " x "; |
---|
78 | } |
---|
79 | |
---|
80 | os << endl << "[ "; |
---|
81 | |
---|
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(); |
---|
84 | int p = 0; |
---|
85 | |
---|
86 | while (iter != end) { |
---|
87 | os << setw(9) << (*iter) << " "; |
---|
88 | ++iter; |
---|
89 | |
---|
90 | // See if we need a linefeed |
---|
91 | ++p; |
---|
92 | if (!(p % 7)) |
---|
93 | os << endl << " "; |
---|
94 | } |
---|
95 | |
---|
96 | os << "]" << endl; |
---|
97 | return os; |
---|
98 | } |
---|
99 | |
---|
100 | /* |
---|
101 | * Input |
---|
102 | */ |
---|
103 | |
---|
104 | template<typename T_numtype, int N_rank> |
---|
105 | istream& operator>>(istream& is, Array<T_numtype,N_rank>& x) |
---|
106 | { |
---|
107 | TinyVector<int,N_rank> extent; |
---|
108 | char sep; |
---|
109 | |
---|
110 | // Read the extent vector: this is separated by 'x's, e.g. |
---|
111 | // 3 x 4 x 5 |
---|
112 | |
---|
113 | for (int i=0; i < N_rank; ++i) |
---|
114 | { |
---|
115 | is >> extent(i); |
---|
116 | |
---|
117 | BZPRECHECK(!is.bad(), "Premature end of input while scanning array"); |
---|
118 | |
---|
119 | if (i != N_rank - 1) |
---|
120 | { |
---|
121 | is >> sep; |
---|
122 | BZPRECHECK(sep == 'x', "Format error while scanning input array" |
---|
123 | << endl << " (expected 'x' between array extents)"); |
---|
124 | } |
---|
125 | } |
---|
126 | |
---|
127 | is >> sep; |
---|
128 | BZPRECHECK(sep == '[', "Format error while scanning input array" |
---|
129 | << endl << " (expected '[' before beginning of array data)"); |
---|
130 | |
---|
131 | x.resize(extent); |
---|
132 | |
---|
133 | _bz_typename Array<T_numtype,N_rank>::iterator iter = x.begin(); |
---|
134 | _bz_typename Array<T_numtype,N_rank>::iterator end = x.end(); |
---|
135 | |
---|
136 | while (iter != end) { |
---|
137 | BZPRECHECK(!is.bad(), "Premature end of input while scanning array"); |
---|
138 | |
---|
139 | is >> (*iter); |
---|
140 | ++iter; |
---|
141 | } |
---|
142 | |
---|
143 | is >> sep; |
---|
144 | BZPRECHECK(sep == ']', "Format error while scanning input array" |
---|
145 | << endl << " (expected ']' after end of array data)"); |
---|
146 | |
---|
147 | return is; |
---|
148 | } |
---|
149 | |
---|
150 | BZ_NAMESPACE_END |
---|
151 | |
---|
152 | #endif // BZ_ARRAYIO_CC |
---|