00001 #ifndef RKSmallVector_H 00002 #define RKSmallVector_H 00003 00004 #include "DataFormats/GeometryVector/interface/PreciseFloatType.h" 00005 00006 #include <iostream> 00007 00008 template<typename T, int N> 00009 class RKSmallVector { 00010 public: 00011 typedef T Scalar; 00012 00013 RKSmallVector() {} 00014 00016 RKSmallVector( const Scalar* d) { 00017 for (int i=0; i<N; ++i) data_[i] = d[i]; 00018 } 00019 00022 template <class Iter> RKSmallVector(Iter begin, Iter end) { 00023 for (int i=0; i<N; i++) data_[i] = *begin++; 00024 } 00025 00026 int size() const {return N;} 00027 int dim() const {return N;} 00028 00030 const T& operator()(int i) const {return data_[i];} 00031 T& operator()(int i) {return data_[i];} 00032 const T& operator[](int i) const {return data_[i];} 00033 T& operator[](int i) {return data_[i];} 00034 00036 00037 template <class U> 00038 RKSmallVector& operator+= ( const RKSmallVector<U,N>& v) { 00039 for (int i=0; i<N; ++i) data_[i] += v[i]; 00040 return *this; 00041 } 00042 00043 template <class U> 00044 RKSmallVector& operator-= ( const RKSmallVector<U,N>& v) { 00045 for (int i=0; i<N; ++i) data_[i] -= v[i]; 00046 return *this; 00047 } 00048 00050 template <class U> 00051 RKSmallVector& increment( const RKSmallVector<U,N>& v, const T& t) { 00052 for (int i=0; i<N; ++i) data_[i] += t*v[i]; 00053 return *this; 00054 } 00055 00056 00058 RKSmallVector operator-() const { 00059 RKSmallVector r; 00060 for (int i=0; i<N; ++i) r[i] = -data_[i]; 00061 return r; 00062 } 00063 00065 RKSmallVector& operator*= ( const T& t) { 00066 for (int i=0; i<N; ++i) data_[i] *= t; 00067 return *this; 00068 } 00070 RKSmallVector& operator/= ( const T& t) { 00071 for (int i=0; i<N; ++i) data_[i] /= t; 00072 return *this; 00073 } 00074 00076 T dot( const RKSmallVector& v) const { 00077 RKSmallVector r; 00078 for (int i=0; i<N; ++i) r[i] = data_[i]*data_[i]; 00079 return r; 00080 } 00081 00082 private: 00083 00084 Scalar data_[N]; 00085 00086 }; 00087 00089 template <class T, int N> 00090 inline std::ostream & operator<<( std::ostream& s, const RKSmallVector<T,N>& v) { 00091 s << std::endl; 00092 for (int i=0; i<N; ++i) s << "v[" << i << "] = " << v[i] << std::endl; 00093 return s; 00094 } 00095 00097 template <class T, class U, int N> 00098 inline RKSmallVector<typename PreciseFloatType<T,U>::Type, N> 00099 operator+( const RKSmallVector<T, N>& a, const RKSmallVector<U, N>& b) { 00100 typedef RKSmallVector<typename PreciseFloatType<T,U>::Type, N> RT; 00101 RT r; 00102 for (int i=0; i<N; ++i) r[i] = a[i]+b[i]; 00103 return r; 00104 } 00105 template <class T, class U, int N> 00106 inline RKSmallVector<typename PreciseFloatType<T,U>::Type, N> 00107 operator-( const RKSmallVector<T, N>& a, const RKSmallVector<U, N>& b) { 00108 typedef RKSmallVector<typename PreciseFloatType<T,U>::Type, N> RT; 00109 RT r; 00110 for (int i=0; i<N; ++i) r[i] = a[i]-b[i]; 00111 return r; 00112 } 00113 00117 template <class T, class Scal, int N> 00118 inline RKSmallVector<T,N> operator*( const RKSmallVector<T,N>& v, const Scal& s) { 00119 T t = static_cast<T>(s); 00120 RKSmallVector<T,N> r; 00121 for (int i=0; i<N; ++i) r[i] = t*v[i]; 00122 return r; 00123 } 00124 template <class T, class Scal, int N> 00125 inline RKSmallVector<T,N> operator*( const Scal& s, const RKSmallVector<T,N>& v) { 00126 T t = static_cast<T>(s); 00127 RKSmallVector<T,N> r; 00128 for (int i=0; i<N; ++i) r[i] = t*v[i]; 00129 return r; 00130 } 00131 00135 template <class T, class Scal, int N> 00136 inline RKSmallVector<T,N> operator/( const RKSmallVector<T,N>& v, const Scal& s) { 00137 T t = static_cast<T>(s); 00138 RKSmallVector<T,N> r; 00139 for (int i=0; i<N; ++i) r[i] = v[i]/t; 00140 return r; 00141 } 00142 00143 00144 #endif