SubList.H
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2011-2016 OpenFOAM Foundation
9  Copyright (C) 2017-2023 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
12  This file is part of OpenFOAM.
13 
14  OpenFOAM is free software: you can redistribute it and/or modify it
15  under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26 
27 Class
28  Foam::SubList
29 
30 Description
31  A non-owning sub-view of a List (allocated or unallocated storage).
32 
33 SourceFiles
34  SubListI.H
35 
36 \*---------------------------------------------------------------------------*/
37 
38 #ifndef Foam_SubList_H
39 #define Foam_SubList_H
40 
41 #include "List.H"
42 #include "labelRange.H"
43 
44 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
45 
46 namespace Foam
47 {
48 
49 // Forward Declarations
50 template<class T, unsigned N> class FixedList;
51 template<class T> class SubList;
52 
53 // Common list types
54 typedef SubList<bool> boolSubList;
55 typedef SubList<char> charSubList;
57 
58 
59 /*---------------------------------------------------------------------------*\
60  Class SubList Declaration
61 \*---------------------------------------------------------------------------*/
62 
63 template<class T>
64 class SubList
65 :
66  public UList<T>
67 {
68 public:
69 
70  // Static Functions
71 
72  //- Return a null SubList (reference to a nullObject).
73  //- Behaves like an empty SubList.
74  static const SubList<T>& null() noexcept
75  {
76  return NullObjectRef<SubList<T>>();
77  }
78 
79 
80  // Generated Methods
81 
82  //- Default construct, zero-sized and nullptr
83  SubList() noexcept = default;
84 
85  //- Copy construct, shallow copy
86  SubList(const SubList<T>&) noexcept = default;
87 
88 
89  // Constructors
90 
91  //- Construct from UList, the entire size
92  inline explicit SubList(const UList<T>& list) noexcept;
93 
94  //- Construct from FixedList, the entire size
95  template<unsigned N>
96  inline explicit SubList(const FixedList<T, N>& list) noexcept;
97 
98  //- Construct from UList and sub-list size, start at 0
99  inline SubList
100  (
101  const UList<T>& list,
102  const label len
103  );
104 
105  //- Construct from UList, sub-list size and start index
106  inline SubList
107  (
108  const UList<T>& list,
109  const label len,
110  const label start
111  );
112 
113  //- Construct from UList and a (start,size) range.
114  // The range is subsetted with the list size itself to ensure that the
115  // result always addresses a valid section of the list.
116  inline SubList
117  (
118  const UList<T>& list,
119  const labelRange& range
120  );
121 
122  //- Construct from UList and a (start,size) range,
123  //- but bypassing run-time range checking.
124  inline SubList
125  (
126  const labelRange& range,
127  const UList<T>& list
128  );
129 
130 
131  // Member Functions
132 
133  //- Reset to zero-sized and nullptr
134  inline UList<T>& reset(std::nullptr_t) noexcept;
135 
136  //- Reset to use entire UList
137  inline UList<T>& reset(const UList<T>& list) noexcept;
138 
139  //- Reset to use UList with sub-list size, start at 0
140  inline UList<T>& reset
141  (
142  const UList<T>& list,
143  const label len
144  );
145 
146  //- Reset to use UList with sub-list size and start index
147  inline UList<T>& reset
148  (
149  const UList<T>& list,
150  const label len,
151  const label start
152  );
153 
154  //- Reset to use UList with a (start,size) range.
155  // The range is subsetted with the list size itself to ensure that the
156  // result always addresses a valid section of the list.
157  inline UList<T>& reset
158  (
159  const UList<T>& list,
160  const labelRange& range
161  );
162 
163  //- Reset to use UList with a (start,size) range, but bypassing
164  //- run-time range checking.
165  inline UList<T>& reset
166  (
167  const labelRange& range,
168  const UList<T>& list
169  );
170 
171 
172  // Member Operators
173 
174  //- Allow cast to a const List<T>&
175  FOAM_DEPRECATED_STRICTER(2025-04, "dereference as SubList, not List?")
176  operator const Foam::List<T>&() const
177  {
178  return *reinterpret_cast<const List<T>*>(this);
179  }
180 
181  //- Copy assign entries (deep copy) from given sub-list.
182  //- Sizes must match!
183  inline void operator=(const SubList<T>& list);
184 
185  //- Copy assign entries (deep copy) from given list.
186  //- Sizes must match!
187  inline void operator=(const UList<T>& list);
188 
189  //- Copy assign entries from given indirect list. Sizes must match!
190  template<class Addr>
191  inline void operator=(const IndirectListBase<T, Addr>& list);
192 
193  //- Assign all entries to the given value
194  inline void operator=(const T& val);
195 
196  //- Assign all entries to zero
197  inline void operator=(Foam::zero);
198 };
199 
200 
201 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
202 
203 } // End namespace Foam
204 
205 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
206 
207 #include "SubListI.H"
208 
209 // * * * * * * * * * * * * * * * Implementations * * * * * * * * * * * * * * //
210 
211 template<class Type>
213 :
214  UList<Type>(list.data(), list.size())
215 {}
216 
217 
218 template<class Type>
220 Foam::UList<Type>::slice(const label pos, label len)
221 {
222  if (len < 0)
223  {
224  len = (this->size() - pos);
225  }
226  return SubList<Type>(*this, len, pos);
227 }
228 
229 
230 template<class Type>
232 Foam::UList<Type>::slice(const label pos, label len) const
233 {
234  if (len < 0)
235  {
236  len = (this->size() - pos);
237  }
238  return SubList<Type>(*this, len, pos);
239 }
240 
241 
242 template<class Type>
244 Foam::UList<Type>::slice(const labelRange& range)
245 {
246  return SubList<Type>(*this, range); // with range checking
247 }
248 
249 
250 template<class Type>
253 {
254  return SubList<Type>(*this, range); // with range checking
255 }
256 
257 
258 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
260 #endif
261 
262 // ************************************************************************* //
SubList< label > labelSubList
A SubList of labels.
Definition: SubList.H:51
A 1D vector of objects of type <T> with a fixed length <N>.
Definition: HashTable.H:107
SubList< char > charSubList
A SubList of chars.
Definition: SubList.H:50
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: BitOps.H:56
friend class SubList< T >
Declare friendship with the SubList class.
Definition: UList.H:212
A range or interval of labels defined by a start and a size.
Definition: labelRange.H:52
SubList() noexcept=default
Default construct, zero-sized and nullptr.
UList< T > & reset(std::nullptr_t) noexcept
Reset to zero-sized and nullptr.
Definition: SubListI.H:109
scalar range
void operator=(const SubList< T > &list)
Copy assign entries (deep copy) from given sub-list. Sizes must match!
Definition: SubListI.H:211
dimensionedScalar pos(const dimensionedScalar &ds)
A non-owning sub-view of a List (allocated or unallocated storage).
Definition: SubList.H:46
SubList< T > slice(const label pos, label len=-1)
Return SubList slice (non-const access) - no range checking.
Definition: SubList.H:259
static const SubList< T > & null() noexcept
Return a null SubList (reference to a nullObject). Behaves like an empty SubList. ...
Definition: SubList.H:71
#define FOAM_DEPRECATED_STRICTER(since, replacement)
Definition: stdFoam.H:56
A 1D vector of objects of type <T>, where the size of the vector is known and can be used for subscri...
Definition: HashTable.H:105
const direction noexcept
Definition: scalarImpl.H:255
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
const Vector< label > N(dict.get< Vector< label >>("N"))
constexpr UList() noexcept
Default construct, zero-sized and nullptr.
Definition: UListI.H:28
A class representing the concept of 0 (zero) that can be used to avoid manipulating objects known to ...
Definition: zero.H:57
Namespace for OpenFOAM.
SubList< bool > boolSubList
A SubList of bools.
Definition: SubList.H:46