00001
00002 #ifndef __VVECTOR_H__
00003 #define __VVECTOR_H__
00004
00005 #include <iostream>
00006 #include <iostream>
00007 #include <algorithm>
00008 #include <functional>
00009 #include <iterator>
00010 #include <Utilities.h>
00011
00012 namespace math {
00013
00014 template<typename T, int N> class Vec;
00015
00017
00021 template<typename T, int N>
00022 struct VVector {
00023
00024 enum { size = N };
00025
00026 typedef T value_type;
00027 typedef T* value_ptr;
00028 typedef const T* value_cptr;
00029 typedef VVector<T, N> vec_type;
00030 typedef const VVector<T, N> cvec_type;
00031
00033 VVector() {}
00035 VVector(const T& val) { assign(val); }
00037 VVector(const VVector<T,N>& rhs) { assign(rhs); }
00038
00039 VVector(const T* vptr) { assign(vptr); }
00040
00041
00042 T* begin() { return &data[0]; };
00043 const T* begin() const { return &data[0]; };
00044 T* end() { return &data[N]; };
00045 const T* end() const { return &data[N]; };
00046
00048 const T& operator[](const unsigned int i) const { return data[i]; };
00050 T& operator[](const unsigned int i) { return data[i]; };
00051
00052
00053
00055 static unsigned int bytesize() { return sizeof(VVector<T,N>); }
00056
00058 VVector<T,N>& assign(const T& val)
00059 { std::fill( begin(), end(), val); return *this; }
00061 VVector<T,N>& assign(const T* vptr)
00062 { std::copy(&vptr[0], &vptr[N], begin()); return *this; }
00064 VVector<T,N>& assign(const VVector<T,N>& rhs)
00065 { std::copy(rhs.begin(), rhs.end(), begin()); return *this; }
00067 VVector<T,N>& operator=(const VVector<T,N>& rhs)
00068 { return assign(rhs); }
00069
00071 bool operator==(const VVector<T,N>& rhs) const {
00072 return std::equal(begin(), end(), rhs.begin());
00073 }
00075 bool operator!=(const VVector<T,N>& rhs) const {
00076 return !operator==(rhs);
00077 }
00079 bool allNotEqual(const VVector<T,N>& rhs) const {
00080 return std::equal(begin(), end(), rhs.begin(), std::not_equal_to<T>());
00081 }
00083 bool operator<(const VVector<T,N>& rhs) const {
00084 return std::equal(begin(), end(), rhs.begin(), std::less<T>());
00085 }
00087 bool operator<=(const VVector<T,N>& rhs) const {
00088 return std::equal(begin(), end(), rhs.begin(), std::less_equal<T>());
00089 }
00091 bool operator>(const VVector<T,N>& rhs) const {
00092 return std::equal(begin(), end(), rhs.begin(), std::greater<T>());
00093 }
00095 bool operator>=(const VVector<T,N>& rhs) const {
00096 return std::equal(begin(), end(), rhs.begin(),std::greater_equal<T>());
00097 }
00099 VVector<T,N>& operator+=(const VVector<T,N>& rhs) {
00100 std::transform(begin(), end(), rhs.begin(), begin(), std::plus<T>());
00101 return *this;
00102 }
00104 VVector<T,N>& operator-=(const VVector<T,N>& rhs) {
00105 std::transform(begin(), end(), rhs.begin(), begin(), std::minus<T>());
00106 return *this;
00107 }
00109 VVector<T,N>& operator*=(const VVector<T,N>& rhs) {
00110 std::transform(begin(), end(), rhs.begin(), begin(),
00111 std::multiplies<T>());
00112 return *this;
00113 }
00115 VVector<T,N>& operator/=(const VVector<T,N>& rhs) {
00116 std::transform(begin(), end(), rhs.begin(), begin(),
00117 std::divides<T>());
00118 return *this;
00119 }
00121 VVector<T,N> operator+(const VVector<T,N>& rhs) const {
00122 VVector<T,N> res;
00123 transform(begin(), end(), rhs.begin(), res.begin(), std::plus<T>());
00124 return res;
00125 }
00127 VVector<T,N> operator-(const VVector<T,N>& rhs) const {
00128 VVector<T,N> res;
00129 transform(begin(), end(), rhs.begin(), res.begin(), std::minus<T>());
00130 return res;
00131 }
00133 VVector<T,N> operator*(const VVector<T,N>& rhs) const {
00134 VVector<T,N> res;
00135 transform(begin(), end(), rhs.begin(), res.begin(),
00136 std::multiplies<T>());
00137 return res;
00138 }
00140 VVector<T,N> operator/(const VVector<T,N>& rhs) const {
00141 VVector<T,N> res;
00142 transform(begin(), end(), rhs.begin(), res.begin(), std::divides<T>());
00143 return res;
00144 }
00146 VVector<T,N> operator+(const T& val) const {
00147 VVector<T,N> res;
00148 transform(begin(), end(), res.begin(), std::bind2nd(std::plus<T>(), val));
00149 return res;
00150 }
00152 VVector<T,N> operator-(const T& val) const {
00153 VVector<T,N> res;
00154 transform(begin(), end(), res.begin(), std::bind2nd(std::minus<T>(),val));
00155 return res;
00156 }
00158 VVector<T,N> operator*(const T& val) const {
00159 VVector<T,N> res;
00160 transform(begin(), end(), res.begin(), std::bind2nd(std::multiplies<T>(),val));
00161 return res;
00162 }
00164 VVector<T,N> operator/(const T& val) const {
00165 VVector<T,N> res;
00166 transform(begin(), end(), res.begin(), std::bind2nd(std::divides<T>(),val));
00167 return res;
00168 }
00170 VVector<T,N> operator+=(const T& val) {
00171 transform(begin(), end(), this->begin(), std::bind2nd(std::plus<T>(), val));
00172 return *this;
00173 }
00175 VVector<T,N> operator-=(const T& val) {
00176 transform(begin(), end(), this->begin(), std::bind2nd(std::minus<T>(),val));
00177 return *this;
00178 }
00180 VVector<T,N> operator*=(const T& val) {
00181 transform(begin(), end(), this->begin(), std::bind2nd(std::multiplies<T>(),val));
00182 return *this;
00183 }
00185 VVector<T,N> operator/=(const T& val) {
00186 transform(begin(), end(), this->begin(), std::bind2nd(std::divides<T>(),val));
00187 return *this;
00188 }
00189
00191 T data[N];
00192 };
00193
00195 template<typename T, int N>
00196 std::ostream& operator<<(std::ostream& os, const VVector<T,N>& rhs) {
00197 std::copy(rhs.begin(), rhs.end(), std::ostream_iterator<T>(os," "));
00198 return os;
00199 }
00200
00202 template<typename T, int N>
00203 std::istream& operator>>(std::istream& is, VVector<T,N>& rhs) {
00204 std::copy(std::istream_iterator<T>(is), std::istream_iterator<T>(),
00205 rhs.begin());
00206 return is;
00207 }
00208
00209 template class VVector<float,2>;
00210 template class VVector<float,3>;
00211 template class VVector<float,4>;
00212 template class VVector<double,2>;
00213 template class VVector<double,3>;
00214 template class VVector<double,4>;
00215 template class VVector<int,2>;
00216 template class VVector<int,3>;
00217 template class VVector<int,4>;
00218
00219 typedef VVector<float,2> Vector2f;
00220 typedef VVector<float,3> Vector3f;
00221 typedef VVector<float,4> Vector4f;
00222 typedef VVector<double,2> Vector2d;
00223 typedef VVector<double,3> Vector3d;
00224 typedef VVector<double,4> Vector4d;
00225 typedef VVector<int,2> Vector2i;
00226 typedef VVector<int,3> Vector3i;
00227 typedef VVector<int,4> Vector4i;
00228
00229 }
00230
00231 #endif