5 * Hewlett-Packard Company
7 * Copyright (c) 1996,1997
8 * Silicon Graphics Computer Systems, Inc.
11 * Moscow Center for SPARC Technology
16 * This material is provided "as is", with absolutely no warranty expressed
17 * or implied. Any use is at your own risk.
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.
26 #ifndef _STLP_VALARRAY_C
27 #define _STLP_VALARRAY_C
29 #ifndef _STLP_VALARRAY_H
30 # include <stl/_valarray.h>
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];
43 // Behavior is undefined if __x and *this have different sizes
45 valarray<_Tp>& valarray<_Tp>::operator=(const slice_array<_Tp>& __x)
47 size_t __index = __x._M_slice.start();
49 __i < __x._M_slice.size();
50 ++__i, __index += __x._M_slice.stride())
51 (*this)[__i] = __x._M_array[__index];
56 valarray<_Tp> valarray<_Tp>::operator[](slice __slice) const {
57 valarray<_Tp> __tmp(__slice.size(), _NoInit());
58 size_t __index = __slice.start();
61 ++__i, __index += __slice.stride())
62 __tmp[__i] = (*this)[__index];
66 template <class _Size>
67 bool _Gslice_Iter_tmpl<_Size>::_M_incr() {
68 size_t __dim = _M_indices.size() - 1;
71 _M_1d_idx += _M_gslice._M_strides[__dim];
72 if (++_M_indices[__dim] != _M_gslice._M_lengths[__dim])
74 else if (__dim != 0) {
75 _M_1d_idx -= _M_gslice._M_strides[__dim] * _M_gslice._M_lengths[__dim];
76 _M_indices[__dim] = 0;
84 // Behavior is undefined if __x and *this have different sizes, or if
85 // __x was constructed from a degenerate gslice.
87 valarray<_Tp>& valarray<_Tp>::operator=(const gslice_array<_Tp>& __x)
89 if (this->size() != 0) {
90 _Gslice_Iter __i(__x._M_gslice);
92 (*this)[__i._M_step] = __x._M_array[__i._M_1d_idx];
99 valarray<_Tp> valarray<_Tp>::operator[](const gslice& __slice) const
101 valarray<_Tp> __tmp(__slice._M_size(), _NoInit());
102 if (__tmp.size() != 0) {
103 _Gslice_Iter __i(__slice);
104 do __tmp[__i._M_step] = (*this)[__i._M_1d_idx]; while(__i._M_incr());
110 valarray<_Tp> valarray<_Tp>::operator[](const _Valarray_bool& __mask) const
114 for (size_t __i = 0; __i < __mask.size(); ++__i)
115 if (__mask[__i]) ++_p_size;
118 valarray<_Tp> __tmp(_p_size, _NoInit());
121 for (size_t __i = 0; __i < __mask.size(); ++__i)
122 if (__mask[__i]) __tmp[__idx++] = (*this)[__i];
129 valarray<_Tp>& valarray<_Tp>::operator=(const indirect_array<_Tp>& __x) {
130 for (size_t __i = 0; __i < __x._M_addr.size(); ++__i)
131 (*this)[__i] = __x._M_array[__x._M_addr[__i]];
137 valarray<_Tp>::operator[](const _Valarray_size_t& __addr) const
139 valarray<_Tp> __tmp(__addr.size(), _NoInit());
140 for (size_t __i = 0; __i < __addr.size(); ++__i)
141 __tmp[__i] = (*this)[__addr[__i]];
145 //----------------------------------------------------------------------
146 // Other valarray noninline member functions
151 valarray<_Tp> valarray<_Tp>::shift(int __n) const
153 valarray<_Tp> __tmp(this->size());
156 if (__n < this->size())
157 copy(this->_M_first + __n, this->_M_first + this->size(),
161 if (-__n < this->size())
162 copy(this->_M_first, this->_M_first + this->size() + __n,
163 __tmp._M_first - __n);
169 valarray<_Tp> valarray<_Tp>::cshift(int __m) const
171 valarray<_Tp> __tmp(this->size());
173 if( this->size() == 0 )
176 // Reduce __m to an equivalent number in the range [0, size()). We
177 // have to be careful with negative numbers, since the sign of a % b
178 // is unspecified when a < 0.
180 if (this->size() < (numeric_limits<long>::max)())
181 __n %= long(this->size());
185 copy(this->_M_first, this->_M_first + __n,
186 __tmp._M_first + (this->size() - __n));
187 copy(this->_M_first + __n, this->_M_first + this->size(),
195 #endif /* _STLP_VALARRAY_C */