blitz Version 0.10
blitz/tinyvec.h
Go to the documentation of this file.
00001 // -*- C++ -*-
00002 /***************************************************************************
00003  * blitz/tinyvec.h      Declaration of the TinyVector<T, N> class
00004  *
00005  * $Id: tinyvec.h,v 1.11 2011/03/25 22:41:16 julianc Exp $
00006  *
00007  * Copyright (C) 1997-2011 Todd Veldhuizen <tveldhui@acm.org>
00008  *
00009  * This file is a part of Blitz.
00010  *
00011  * Blitz is free software: you can redistribute it and/or modify 
00012  * it under the terms of the GNU Lesser General Public License
00013  * as published by the Free Software Foundation, either version 3
00014  * of the License, or (at your option) any later version.
00015  *
00016  * Blitz is distributed in the hope that it will be useful,
00017  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00018  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019  * GNU Lesser General Public License for more details.
00020  *
00021  * You should have received a copy of the GNU Lesser General Public 
00022  * License along with Blitz.  If not, see <http://www.gnu.org/licenses/>.
00023  * 
00024  * Suggestions:          blitz-devel@lists.sourceforge.net
00025  * Bugs:                 blitz-support@lists.sourceforge.net    
00026  *
00027  * For more information, please see the Blitz++ Home Page:
00028  *    https://sourceforge.net/projects/blitz/
00029  *
00030  ***************************************************************************/
00031 
00032 #ifndef BZ_TINYVEC_H
00033 #define BZ_TINYVEC_H
00034 
00035 #include <blitz/blitz.h>
00036 #include <blitz/range.h>
00037 #include <blitz/listinit.h>
00038 #include <blitz/tiny.h>
00039 
00040 #ifdef BZ_HAVE_CSTRING
00041 #include <cstring> // For memcpy
00042 #endif
00043 
00044 BZ_NAMESPACE(blitz)
00045 
00046 /*****************************************************************************
00047  * Forward declarations
00048  */
00049 
00050 template<typename P_numtype, int N_length, int N_stride BZ_TEMPLATE_DEFAULT(1) >
00051 class TinyVectorIter;
00052 
00053 template<typename P_numtype, int N_length, int N_stride BZ_TEMPLATE_DEFAULT(1) >
00054 class TinyVectorIterConst;
00055 
00056 template<typename P_numtype>
00057 class Vector;
00058 
00059 template<typename P_expr>
00060 class _bz_VecExpr;
00061 
00062 template<typename P_distribution>
00063 class Random;
00064 
00065 template<typename P_numtype>
00066 class VectorPick;
00067 
00068 template<typename T_numtype1, typename T_numtype2, int N_rows, int N_columns,
00069     int N_vecStride>
00070 class _bz_matrixVectorProduct;
00071 
00072 
00073 
00074 /*****************************************************************************
00075  * Declaration of class TinyVector
00076  */
00077 
00078 template<typename P_numtype, int N_length>
00079 class TinyVector {
00080 public:
00081 
00083     // Public Types
00085 
00086     typedef P_numtype                                    T_numtype;
00087     typedef TinyVector<T_numtype,N_length>               T_vector;
00088     typedef TinyVectorIter<T_numtype,N_length,1>         T_iterator;
00089     typedef TinyVectorIterConst<T_numtype,N_length,1>    T_constIterator;
00090     typedef T_numtype*                                   iterator;
00091     typedef const T_numtype*                             const_iterator;
00092     enum { numElements = N_length };
00093 
00094     TinyVector()  { }
00095     ~TinyVector() { }
00096 
00097     inline TinyVector(const TinyVector<T_numtype,N_length>& x);
00098 
00099     template <typename T_numtype2>
00100     inline TinyVector(const TinyVector<T_numtype2,N_length>& x);
00101 
00102     inline TinyVector(const T_numtype initValue);
00103 
00104     inline TinyVector(const T_numtype x[]) {
00105         memcpy(data_,x,N_length*sizeof(T_numtype));
00106     }
00107 
00108     TinyVector(T_numtype x0, T_numtype x1)
00109     {
00110         data_[0] = x0;
00111         data_[1] = x1;
00112     }
00113 
00114     TinyVector(T_numtype x0, T_numtype x1, T_numtype x2)
00115     {
00116         data_[0] = x0;
00117         data_[1] = x1;
00118         data_[2] = x2;
00119     }
00120 
00121     TinyVector(T_numtype x0, T_numtype x1, T_numtype x2,
00122         T_numtype x3)
00123     {
00124         data_[0] = x0;
00125         data_[1] = x1;
00126         data_[2] = x2;
00127         data_[3] = x3;
00128     }
00129 
00130     TinyVector(T_numtype x0, T_numtype x1, T_numtype x2,
00131         T_numtype x3, T_numtype x4)
00132     {
00133         data_[0] = x0;
00134         data_[1] = x1;
00135         data_[2] = x2;
00136         data_[3] = x3;
00137         data_[4] = x4;
00138     }
00139 
00140     TinyVector(T_numtype x0, T_numtype x1, T_numtype x2,
00141         T_numtype x3, T_numtype x4, T_numtype x5)
00142     {
00143         data_[0] = x0;
00144         data_[1] = x1;
00145         data_[2] = x2;
00146         data_[3] = x3;
00147         data_[4] = x4;
00148         data_[5] = x5;
00149     }
00150 
00151     TinyVector(T_numtype x0, T_numtype x1, T_numtype x2,
00152         T_numtype x3, T_numtype x4, T_numtype x5, T_numtype x6)
00153     {
00154         data_[0] = x0;
00155         data_[1] = x1;
00156         data_[2] = x2;
00157         data_[3] = x3;
00158         data_[4] = x4;
00159         data_[5] = x5;
00160         data_[6] = x6;
00161     }
00162 
00163     TinyVector(T_numtype x0, T_numtype x1, T_numtype x2,
00164         T_numtype x3, T_numtype x4, T_numtype x5, T_numtype x6,
00165         T_numtype x7)
00166     {
00167         data_[0] = x0;
00168         data_[1] = x1;
00169         data_[2] = x2;
00170         data_[3] = x3;
00171         data_[4] = x4;
00172         data_[5] = x5;
00173         data_[6] = x6;
00174         data_[7] = x7;
00175     }
00176 
00177     TinyVector(T_numtype x0, T_numtype x1, T_numtype x2,
00178         T_numtype x3, T_numtype x4, T_numtype x5, T_numtype x6,
00179         T_numtype x7, T_numtype x8)
00180     {
00181         data_[0] = x0;
00182         data_[1] = x1;
00183         data_[2] = x2;
00184         data_[3] = x3;
00185         data_[4] = x4;
00186         data_[5] = x5;
00187         data_[6] = x6;
00188         data_[7] = x7;
00189         data_[8] = x8;
00190     }
00191 
00192     TinyVector(T_numtype x0, T_numtype x1, T_numtype x2,
00193         T_numtype x3, T_numtype x4, T_numtype x5, T_numtype x6,
00194         T_numtype x7, T_numtype x8, T_numtype x9)
00195     {
00196         data_[0] = x0;
00197         data_[1] = x1;
00198         data_[2] = x2;
00199         data_[3] = x3;
00200         data_[4] = x4;
00201         data_[5] = x5;
00202         data_[6] = x6;
00203         data_[7] = x7;
00204         data_[8] = x8;
00205         data_[9] = x9;
00206     }
00207 
00208     TinyVector(T_numtype x0, T_numtype x1, T_numtype x2,
00209         T_numtype x3, T_numtype x4, T_numtype x5, T_numtype x6,
00210         T_numtype x7, T_numtype x8, T_numtype x9, T_numtype x10)
00211     {
00212         data_[0] = x0;
00213         data_[1] = x1;
00214         data_[2] = x2;
00215         data_[3] = x3;
00216         data_[4] = x4;
00217         data_[5] = x5;
00218         data_[6] = x6;
00219         data_[7] = x7;
00220         data_[8] = x8;
00221         data_[9] = x9;
00222         data_[10] = x10;
00223     }
00224 
00225     // Constructor added by Peter Nordlund
00226     template<typename P_expr>
00227     inline TinyVector(_bz_VecExpr<P_expr> expr);
00228 
00229     T_iterator      beginFast()       { return T_iterator(*this);      }
00230     T_constIterator beginFast() const { return T_constIterator(*this); }
00231 
00232     iterator       begin()       { return data_; }
00233     const_iterator begin() const { return data_; }
00234 
00235     iterator       end()       { return data_ + N_length; }
00236     const_iterator end() const { return data_ + N_length; }
00237 
00238     T_numtype * restrict data()
00239     { return data_; }
00240 
00241     const T_numtype * restrict data() const
00242     { return data_; }
00243 
00244     T_numtype * restrict dataFirst()
00245     { return data_; }
00246 
00247     const T_numtype * restrict dataFirst() const
00248     { return data_; }
00249 
00250     unsigned length() const
00251     { return N_length; }
00252 
00254     // Library-internal member functions
00255     // These are undocumented and may change or
00256     // disappear in future releases.
00258 
00259     unsigned _bz_suggestLength() const
00260     { return N_length; }
00261 
00262     bool _bz_hasFastAccess() const
00263     { return true; }
00264 
00265     T_numtype& restrict _bz_fastAccess(unsigned i)
00266     { return data_[i]; }
00267 
00268     T_numtype _bz_fastAccess(unsigned i) const
00269     { return data_[i]; }
00270 
00271     template<typename P_expr, typename P_updater>
00272     void _bz_assign(P_expr, P_updater);
00273 
00274     _bz_VecExpr<T_constIterator> _bz_asVecExpr() const
00275     { return _bz_VecExpr<T_constIterator>(beginFast()); }
00276    
00278     // Subscripting operators
00280 
00281     bool lengthCheck(unsigned i) const
00282     {
00283         BZPRECHECK(i < N_length, 
00284             "TinyVector<" << BZ_DEBUG_TEMPLATE_AS_STRING_LITERAL(T_numtype) 
00285             << "," << N_length << "> index out of bounds: " << i);
00286         return true;
00287     }
00288 
00289     const T_numtype& operator()(unsigned i) const
00290     {
00291         BZPRECONDITION(lengthCheck(i));
00292         return data_[i];
00293     }
00294 
00295     T_numtype& restrict operator()(unsigned i)
00296     { 
00297         BZPRECONDITION(lengthCheck(i));
00298         return data_[i];
00299     }
00300 
00301     const T_numtype& operator[](unsigned i) const
00302     {
00303         BZPRECONDITION(lengthCheck(i));
00304         return data_[i];
00305     }
00306 
00307     T_numtype& restrict operator[](unsigned i)
00308     {
00309         BZPRECONDITION(lengthCheck(i));
00310         return data_[i];
00311     }
00312 
00314     // Assignment operators
00316 
00317     // Scalar operand
00318     ListInitializationSwitch<T_vector,T_numtype*> operator=(T_numtype x)
00319     {
00320         return ListInitializationSwitch<T_vector,T_numtype*>(*this, x);
00321     }
00322 
00323     T_vector& initialize(const T_numtype);
00324     T_vector& operator+=(const T_numtype);
00325     T_vector& operator-=(const T_numtype);
00326     T_vector& operator*=(const T_numtype);
00327     T_vector& operator/=(const T_numtype);
00328     T_vector& operator%=(const T_numtype);
00329     T_vector& operator^=(const T_numtype);
00330     T_vector& operator&=(const T_numtype);
00331     T_vector& operator|=(const T_numtype);
00332     T_vector& operator>>=(const int);
00333     T_vector& operator<<=(const int);
00334 
00335     template<typename P_numtype2> 
00336     T_vector& operator=(const TinyVector<P_numtype2, N_length> &);
00337     template<typename P_numtype2>
00338     T_vector& operator+=(const TinyVector<P_numtype2, N_length> &);
00339     template<typename P_numtype2>
00340     T_vector& operator-=(const TinyVector<P_numtype2, N_length> &);
00341     template<typename P_numtype2>
00342     T_vector& operator*=(const TinyVector<P_numtype2, N_length> &);
00343     template<typename P_numtype2>
00344     T_vector& operator/=(const TinyVector<P_numtype2, N_length> &);
00345     template<typename P_numtype2>
00346     T_vector& operator%=(const TinyVector<P_numtype2, N_length> &);
00347     template<typename P_numtype2>
00348     T_vector& operator^=(const TinyVector<P_numtype2, N_length> &);
00349     template<typename P_numtype2>
00350     T_vector& operator&=(const TinyVector<P_numtype2, N_length> &);
00351     template<typename P_numtype2>
00352     T_vector& operator|=(const TinyVector<P_numtype2, N_length> &);
00353     template<typename P_numtype2>
00354     T_vector& operator>>=(const TinyVector<P_numtype2, N_length> &);
00355     template<typename P_numtype2>
00356     T_vector& operator<<=(const TinyVector<P_numtype2, N_length> &);
00357 
00358     template<typename P_numtype2> T_vector& operator=(const Vector<P_numtype2> &);
00359     template<typename P_numtype2> T_vector& operator+=(const Vector<P_numtype2> &);
00360     template<typename P_numtype2> T_vector& operator-=(const Vector<P_numtype2> &);
00361     template<typename P_numtype2> T_vector& operator*=(const Vector<P_numtype2> &);
00362     template<typename P_numtype2> T_vector& operator/=(const Vector<P_numtype2> &);
00363     template<typename P_numtype2> T_vector& operator%=(const Vector<P_numtype2> &);
00364     template<typename P_numtype2> T_vector& operator^=(const Vector<P_numtype2> &);
00365     template<typename P_numtype2> T_vector& operator&=(const Vector<P_numtype2> &);
00366     template<typename P_numtype2> T_vector& operator|=(const Vector<P_numtype2> &);
00367     template<typename P_numtype2> T_vector& operator>>=(const Vector<P_numtype2> &);
00368     template<typename P_numtype2> T_vector& operator<<=(const Vector<P_numtype2> &);
00369 
00370     // Vector expression operand
00371     template<typename P_expr> T_vector& operator=(_bz_VecExpr<P_expr>);
00372     template<typename P_expr> T_vector& operator+=(_bz_VecExpr<P_expr>);
00373     template<typename P_expr> T_vector& operator-=(_bz_VecExpr<P_expr>);
00374     template<typename P_expr> T_vector& operator*=(_bz_VecExpr<P_expr>);
00375     template<typename P_expr> T_vector& operator/=(_bz_VecExpr<P_expr>);
00376     template<typename P_expr> T_vector& operator%=(_bz_VecExpr<P_expr>);
00377     template<typename P_expr> T_vector& operator^=(_bz_VecExpr<P_expr>);
00378     template<typename P_expr> T_vector& operator&=(_bz_VecExpr<P_expr>);
00379     template<typename P_expr> T_vector& operator|=(_bz_VecExpr<P_expr>);
00380     template<typename P_expr> T_vector& operator>>=(_bz_VecExpr<P_expr>);
00381     template<typename P_expr> T_vector& operator<<=(_bz_VecExpr<P_expr>);
00382 
00383     // VectorPick operand
00384     template<typename P_numtype2>
00385     T_vector& operator=(const VectorPick<P_numtype2> &);
00386     template<typename P_numtype2>
00387     T_vector& operator+=(const VectorPick<P_numtype2> &);
00388     template<typename P_numtype2>
00389     T_vector& operator-=(const VectorPick<P_numtype2> &);
00390     template<typename P_numtype2>
00391     T_vector& operator*=(const VectorPick<P_numtype2> &);
00392     template<typename P_numtype2>
00393     T_vector& operator/=(const VectorPick<P_numtype2> &);
00394     template<typename P_numtype2>
00395     T_vector& operator%=(const VectorPick<P_numtype2> &);
00396     template<typename P_numtype2>
00397     T_vector& operator^=(const VectorPick<P_numtype2> &);
00398     template<typename P_numtype2>
00399     T_vector& operator&=(const VectorPick<P_numtype2> &);
00400     template<typename P_numtype2>
00401     T_vector& operator|=(const VectorPick<P_numtype2> &);
00402     template<typename P_numtype2>
00403     T_vector& operator>>=(const VectorPick<P_numtype2> &);
00404     template<typename P_numtype2>
00405     T_vector& operator<<=(const VectorPick<P_numtype2> &);
00406 
00407     // Range operand
00408     T_vector& operator=(const Range&);
00409     T_vector& operator+=(const Range&);
00410     T_vector& operator-=(const Range&);
00411     T_vector& operator*=(const Range&);
00412     T_vector& operator/=(const Range&);
00413     T_vector& operator%=(const Range&);
00414     T_vector& operator^=(const Range&);
00415     T_vector& operator&=(const Range&);
00416     T_vector& operator|=(const Range&);
00417     T_vector& operator>>=(const Range&);
00418     T_vector& operator<<=(const Range&);
00419 
00420     T_numtype* restrict getInitializationIterator()
00421     { return dataFirst(); }
00422 
00423 private:
00424     T_numtype data_[N_length];
00425 };
00426 
00427 
00428 // Specialization for N = 0: KCC is giving some
00429 // peculiar errors, perhaps this will fix.
00430 
00431 template<typename T>
00432 class TinyVector<T,0> {
00433 };
00434 
00435 BZ_NAMESPACE_END
00436 
00437 #include <blitz/tinyveciter.h>  // Iterators
00438 #include <blitz/tvecglobs.h>    // Global functions
00439 #include <blitz/vector.h>       // Expression templates
00440 #include <blitz/tinyvec.cc>     // Member functions
00441 #include <blitz/tinyvecio.cc>   // I/O functions
00442 
00443 #endif // BZ_TINYVEC_H
00444 
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Defines