File: io.cc

package info (click to toggle)
blitz%2B%2B 1%3A0.10-3.2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 13,276 kB
  • ctags: 12,037
  • sloc: cpp: 70,465; sh: 11,116; fortran: 1,510; python: 1,246; f90: 852; makefile: 701
file content (215 lines) | stat: -rw-r--r-- 6,252 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/***************************************************************************
 * blitz/array/io.cc  Input/output of arrays.
 *
 * $Id$
 *
 * Copyright (C) 1997-2011 Todd Veldhuizen <tveldhui@acm.org>
 *
 * This file is a part of Blitz.
 *
 * Blitz is free software: you can redistribute it and/or modify 
 * it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation, either version 3
 * of the License, or (at your option) any later version.
 *
 * Blitz is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public 
 * License along with Blitz.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * Suggestions:          blitz-devel@lists.sourceforge.net
 * Bugs:                 blitz-support@lists.sourceforge.net    
 *
 * For more information, please see the Blitz++ Home Page:
 *    https://sourceforge.net/projects/blitz/
 *
 ****************************************************************************/
#ifndef BZ_ARRAYIO_CC
#define BZ_ARRAYIO_CC

#ifndef BZ_ARRAY_H
 #error <blitz/array/io.cc> must be included via <blitz/array.h>
#endif

BZ_NAMESPACE(blitz)

// NEEDS_WORK???
// This version of operator<< is updated on August 2005
// by Sergei Mingaleev <mingaleev@gmail.com>.
// Also, the corresponding operator>> is updated.

template<typename T_numtype>
ostream& operator<<(ostream& os, const Array<T_numtype,1>& x)
{
  // Write the extent vector: e.g., (-4, 4)

  os << "(" << x.lbound(0) << "," << x.ubound(0) << ")";
  os << endl << "[ ";

  for (int i1=x.lbound(0); i1<=x.ubound(0); i1++) {
    os << x(i1) << " ";
  }
  os << "]" << endl;
  return os;
}

template<typename T_numtype, int N_rank>
ostream& operator<<(ostream& os, const Array<T_numtype,N_rank>& x)
{
  // Write the extent vector: this is separated by 'x's, e.g.
  // (1, 10) x (-4, 4) x (-5, 5) 

  for (int i=0; i < N_rank; ++i) {
    os << "(";
    os << x.lbound(i); 
    os << ",";
    os << x.ubound(i); 
    os << ")";
    if (i != N_rank-1) os << " x ";
  }
  os << endl << "[ ";

  switch (N_rank) {
    case 2:  
      for (int i1=x.lbound(0); i1<=x.ubound(0); i1++) {
        for (int i2=x.lbound(1); i2<=x.ubound(1); i2++) {
          os << x(i1,i2) << " ";
        }
        if (i1 != x.ubound(0)) os << endl << "  ";
      }
      break;
    case 3:
      for (int i1=x.lbound(0); i1<=x.ubound(0); i1++) {
	for (int i2=x.lbound(1); i2<=x.ubound(1); i2++) {
          for (int i3=x.lbound(2); i3<=x.ubound(2); i3++) {
              os << x(i1,i2,i3) << " ";
	  }
	  if (i1 != x.ubound(0) || i2 != x.ubound(1)) os << endl << "  ";
	}
      }
      break;
    case 4:
      for (int i1=x.lbound(0); i1<=x.ubound(0); i1++) {
	for (int i2=x.lbound(1); i2<=x.ubound(1); i2++) {
          for (int i3=x.lbound(2); i3<=x.ubound(2); i3++) {
            for (int i4=x.lbound(3); i4<=x.ubound(3); i4++) {
                os << x(i1,i2,i3,i4) << " ";
	    }
	    if (i1 != x.ubound(0) || i2 != x.ubound(1) || i3 != x.ubound(2)) 
              os << endl << "  ";
	  }
	}
      }
      break;
    default:
      cout << "Error: operator<< for " << N_rank
           << "D Array is not supported!" << endl;
      BZASSERT("ERROR!");
      break;
  };

  os << "]" << endl;
  return os;
}

/*
 *  Input
 */

template<typename T_numtype, int N_rank>
istream& operator>>(istream& is, Array<T_numtype,N_rank>& x)
{
  TinyVector<int,N_rank> lower_bounds, upper_bounds, extent;
  char sep;

  // Read the extent vector: this is separated by 'x's, e.g.
  // (1, 10) x (-4, 4) x (-5, 5) 

  for (int i=0; i < N_rank; ++i) {
    is >> sep;
    BZPRECHECK(!is.bad(), "Premature end of input while scanning Array");
    BZPRECHECK(sep == '(', "Format error while scanning input \
Array \n -- expected '(' opening Array extents");

    is >> lower_bounds(i); 
    is >> sep; 
    BZPRECHECK(sep == ',', "Format error while scanning input \
Array \n -- expected ',' between Array extents");
    is >> upper_bounds(i);

    is >> sep; 
    BZPRECHECK(sep == ')', "Format error while scanning input \
Array \n -- expected ',' closing Array extents");

    if (i != N_rank-1) {
      is >> sep;
      BZPRECHECK(sep == 'x', "Format error while scanning input \
Array \n (expected 'x' between Array extents)");
    }
  }

  is >> sep;
  BZPRECHECK(sep == '[', "Format error while scanning input \
Array \n (expected '[' before beginning of Array data)");

  for (int i=0; i < N_rank; ++i)
      extent(i) = upper_bounds(i) - lower_bounds(i) + 1;
  x.resize(extent);
  x.reindexSelf(lower_bounds);

  switch (N_rank) {
    case 1:
      for (int i1=x.lbound(0); i1<=x.ubound(0); i1++) {
        BZPRECHECK(!is.bad(), "Premature end of input while scanning Array");
	is >> x(i1);
      }
      break;
    case 2:
      for (int i1=x.lbound(0); i1<=x.ubound(0); i1++) {
        for (int i2=x.lbound(1); i2<=x.ubound(1); i2++) {
          BZPRECHECK(!is.bad(), "Premature end of input while scanning Array");
	  is >> x(i1,i2);
	}
      }
      break;
    case 3:
      for (int i1=x.lbound(0); i1<=x.ubound(0); i1++) {
	for (int i2=x.lbound(1); i2<=x.ubound(1); i2++) {
          for (int i3=x.lbound(2); i3<=x.ubound(2); i3++) {
            BZPRECHECK(!is.bad(), "Premature end of input while scanning Array");
	    is >> x(i1,i2,i3);
	  }
	}
      }
      break;
    case 4:
      for (int i1=x.lbound(0); i1<=x.ubound(0); i1++) {
	for (int i2=x.lbound(1); i2<=x.ubound(1); i2++) {
          for (int i3=x.lbound(2); i3<=x.ubound(2); i3++) {
            for (int i4=x.lbound(3); i4<=x.ubound(3); i4++) {
              BZPRECHECK(!is.bad(), "Premature end of input while scanning Array");
  	      is >> x(i1,i2,i3,i4);
	    }
	  }
	}
      }
      break;
    default:
      cout << "Error: read() for " << N_rank 
           << "D Array is not supported!" << endl;
      BZASSERT("ERROR!");
      break;
  };

  is >> sep;
  BZPRECHECK(sep == ']', "Format error while scanning input \
Array \n (expected ']' after end of Array data)");
  return is;
}

BZ_NAMESPACE_END

#endif // BZ_ARRAYIO_CC