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 
43 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
44 
45 namespace Foam
46 {
47 
48 // Forward Declarations
49 template<class T> class SubList;
50 
51 // Common list types
52 typedef SubList<bool> boolSubList;
53 typedef SubList<char> charSubList;
55 
56 
57 /*---------------------------------------------------------------------------*\
58  Class SubList Declaration
59 \*---------------------------------------------------------------------------*/
60 
61 template<class T>
62 class SubList
63 :
64  public UList<T>
65 {
66 public:
67 
68  // Static Functions
69 
70  //- Return a null SubList (reference to a nullObject).
71  //- Behaves like an empty SubList.
72  static const SubList<T>& null() noexcept
73  {
74  return NullObjectRef<SubList<T>>();
75  }
76 
77 
78  // Generated Methods
79 
80  //- Default construct, zero-sized and nullptr
81  SubList() noexcept = default;
82 
83  //- Copy construct, shallow copy
84  SubList(const SubList<T>&) noexcept = default;
85 
86 
87  // Constructors
88 
89  //- Construct from UList, the entire size
90  inline explicit SubList(const UList<T>& list) noexcept;
91 
92  //- Construct from FixedList, the entire size
93  template<unsigned N>
94  inline explicit SubList(const FixedList<T, N>& list) noexcept;
95 
96  //- Construct from UList and sub-list size, start at 0
97  inline SubList
98  (
99  const UList<T>& list,
100  const label len
101  );
102 
103  //- Construct from UList, sub-list size and start index
104  inline SubList
105  (
106  const UList<T>& list,
107  const label len,
108  const label start
109  );
110 
111  //- Construct from UList and a (start,size) range.
112  // The range is subsetted with the list size itself to ensure that the
113  // result always addresses a valid section of the list.
114  inline SubList
115  (
116  const UList<T>& list,
117  const labelRange& range
118  );
119 
120  //- Construct from UList and a (start,size) range,
121  //- but bypassing run-time range checking.
122  inline SubList
123  (
124  const labelRange& range,
125  const UList<T>& list
126  );
127 
128 
129  // Member Functions
130 
131  //- Reset to zero-sized and nullptr
132  inline UList<T>& reset(std::nullptr_t) noexcept;
133 
134  //- Reset to use entire UList
135  inline UList<T>& reset(const UList<T>& list) noexcept;
136 
137  //- Reset to use UList with sub-list size, start at 0
138  inline UList<T>& reset
139  (
140  const UList<T>& list,
141  const label len
142  );
143 
144  //- Reset to use UList with sub-list size and start index
145  inline UList<T>& reset
146  (
147  const UList<T>& list,
148  const label len,
149  const label start
150  );
151 
152  //- Reset to use UList with a (start,size) range.
153  // The range is subsetted with the list size itself to ensure that the
154  // result always addresses a valid section of the list.
155  inline UList<T>& reset
156  (
157  const UList<T>& list,
158  const labelRange& range
159  );
160 
161  //- Reset to use UList with a (start,size) range, but bypassing
162  //- run-time range checking.
163  inline UList<T>& reset
164  (
165  const labelRange& range,
166  const UList<T>& list
167  );
168 
169 
170  // Member Operators
171 
172  //- Allow cast to a const List<T>&
173  FOAM_DEPRECATED_STRICTER(2025-04, "dereference as SubList, not List?")
174  operator const Foam::List<T>&() const
175  {
176  return *reinterpret_cast<const List<T>*>(this);
177  }
178 
179  //- Copy assign entries (deep copy) from given sub-list.
180  //- Sizes must match!
181  inline void operator=(const SubList<T>& list);
182 
183  //- Copy assign entries (deep copy) from given list.
184  //- Sizes must match!
185  inline void operator=(const UList<T>& list);
186 
187  //- Copy assign entries from given indirect list. Sizes must match!
188  template<class Addr>
189  inline void operator=(const IndirectListBase<T, Addr>& list);
190 
191  //- Assign all entries to the given value
192  inline void operator=(const T& val);
193 
194  //- Assign all entries to zero
195  inline void operator=(Foam::zero);
196 };
197 
198 
199 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
200 
201 } // End namespace Foam
202 
203 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
204 
205 #include "SubListI.H"
206 
207 // * * * * * * * * * * * * * * * Implementations * * * * * * * * * * * * * * //
208 
209 template<class Type>
211 :
212  UList<Type>(list.data(), list.size())
213 {}
214 
215 
216 template<class Type>
218 Foam::UList<Type>::slice(const label pos, label len)
219 {
220  if (len < 0)
221  {
222  len = (this->size() - pos);
223  }
224  return SubList<Type>(*this, len, pos);
225 }
226 
227 
228 template<class Type>
230 Foam::UList<Type>::slice(const label pos, label len) const
231 {
232  if (len < 0)
233  {
234  len = (this->size() - pos);
235  }
236  return SubList<Type>(*this, len, pos);
237 }
238 
239 
240 template<class Type>
242 Foam::UList<Type>::slice(const labelRange& range)
243 {
244  return SubList<Type>(*this, range); // with range checking
245 }
246 
247 
248 template<class Type>
251 {
252  return SubList<Type>(*this, range); // with range checking
253 }
254 
255 
256 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
258 #endif
259 
260 // ************************************************************************* //
SubList< label > labelSubList
A SubList of labels.
Definition: SubList.H:49
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:48
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:217
A range or interval of labels defined by a start and a size.
Definition: labelRange.H:63
SubList() noexcept=default
Default construct, zero-sized and nullptr.
Definition: FixedList.H:920
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:44
SubList< T > slice(const label pos, label len=-1)
Return SubList slice (non-const access) - no range checking.
Definition: SubList.H:257
static const SubList< T > & null() noexcept
Return a null SubList (reference to a nullObject). Behaves like an empty SubList. ...
Definition: SubList.H:69
#define FOAM_DEPRECATED_STRICTER(since, replacement)
Definition: stdFoam.H:57
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:265
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:44