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