blitz/veciter.h

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 /***************************************************************************
00003  * blitz/veciter.h      Iterator classes for Vector<P_numtype>
00004  *
00005  * $Id: veciter.h,v 1.5 2005/05/07 04:17:56 julianc Exp $
00006  *
00007  * Copyright (C) 1997-2001 Todd Veldhuizen <tveldhui@oonumerics.org>
00008  *
00009  * This program is free software; you can redistribute it and/or
00010  * modify it under the terms of the GNU General Public License
00011  * as published by the Free Software Foundation; either version 2
00012  * of the License, or (at your option) any later version.
00013  *
00014  * This program is distributed in the hope that it will be useful,
00015  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017  * GNU General Public License for more details.
00018  *
00019  * Suggestions:          blitz-dev@oonumerics.org
00020  * Bugs:                 blitz-bugs@oonumerics.org
00021  *
00022  * For more information, please see the Blitz++ Home Page:
00023  *    http://oonumerics.org/blitz/
00024  *
00025  ***************************************************************************/
00026 
00027 
00028 #ifndef BZ_VECITER_H
00029 #define BZ_VECITER_H
00030 
00031 #ifndef BZ_VECTOR_H
00032  #error <blitz/veciter.h> should be included via <blitz/vector.h>
00033 #endif
00034 
00035 BZ_NAMESPACE(blitz)
00036 
00037 // Declaration of class VectorIter
00038 template<typename P_numtype>
00039 class VectorIter {
00040 public:
00041     typedef P_numtype T_numtype;
00042 
00043     explicit VectorIter(Vector<P_numtype>& x)
00044         : data_(x.data())
00045     {
00046         stride_ = x.stride();
00047         length_ = x.length();
00048     }
00049 
00050     VectorIter(P_numtype* restrict data, int stride, int length)
00051         : data_(data), stride_(stride), length_(length)
00052     { }
00053 
00054 #ifdef BZ_MANUAL_VECEXPR_COPY_CONSTRUCTOR
00055     VectorIter(const VectorIter<P_numtype>& x)
00056     {
00057         data_ = x.data_;
00058         stride_ = x.stride_;
00059         length_ = x.length_;
00060     }
00061 #endif
00062 
00063     P_numtype operator[](int i) const
00064     { 
00065         BZPRECONDITION(i < length_);
00066         return data_[i*stride_]; 
00067     }
00068 
00069     P_numtype& restrict operator[](int i)
00070     { 
00071         BZPRECONDITION(i < length_);
00072         return data_[i*stride_]; 
00073     }
00074 
00075     P_numtype operator()(int i) const
00076     {
00077         BZPRECONDITION(i < length_);
00078         return data_[i*stride_];
00079     }
00080 
00081     P_numtype& restrict operator()(int i) 
00082     {
00083         BZPRECONDITION(i < length_);
00084         return data_[i*stride_];
00085     }
00086 
00087     P_numtype operator*() const
00088     { return *data_; }
00089 
00090     P_numtype& operator*()
00091     { return *data_; }
00092 
00093     VectorIter<P_numtype> operator+(int i)
00094     {
00095         // NEEDS_WORK -- precondition checking?
00096         return VectorIter<P_numtype>(data_+i*stride_, stride_, length_-i);
00097     }
00098 
00099     int length(int) const
00100     { return length_; }
00101 
00102     bool isUnitStride() const
00103     { return (stride_ == 1); }
00104 
00106     // Library-internal member functions
00107     // These are undocumented and may change or
00108     // disappear in future releases.
00110 
00111     static const int 
00112         _bz_staticLengthCount = 0,
00113         _bz_dynamicLengthCount = 1,
00114         _bz_staticLength = 0;
00115 
00116     bool _bz_hasFastAccess() const
00117     { return isUnitStride(); }
00118 
00119     P_numtype _bz_fastAccess(int i) const
00120     { return data_[i]; }
00121 
00122     P_numtype& restrict _bz_fastAccess(int i)
00123     { return data_[i]; }
00124 
00125     int _bz_suggestLength() const
00126     { return length_; }
00127 
00128 private:
00129     VectorIter() { }
00130     P_numtype * restrict data_;
00131     int stride_;
00132     int length_;
00133 };
00134 
00135 
00136 template<typename P_numtype>
00137 class VectorIterConst {
00138 public:
00139     typedef P_numtype T_numtype;
00140 
00141     explicit VectorIterConst(const Vector<P_numtype>& x)
00142         : data_(x.data())
00143     {
00144         stride_ = x.stride();
00145         length_ = x.length();
00146     }
00147 
00148 #ifdef BZ_MANUAL_VECEXPR_COPY_CONSTRUCTOR
00149     VectorIterConst(const VectorIterConst<P_numtype>& x)
00150     {
00151         data_ = x.data_;
00152         stride_ = x.stride_;
00153         length_ = x.length_;
00154     }
00155 #endif
00156 
00157     P_numtype operator[](int i) const
00158     { 
00159         BZPRECONDITION(i < length_);
00160         return data_[i*stride_]; 
00161     }
00162 
00163     P_numtype operator()(int i) const
00164     {
00165         BZPRECONDITION(i < length_);
00166         return data_[i*stride_];
00167     }
00168 
00169     int length(int) const
00170     { return length_; }
00171 
00172     bool isUnitStride() const
00173     { return (stride_ == 1); }
00174 
00176     // Library-internal member functions
00177     // These are undocumented and may change or
00178     // disappear in future releases.
00180 
00181     static const int 
00182         _bz_staticLengthCount = 0,
00183         _bz_dynamicLengthCount = 1,
00184         _bz_staticLength = 0;
00185 
00186     bool  _bz_hasFastAccess() const
00187     { return isUnitStride(); }
00188 
00189     P_numtype _bz_fastAccess(int i) const
00190     {
00191         return data_[i];
00192     }
00193 
00194     int _bz_suggestLength() const
00195     { return length_; }
00196 
00197 private:
00198     const P_numtype * restrict data_;
00199     int stride_;
00200     int length_;
00201 };
00202 
00203 BZ_NAMESPACE_END
00204 
00205 #endif // BZ_VECITER_H

Generated on Wed Oct 17 15:37:28 2007 for blitz by  doxygen 1.5.2