blitz  Version 0.9
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
veciter.h
Go to the documentation of this file.
1 // -*- C++ -*-
2 /***************************************************************************
3  * blitz/veciter.h Iterator classes for Vector<P_numtype>
4  *
5  * $Id: veciter.h,v 1.5 2005/05/07 04:17:56 julianc Exp $
6  *
7  * Copyright (C) 1997-2001 Todd Veldhuizen <tveldhui@oonumerics.org>
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License
11  * as published by the Free Software Foundation; either version 2
12  * of the License, or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * Suggestions: blitz-dev@oonumerics.org
20  * Bugs: blitz-bugs@oonumerics.org
21  *
22  * For more information, please see the Blitz++ Home Page:
23  * http://oonumerics.org/blitz/
24  *
25  ***************************************************************************/
26 
27 
28 #ifndef BZ_VECITER_H
29 #define BZ_VECITER_H
30 
31 #ifndef BZ_VECTOR_H
32  #error <blitz/veciter.h> should be included via <blitz/vector.h>
33 #endif
34 
35 BZ_NAMESPACE(blitz)
36 
37 // Declaration of class VectorIter
38 template<typename P_numtype>
39 class VectorIter {
40 public:
41  typedef P_numtype T_numtype;
42 
43  explicit VectorIter(Vector<P_numtype>& x)
44  : data_(x.data())
45  {
46  stride_ = x.stride();
47  length_ = x.length();
48  }
49 
50  VectorIter(P_numtype* restrict data, int stride, int length)
51  : data_(data), stride_(stride), length_(length)
52  { }
53 
54 #ifdef BZ_MANUAL_VECEXPR_COPY_CONSTRUCTOR
55  VectorIter(const VectorIter<P_numtype>& x)
56  {
57  data_ = x.data_;
58  stride_ = x.stride_;
59  length_ = x.length_;
60  }
61 #endif
62 
63  P_numtype operator[](int i) const
64  {
65  BZPRECONDITION(i < length_);
66  return data_[i*stride_];
67  }
68 
69  P_numtype& restrict operator[](int i)
70  {
71  BZPRECONDITION(i < length_);
72  return data_[i*stride_];
73  }
74 
75  P_numtype operator()(int i) const
76  {
77  BZPRECONDITION(i < length_);
78  return data_[i*stride_];
79  }
80 
81  P_numtype& restrict operator()(int i)
82  {
83  BZPRECONDITION(i < length_);
84  return data_[i*stride_];
85  }
86 
87  P_numtype operator*() const
88  { return *data_; }
89 
90  P_numtype& operator*()
91  { return *data_; }
92 
93  VectorIter<P_numtype> operator+(int i)
94  {
95  // NEEDS_WORK -- precondition checking?
96  return VectorIter<P_numtype>(data_+i*stride_, stride_, length_-i);
97  }
98 
99  int length(int) const
100  { return length_; }
101 
102  bool isUnitStride() const
103  { return (stride_ == 1); }
104 
106  // Library-internal member functions
107  // These are undocumented and may change or
108  // disappear in future releases.
110 
111  static const int
112  _bz_staticLengthCount = 0,
113  _bz_dynamicLengthCount = 1,
114  _bz_staticLength = 0;
115 
116  bool _bz_hasFastAccess() const
117  { return isUnitStride(); }
118 
119  P_numtype _bz_fastAccess(int i) const
120  { return data_[i]; }
121 
122  P_numtype& restrict _bz_fastAccess(int i)
123  { return data_[i]; }
124 
125  int _bz_suggestLength() const
126  { return length_; }
127 
128 private:
130  P_numtype * restrict data_;
131  int stride_;
132  int length_;
133 };
134 
135 
136 template<typename P_numtype>
138 public:
139  typedef P_numtype T_numtype;
140 
142  : data_(x.data())
143  {
144  stride_ = x.stride();
145  length_ = x.length();
146  }
147 
148 #ifdef BZ_MANUAL_VECEXPR_COPY_CONSTRUCTOR
150  {
151  data_ = x.data_;
152  stride_ = x.stride_;
153  length_ = x.length_;
154  }
155 #endif
156 
157  P_numtype operator[](int i) const
158  {
159  BZPRECONDITION(i < length_);
160  return data_[i*stride_];
161  }
162 
163  P_numtype operator()(int i) const
164  {
165  BZPRECONDITION(i < length_);
166  return data_[i*stride_];
167  }
168 
169  int length(int) const
170  { return length_; }
171 
172  bool isUnitStride() const
173  { return (stride_ == 1); }
174 
176  // Library-internal member functions
177  // These are undocumented and may change or
178  // disappear in future releases.
180 
181  static const int
185 
186  bool _bz_hasFastAccess() const
187  { return isUnitStride(); }
188 
189  P_numtype _bz_fastAccess(int i) const
190  {
191  return data_[i];
192  }
193 
194  int _bz_suggestLength() const
195  { return length_; }
196 
197 private:
198  const P_numtype * restrict data_;
199  int stride_;
200  int length_;
201 };
202 
204 
205 #endif // BZ_VECITER_H