00001 #ifndef VECTOR_H 00002 #define VECTOR_H 00003 00004 #include <stdexcept> 00005 #include <string> 00006 #include <typeinfo> 00007 #include "utils.h" 00008 00010 00024 template<size_t N, typename T> 00025 class Vector 00026 { 00027 public: 00031 Vector(); 00036 Vector(T v); 00041 Vector(Vector<N, T> const& other); 00045 T get(size_t i) const; 00051 void set(size_t i, T v); 00052 00056 size_t getSize() const { return N; } 00057 00063 T& at(size_t const i); 00064 00070 T& operator[](size_t const i) { return at(i); } 00071 00076 Vector<N,T>& operator=(Vector<N,T> const& other); 00080 Vector<N,T>& operator+=(Vector<N,T> const& other); 00084 Vector<N,T>& operator-=(Vector<N,T> const& other); 00088 Vector<N,T>& operator*=(T s); 00092 Vector<N,T>& operator/=(T s); 00093 00094 private: 00095 T m_values[N]; 00096 }; 00097 00098 template<size_t N, typename T> 00099 Vector<N, T>::Vector() 00100 { 00101 for (size_t i = 0; i < N; ++i) { 00102 m_values[i] = T(); 00103 } 00104 } 00105 00106 template<size_t N, typename T> 00107 Vector<N, T>::Vector(T v) 00108 { 00109 for (size_t i = 0; i < N; ++i) { 00110 m_values[i] = v; 00111 } 00112 } 00113 00114 template<size_t N, typename T> 00115 Vector<N, T>::Vector(Vector<N,T> const& other) 00116 { 00117 for (size_t i = 0; i < N; ++i) { 00118 set(i, other.get(i)); 00119 } 00120 } 00121 00122 template<size_t N, typename T> 00123 T Vector<N, T>::get(size_t i) const 00124 { 00125 if (i < 0 || i >= N) { 00126 std::string err = "Vector<" + convertTo<size_t, std::string>(N) + ", "; 00127 err += typeid(T).name(); 00128 err += ">::get(" + convertTo<size_t, std::string>(i); 00129 err += "): index out of range"; 00130 throw std::out_of_range(err.c_str()); 00131 } 00132 return m_values[i]; 00133 } 00134 00135 template<size_t N, typename T> 00136 void Vector<N, T>::set(size_t i, T v) 00137 { 00138 if (i < 0 || i >= N) { 00139 std::string err = "Vector<" + convertTo<size_t, std::string>(N) + ", "; 00140 err += typeid(T).name(); 00141 err += ">::set(" + convertTo<size_t, std::string>(i); 00142 err += "): index out of range"; 00143 throw std::out_of_range(err.c_str()); 00144 } 00145 m_values[i] = v; 00146 } 00147 00148 template<size_t N, typename T> 00149 T& Vector<N,T>::at(const size_t i) 00150 { 00151 if (i < 0 || i >= N) { 00152 std::string err = "Vector<" + convertTo<size_t, std::string>(N) + ", "; 00153 err += typeid(T).name(); 00154 err += ">::at(" + convertTo<size_t, std::string>(i); 00155 err += "): index out of range"; 00156 throw std::out_of_range(err.c_str()); 00157 } 00158 return m_values[i]; 00159 } 00160 00161 template<size_t N, typename T> 00162 Vector<N,T>& Vector<N,T>::operator=(Vector<N,T> const& other) 00163 { 00164 if (other == *this) 00165 return *this; 00166 00167 for (size_t i = 0; i < N; ++i) { 00168 set(i, other.get(i)); 00169 } 00170 return *this; 00171 } 00172 00173 template<size_t N, typename T> 00174 Vector<N,T>& Vector<N,T>::operator+=(Vector<N,T> const& other) 00175 { 00176 for (size_t i = 0; i < N; ++i) { 00177 m_values[i] += other.get(i); 00178 } 00179 return *this; 00180 } 00181 00182 template<size_t N, typename T> 00183 Vector<N,T>& Vector<N,T>::operator-=(Vector<N,T> const& other) 00184 { 00185 for (size_t i = 0; i < N; ++i) { 00186 m_values[i] -= other.get(i); 00187 } 00188 return *this; 00189 } 00190 00191 template<size_t N, typename T> 00192 Vector<N,T>& Vector<N,T>::operator*=(T s) 00193 { 00194 for (size_t i = 0; i < N; ++i) { 00195 m_values[i] *= s; 00196 } 00197 return *this; 00198 } 00199 00200 template<size_t N, typename T> 00201 Vector<N,T>& Vector<N,T>::operator/=(T s) 00202 { 00203 for (size_t i = 0; i < N; ++i) { 00204 m_values[i] /= s; 00205 } 00206 return *this; 00207 } 00208 00209 00215 template<size_t N, typename T> 00216 Vector<N,T> const operator+(Vector<N,T> const& a, Vector<N,T> const& b) 00217 { 00218 Vector<N, T> temp(a); 00219 temp += b; 00220 return temp; 00221 } 00222 00228 template<size_t N, typename T> 00229 Vector<N,T> const operator-(Vector<N,T> const& a, Vector<N,T> const& b) 00230 { 00231 Vector<N, T> temp(a); 00232 temp -= b; 00233 return temp; 00234 } 00235 00241 template<size_t N, typename T> 00242 Vector<N,T> const operator*(Vector<N,T> const& a, T s) 00243 { 00244 Vector<N, T> temp(a); 00245 temp *= s; 00246 return temp; 00247 } 00248 00254 template<size_t N, typename T> 00255 Vector<N,T> const operator*(T s, Vector<N,T> const& a) 00256 { 00257 Vector<N, T> temp(a); 00258 temp *= s; 00259 return temp; 00260 } 00261 00267 template<size_t N, typename T> 00268 Vector<N,T> const operator/(Vector<N,T> const& a, T s) 00269 { 00270 Vector<N, T> temp(a); 00271 temp /= s; 00272 return temp; 00273 } 00274 00280 template<size_t N, typename T> 00281 bool operator==(Vector<N,T> const& a, Vector<N,T> const& b) 00282 { 00283 for (size_t i = 0; i < N; ++i) { 00284 if (a.get(i) != b.get(i)) 00285 return false; 00286 } 00287 return true; 00288 } 00289 00295 template<size_t N, typename T> 00296 bool operator!=(Vector<N,T> const& a, Vector<N,T> const& b) 00297 { 00298 return !(a == b); 00299 } 00300 00301 typedef Vector<3, double> Vector3d; 00302 00303 #endif