home *** CD-ROM | disk | FTP | other *** search
/ Chip 2001 February / Chip_2001-02_cd1.bin / bonus / demos / CS / exp / SOURCES / GLENGINE / nvec.h < prev    next >
C/C++ Source or Header  |  2000-07-16  |  4KB  |  211 lines

  1. /*
  2.  * N-dimensional vector
  3.  */
  4.  
  5. #ifndef __OGL2_NVEC__
  6. #define __OGL2_NVEC__
  7.  
  8. #if !defined (__GNUG__) && !defined (__attribute__)
  9. #define __attribute__(foo) /* Ignore.  */
  10. #endif
  11.  
  12. #include <iostream>
  13.  
  14. extern "C++" {
  15.  
  16. template <class __type>
  17. struct nvec
  18. {
  19.   int dim;
  20.   mutable __type* v;
  21.  
  22.   nvec() : dim(0), v(0) {}
  23.   nvec(int n) : dim(n) {
  24.     v = new __type[n];
  25.     __type *d = v;
  26.     for(int i=dim; i; i--, d++)
  27.       *d = 0;
  28.   }
  29.   nvec(const nvec<__type>& B) {
  30.     dim = B.dim;
  31.     v = new __type[B.dim];
  32.     __type* d = v;
  33.     __type* s = B.v;
  34.     for(int i=dim; i; i--, d++, s++) 
  35.       *d = *s;
  36.   }
  37.   nvec<__type>& operator = (const nvec<__type>& B) {
  38.     delete[] v;
  39.     dim = B.dim;
  40.     v = new __type[B.dim];
  41.     __type* d = v;
  42.     __type* s = B.v;
  43.     for(int i=dim; i; i--, d++, s++) 
  44.       *d = *s;
  45.   }
  46.   __type& operator [] (int i) const { return v[i]; }
  47.   nvec<__type>& operator += (const nvec<__type>& b) {
  48.     int i = b.dim>dim ? dim : b.dim;
  49.     __type* d = v;
  50.     __type* s = b.v;
  51.     for(;i;i--, s++, d++) 
  52.       *d += *s;
  53.     return *this;
  54.   }
  55.   nvec<__type>& operator -= (const nvec<__type>& b) {
  56.     int i = b.dim>dim ? dim : b.dim;
  57.     __type* d = v;
  58.     __type* s = b.v;
  59.     for(;i;i--, s++, d++)
  60.       *d -= *s;
  61.     return *this;
  62.   }
  63.   nvec<__type>& operator *= (const nvec<__type>& b) {
  64.     int i = b.dim>dim ? dim : b.dim;
  65.     __type* d = v;
  66.     __type* s = b.v;
  67.     for(;i;i--, s++, d++) 
  68.       *d *= *s;
  69.     return *this;
  70.   }
  71.  
  72.   ~nvec() {
  73.     delete[] v;
  74.   }
  75. };
  76.  
  77. template class nvec<float>;
  78. typedef nvec<float> nvecf;
  79.  
  80. template class nvec<double>;
  81. typedef nvec<double> nvecd;
  82.  
  83. template class nvec<long double>;
  84. typedef nvec<long double> nvecld;
  85.  
  86. template class nvec<int>;
  87. typedef nvec<int> nveci;
  88.  
  89. template <class __type>
  90. ostream& operator << (ostream& s, const nvec<__type>& a)
  91. {
  92.   int i=0;
  93.   s << "(";
  94.   while(i<(a.dim-1))
  95.   {
  96.     s << a[i] << ",";
  97.     i++;
  98.   }
  99.   s << a[i] << ")";
  100.   return (s);
  101. }
  102.  
  103.  
  104. template <class __type> inline nvec<__type>
  105. operator + (const nvec<__type>& a) __attribute__ ((const));
  106.  
  107. template <class __type> inline nvec<__type>
  108. operator + (const nvec<__type>& a)
  109. {
  110.   return a;
  111. }
  112.  
  113. template <class __type> inline nvec<__type>
  114. operator - (const nvec<__type>& a) __attribute__ ((const));
  115.  
  116. template <class __type> inline nvec<__type>
  117. operator - (const nvec<__type>& a)
  118. {
  119.   nvec<__type> u(a.dim);
  120.   __type* s = a.v;
  121.   __type* d = u.v;
  122.   for(int i=a.dim; i; i--, s++, d++)
  123.     *d = -(*s);
  124.   return u;
  125. }
  126.  
  127. template <class __type> inline nvec<__type>
  128. operator + (const nvec<__type>& a, const nvec<__type>& b) __attribute__ ((const));
  129.  
  130. template <class __type> inline nvec<__type>
  131. operator + (const nvec<__type>& a, const nvec<__type>& b)
  132. {
  133.   if(a.dim>b.dim) {
  134.     nvec<__type> c(a); 
  135.     c+=b;
  136.     return c;
  137.   }
  138.   else {
  139.     nvec<__type> c(b);
  140.     c+=a;
  141.     return c;
  142.   }
  143. }
  144.  
  145. template <class __type> inline nvec<__type>
  146. operator - (const nvec<__type>& a, const nvec<__type>& b) __attribute__ ((const));
  147.  
  148. template <class __type> inline nvec<__type>
  149. operator - (const nvec<__type>& a, const nvec<__type>& b)
  150. {
  151.   if(a.dim>b.dim) {
  152.     nvec<__type> c(a); 
  153.     c-=b;
  154.     return c;
  155.   }
  156.   else {
  157.     nvec<__type> c(b.dim);
  158.     c+=a;
  159.     c-=b;
  160.     return c;
  161.   }
  162. }
  163.  
  164. template <class __type> inline nvec<__type>
  165. operator * (const nvec<__type>& a, __type s) __attribute__ ((const));
  166.  
  167. template <class __type> inline nvec<__type>
  168. operator * (const nvec<__type>& a, __type s)
  169. {
  170.   nvec<__type> c(a);
  171.   __type *d = c.v;
  172.   for(int i=c.dim; i; i--, d++)
  173.     *d *= s;
  174.   return c;
  175. }
  176.  
  177. template <class __type> inline nvec<__type>
  178. operator * (__type s, const nvec<__type>& a) __attribute__ ((const));
  179.  
  180. template <class __type> inline nvec<__type>
  181. operator * (__type s, const nvec<__type>& a)
  182. {
  183.   nvec<__type> c(a);
  184.   __type *d = c.v;
  185.   for(int i=c.dim; i; i--, d++)
  186.     *d *= s;
  187.   return c;
  188. }
  189.  
  190. template <class __type> inline nvec<__type>
  191. operator * (const nvec<__type>& a, const nvec<__type>& b) __attribute__ ((const));
  192.  
  193. template <class __type> inline nvec<__type>
  194. operator * (const nvec<__type>& a, const nvec<__type>& b)
  195. {
  196.   if(a.dim>b.dim) {
  197.     nvec<__type> c(a); 
  198.     c*=b;
  199.     return c;
  200.   }
  201.   else {
  202.     nvec<__type> c(b);
  203.     c*=a;
  204.     return c;
  205.   }
  206. }
  207.  
  208. } // extern "C++"
  209.  
  210. #endif 
  211.