OSDN Git Service

Remove trailing whitespace (see ChangeLog for longwinded description).
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / include / bits / slice_array.h
1 // The template and inlines for the -*- C++ -*- slice_array class.
2
3 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2004
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.
11
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.
16
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, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
20 // USA.
21
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.
30
31 // Written by Gabriel Dos Reis <Gabriel.Dos-Reis@DPTMaths.ENS-Cachan.Fr>
32
33 /** @file slice_array.h
34  *  This is an internal header file, included by other library headers.
35  *  You should not attempt to use it directly.
36  */
37
38 #ifndef _SLICE_ARRAY_H
39 #define _SLICE_ARRAY_H 1
40
41 #pragma GCC system_header
42
43 namespace std
44 {
45   /**
46    *  @brief  Class defining one-dimensional subset of an array.
47    *
48    *  The slice class represents a one-dimensional subset of an array,
49    *  specified by three parameters: start offset, size, and stride.  The
50    *  start offset is the index of the first element of the array that is part
51    *  of the subset.  The size is the total number of elements in the subset.
52    *  Stride is the distance between each successive array element to include
53    *  in the subset.
54    *
55    *  For example, with an array of size 10, and a slice with offset 1, size 3
56    *  and stride 2, the subset consists of array elements 1, 3, and 5.
57    */
58   class slice
59   {
60   public:
61     ///  Construct an empty slice.
62     slice();
63
64     /**
65      *  @brief  Construct a slice.
66      *
67      *  @param  o  Offset in array of first element.
68      *  @param  d  Number of elements in slice.
69      *  @param  s  Stride between array elements.
70      */
71     slice(size_t, size_t, size_t);
72
73     ///  Return array offset of first slice element.
74     size_t start() const;
75     ///  Return size of slice.
76     size_t size() const;
77     ///  Return array stride of slice.
78     size_t stride() const;
79
80   private:
81     size_t _M_off;                      // offset
82     size_t _M_sz;                       // size
83     size_t _M_st;                       // stride unit
84   };
85
86   // The default constructor constructor is not required to initialize
87   // data members with any meaningful values, so we choose to do nothing.
88   inline
89   slice::slice() {}
90
91   inline
92   slice::slice(size_t __o, size_t __d, size_t __s)
93     : _M_off(__o), _M_sz(__d), _M_st(__s) {}
94
95   inline size_t
96   slice::start() const
97   { return _M_off; }
98
99   inline size_t
100   slice::size() const
101   { return _M_sz; }
102
103   inline size_t
104   slice::stride() const
105   { return _M_st; }
106
107   /**
108    *  @brief  Reference to one-dimensional subset of an array.
109    *
110    *  A slice_array is a reference to the actual elements of an array
111    *  specified by a slice.  The way to get a slice_array is to call
112    *  operator[](slice) on a valarray.  The returned slice_array then permits
113    *  carrying operations out on the referenced subset of elements in the
114    *  original valarray.  For example, operator+=(valarray) will add values
115    *  to the subset of elements in the underlying valarray this slice_array
116    *  refers to.
117    *
118    *  @param  Tp  Element type.
119    */
120   template<typename _Tp>
121     class slice_array
122     {
123     public:
124       typedef _Tp value_type;
125
126       // This constructor is implemented since we need to return a value.
127       ///  Copy constructor.  Both slices refer to the same underlying array.
128       slice_array(const slice_array&);
129
130       // This operator must be public.  See DR-253.
131       ///  Assignment operator.  Assigns slice elements to corresponding
132       ///  elements of @a a.
133       slice_array& operator=(const slice_array&);
134
135       ///  Assign slice elements to corresponding elements of @a v.
136       void operator=(const valarray<_Tp>&) const;
137       ///  Multiply slice elements by corresponding elements of @a v.
138       void operator*=(const valarray<_Tp>&) const;
139       ///  Divide slice elements by corresponding elements of @a v.
140       void operator/=(const valarray<_Tp>&) const;
141       ///  Modulo slice elements by corresponding elements of @a v.
142       void operator%=(const valarray<_Tp>&) const;
143       ///  Add corresponding elements of @a v to slice elements.
144       void operator+=(const valarray<_Tp>&) const;
145       ///  Subtract corresponding elements of @a v from slice elements.
146       void operator-=(const valarray<_Tp>&) const;
147       ///  Logical xor slice elements with corresponding elements of @a v.
148       void operator^=(const valarray<_Tp>&) const;
149       ///  Logical and slice elements with corresponding elements of @a v.
150       void operator&=(const valarray<_Tp>&) const;
151       ///  Logical or slice elements with corresponding elements of @a v.
152       void operator|=(const valarray<_Tp>&) const;
153       ///  Left shift slice elements by corresponding elements of @a v.
154       void operator<<=(const valarray<_Tp>&) const;
155       ///  Right shift slice elements by corresponding elements of @a v.
156       void operator>>=(const valarray<_Tp>&) const;
157       ///  Assign all slice elements to @a t.
158       void operator=(const _Tp &) const;
159       //        ~slice_array ();
160
161       template<class _Dom>
162         void operator=(const _Expr<_Dom,_Tp>&) const;
163       template<class _Dom>
164         void operator*=(const _Expr<_Dom,_Tp>&) const;
165       template<class _Dom>
166         void operator/=(const _Expr<_Dom,_Tp>&) const;
167       template<class _Dom>
168         void operator%=(const _Expr<_Dom,_Tp>&) const;
169       template<class _Dom>
170         void operator+=(const _Expr<_Dom,_Tp>&) const;
171       template<class _Dom>
172         void operator-=(const _Expr<_Dom,_Tp>&) const;
173       template<class _Dom>
174         void operator^=(const _Expr<_Dom,_Tp>&) const;
175       template<class _Dom>
176         void operator&=(const _Expr<_Dom,_Tp>&) const;
177       template<class _Dom>
178         void operator|=(const _Expr<_Dom,_Tp>&) const;
179       template<class _Dom>
180         void operator<<=(const _Expr<_Dom,_Tp>&) const;
181       template<class _Dom>
182         void operator>>=(const _Expr<_Dom,_Tp>&) const;
183
184     private:
185       friend class valarray<_Tp>;
186       slice_array(_Array<_Tp>, const slice&);
187
188       const size_t     _M_sz;
189       const size_t     _M_stride;
190       const _Array<_Tp> _M_array;
191
192       // not implemented
193       slice_array();
194     };
195
196   template<typename _Tp>
197     inline
198     slice_array<_Tp>::slice_array(_Array<_Tp> __a, const slice& __s)
199       : _M_sz(__s.size()), _M_stride(__s.stride()),
200         _M_array(__a.begin() + __s.start()) {}
201
202   template<typename _Tp>
203     inline
204     slice_array<_Tp>::slice_array(const slice_array<_Tp>& a)
205       : _M_sz(a._M_sz), _M_stride(a._M_stride), _M_array(a._M_array) {}
206
207   //    template<typename _Tp>
208   //    inline slice_array<_Tp>::~slice_array () {}
209
210   template<typename _Tp>
211     inline slice_array<_Tp>&
212     slice_array<_Tp>::operator=(const slice_array<_Tp>& __a)
213     {
214       std::__valarray_copy(__a._M_array, __a._M_sz, __a._M_stride,
215                            _M_array, _M_stride);
216       return *this;
217     }
218
219   template<typename _Tp>
220     inline void
221     slice_array<_Tp>::operator=(const _Tp& __t) const
222     { std::__valarray_fill(_M_array, _M_sz, _M_stride, __t); }
223
224   template<typename _Tp>
225     inline void
226     slice_array<_Tp>::operator=(const valarray<_Tp>& __v) const
227     { std::__valarray_copy(_Array<_Tp>(__v), _M_array, _M_sz, _M_stride); }
228
229   template<typename _Tp>
230   template<class _Dom>
231     inline void
232     slice_array<_Tp>::operator=(const _Expr<_Dom,_Tp>& __e) const
233     { std::__valarray_copy(__e, _M_sz, _M_array, _M_stride); }
234
235 #undef _DEFINE_VALARRAY_OPERATOR
236 #define _DEFINE_VALARRAY_OPERATOR(_Op,_Name)                            \
237   template<typename _Tp>                                                \
238     inline void                                                         \
239     slice_array<_Tp>::operator _Op##=(const valarray<_Tp>& __v) const   \
240     {                                                                   \
241       _Array_augmented_##_Name(_M_array, _M_sz, _M_stride, _Array<_Tp>(__v));\
242     }                                                                   \
243                                                                         \
244   template<typename _Tp>                                                \
245     template<class _Dom>                                                \
246       inline void                                                       \
247       slice_array<_Tp>::operator _Op##=(const _Expr<_Dom,_Tp>& __e) const\
248       {                                                                 \
249           _Array_augmented_##_Name(_M_array, _M_stride, __e, _M_sz);    \
250       }
251
252
253 _DEFINE_VALARRAY_OPERATOR(*, __multiplies)
254 _DEFINE_VALARRAY_OPERATOR(/, __divides)
255 _DEFINE_VALARRAY_OPERATOR(%, __modulus)
256 _DEFINE_VALARRAY_OPERATOR(+, __plus)
257 _DEFINE_VALARRAY_OPERATOR(-, __minus)
258 _DEFINE_VALARRAY_OPERATOR(^, __bitwise_xor)
259 _DEFINE_VALARRAY_OPERATOR(&, __bitwise_and)
260 _DEFINE_VALARRAY_OPERATOR(|, __bitwise_or)
261 _DEFINE_VALARRAY_OPERATOR(<<, __shift_left)
262 _DEFINE_VALARRAY_OPERATOR(>>, __shift_right)
263
264 #undef _DEFINE_VALARRAY_OPERATOR
265
266 } // std::
267
268 #endif /* _SLICE_ARRAY_H */
269
270 // Local Variables:
271 // mode:c++
272 // End: