00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 
00025 
00026 
00027 
00028 
00029 
00030 
00031 #ifndef SUNDANCE_FIXEDARRAY_H
00032 #define SUNDANCE_FIXEDARRAY_H
00033 
00034 
00035 #include "SundanceDefs.hpp"
00036 #include <iostream>
00037 #include <algorithm>
00038 
00039 namespace Sundance
00040 {
00041 
00042 
00043 
00044 
00045 template <int N, class T> class FixedArray
00046 {
00047 public:
00048 
00049   FixedArray(){;}
00050 
00051 
00052   const T& operator[](int i) const 
00053     {
00054       TEUCHOS_TEST_FOR_EXCEPT(i<0);
00055       TEUCHOS_TEST_FOR_EXCEPT(i>=N);
00056       return data_[i];
00057     }
00058 
00059 
00060   T& operator[](int i) 
00061     {
00062       TEUCHOS_TEST_FOR_EXCEPT(i<0);
00063       TEUCHOS_TEST_FOR_EXCEPT(i>=N);
00064       return data_[i];
00065     }
00066 
00067 
00068   int size() const {return N;}
00069 
00070 
00071   const T* begin() const {return &(data_[0]);}
00072 
00073 
00074   const T* end() const {return begin()+N;}
00075 
00076 
00077   T* begin() {return data_;}
00078   
00079 
00080   T* end() {return data_+N;}
00081 
00082 
00083   bool operator<(const FixedArray<N, T>& other) const
00084     {
00085       return std::lexicographical_compare(begin(), end(), 
00086         other.begin(), other.end());
00087     }
00088 
00089 private:
00090   T data_[N];
00091 };
00092 
00093 
00094 
00095 
00096 template<class T>
00097 FixedArray<2,T> makeFA(const T& a, const T& b)
00098 {
00099   FixedArray<2,T> rtn;
00100   rtn[0] = a;
00101   rtn[1] = b;
00102   return rtn;
00103 }
00104 
00105 
00106 template<class T>
00107 FixedArray<3,T> makeFA(const T& a, const T& b, const T& c)
00108 {
00109   FixedArray<3,T> rtn;
00110   rtn[0] = a;
00111   rtn[1] = b;
00112   rtn[2] = c;
00113   return rtn;
00114 }
00115 
00116 
00117 
00118 template<class T>
00119 FixedArray<4,T> makeFA(const T& a, const T& b, const T& c, const T& d)
00120 {
00121   FixedArray<4,T> rtn;
00122   rtn[0] = a;
00123   rtn[1] = b;
00124   rtn[2] = c;
00125   rtn[2] = d;
00126   return rtn;
00127 }
00128 
00129 }
00130 
00131 
00132 namespace std
00133 {
00134 
00135 template <int N, class T> 
00136 ostream& operator<<(std::ostream& os, const Sundance::FixedArray<N, T>& f)
00137 {
00138   os << "{";
00139   for (int i=0; i<N; i++) 
00140   {
00141     if (i>0) os << ", ";
00142     os << f[i];
00143   }
00144   os << "}";
00145   return os;
00146 }
00147 
00148 }
00149 
00150 #endif