1 // The template and inlines for the -*- C++ -*- gslice class.
3 // Copyright (C) 1997, 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
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)
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.
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,
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.
30 // Written by Gabriel Dos Reis <Gabriel.Dos-Reis@DPTMaths.ENS-Cachan.Fr>
33 * This is an internal header file, included by other library headers.
34 * You should not attempt to use it directly.
40 #pragma GCC system_header
45 * @brief Class defining multi-dimensional subset of an array.
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.
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].
65 /// Construct an empty slice.
69 * @brief Construct a slice.
71 * Constructs a slice with as many dimensions as the length of the @a l
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.
78 gslice(size_t, const valarray
<size_t>&, const valarray
<size_t>&);
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
84 gslice(const gslice
&);
89 // XXX: See the note above.
90 /// Assignment operator.
91 gslice
& operator=(const gslice
&);
93 /// Return array offset of first slice element.
96 /// Return array of sizes of slice dimensions.
97 valarray
<size_t> size() const;
99 /// Return array of array strides for each dimension.
100 valarray
<size_t> stride() const;
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
; }
117 template<typename _Tp
> friend class valarray
;
121 gslice::start () const
122 { return _M_index
? _M_index
->_M_start
: 0; }
124 inline valarray
<size_t>
125 gslice::size () const
126 { return _M_index
? _M_index
->_M_size
: valarray
<size_t>(); }
128 inline valarray
<size_t>
129 gslice::stride () const
130 { return _M_index
? _M_index
->_M_stride
: valarray
<size_t>(); }
132 inline gslice::gslice () : _M_index(0) {}
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
)) {}
140 gslice::gslice(const gslice
& __g
) : _M_index(__g
._M_index
)
141 { if (_M_index
) _M_index
->_M_increment_use(); }
145 { if (_M_index
&& _M_index
->_M_decrement_use() == 0) delete _M_index
; }
148 gslice::operator= (const gslice
& __g
)
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
;
160 #endif /* _GSLICE_H */