Blender V2.61 - r43446

vecadaptor.h

Go to the documentation of this file.
00001 
00004 /*
00005 
00006 *
00007 * Template Numerical Toolkit (TNT): Linear Algebra Module
00008 *
00009 * Mathematical and Computational Sciences Division
00010 * National Institute of Technology,
00011 * Gaithersburg, MD USA
00012 *
00013 *
00014 * This software was developed at the National Institute of Standards and
00015 * Technology (NIST) by employees of the Federal Government in the course
00016 * of their official duties. Pursuant to title 17 Section 105 of the
00017 * United States Code, this software is not subject to copyright protection
00018 * and is in the public domain.  The Template Numerical Toolkit (TNT) is
00019 * an experimental system.  NIST assumes no responsibility whatsoever for
00020 * its use by other parties, and makes no guarantees, expressed or implied,
00021 * about its quality, reliability, or any other characteristic.
00022 *
00023 * BETA VERSION INCOMPLETE AND SUBJECT TO CHANGE
00024 * see http://math.nist.gov/tnt for latest updates.
00025 *
00026 */
00027 
00028 
00029 
00030 #ifndef VECADAPTOR_H
00031 #define VECADAPTOR_H
00032 
00033 #include <cstdlib>
00034 #include <iostream>
00035 #include <cassert>
00036 
00037 #include "subscript.h"
00038 
00039 #ifdef TNT_USE_REGIONS
00040 #include "region1d.h"
00041 #endif
00042 
00043 namespace TNT
00044 {
00045 
00046 //  see "tntreq.h" for TNT requirements for underlying vector
00047 //  class.  This need NOT be the STL vector<> class, but a subset
00048 //  that provides minimal services.
00049 //
00050 //  This is a container adaptor that provides the following services.
00051 //
00052 //      o)  adds 1-offset operator() access ([] is always 0 offset)
00053 //      o)  adds TNT_BOUNDS_CHECK to () and []
00054 //      o)  adds initialization from strings, e.g.  "1.0 2.0 3.0";
00055 //      o)  adds newsize(N) function (does not preserve previous values)
00056 //      o)  adds dim() and dim(1)
00057 //      o)  adds free() function to release memory used by vector
00058 //      o)  adds regions, e.g. A(Index(1,10)) = ... 
00059 //      o)  add getVector() method to return adapted container
00060 //      o)  adds simple I/O for ostreams
00061 
00062 template <class BBVec>
00063 class Vector_Adaptor
00064 {
00065 
00066   public:
00067     typedef   typename BBVec::value_type T;
00068     typedef         T   value_type;
00069     typedef         T   element_type;
00070     typedef         T*  pointer;
00071     typedef         T*  iterator;
00072     typedef         T&  reference;
00073     typedef const   T*  const_iterator;
00074     typedef const   T&  const_reference;
00075     
00076     Subscript lbound() const { return 1; }
00077 
00078   protected:
00079     BBVec v_;
00080     T* vm1_;
00081 
00082   public:
00083 
00084     Subscript size() const { return v_.size(); }
00085 
00086     // These were removed so that the ANSI C++ valarray class
00087     // would work as a possible storage container.
00088     //
00089     //
00090     //iterator begin() { return v_.begin();}
00091     //iterator begin() { return &v_[0];}
00092     //
00093     //iterator end()   { return v_.end(); }
00094     //iterator end()   { return &v_[0] + v_.size(); }
00095     //
00096     //const_iterator begin() const { return v_.begin();}
00097     //const_iterator begin() const { return &v_[0];}
00098     //
00099     //const_iterator end()  const { return v_.end(); }
00100     //const_iterator end()  const { return &v_[0] + v_.size(); }
00101 
00102     BBVec& getVector() { return v_; }
00103     Subscript dim() const { return v_.size(); }
00104     Subscript dim(Subscript i)
00105     {
00106 #ifdef TNT_BOUNDS_CHECK
00107         assert(i==TNT_BASE_OFFSET);
00108 #endif
00109         return (i==TNT_BASE_OFFSET ? v_.size() : 0 );
00110     }
00111     Vector_Adaptor() : v_() {};
00112     Vector_Adaptor(const Vector_Adaptor<BBVec> &A) : v_(A.v_) 
00113     { 
00114         vm1_ = ( v_.size() > 0 ? &(v_[0]) -1 : NULL); 
00115 
00116     } 
00117 
00118     Vector_Adaptor(Subscript N, const T& value = T()) : v_(N)
00119     {
00120         for (Subscript i=0; i<N; i++)
00121              v_[i]  = value;
00122         
00123         vm1_ = ( v_.size() > 0 ? &(v_[0]) -1 : NULL); 
00124     }
00125 
00126     Vector_Adaptor(Subscript N, const T* values) : v_(N)
00127     {
00128         for (Subscript i=0; i<N; i++)
00129              v_[i]  = values[i];
00130         vm1_ = ( v_.size() > 0 ? &(v_[0]) -1 : NULL); 
00131     } 
00132     Vector_Adaptor(const BBVec & A) : v_(A) 
00133     {
00134         vm1_ = ( v_.size() > 0 ? &(v_[0]) -1 : NULL); 
00135     }
00136 
00137     // NOTE: this assumes that BBVec(0) constructor creates an 
00138     //  null vector that does not take up space...  It would be
00139     //  great to require that BBVec have a corresponding free()
00140     //  function, but in particular STL vectors do not.
00141     //
00142     Vector_Adaptor<BBVec>& free()
00143     {
00144         return *this = Vector_Adaptor<BBVec>(0);
00145     }
00146 
00147     Vector_Adaptor<BBVec>& operator=(const Vector_Adaptor<BBVec> &A) 
00148     { 
00149         v_ = A.v_ ; 
00150         vm1_ = ( v_.size() > 0 ? &(v_[0]) -1 : NULL); 
00151         return *this;
00152     }
00153 
00154     Vector_Adaptor<BBVec>& newsize(Subscript N)
00155     {
00156         // NOTE: this is not as efficient as it could be
00157         // but to retain compatiblity with STL interface
00158         // we cannot assume underlying implementation
00159         // has a newsize() function.
00160 
00161         return *this = Vector_Adaptor<BBVec>(N);
00162 
00163     }
00164 
00165     Vector_Adaptor<BBVec>& operator=(const T &a) 
00166     {
00167         Subscript i;
00168         Subscript N = v_.size();    
00169         for (i=0; i<N; i++)
00170             v_[i] = a;
00171 
00172         return *this;
00173     }
00174 
00175     Vector_Adaptor<BBVec>& resize(Subscript N) 
00176     { 
00177         if (N == size()) return *this;
00178 
00179         Vector_Adaptor<BBVec> tmp(N);
00180         Subscript n =  (N < size() ? N : size());  // min(N, size());
00181         Subscript i;
00182 
00183         for (i=0; i<n; i++)
00184             tmp[i] = v_[i];
00185             
00186 
00187         return (*this = tmp);
00188 
00189     }
00190 
00191 
00192     reference operator()(Subscript i)
00193     { 
00194 #ifdef TNT_BOUNDS_CHECK
00195         assert(1<=i);
00196         assert(i<=dim());
00197 #endif
00198         return vm1_[i]; 
00199     }
00200 
00201     const_reference operator()(Subscript i) const
00202     { 
00203 #ifdef TNT_BOUNDS_CHECK
00204         assert(1<=i);
00205         assert(i<=dim());
00206 #endif
00207         return vm1_[i]; 
00208     }
00209 
00210     reference operator[](Subscript i)
00211     { 
00212 #ifdef TNT_BOUNDS_CHECK
00213         assert(0<=i);
00214         assert(i<dim());
00215 #endif
00216         return v_[i]; 
00217     }
00218 
00219     const_reference operator[](Subscript i) const
00220     { 
00221 #ifdef TNT_BOUNDS_CHECK
00222         assert(0<=i);
00223         assert(i<dim());
00224 #endif
00225         return v_[i]; 
00226     }
00227 
00228 
00229 #ifdef TNT_USE_REGIONS
00230     // "index-aware" features, all of these are 1-based offsets
00231 
00232     typedef Region1D<Vector_Adaptor<BBVec> > Region;
00233 
00234     typedef const_Region1D< Vector_Adaptor<BBVec> > const_Region;
00235 
00236     Region operator()(const Index1D &I)
00237     {   return Region(*this, I); }
00238 
00239     Region operator()(const Subscript i1, Subscript i2)
00240     {   return Region(*this, i1, i2); }
00241 
00242     const_Region operator()(const Index1D &I) const
00243     {   return const_Region(*this, I); }
00244 
00245     const_Region operator()(const Subscript i1, Subscript i2) const
00246     {   return const_Region(*this, i1, i2); }
00247 #endif
00248 // TNT_USE_REGIONS
00249 
00250 
00251 };
00252 
00253 #include <iostream>
00254 
00255 template <class BBVec>
00256 std::ostream& operator<<(std::ostream &s, const Vector_Adaptor<BBVec> &A)
00257 {
00258     Subscript M=A.size();
00259 
00260     s << M << endl;
00261     for (Subscript i=1; i<=M; i++)
00262             s << A(i) << endl;
00263     return s;
00264 }
00265 
00266 template <class BBVec>
00267 std::istream& operator>>(std::istream &s, Vector_Adaptor<BBVec> &A)
00268 {
00269     Subscript N;
00270     
00271     s >> N;
00272 
00273     A.resize(N);
00274 
00275     for (Subscript i=1; i<=N; i++)
00276         s >> A(i);
00277 
00278     return s;
00279 }
00280 
00281 } // namespace TNT
00282 
00283 #endif
00284