_valarray.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. #ifndef _STLP_VALARRAY_H
  29. # include <stl/_valarray.h>
  30. #endif
  31. _STLP_BEGIN_NAMESPACE
  32. template <class _Tp>
  33. _Valarray_bool valarray<_Tp>:: operator!() const {
  34. _Valarray_bool __tmp(this->size(), _Valarray_bool::_NoInit());
  35. for (size_t __i = 0; __i < this->size(); ++__i)
  36. __tmp[__i] = !(*this)[__i];
  37. return __tmp;
  38. }
  39. // Behavior is undefined if __x and *this have different sizes
  40. template <class _Tp>
  41. valarray<_Tp>& valarray<_Tp>::operator=(const slice_array<_Tp>& __x) {
  42. _STLP_ASSERT(__x._M_slice.size() == this->size())
  43. size_t __index = __x._M_slice.start();
  44. for (size_t __i = 0;
  45. __i < __x._M_slice.size();
  46. ++__i, __index += __x._M_slice.stride())
  47. (*this)[__i] = __x._M_array[__index];
  48. return *this;
  49. }
  50. template <class _Tp>
  51. valarray<_Tp> valarray<_Tp>::operator[](slice __slice) const {
  52. valarray<_Tp> __tmp(__slice.size(), _NoInit());
  53. size_t __index = __slice.start();
  54. for (size_t __i = 0;
  55. __i < __slice.size();
  56. ++__i, __index += __slice.stride())
  57. __tmp[__i] = (*this)[__index];
  58. return __tmp;
  59. }
  60. template <class _Size>
  61. bool _Gslice_Iter_tmpl<_Size>::_M_incr() {
  62. size_t __dim = _M_indices.size() - 1;
  63. ++_M_step;
  64. for (;;) {
  65. _M_1d_idx += _M_gslice._M_strides[__dim];
  66. if (++_M_indices[__dim] != _M_gslice._M_lengths[__dim])
  67. return true;
  68. else if (__dim != 0) {
  69. _M_1d_idx -= _M_gslice._M_strides[__dim] * _M_gslice._M_lengths[__dim];
  70. _M_indices[__dim] = 0;
  71. --__dim;
  72. }
  73. else
  74. return false;
  75. }
  76. }
  77. // Behavior is undefined if __x and *this have different sizes, or if
  78. // __x was constructed from a degenerate gslice.
  79. template <class _Tp>
  80. valarray<_Tp>& valarray<_Tp>::operator=(const gslice_array<_Tp>& __x) {
  81. if (this->size() != 0) {
  82. _Gslice_Iter __i(__x._M_gslice);
  83. do
  84. (*this)[__i._M_step] = __x._M_array[__i._M_1d_idx];
  85. while(__i._M_incr());
  86. }
  87. return *this;
  88. }
  89. template <class _Tp>
  90. valarray<_Tp> valarray<_Tp>::operator[](const gslice& __slice) const {
  91. valarray<_Tp> __tmp(__slice._M_size(), _NoInit());
  92. if (__tmp.size() != 0) {
  93. _Gslice_Iter __i(__slice);
  94. do __tmp[__i._M_step] = (*this)[__i._M_1d_idx]; while(__i._M_incr());
  95. }
  96. return __tmp;
  97. }
  98. template <class _Tp>
  99. valarray<_Tp> valarray<_Tp>::operator[](const _Valarray_bool& __mask) const {
  100. size_t _p_size = 0;
  101. {
  102. for (size_t __i = 0; __i < __mask.size(); ++__i)
  103. if (__mask[__i]) ++_p_size;
  104. }
  105. valarray<_Tp> __tmp(_p_size, _NoInit());
  106. size_t __idx = 0;
  107. {
  108. for (size_t __i = 0; __i < __mask.size(); ++__i)
  109. if (__mask[__i]) __tmp[__idx++] = (*this)[__i];
  110. }
  111. return __tmp;
  112. }
  113. template <class _Tp>
  114. valarray<_Tp>& valarray<_Tp>::operator=(const indirect_array<_Tp>& __x) {
  115. _STLP_ASSERT(__x._M_addr.size() == this->size())
  116. for (size_t __i = 0; __i < __x._M_addr.size(); ++__i)
  117. (*this)[__i] = __x._M_array[__x._M_addr[__i]];
  118. return *this;
  119. }
  120. template <class _Tp>
  121. valarray<_Tp>
  122. valarray<_Tp>::operator[](const _Valarray_size_t& __addr) const {
  123. valarray<_Tp> __tmp(__addr.size(), _NoInit());
  124. for (size_t __i = 0; __i < __addr.size(); ++__i)
  125. __tmp[__i] = (*this)[__addr[__i]];
  126. return __tmp;
  127. }
  128. //----------------------------------------------------------------------
  129. // Other valarray noninline member functions
  130. // Shift and cshift
  131. template <class _Tp>
  132. valarray<_Tp> valarray<_Tp>::shift(int __n) const {
  133. valarray<_Tp> __tmp(this->size());
  134. if (__n >= 0) {
  135. if (__n < this->size())
  136. copy(this->_M_first + __n, this->_M_first + this->size(),
  137. __tmp._M_first);
  138. }
  139. else {
  140. if (-__n < this->size())
  141. copy(this->_M_first, this->_M_first + this->size() + __n,
  142. __tmp._M_first - __n);
  143. }
  144. return __tmp;
  145. }
  146. template <class _Tp>
  147. valarray<_Tp> valarray<_Tp>::cshift(int __m) const {
  148. valarray<_Tp> __tmp(this->size());
  149. if (this->size() == 0)
  150. return __tmp;
  151. // Reduce __m to an equivalent number in the range [0, size()). We
  152. // have to be careful with negative numbers, since the sign of a % b
  153. // is unspecified when a < 0.
  154. long __n = __m;
  155. if (this->size() < (numeric_limits<long>::max)())
  156. __n %= long(this->size());
  157. if (__n < 0)
  158. __n += this->size();
  159. copy(this->_M_first, this->_M_first + __n,
  160. __tmp._M_first + (this->size() - __n));
  161. copy(this->_M_first + __n, this->_M_first + this->size(),
  162. __tmp._M_first);
  163. return __tmp;
  164. }
  165. _STLP_END_NAMESPACE
  166. #endif /* _STLP_VALARRAY_C */
  167. // Local Variables:
  168. // mode:C++
  169. // End: