00001 #ifndef NMWR_GB_TUPLE_T_N_H
00002 #define NMWR_GB_TUPLE_T_N_H
00003
00004
00005
00006
00007
00018 template<class T, unsigned N>
00019 class tuple_base {
00020 public:
00021 typedef T* iterator;
00022 typedef const T* const_iterator;
00023 typedef T value_type;
00024 typedef T c_array[N];
00025
00026
00027
00028 T& operator[](unsigned i) {
00029 check_range(i);
00030 return X[i];
00031 }
00032 T operator[](unsigned i) const {return X[i];}
00033
00034 iterator begin() { return &(X[0]);}
00035 iterator end() { return (begin() + N);}
00036 const_iterator begin() const { return &(X[0]);}
00037 const_iterator end() const { return (begin() + N);}
00038
00039 unsigned size() const { return N;}
00040 bool empty() const { return false;}
00041 protected:
00042 T X[N];
00043 private:
00044 void check_range(unsigned i) const {
00045 REQUIRE( ((0 <= i ) && (i < N)),
00046 " i = " << i << " must be in [0," << N << "]\n",1);
00047 }
00048 };
00049
00050
00051 template<class T, unsigned N>
00052 class tuple : public tuple_base<T,N> {
00053 typedef tuple_base<T,N> base;
00054
00055 public:
00056
00057 typedef typename base::iterator iterator;
00058 typedef typename base::const_iterator const_iterator;
00059
00060 tuple() {}
00061 tuple(const T& t) { for(iterator i = begin(); i != end(); ++i) *i = t;}
00062 tuple(const c_array& rs) { for(iterator i = begin(); i != end(); ++i) *i = rs[i-begin()];}
00063 tuple(const_iterator b, const_iterator )
00064 {
00065
00066
00067 for(iterator it = begin(); it != end(); ++it,++b)
00068 *it = *b;
00069 }
00070 template<class It>
00071 tuple(It b, It e) {
00072 for(iterator it = begin(); it != end(); ++it,++b)
00073 *it = *b;
00074 REQUIRE( (b == e), "invalid range in constructor!\n",1);
00075 }
00076
00077 };
00078
00079 template<class T>
00080 class tuple<T,3> : public tuple_base<T,3> {
00081 typedef tuple_base<T,3> base;
00082 public:
00083
00084 typedef typename base::iterator iterator;
00085 typedef typename base::const_iterator const_iterator;
00086
00087 tuple() {}
00088 tuple(const T& t) { for(iterator i = begin(); i != end(); ++i) *i = t;}
00089 tuple(const c_array& rs) { for(iterator i = begin(); i != end(); ++i) *i = rs[i-begin()];}
00090 tuple(const_iterator b, const_iterator )
00091 {
00092
00093
00094 for(iterator it = begin(); it != end(); ++it,++b)
00095 *it = *b;
00096 }
00097 template<class It>
00098 tuple(It b, It e) {
00099 for(iterator it = begin(); it != end(); ++it,++b)
00100 *it = *b;
00101 REQUIRE( (b == e), "invalid range in constructor!\n",1);
00102 }
00103
00104 tuple(T const& t1, T const& t2, T const& t3) { X[0] = t1; X[1] = t2; X[2] = t3;}
00105 };
00106
00107
00108
00109
00110
00111 template<class T, unsigned N>
00112 inline
00113 tuple<T,N> operator+(tuple<T,N> const& lhs, tuple<T,N> const& rhs)
00114 {
00115 tuple<T,N> tmp(lhs);
00116 for(unsigned k = 0; k < N; ++k)
00117 tmp[k] += rhs[k];
00118 return tmp;
00119 }
00120
00121 template<class T, unsigned N>
00122 inline
00123 tuple<T,N> operator-(tuple<T,N> const& lhs, tuple<T,N> const& rhs)
00124 {
00125 tuple<T,N> tmp(lhs);
00126 for(unsigned k = 0; k < N; ++k)
00127 tmp[k] -= rhs[k];
00128 return tmp;
00129 }
00130
00131 #endif
00132