~ubuntu-branches/ubuntu/utopic/blitz++/utopic

« back to all changes in this revision

Viewing changes to blitz/array/io.cc

  • Committer: Package Import Robot
  • Author(s): Christophe Trophime
  • Date: 2012-07-06 09:15:30 UTC
  • mfrom: (11.1.5 sid)
  • Revision ID: package-import@ubuntu.com-20120706091530-vzrb8zf0vpbf8tp9
Tags: 1:0.10-1
* New upstream release
  Closes: #679407
* debian/rules:
  - update for new release
  - add override_dh_auto_test target
  - regenerate configure and Makefile.am
* debian/control:
  - add libtool, automake to BuildDepends
* debian/libblitz-doc.install
  - modify path for html files
* remove uneeded patches
* add examples.patch

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/***************************************************************************
2
2
 * blitz/array/io.cc  Input/output of arrays.
3
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
 
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
9
13
 * of the License, or (at your option) any later version.
10
14
 *
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.
15
19
 *
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/>.
 
22
 * 
 
23
 * Suggestions:          blitz-devel@lists.sourceforge.net
 
24
 * Bugs:                 blitz-support@lists.sourceforge.net    
18
25
 *
19
26
 * For more information, please see the Blitz++ Home Page:
20
 
 *    http://oonumerics.org/blitz/
 
27
 *    https://sourceforge.net/projects/blitz/
21
28
 *
22
29
 ****************************************************************************/
23
30
#ifndef BZ_ARRAYIO_CC
29
36
 
30
37
BZ_NAMESPACE(blitz)
31
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
 
32
44
template<typename T_numtype>
33
45
ostream& operator<<(ostream& os, const Array<T_numtype,1>& x)
34
46
{
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;
 
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;
68
57
}
69
58
 
70
59
template<typename T_numtype, int N_rank>
71
60
ostream& operator<<(ostream& os, const Array<T_numtype,N_rank>& x)
72
61
{
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;
 
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;
98
116
}
99
117
 
100
118
/*
104
122
template<typename T_numtype, int N_rank>
105
123
istream& operator>>(istream& is, Array<T_numtype,N_rank>& x)
106
124
{
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;
 
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 \
 
135
Array \n -- expected '(' opening Array extents");
 
136
 
 
137
    is >> lower_bounds(i); 
 
138
    is >> sep; 
 
139
    BZPRECHECK(sep == ',', "Format error while scanning input \
 
140
Array \n -- expected ',' between Array extents");
 
141
    is >> upper_bounds(i);
 
142
 
 
143
    is >> sep; 
 
144
    BZPRECHECK(sep == ')', "Format error while scanning input \
 
145
Array \n -- expected ',' closing Array extents");
 
146
 
 
147
    if (i != N_rank-1) {
 
148
      is >> sep;
 
149
      BZPRECHECK(sep == 'x', "Format error while scanning input \
 
150
Array \n (expected 'x' between Array extents)");
 
151
    }
 
152
  }
 
153
 
 
154
  is >> sep;
 
155
  BZPRECHECK(sep == '[', "Format error while scanning input \
 
156
Array \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 \
 
209
Array \n (expected ']' after end of Array data)");
 
210
  return is;
148
211
}
149
212
 
150
213
BZ_NAMESPACE_END