_vector.h
上传用户:gzelex
上传日期:2007-01-07
资源大小:707k
文件大小:7k
开发平台:

MultiPlatform

  1. /*******************************************************************************
  2. +
  3. +  LEDA-R  3.2.3
  4. +
  5. +  _vector.h
  6. +
  7. +  Copyright (c) 1995  by  Max-Planck-Institut fuer Informatik
  8. +  Im Stadtwald, 66123 Saarbruecken, Germany     
  9. +  All rights reserved.
  10. *******************************************************************************/
  11. #ifndef LEDA_PAR_VECTOR_H
  12. #define LEDA_PAR_VECTOR_H
  13. //----------------------------------------------------------------------
  14. //  parameterized vectors:  _vector<T>
  15. //----------------------------------------------------------------------
  16. //
  17. // generic vectors with entries of type T
  18. // T must support the following operators and functions:
  19. //
  20. // T::operator+, T::operator-, T::operator*, T::operator/
  21. // T::operator+=, T::operator-=, T::operator*=, T::operator/=
  22. // T::operator=, T::operator==
  23. // T::operator<, T::operator>
  24. // T::operator<<, T::operator>>
  25. // sqrt(), acos()
  26. #include <LEDA/basic.h>
  27. #include <math.h>
  28. template<class T> class _vector
  29. {
  30.   T* v;
  31.   int d;
  32.   void check_dimensions(const _vector<T>&) const;
  33.  
  34. public:
  35.   _vector(); 
  36.   _vector(int); 
  37.   _vector(const T&, const T&);
  38.   _vector(const T&, const T&, const T&);
  39.   _vector(const _vector<T>&);
  40.   ~_vector();
  41.  
  42.   T length() const;
  43.   
  44.   int dim() const;
  45.   _vector<T> norm() const;
  46.   
  47.   T angle(const _vector<T>&) const; 
  48.   
  49.   _vector<T>& operator=(const _vector<T>&);
  50.   
  51.   T& operator[](int);
  52.   T operator[](int) const;
  53.   
  54.   _vector<T> operator+(const _vector<T>&) const;
  55.   _vector<T> operator-(const _vector<T>&) const;
  56.   _vector<T> operator*(const T&) const;
  57.   _vector<T> operator/(const T&) const;
  58.   T operator*(const _vector<T>&) const;
  59.   int operator==(const _vector<T>&) const;
  60.   int operator!=(const _vector<T>&) const;
  61.   
  62.   
  63.   friend _vector<T> operator-(const _vector<T>&);
  64.   
  65.   friend ostream& operator<<(ostream&, const _vector<T>&);
  66.   friend istream& operator>>(istream&, _vector<T>&);
  67.   friend int compare(const _vector<T>&, const _vector<T>&);
  68. };
  69. template<class T> void
  70. _vector<T>::check_dimensions(const _vector<T>& p) const
  71. { if (d != p.d) error_handler(1,"_vector: _vector arguments have different dimensions"); };
  72. template<class T>
  73. _vector<T>::_vector()
  74. { d = 0; v = nil; };
  75. template<class T>
  76. _vector<T>::_vector(int n)
  77. {
  78.   if (n < 0) error_handler(1,"_vector: negative dimension!");
  79.   d = n;
  80.   if (d > 0) {
  81.     v = new T[d];
  82.     T* pv = v + d;
  83.     while (n--) { *--pv = 0; }
  84.   }
  85.   else { v = nil; }
  86. };
  87. template<class T>
  88. _vector<T>::_vector(const T& x, const T& y)
  89. {
  90.   d = 2;
  91.   v = new T[d];
  92.   v[0] = x;
  93.   v[1] = y;
  94. };
  95. template<class T>
  96. _vector<T>::_vector(const T& x, const T& y, const T& z)
  97. {
  98.   d = 3;
  99.   v = new T[d];
  100.   v[0] = x;
  101.   v[1] = y;
  102.   v[2] = z;
  103. };
  104. template<class T>
  105. _vector<T>::_vector(const _vector<T>& p)
  106. {
  107.   d = p.d;
  108.   if (d > 0) {
  109.     v = new T[d];
  110.     register int stop = d;
  111.     register T* pv = v + d;
  112.     register T* pp = p.v + d;
  113.     while (stop--) *--pv = *--pp;
  114.   }
  115.   else v = nil;
  116. };
  117. template<class T>
  118. _vector<T>::~_vector()
  119. { if (v) delete v; };
  120. template<class T> T
  121. _vector<T>::length() const
  122. { return sqrt((*this) * (*this)); };
  123. int
  124. _vector<int>::length() const
  125. { error_handler(1,"_vector<int>: length not implemented"); return 0; };
  126. template<class T> int
  127. _vector<T>::dim() const
  128. { return d; };
  129. template<class T> _vector<T>
  130. _vector<T>::norm() const
  131. { return (*this)/length(); };
  132. _vector<int>
  133. _vector<int>::norm() const
  134. {
  135.   error_handler(1,"_vector<int>: norm not implemented");
  136.   _vector<int> v;
  137.   return v;
  138. };
  139. template<class T> T
  140. _vector<T>::angle(const _vector<T>& p) const 
  141. {
  142.   T lv = length();
  143.   T lp = p.length();
  144.   if ((lv == 0) || (lp == 0)) {
  145.     error_handler(1,"_vector: zero length _vector");
  146.   }
  147.   return acos(((*this) * p) / (lv * lp));
  148. };
  149. int _vector<int>::angle(const _vector<int>&) const
  150. { error_handler(1,"_vector<int>: angle not implemented"); return 0; };
  151. template<class T> _vector<T>&
  152. _vector<T>::operator=(const _vector<T>& p)
  153. {
  154.   register int n = p.d;
  155.   if (d != n) {
  156.     delete v;
  157.     d = n;
  158.     v = new T[d];
  159.   }
  160.   register T* pv = v + d;
  161.   register T* pp = p.v + d;
  162.   while (n--) { *--pv = *--pp; }
  163.   return (*this);
  164. };
  165. template<class T> T&
  166. _vector<T>::operator[](int i)
  167. {
  168.   if ((i < 0) || (i >= d)) {
  169.     error_handler(1,"_vector: index out of range");
  170.   }
  171.   return v[i]; 
  172. };
  173. template<class T> T
  174. _vector<T>::operator[](int i) const
  175. {
  176.   if ((i < 0) || (i >= d)) {
  177.     error_handler(1,"_vector: index out of range");
  178.   }
  179.   return v[i]; 
  180. };
  181. template<class T> _vector<T>
  182. _vector<T>::operator+(const _vector<T>& p) const
  183. {
  184.   check_dimensions(p);
  185.   register int n = d;
  186.   _vector<T> result(*this);
  187.   register T* pr = result.v + d;
  188.   register T* pp = p.v + d;
  189.   while (n--) { *--pr += *--pp; }
  190.   return result;
  191. };
  192. template<class T> _vector<T>
  193. _vector<T>::operator-(const _vector<T>& q) const
  194. {
  195.   check_dimensions(q);
  196.   _vector<T> result(*this);
  197.   register int n = result.d;
  198.   register T* pr = result.v + n;
  199.   register T* pq = q.v + q.d;
  200.   while (n--) { *--pr -= *--pq; }
  201.   return result;
  202. };
  203. template<class T> _vector<T>
  204. _vector<T>::operator*(const T& s) const
  205. {
  206.   register int n = d;
  207.   _vector<T> result(*this);
  208.   register T* pr = result.v + d;
  209.   while (n--) { *--pr *= s; }
  210.   return result;
  211. };
  212. template<class T> _vector<T>
  213. operator*(const T& s, const _vector<T>& p)
  214. { return p*s; };
  215. template<class T> _vector<T>
  216. _vector<T>::operator/(const T& s) const
  217. {
  218.   if (s == 0) error_handler(1,"_vector: division by 0");
  219.   register int n = d;
  220.   _vector<T> result(*this);
  221.   register T* pr = result.v + d;
  222.   while (n--) { *--pr /= s; }
  223.   return result;
  224. };
  225. template<class T> T
  226. _vector<T>::operator*(const _vector<T>& p) const
  227. {
  228.   check_dimensions(p);
  229.   register int n = d;
  230.   T result = 0;
  231.   register T* pv = v + d;
  232.   register T* pp = p.v + d;
  233.   while (n--) result += ((*--pv) * (*--pp));
  234.   return result;
  235. };
  236.   
  237. template<class T> int
  238. _vector<T>::operator==(const _vector<T>& p) const
  239. {
  240.   if (p.d != d) return false;
  241.   register int i = d;
  242.   register T* pv = v + d;
  243.   register T* pp = p.v + d;
  244.   while ((i) && ((*--pv) == (*--pp))) i--;
  245.   return (!i);
  246. };
  247. template<class T> int
  248. _vector<T>::operator!=(const _vector<T>& w) const
  249. { return !(*this == w); };
  250. template<class T> _vector<T>
  251. operator-(const _vector<T>& p)
  252. {
  253.   _vector<T> zerovec(p.d); // initialized with zeros
  254.   return zerovec - p;
  255. };
  256. template<class T> ostream&
  257. operator<<(ostream& out, const _vector<T>& p)
  258. {
  259.   register int i;
  260.   for (i = 0; i < p.d; i++) {
  261.     out << p.v[i] << " ";
  262.   }
  263.   return out;
  264. }
  265. template<class T> istream&
  266. operator>>(istream& in, _vector<T>& p)
  267. { int i=0;
  268.   while (i < p.d && in >> p.v[i++]);
  269.   return in;
  270. }
  271. template<class T> int
  272. compare(const _vector<T>& v1, const _vector<T>& v2)
  273. { v1.check_dimensions(v2);
  274.   register T* pv1 = v1.v;
  275.   register T* pv2 = v2.v;
  276.   register T* stopv1 = v1.v + v1.d;
  277.   while ((pv1 < stopv1) && ((*pv1) == (*pv2))) { *pv1++; *pv2++; };
  278.   if (pv1 == stopv1) return 0;
  279.   else return (*pv1 < *pv2) ? -1 : 1;
  280. }
  281. template<class T> void
  282. Print(const _vector<T>& v, ostream& out)
  283. { out << v; };
  284. template<class T> void
  285. Read(_vector<T>& v, istream& in)
  286. { in >> v;  };
  287. #endif