• Main Page
  • Namespaces
  • Data Structures
  • Files
  • File List

/export/development/ViennaMath/viennamath/compiletime/ct_vector.hpp

Go to the documentation of this file.
00001 #ifndef VIENNAMATH_COMPILETIME_CT_VECTOR_HPP
00002 #define VIENNAMATH_COMPILETIME_CT_VECTOR_HPP
00003 
00004 /* =======================================================================
00005    Copyright (c) 2012, Institute for Microelectronics,
00006                        Institute for Analysis and Scientific Computing,
00007                        TU Wien.
00008                              -----------------
00009                ViennaMath - Symbolic and Numerical Math in C++
00010                              -----------------
00011 
00012    Author:     Karl Rupp                          rupp@iue.tuwien.ac.at
00013 
00014    License:    MIT (X11), see file LICENSE in the ViennaMath base directory
00015 ======================================================================= */
00016 
00017 
00018 
00019 
00020 #include <ostream>
00021 #include "viennamath/forwards.h"
00022 #include "viennamath/exception.hpp"
00023 
00028 namespace viennamath
00029 {
00030   
00035   template <typename T0>
00036   class ct_vector_1
00037   {
00038     public:
00039       typedef T0   type_0;
00040       
00041       ct_vector_1(T0 const & t0) : t0_(t0) {};
00042     
00044       T0 const & operator[](ct_index<0>) const { return t0_; }
00045       
00046       //compile time index out of bounds:
00047       template <long i>
00048       void operator[](ct_index<i>) const
00049       {
00050         typedef typename ct_index<i>::ERROR_VECTOR_1_INDEX_OUT_OF_BOUNDS  error_message; 
00051       }
00052       
00053       size_t size() const { return 1; }
00054 
00055     private:
00056       T0 const & t0_;
00057   };
00058   
00060   template <typename T0>
00061   std::ostream& operator<<(std::ostream & stream, ct_vector_1<T0> const & v)
00062   {
00063     stream << "vector<" << v[ct_index<0>()] << ">";
00064     return stream;
00065   }
00066   
00067   
00068   
00069   //compile time vector with two elements
00075   template <typename T0, typename T1>
00076   class ct_vector_2
00077   {
00078     public:
00079       typedef T0   type_0;
00080       typedef T1   type_1;
00081       
00082       ct_vector_2(T0 const & t0, T1 const & t1) : t0_(t0), t1_(t1) {};
00083     
00085       T0 const & operator[](ct_index<0>) const { return t0_; }
00087       T1 const & operator[](ct_index<1>) const { return t1_; }
00088 
00089       //compile time index out of bounds:
00090       template <long i>
00091       void operator[](ct_index<i>) const
00092       {
00093         typedef typename ct_index<i>::ERROR_VECTOR_2_INDEX_OUT_OF_BOUNDS  error_message; 
00094       }
00095 
00096       std::size_t size() const { return 2; }
00097 
00098     private:
00099       T0 const & t0_;
00100       T1 const & t1_;
00101   };
00102 
00104   template <typename T0, typename T1>
00105   std::ostream& operator<<(std::ostream & stream, ct_vector_2<T0, T1> const & v)
00106   {
00107     stream << "vector<" << v[ct_index<0>()] << ", " << v[ct_index<1>()] << ">";
00108     return stream;
00109   }
00110   
00111   
00112   //compile time vector with three elements
00119   template <typename T0, typename T1, typename T2>
00120   class ct_vector_3
00121   {
00122     public:
00123       typedef T0   type_0;
00124       typedef T1   type_1;
00125       typedef T2   type_2;
00126       //typedef typename type_0::numeric_type  numeric_type;
00127       
00128       ct_vector_3(T0 const & t0, T1 const & t1, T2 const & t2) : t0_(t0), t1_(t1), t2_(t2) {};
00129       
00130       ct_vector_3(const ct_vector_3 & other) : t0_(other.t0_), t1_(other.t1_), t2_(other.t2_) {}
00131     
00133       T0 const & operator[](ct_index<0>) const { return t0_; }
00135       T1 const & operator[](ct_index<1>) const { return t1_; }
00137       T2 const & operator[](ct_index<2>) const { return t2_; }
00138 
00139       //compile time index out of bounds:
00140       template <long i>
00141       void operator[](ct_index<i>) const
00142       {
00143         typedef typename ct_index<i>::ERROR_VECTOR_3_INDEX_OUT_OF_BOUNDS  error_message; 
00144       }
00145 
00146       std::size_t size() const { return 3; }
00147 
00148     private:
00149       T0 t0_;
00150       T1 t1_;
00151       T2 t2_;
00152   };
00153   
00155   template <typename T0, typename T1, typename T2>
00156   std::ostream& operator<<(std::ostream & stream, ct_vector_3<T0, T1, T2> const & v)
00157   {
00158     stream << "vector<" << v[ct_index<0>()] << ", "
00159                         << v[ct_index<1>()] << ", "                            
00160                         << v[ct_index<2>()] << ">";
00161     return stream;
00162   }
00163   
00164   
00165   /***************** make_vector() - A helper function for creation vector_X types */
00167   template <typename T0>
00168   ct_vector_1<T0> make_vector(T0 const & t0)
00169   {
00170     return ct_vector_1<T0>(t0);
00171   }
00172   
00174   template <typename T0, typename T1>
00175   ct_vector_2<T0, T1> make_vector(T0 const & t0, T1 const & t1)
00176   {
00177     return ct_vector_2<T0, T1>(t0, t1);
00178   }
00179   
00181   template <typename T0, typename T1, typename T2>
00182   ct_vector_3<T0, T1, T2> make_vector(T0 const & t0, T1 const & t1, T2 const & t2)
00183   {
00184     return ct_vector_3<T0, T1, T2>(t0, t1, t2);
00185   }
00186   
00187 } //namespace viennamath
00188 
00189 #endif

Generated on Wed Feb 29 2012 21:50:43 for ViennaMath by  doxygen 1.7.1