libstdc++
gslice.h
Go to the documentation of this file.
1 // The template and inlines for the -*- C++ -*- gslice class.
2 
3 // Copyright (C) 1997-2025 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 3, 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 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
19 
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
24 
25 /** @file bits/gslice.h
26  * This is an internal header file, included by other library headers.
27  * Do not attempt to use it directly. @headername{valarray}
28  */
29 
30 // Written by Gabriel Dos Reis <Gabriel.Dos-Reis@DPTMaths.ENS-Cachan.Fr>
31 
32 #ifndef _GSLICE_H
33 #define _GSLICE_H 1
34 
35 #ifdef _GLIBCXX_SYSHDR
36 #pragma GCC system_header
37 #endif
38 
39 namespace std _GLIBCXX_VISIBILITY(default)
40 {
41 _GLIBCXX_BEGIN_NAMESPACE_VERSION
42 
43  /**
44  * @addtogroup numeric_arrays
45  * @{
46  */
47 
48  /**
49  * @brief Class defining multi-dimensional subset of an array.
50  *
51  * The slice class represents a multi-dimensional subset of an array,
52  * specified by three parameter sets: start offset, size array, and stride
53  * array. The start offset is the index of the first element of the array
54  * that is part of the subset. The size and stride array describe each
55  * dimension of the slice. Size is the number of elements in that
56  * dimension, and stride is the distance in the array between successive
57  * elements in that dimension. Each dimension's size and stride is taken
58  * to begin at an array element described by the previous dimension. The
59  * size array and stride array must be the same size.
60  *
61  * For example, if you have offset==3, stride[0]==11, size[1]==3,
62  * stride[1]==3, then slice[0,0]==array[3], slice[0,1]==array[6],
63  * slice[0,2]==array[9], slice[1,0]==array[14], slice[1,1]==array[17],
64  * slice[1,2]==array[20].
65  */
66  class gslice
67  {
68  public:
69  /// Construct an empty slice.
70  gslice();
71 
72  /**
73  * @brief Construct a slice.
74  *
75  * Constructs a slice with as many dimensions as the length of the @a l
76  * and @a s arrays.
77  *
78  * @param __o Offset in array of first element.
79  * @param __l Array of dimension lengths.
80  * @param __s Array of dimension strides between array elements.
81  */
82  gslice(size_t __o, const valarray<size_t>& __l,
83  const valarray<size_t>& __s);
84 
85  // XXX: the IS says the copy-ctor and copy-assignment operators are
86  // synthesized by the compiler but they are just unsuitable
87  // for a ref-counted semantic
88  /// Copy constructor.
89  gslice(const gslice&);
90 
91  /// Destructor.
92  ~gslice();
93 
94  // XXX: See the note above.
95  /// Assignment operator.
96  gslice& operator=(const gslice&);
97 
98  /// Return array offset of first slice element.
99  size_t start() const;
100 
101  /// Return array of sizes of slice dimensions.
102  valarray<size_t> size() const;
103 
104  /// Return array of array strides for each dimension.
105  valarray<size_t> stride() const;
106 
107  private:
108  struct _Indexer
109  {
110  size_t _M_count;
111  size_t _M_start;
112  valarray<size_t> _M_size;
113  valarray<size_t> _M_stride;
114  valarray<size_t> _M_index; // Linear array of referenced indices
115 
116  _Indexer()
117  : _M_count(1), _M_start(0), _M_size(), _M_stride(), _M_index() {}
118 
119  _Indexer(size_t, const valarray<size_t>&,
120  const valarray<size_t>&);
121 
122  void
123  _M_increment_use()
124  { ++_M_count; }
125 
126  size_t
127  _M_decrement_use()
128  { return --_M_count; }
129  };
130 
131  _Indexer* _M_index;
132 
133  template<typename _Tp> friend class valarray;
134  };
135 
136  inline size_t
138  { return _M_index ? _M_index->_M_start : 0; }
139 
140  inline valarray<size_t>
141  gslice::size() const
142  { return _M_index ? _M_index->_M_size : valarray<size_t>(); }
143 
144  inline valarray<size_t>
146  { return _M_index ? _M_index->_M_stride : valarray<size_t>(); }
147 
148  // _GLIBCXX_RESOLVE_LIB_DEFECTS
149  // 543. valarray slice default constructor
150  inline
152  : _M_index(new gslice::_Indexer()) {}
153 
154  inline
155  gslice::gslice(size_t __o, const valarray<size_t>& __l,
156  const valarray<size_t>& __s)
157  : _M_index(new gslice::_Indexer(__o, __l, __s)) {}
158 
159  inline
160  gslice::gslice(const gslice& __g)
161  : _M_index(__g._M_index)
162  { if (_M_index) _M_index->_M_increment_use(); }
163 
164  inline
166  {
167  if (_M_index && _M_index->_M_decrement_use() == 0)
168  delete _M_index;
169  }
170 
171  inline gslice&
173  {
174  // Safe for self-assignment. Checking for it would add overhead just to
175  // optimize a case that should never happen anyway.
176  if (__g._M_index)
177  __g._M_index->_M_increment_use();
178  if (_M_index && _M_index->_M_decrement_use() == 0)
179  delete _M_index;
180  _M_index = __g._M_index;
181  return *this;
182  }
183 
184  /// @} group numeric_arrays
185 
186 _GLIBCXX_END_NAMESPACE_VERSION
187 } // namespace
188 
189 #endif /* _GSLICE_H */
~gslice()
Destructor.
Definition: gslice.h:165
gslice()
Construct an empty slice.
Definition: gslice.h:151
gslice & operator=(const gslice &)
Assignment operator.
Definition: gslice.h:172
size_t start() const
Return array offset of first slice element.
Definition: gslice.h:137
valarray< size_t > stride() const
Return array of array strides for each dimension.
Definition: gslice.h:145
valarray< size_t > size() const
Return array of sizes of slice dimensions.
Definition: gslice.h:141
ISO C++ entities toplevel namespace is std.
Class defining multi-dimensional subset of an array.
Definition: gslice.h:67