OSDN Git Service

2004-02-02 Jerry Quinn <jlquinn@optonline.net>
[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 Free Software Foundation, Inc.
4 //
5 // This file is part of the GNU ISO C++ Library.  This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 2, or (at your option)
9 // any later version.
10
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 // GNU General Public License for more details.
15
16 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING.  If not, write to the Free
18 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
19 // USA.
20
21 // As a special exception, you may use this file as part of a free software
22 // library without restriction.  Specifically, if other files instantiate
23 // templates or use macros or inline functions from this file, or you compile
24 // this file and link it with other files to produce an executable, this
25 // file does not by itself cause the resulting executable to be covered by
26 // the GNU General Public License.  This exception does not however
27 // invalidate any other reasons why the executable file might be covered by
28 // the GNU General Public License.
29
30 // Written by Gabriel Dos Reis <Gabriel.Dos-Reis@DPTMaths.ENS-Cachan.Fr>
31
32 /** @file slice_array.h
33  *  This is an internal header file, included by other library headers.
34  *  You should not attempt to use it directly.
35  */
36
37 #ifndef _SLICE_ARRAY_H
38 #define _SLICE_ARRAY_H 1
39
40 #pragma GCC system_header
41
42 namespace std
43 {
44   /**
45    *  @brief  Class defining one-dimensional subset of an array.
46    *
47    *  The slice class represents a one-dimensional subset of an array,
48    *  specified by three parameters: start offset, size, and stride.  The
49    *  start offset is the index of the first element of the array that is part
50    *  of the subset.  The size is the total number of elements in the subset.
51    *  Stride is the distance between each successive array element to include
52    *  in the subset.
53    *
54    *  For example, with an array of size 10, and a slice with offset 1, size 3
55    *  and stride 2, the subset consists of array elements 1, 3, and 5.
56    */
57   class slice
58   {
59   public:
60     ///  Construct an empty slice.
61     slice();
62
63     /**
64      *  @brief  Construct a slice.
65      *
66      *  @param  o  Offset in array of first element.
67      *  @param  d  Number of elements in slice.
68      *  @param  s  Stride between array elements.
69      */
70     slice(size_t, size_t, size_t);
71     
72     ///  Return array offset of first slice element.
73     size_t start() const;
74     ///  Return size of slice.
75     size_t size() const;
76     ///  Return array stride of slice.
77     size_t stride() const;
78     
79   private:
80     size_t _M_off;                      // offset
81     size_t _M_sz;                       // size
82     size_t _M_st;                       // stride unit
83   };
84
85   // The default constructor constructor is not required to initialize
86   // data members with any meaningful values, so we choose to do nothing.
87   inline 
88   slice::slice() {}
89   
90   inline 
91   slice::slice(size_t __o, size_t __d, size_t __s)
92     : _M_off(__o), _M_sz(__d), _M_st(__s) {}
93   
94   inline size_t
95   slice::start() const
96   { return _M_off; }
97   
98   inline size_t
99   slice::size() const
100   { return _M_sz; }
101   
102   inline size_t
103   slice::stride() const
104   { return _M_st; }
105
106   /**
107    *  @brief  Reference to one-dimensional subset of an array.
108    *
109    *  A slice_array is a reference to the actual elements of an array
110    *  specified by a slice.  The way to get a slice_array is to call
111    *  operator[](slice) on a valarray.  The returned slice_array then permits
112    *  carrying operations out on the referenced subset of elements in the
113    *  original valarray.  For example, operator+=(valarray) will add values
114    *  to the subset of elements in the underlying valarray this slice_array
115    *  refers to.
116    *
117    *  @param  Tp  Element type.
118    */
119   template<typename _Tp>
120     class slice_array
121     {
122     public:
123       typedef _Tp value_type;
124
125       // This constructor is implemented since we need to return a value.
126       ///  Copy constructor.  Both slices refer to the same underlying array.
127       slice_array(const slice_array&);
128
129       // This operator must be public.  See DR-253.
130       ///  Assignment operator.  Assigns slice elements to corresponding
131       ///  elements of @a a.
132       slice_array& operator=(const slice_array&);
133
134       ///  Assign slice elements to corresponding elements of @a v.
135       void operator=(const valarray<_Tp>&) const;
136       ///  Multiply slice elements by corresponding elements of @a v.
137       void operator*=(const valarray<_Tp>&) const;
138       ///  Divide slice elements by corresponding elements of @a v.
139       void operator/=(const valarray<_Tp>&) const;
140       ///  Modulo slice elements by corresponding elements of @a v.
141       void operator%=(const valarray<_Tp>&) const;
142       ///  Add corresponding elements of @a v to slice elements.
143       void operator+=(const valarray<_Tp>&) const;
144       ///  Subtract corresponding elements of @a v from slice elements.
145       void operator-=(const valarray<_Tp>&) const;
146       ///  Logical xor slice elements with corresponding elements of @a v.
147       void operator^=(const valarray<_Tp>&) const;
148       ///  Logical and slice elements with corresponding elements of @a v.
149       void operator&=(const valarray<_Tp>&) const;
150       ///  Logical or slice elements with corresponding elements of @a v.
151       void operator|=(const valarray<_Tp>&) const;
152       ///  Left shift slice elements by corresponding elements of @a v.
153       void operator<<=(const valarray<_Tp>&) const;
154       ///  Right shift slice elements by corresponding elements of @a v.
155       void operator>>=(const valarray<_Tp>&) const;
156       ///  Assign all slice elements to @a t.
157       void operator=(const _Tp &) const;
158       //        ~slice_array ();
159
160       template<class _Dom>
161         void operator=(const _Expr<_Dom,_Tp>&) const;
162       template<class _Dom>
163         void operator*=(const _Expr<_Dom,_Tp>&) const;
164       template<class _Dom>
165         void operator/=(const _Expr<_Dom,_Tp>&) const;
166       template<class _Dom>
167         void operator%=(const _Expr<_Dom,_Tp>&) const;
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
183     private:
184       friend class valarray<_Tp>;
185       slice_array(_Array<_Tp>, const slice&);
186
187       const size_t     _M_sz;
188       const size_t     _M_stride;
189       const _Array<_Tp> _M_array;
190
191       // not implemented
192       slice_array();
193     };
194
195   template<typename _Tp>
196     inline 
197     slice_array<_Tp>::slice_array(_Array<_Tp> __a, const slice& __s)
198       : _M_sz(__s.size()), _M_stride(__s.stride()),
199         _M_array(__a.begin() + __s.start()) {}
200
201   template<typename _Tp>
202     inline 
203     slice_array<_Tp>::slice_array(const slice_array<_Tp>& a)
204       : _M_sz(a._M_sz), _M_stride(a._M_stride), _M_array(a._M_array) {}
205     
206   //    template<typename _Tp>
207   //    inline slice_array<_Tp>::~slice_array () {}
208
209   template<typename _Tp>
210     inline slice_array<_Tp>&
211     slice_array<_Tp>::operator=(const slice_array<_Tp>& __a)
212     {
213       std::__valarray_copy(__a._M_array, __a._M_sz, __a._M_stride,
214                            _M_array, _M_stride);
215       return *this;
216     }
217
218   template<typename _Tp>
219     inline void
220     slice_array<_Tp>::operator=(const _Tp& __t) const
221     { std::__valarray_fill(_M_array, _M_sz, _M_stride, __t); }
222     
223   template<typename _Tp>
224     inline void
225     slice_array<_Tp>::operator=(const valarray<_Tp>& __v) const
226     { std::__valarray_copy(_Array<_Tp>(__v), _M_array, _M_sz, _M_stride); }
227     
228   template<typename _Tp>
229   template<class _Dom>
230     inline void
231     slice_array<_Tp>::operator=(const _Expr<_Dom,_Tp>& __e) const
232     { std::__valarray_copy(__e, _M_sz, _M_array, _M_stride); }
233
234 #undef _DEFINE_VALARRAY_OPERATOR
235 #define _DEFINE_VALARRAY_OPERATOR(_Op,_Name)                            \
236   template<typename _Tp>                                                \
237     inline void                                                         \
238     slice_array<_Tp>::operator _Op##=(const valarray<_Tp>& __v) const   \
239     {                                                                   \
240       _Array_augmented_##_Name(_M_array, _M_sz, _M_stride, _Array<_Tp>(__v));\
241     }                                                                   \
242                                                                         \
243   template<typename _Tp>                                                \
244     template<class _Dom>                                                \
245       inline void                                                       \
246       slice_array<_Tp>::operator _Op##=(const _Expr<_Dom,_Tp>& __e) const\
247       {                                                                 \
248           _Array_augmented_##_Name(_M_array, _M_stride, __e, _M_sz);    \
249       }
250         
251
252 _DEFINE_VALARRAY_OPERATOR(*, __multiplies)
253 _DEFINE_VALARRAY_OPERATOR(/, __divides)
254 _DEFINE_VALARRAY_OPERATOR(%, __modulus)
255 _DEFINE_VALARRAY_OPERATOR(+, __plus)
256 _DEFINE_VALARRAY_OPERATOR(-, __minus)
257 _DEFINE_VALARRAY_OPERATOR(^, __bitwise_xor)
258 _DEFINE_VALARRAY_OPERATOR(&, __bitwise_and)
259 _DEFINE_VALARRAY_OPERATOR(|, __bitwise_or)
260 _DEFINE_VALARRAY_OPERATOR(<<, __shift_left)
261 _DEFINE_VALARRAY_OPERATOR(>>, __shift_right)
262
263 #undef _DEFINE_VALARRAY_OPERATOR
264
265 } // std::
266
267 #endif /* _SLICE_ARRAY_H */
268
269 // Local Variables:
270 // mode:c++
271 // End: