OSDN Git Service

2004-02-02 Jerry Quinn <jlquinn@optonline.net>
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / include / bits / gslice.h
1 // The template and inlines for the -*- C++ -*- gslice class.
2
3 // Copyright (C) 1997, 1998, 1999, 2000, 2001 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 gslice.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 _GSLICE_H
38 #define _GSLICE_H 1
39
40 #pragma GCC system_header
41
42 namespace std {
43     
44   /**
45    *  @brief  Class defining multi-dimensional subset of an array.
46    *
47    *  The slice class represents a multi-dimensional subset of an array,
48    *  specified by three parameter sets: start offset, size array, and stride
49    *  array.  The start offset is the index of the first element of the array
50    *  that is part of the subset.  The size and stride array describe each
51    *  dimension of the slice.  Size is the number of elements in that
52    *  dimension, and stride is the distance in the array between successive
53    *  elements in that dimension.  Each dimension's size and stride is taken
54    *  to begin at an array element described by the previous dimension.  The
55    *  size array and stride array must be the same size.
56    *
57    *  For example, if you have offset==3, stride[0]==11, size[1]==3,
58    *  stride[1]==3, then slice[0,0]==array[3], slice[0,1]==array[6],
59    *  slice[0,2]==array[9], slice[1,0]==array[14], slice[1,1]==array[17],
60    *  slice[1,2]==array[20].
61    */
62     class gslice
63     {
64     public:
65       ///  Construct an empty slice.
66       gslice ();
67
68       /**
69        *  @brief  Construct a slice.
70        *
71        *  Constructs a slice with as many dimensions as the length of the @a l
72        *  and @a s arrays.
73        *
74        *  @param  o  Offset in array of first element.
75        *  @param  l  Array of dimension lengths.
76        *  @param  s  Array of dimension strides between array elements.
77        */
78       gslice(size_t, const valarray<size_t>&, const valarray<size_t>&);
79
80       // XXX: the IS says the copy-ctor and copy-assignment operators are
81       //      synthetized by the compiler but they are just unsuitable
82       //      for a ref-counted semantic
83       ///  Copy constructor.
84       gslice(const gslice&);
85
86       ///  Destructor.
87       ~gslice();
88
89       // XXX: See the note above.
90       ///  Assignment operator.
91       gslice& operator=(const gslice&);
92         
93       ///  Return array offset of first slice element.
94       size_t           start() const;
95
96       ///  Return array of sizes of slice dimensions.
97       valarray<size_t> size() const;
98
99       ///  Return array of array strides for each dimension.
100       valarray<size_t> stride() const;
101         
102     private:
103       struct _Indexer {
104         size_t _M_count;
105         size_t _M_start;
106         valarray<size_t> _M_size;
107         valarray<size_t> _M_stride;
108         valarray<size_t> _M_index;
109         _Indexer(size_t, const valarray<size_t>&,
110                  const valarray<size_t>&);
111         void _M_increment_use() { ++_M_count; }
112         size_t _M_decrement_use() { return --_M_count; }
113       };
114
115       _Indexer* _M_index;
116         
117       template<typename _Tp> friend class valarray;
118     };
119     
120     inline size_t
121     gslice::start () const
122     { return _M_index ? _M_index->_M_start : 0; }
123     
124     inline valarray<size_t>
125     gslice::size () const
126     { return _M_index ? _M_index->_M_size : valarray<size_t>(); }
127     
128     inline valarray<size_t>
129     gslice::stride () const
130     { return _M_index ? _M_index->_M_stride : valarray<size_t>(); }
131     
132     inline gslice::gslice () : _M_index(0) {}
133
134     inline
135     gslice::gslice(size_t __o, const valarray<size_t>& __l,
136                    const valarray<size_t>& __s)
137             : _M_index(new gslice::_Indexer(__o, __l, __s)) {}
138
139     inline
140     gslice::gslice(const gslice& __g) : _M_index(__g._M_index)
141     { if (_M_index) _M_index->_M_increment_use(); }
142     
143     inline
144     gslice::~gslice()
145     { if (_M_index && _M_index->_M_decrement_use() == 0) delete _M_index; }
146
147     inline gslice&
148     gslice::operator= (const gslice& __g)
149     {
150         if (__g._M_index) __g._M_index->_M_increment_use();
151         if (_M_index && _M_index->_M_decrement_use() == 0) delete _M_index;
152         _M_index = __g._M_index;
153         return *this;
154     }
155             
156     
157 } // std::
158
159
160 #endif /* _GSLICE_H */
161
162 // Local Variables:
163 // mode:c++
164 // End: