vtf-logo

shells/utilities/VArray.h

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 //----------------------------------------------------------------------
00003 //
00004 //                           Fehmi Cirak
00005 //                California Institute of Technology
00006 //                   (C) 2004 All Rights Reserved
00007 //
00008 //----------------------------------------------------------------------
00009 //
00010 #ifndef VArray_h
00011 #define VArray_h
00012 
00013 #include <functional>
00014 #include <algorithm>
00015 #include <numeric>
00016 #include <iosfwd>
00017 
00018 namespace utilities {
00019 
00020 template <int N, typename T=double>
00021 class VArray {
00022 public:
00023     typedef T value_type;
00024     typedef value_type* iterator;
00025     typedef const value_type*  const_iterator;
00026     typedef unsigned size_type; 
00027     
00028 protected:
00029     value_type _data[N];       
00030     
00031 public:
00032     VArray(){}
00033     ~VArray(){}
00034     
00035     // Copy constructor
00036     VArray(const VArray& x) {
00037         std::copy(x.begin(), x.end(), _data);
00038     }
00039     
00040     // Specify an initializing value for the components
00041     explicit VArray(value_type x);
00042     
00043     // Assignment operators
00044     VArray& operator=(const VArray& x);
00045     VArray& operator=(value_type x);
00046     
00047     // accessors - iterators
00048     iterator begin() {return _data;}
00049     iterator end() {return _data+N;}
00050 
00051     // accessors - const_iterators
00052     const_iterator begin() const {return _data;}
00053     const_iterator end() const {return _data+N;}
00054     
00055     // size of the array
00056     static size_type size() {return N;}
00057     
00058     // return the i_th component
00059     value_type operator[](size_type i) const {
00060         return _data[i];
00061     }
00062     
00063     // return the i_th component.
00064     value_type&  operator[](size_type i) {
00065         return _data[i];
00066     }
00067     
00068     // operations with a scalar
00069     VArray& operator+=(value_type x);
00070     VArray& operator-=(value_type x);    
00071     VArray& operator*=(value_type x);
00072     VArray& operator/=(value_type x);
00073     
00074     // operations with a VArray
00075     VArray& operator+=(const VArray& x);
00076     VArray& operator-=(const VArray& x);
00077 };
00078 
00079     
00080 // Partial template specialization for N = 3.
00081     template <typename T>
00082     class VArray<3,T> {
00083     public:
00084         typedef T value_type;
00085         typedef value_type* iterator;
00086         typedef const value_type*  const_iterator;
00087         typedef unsigned  size_type; 
00088         
00089     protected:
00090         value_type _data[3];
00091     
00092     public:
00093         VArray(){}
00094         VArray(value_type x){
00095             _data[0] = x;
00096             _data[1] = x;
00097             _data[2] = x;       
00098         }
00099         
00100         ~VArray(){}
00101         
00102         // Copy constructor
00103         VArray(const VArray& x){
00104             _data[0] = x._data[0];
00105             _data[1] = x._data[1];
00106             _data[2] = x._data[2];
00107         }
00108         
00109         // constructor with three components
00110         explicit VArray(value_type x0, value_type x1, value_type x2) {
00111             _data[0] = x0;
00112             _data[1] = x1;
00113             _data[2] = x2;
00114         }
00115         
00116         // assignment operatos
00117         VArray& operator=(const VArray& x) {
00118             if (&x != this) {
00119                 _data[0] = x._data[0];
00120                 _data[1] = x._data[1];
00121                 _data[2] = x._data[2];
00122             }
00123             return *this;
00124         }
00125         
00126         VArray& operator=(value_type x) {
00127             _data[0] = x;
00128             _data[1] = x;
00129             _data[2] = x;
00130             return *this;
00131         }
00132         
00133         // accessors iterators
00134         iterator begin() {return _data;}
00135         iterator end() {return _data + 3;}
00136         const_iterator begin() const {return _data;}    
00137         const_iterator end() const {return _data+3;}
00138         
00139         // size of the array
00140         static size_type size() {return 3;}
00141     
00142         // return the ith component
00143         value_type operator[](size_type i) const {return _data[i];}
00144         value_type& operator[](size_type i) {return _data[i];}
00145         
00146     
00147         // operations with a scalar
00148         VArray& operator+=(value_type x){
00149             _data[0] += x;
00150             _data[1] += x;
00151             _data[2] += x;
00152             
00153             return *this;
00154         }
00155         
00156         VArray& operator-=(value_type x){
00157             _data[0] -= x;
00158             _data[1] -= x;
00159             _data[2] -= x;
00160             return *this;
00161         }
00162         
00163         VArray& operator*=(value_type x){
00164             _data[0] *= x;
00165             _data[1] *= x;
00166             _data[2] *= x;
00167             return *this;
00168         }
00169         
00170         VArray& operator/=(value_type x){
00171             _data[0] /= x;
00172             _data[1] /= x;
00173             _data[2] /= x;
00174             return *this;
00175         }
00176         
00177         // operations with a VArray
00178         VArray& operator+=(const VArray& x){
00179             _data[0] += x[0];
00180             _data[1] += x[1];
00181             _data[2] += x[2];
00182             return *this;
00183         }
00184         
00185         VArray& operator-=(const VArray& x) {
00186             _data[0] -= x[0];
00187             _data[1] -= x[1];
00188             _data[2] -= x[2];
00189             return *this;
00190         }
00191         
00192     };
00193 
00194 
00195     template <int N, typename T>
00196     inline VArray<N,T>::VArray(value_type x) {
00197         std::fill(begin(), end(), x);
00198     }
00199     
00200     
00201     template <int N, typename T> 
00202     inline VArray<N,T>& VArray<N,T>::operator+=(value_type x) {
00203         
00204         std::transform(begin(), end(), begin(), std::bind2nd(std::plus<T>(), x));
00205         
00206         return *this;
00207     }
00208     
00209     
00210     template <int N, typename T>
00211     inline VArray<N,T>& VArray<N,T>::operator-=(value_type x) {
00212         
00213         std::transform(begin(), end(), begin(), std::bind2nd(std::minus<T>(), x));
00214         
00215         return *this;
00216     }
00217     
00218     
00219     template <int N, typename T>
00220     inline VArray<N,T>& VArray<N,T>::operator*=(value_type x){
00221         
00222         std::transform(begin(), end(), begin(), std::bind2nd(std::multiplies<T>(), x));
00223         
00224         return *this;
00225     }
00226     
00227     
00228     template <int N, typename T>
00229     inline VArray<N,T>& VArray<N,T>:: operator/=(value_type x) {
00230         
00231         std::transform(begin(), end(), begin(), std::bind2nd(std::divides<T>(), x));
00232         
00233         return *this;
00234     }
00235     
00236     
00237     template <int N, typename T>
00238     inline VArray<N,T>& VArray<N,T>::operator+=(const VArray<N,T>& x) {
00239         
00240         std::transform(begin(), end(), x.begin(), begin(), std::plus<T>());
00241         
00242         return *this;
00243     }
00244     
00245     
00246     template <int N, typename T>
00247     inline VArray<N,T>& VArray<N,T>::operator-=(const VArray<N, T>& x){
00248         
00249         std::transform(begin(), end(), x.begin(), begin(), std::minus<T>());
00250         
00251         return *this;
00252     }
00253     
00254     
00255     template <int N, typename T>
00256     inline std::ostream& operator<<( std::ostream& out, const VArray<N,T>& x) {
00257         typename VArray<N,T>::const_iterator begin = x.begin(), end = x.end();
00258         if (begin != end) {
00259             out << *begin;
00260             ++begin;
00261         }
00262         for ( ; begin != end; ++begin) {
00263             out << " " << *begin;
00264         }
00265         return out;
00266     }
00267 
00268 } // namespace utilities
00269 
00270 #endif
00271 
00272 // End of file.

Generated on Fri Aug 24 13:00:24 2007 for SFC Thin-Shell Finite Element Solver by  doxygen 1.4.7