2009-04-07 Andrew Stubbs <ams@codesourcery.com>
[official-gcc.git] / libstdc++-v3 / include / bits / slice_array.h
blob27e794e73a647b6cc44a26a5a49b97b75a1b1683
1 // The template and inlines for the -*- C++ -*- slice_array class.
3 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2004, 2005, 2006, 2009
4 // Free Software Foundation, Inc.
5 //
6 // This file is part of the GNU ISO C++ Library. This library is free
7 // software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the
9 // Free Software Foundation; either version 2, or (at your option)
10 // any later version.
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
17 // You should have received a copy of the GNU General Public License along
18 // with this library; see the file COPYING. If not, write to the Free
19 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
20 // USA.
22 // As a special exception, you may use this file as part of a free software
23 // library without restriction. Specifically, if other files instantiate
24 // templates or use macros or inline functions from this file, or you compile
25 // this file and link it with other files to produce an executable, this
26 // file does not by itself cause the resulting executable to be covered by
27 // the GNU General Public License. This exception does not however
28 // invalidate any other reasons why the executable file might be covered by
29 // the GNU General Public License.
31 /** @file slice_array.h
32 * This is an internal header file, included by other library headers.
33 * You should not attempt to use it directly.
36 // Written by Gabriel Dos Reis <Gabriel.Dos-Reis@DPTMaths.ENS-Cachan.Fr>
38 #ifndef _SLICE_ARRAY_H
39 #define _SLICE_ARRAY_H 1
41 #pragma GCC system_header
43 _GLIBCXX_BEGIN_NAMESPACE(std)
45 /**
46 * @addtogroup numeric_arrays
47 * @{
50 /**
51 * @brief Class defining one-dimensional subset of an array.
53 * The slice class represents a one-dimensional subset of an array,
54 * specified by three parameters: start offset, size, and stride. The
55 * start offset is the index of the first element of the array that is part
56 * of the subset. The size is the total number of elements in the subset.
57 * Stride is the distance between each successive array element to include
58 * in the subset.
60 * For example, with an array of size 10, and a slice with offset 1, size 3
61 * and stride 2, the subset consists of array elements 1, 3, and 5.
63 class slice
65 public:
66 /// Construct an empty slice.
67 slice();
69 /**
70 * @brief Construct a slice.
72 * @param o Offset in array of first element.
73 * @param d Number of elements in slice.
74 * @param s Stride between array elements.
76 slice(size_t, size_t, size_t);
78 /// Return array offset of first slice element.
79 size_t start() const;
80 /// Return size of slice.
81 size_t size() const;
82 /// Return array stride of slice.
83 size_t stride() const;
85 private:
86 size_t _M_off; // offset
87 size_t _M_sz; // size
88 size_t _M_st; // stride unit
91 // _GLIBCXX_RESOLVE_LIB_DEFECTS
92 // 543. valarray slice default constructor
93 inline
94 slice::slice()
95 : _M_off(0), _M_sz(0), _M_st(0) {}
97 inline
98 slice::slice(size_t __o, size_t __d, size_t __s)
99 : _M_off(__o), _M_sz(__d), _M_st(__s) {}
101 inline size_t
102 slice::start() const
103 { return _M_off; }
105 inline size_t
106 slice::size() const
107 { return _M_sz; }
109 inline size_t
110 slice::stride() const
111 { return _M_st; }
114 * @brief Reference to one-dimensional subset of an array.
116 * A slice_array is a reference to the actual elements of an array
117 * specified by a slice. The way to get a slice_array is to call
118 * operator[](slice) on a valarray. The returned slice_array then permits
119 * carrying operations out on the referenced subset of elements in the
120 * original valarray. For example, operator+=(valarray) will add values
121 * to the subset of elements in the underlying valarray this slice_array
122 * refers to.
124 * @param Tp Element type.
126 template<typename _Tp>
127 class slice_array
129 public:
130 typedef _Tp value_type;
132 // _GLIBCXX_RESOLVE_LIB_DEFECTS
133 // 253. valarray helper functions are almost entirely useless
135 /// Copy constructor. Both slices refer to the same underlying array.
136 slice_array(const slice_array&);
138 /// Assignment operator. Assigns slice elements to corresponding
139 /// elements of @a a.
140 slice_array& operator=(const slice_array&);
142 /// Assign slice elements to corresponding elements of @a v.
143 void operator=(const valarray<_Tp>&) const;
144 /// Multiply slice elements by corresponding elements of @a v.
145 void operator*=(const valarray<_Tp>&) const;
146 /// Divide slice elements by corresponding elements of @a v.
147 void operator/=(const valarray<_Tp>&) const;
148 /// Modulo slice elements by corresponding elements of @a v.
149 void operator%=(const valarray<_Tp>&) const;
150 /// Add corresponding elements of @a v to slice elements.
151 void operator+=(const valarray<_Tp>&) const;
152 /// Subtract corresponding elements of @a v from slice elements.
153 void operator-=(const valarray<_Tp>&) const;
154 /// Logical xor slice elements with corresponding elements of @a v.
155 void operator^=(const valarray<_Tp>&) const;
156 /// Logical and slice elements with corresponding elements of @a v.
157 void operator&=(const valarray<_Tp>&) const;
158 /// Logical or slice elements with corresponding elements of @a v.
159 void operator|=(const valarray<_Tp>&) const;
160 /// Left shift slice elements by corresponding elements of @a v.
161 void operator<<=(const valarray<_Tp>&) const;
162 /// Right shift slice elements by corresponding elements of @a v.
163 void operator>>=(const valarray<_Tp>&) const;
164 /// Assign all slice elements to @a t.
165 void operator=(const _Tp &) const;
166 // ~slice_array ();
168 template<class _Dom>
169 void operator=(const _Expr<_Dom, _Tp>&) const;
170 template<class _Dom>
171 void operator*=(const _Expr<_Dom, _Tp>&) const;
172 template<class _Dom>
173 void operator/=(const _Expr<_Dom, _Tp>&) const;
174 template<class _Dom>
175 void operator%=(const _Expr<_Dom, _Tp>&) const;
176 template<class _Dom>
177 void operator+=(const _Expr<_Dom, _Tp>&) const;
178 template<class _Dom>
179 void operator-=(const _Expr<_Dom, _Tp>&) const;
180 template<class _Dom>
181 void operator^=(const _Expr<_Dom, _Tp>&) const;
182 template<class _Dom>
183 void operator&=(const _Expr<_Dom, _Tp>&) const;
184 template<class _Dom>
185 void operator|=(const _Expr<_Dom, _Tp>&) const;
186 template<class _Dom>
187 void operator<<=(const _Expr<_Dom, _Tp>&) const;
188 template<class _Dom>
189 void operator>>=(const _Expr<_Dom, _Tp>&) const;
191 private:
192 friend class valarray<_Tp>;
193 slice_array(_Array<_Tp>, const slice&);
195 const size_t _M_sz;
196 const size_t _M_stride;
197 const _Array<_Tp> _M_array;
199 // not implemented
200 slice_array();
203 template<typename _Tp>
204 inline
205 slice_array<_Tp>::slice_array(_Array<_Tp> __a, const slice& __s)
206 : _M_sz(__s.size()), _M_stride(__s.stride()),
207 _M_array(__a.begin() + __s.start()) {}
209 template<typename _Tp>
210 inline
211 slice_array<_Tp>::slice_array(const slice_array<_Tp>& a)
212 : _M_sz(a._M_sz), _M_stride(a._M_stride), _M_array(a._M_array) {}
214 // template<typename _Tp>
215 // inline slice_array<_Tp>::~slice_array () {}
217 template<typename _Tp>
218 inline slice_array<_Tp>&
219 slice_array<_Tp>::operator=(const slice_array<_Tp>& __a)
221 std::__valarray_copy(__a._M_array, __a._M_sz, __a._M_stride,
222 _M_array, _M_stride);
223 return *this;
226 template<typename _Tp>
227 inline void
228 slice_array<_Tp>::operator=(const _Tp& __t) const
229 { std::__valarray_fill(_M_array, _M_sz, _M_stride, __t); }
231 template<typename _Tp>
232 inline void
233 slice_array<_Tp>::operator=(const valarray<_Tp>& __v) const
234 { std::__valarray_copy(_Array<_Tp>(__v), _M_array, _M_sz, _M_stride); }
236 template<typename _Tp>
237 template<class _Dom>
238 inline void
239 slice_array<_Tp>::operator=(const _Expr<_Dom,_Tp>& __e) const
240 { std::__valarray_copy(__e, _M_sz, _M_array, _M_stride); }
242 #undef _DEFINE_VALARRAY_OPERATOR
243 #define _DEFINE_VALARRAY_OPERATOR(_Op,_Name) \
244 template<typename _Tp> \
245 inline void \
246 slice_array<_Tp>::operator _Op##=(const valarray<_Tp>& __v) const \
248 _Array_augmented_##_Name(_M_array, _M_sz, _M_stride, _Array<_Tp>(__v));\
251 template<typename _Tp> \
252 template<class _Dom> \
253 inline void \
254 slice_array<_Tp>::operator _Op##=(const _Expr<_Dom,_Tp>& __e) const\
256 _Array_augmented_##_Name(_M_array, _M_stride, __e, _M_sz); \
260 _DEFINE_VALARRAY_OPERATOR(*, __multiplies)
261 _DEFINE_VALARRAY_OPERATOR(/, __divides)
262 _DEFINE_VALARRAY_OPERATOR(%, __modulus)
263 _DEFINE_VALARRAY_OPERATOR(+, __plus)
264 _DEFINE_VALARRAY_OPERATOR(-, __minus)
265 _DEFINE_VALARRAY_OPERATOR(^, __bitwise_xor)
266 _DEFINE_VALARRAY_OPERATOR(&, __bitwise_and)
267 _DEFINE_VALARRAY_OPERATOR(|, __bitwise_or)
268 _DEFINE_VALARRAY_OPERATOR(<<, __shift_left)
269 _DEFINE_VALARRAY_OPERATOR(>>, __shift_right)
271 #undef _DEFINE_VALARRAY_OPERATOR
273 // @} group numeric_arrays
275 _GLIBCXX_END_NAMESPACE
277 #endif /* _SLICE_ARRAY_H */