home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / stlpt453.zip / STLport-4.5.3 / stlport / stl / _valarray.c < prev    next >
C/C++ Source or Header  |  2002-02-02  |  5KB  |  198 lines

  1. /*
  2.  *
  3.  *
  4.  * Copyright (c) 1994
  5.  * Hewlett-Packard Company
  6.  *
  7.  * Copyright (c) 1996,1997
  8.  * Silicon Graphics Computer Systems, Inc.
  9.  *
  10.  * Copyright (c) 1997
  11.  * Moscow Center for SPARC Technology
  12.  *
  13.  * Copyright (c) 1999 
  14.  * Boris Fomitchev
  15.  *
  16.  * This material is provided "as is", with absolutely no warranty expressed
  17.  * or implied. Any use is at your own risk.
  18.  *
  19.  * Permission to use or copy this software for any purpose is hereby granted 
  20.  * without fee, provided the above notices are retained on all copies.
  21.  * Permission to modify the code and to distribute modified code is granted,
  22.  * provided the above notices are retained, and a notice that the code was
  23.  * modified is included with the above copyright notice.
  24.  *
  25.  */
  26. #ifndef _STLP_VALARRAY_C
  27. #define _STLP_VALARRAY_C
  28.  
  29. #ifndef _STLP_VALARRAY_H
  30. # include <stl/_valarray.h>
  31. #endif
  32.  
  33. _STLP_BEGIN_NAMESPACE
  34.  
  35. template <class _Tp>
  36. _Valarray_bool valarray<_Tp>:: operator!() const {
  37.   _Valarray_bool __tmp(this->size(), _Valarray_bool::_NoInit());
  38.   for (size_t __i = 0; __i < this->size(); ++__i)
  39.     __tmp[__i] = !(*this)[__i];
  40.   return __tmp;
  41. }
  42.  
  43. // Behavior is undefined if __x and *this have different sizes
  44. template <class _Tp>
  45. valarray<_Tp>& valarray<_Tp>::operator=(const slice_array<_Tp>& __x)
  46. {
  47.   size_t __index = __x._M_slice.start();
  48.   for (size_t __i = 0;
  49.        __i < __x._M_slice.size();
  50.        ++__i, __index += __x._M_slice.stride())
  51.     (*this)[__i] = __x._M_array[__index];
  52.   return *this;
  53. }
  54.  
  55. template <class _Tp>
  56. valarray<_Tp> valarray<_Tp>::operator[](slice __slice) const {
  57.   valarray<_Tp> __tmp(__slice.size(), _NoInit());
  58.   size_t __index = __slice.start();
  59.   for (size_t __i = 0;
  60.        __i < __slice.size();
  61.        ++__i, __index += __slice.stride())
  62.     __tmp[__i] = (*this)[__index];
  63.   return __tmp;
  64. }
  65.  
  66. template <class _Size>
  67. bool _Gslice_Iter_tmpl<_Size>::_M_incr() {
  68.   size_t __dim = _M_indices.size() - 1;
  69.   ++_M_step;
  70.   while (true) {
  71.     _M_1d_idx += _M_gslice._M_strides[__dim];
  72.     if (++_M_indices[__dim] != _M_gslice._M_lengths[__dim])
  73.       return true;
  74.     else if (__dim != 0) {
  75.       _M_1d_idx -=
  76.     _M_gslice._M_strides[__dim] * _M_gslice._M_lengths[__dim];
  77.       _M_indices[__dim] = 0;
  78.       --__dim;
  79.     }
  80.     else
  81.       return false;
  82.   }
  83. }
  84.  
  85. // Behavior is undefined if __x and *this have different sizes, or if
  86. // __x was constructed from a degenerate gslice.
  87. template <class _Tp>
  88. valarray<_Tp>& valarray<_Tp>::operator=(const gslice_array<_Tp>& __x)
  89. {
  90.   if (this->size() != 0) {
  91.     _Gslice_Iter __i(__x._M_gslice);
  92.     do
  93.       (*this)[__i._M_step] = __x._M_array[__i._M_1d_idx];
  94.     while(__i._M_incr());
  95.   }
  96.   return *this;
  97. }
  98.  
  99. template <class _Tp>
  100. valarray<_Tp> valarray<_Tp>::operator[](gslice __slice) const
  101. {
  102.   valarray<_Tp> __tmp(__slice._M_size(), _NoInit());
  103.   if (__tmp.size() != 0) {
  104.     _Gslice_Iter __i(__slice);
  105.     do __tmp[__i._M_step] = (*this)[__i._M_1d_idx]; while(__i._M_incr());
  106.   }
  107.   return __tmp;
  108. }
  109.  
  110. template <class _Tp>
  111. valarray<_Tp> valarray<_Tp>::operator[](const _Valarray_bool& __mask) const
  112. {
  113.   size_t _p_size = 0;
  114.   {
  115.     for (size_t __i = 0; __i < __mask.size(); ++__i)
  116.       if (__mask[__i]) ++_p_size;
  117.   }
  118.  
  119.   valarray<_Tp> __tmp(_p_size, _NoInit());
  120.   size_t __idx = 0;
  121.   {
  122.     for (size_t __i = 0; __i < __mask.size(); ++__i)
  123.       if (__mask[__i]) __tmp[__idx++] = (*this)[__i];
  124.   }
  125.  
  126.   return __tmp;
  127. }
  128.  
  129. template <class _Tp>
  130. valarray<_Tp>& valarray<_Tp>::operator=(const indirect_array<_Tp>& __x) {
  131.   for (size_t __i = 0; __i < __x._M_addr.size(); ++__i)
  132.     (*this)[__i] = __x._M_array[__x._M_addr[__i]];
  133.   return *this;
  134. }
  135.  
  136. template <class _Tp>
  137. valarray<_Tp>
  138. valarray<_Tp>::operator[](const _Valarray_size_t& __addr) const
  139. {
  140.   valarray<_Tp> __tmp(__addr.size(), _NoInit());
  141.   for (size_t __i = 0; __i < __addr.size(); ++__i)
  142.     __tmp[__i] = (*this)[__addr[__i]];
  143.   return __tmp;
  144. }
  145.  
  146. //----------------------------------------------------------------------
  147. // Other valarray noninline member functions
  148.  
  149. // Shift and cshift
  150.  
  151. template <class _Tp>
  152. valarray<_Tp> valarray<_Tp>::shift(int __n) const
  153. {
  154.   valarray<_Tp> __tmp(this->size());
  155.  
  156.   if (__n >= 0) {
  157.     if (__n < this->size())
  158.       copy(this->_M_first + __n, this->_M_first + this->size(),
  159.            __tmp._M_first);
  160.   }
  161.   else {
  162.     if (-__n < this->size())
  163.       copy(this->_M_first, this->_M_first + this->size() + __n,
  164.            __tmp._M_first - __n);
  165.   }
  166.   return __tmp;
  167. }
  168.  
  169. template <class _Tp>
  170. valarray<_Tp> valarray<_Tp>::cshift(int __m) const
  171. {
  172.   valarray<_Tp> __tmp(this->size());
  173.  
  174.   // Reduce __m to an equivalent number in the range [0, size()).  We
  175.   // have to be careful with negative numbers, since the sign of a % b
  176.   // is unspecified when a < 0.
  177.   long __n = __m;
  178.   if (this->size() < (numeric_limits<long>::max)())
  179.     __n %= long(this->size());
  180.   if (__n < 0)
  181.     __n += this->size();
  182.  
  183.   copy(this->_M_first,       this->_M_first + __n,
  184.        __tmp._M_first + (this->size() - __n));
  185.   copy(this->_M_first + __n, this->_M_first + this->size(),
  186.        __tmp._M_first);
  187.  
  188.   return __tmp;
  189. }
  190.  
  191. _STLP_END_NAMESPACE
  192.  
  193. #endif /*  _STLP_VALARRAY_C */
  194.  
  195. // Local Variables:
  196. // mode:C++
  197. // End:
  198.