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