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 List obtained as a section of another List.
32 
33  Since the SubList is itself unallocated, no storage is allocated or
34  de-allocated during its use. To achieve this behaviour, SubList is
35  derived from UList rather than List.
36 
37 SourceFiles
38  SubListI.H
39 
40 \*---------------------------------------------------------------------------*/
41 
42 #ifndef Foam_SubList_H
43 #define Foam_SubList_H
44 
45 #include "List.H"
46 #include "labelRange.H"
47 
48 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
49 
50 namespace Foam
51 {
52 
53 // Forward Declarations
54 template<class T, unsigned N> class FixedList;
55 template<class T> class SubList;
56 
57 // Common list types
58 typedef SubList<bool> boolSubList;
59 typedef SubList<char> charSubList;
61 
62 
63 /*---------------------------------------------------------------------------*\
64  Class SubList Declaration
65 \*---------------------------------------------------------------------------*/
66 
67 template<class T>
68 class SubList
69 :
70  public UList<T>
71 {
72 public:
73 
74  // Static Functions
75 
76  //- Return a null SubList
77  inline static const SubList<T>& null();
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  inline operator const Foam::List<T>&() const;
176 
177  //- Copy assign entries from given sub-list. Sizes must match!
178  inline void operator=(const SubList<T>& list);
179 
180  //- Copy assign entries from given list. Sizes must match!
181  inline void operator=(const UList<T>& list);
182 
183  //- Copy assign entries from given indirect list. Sizes must match!
184  template<class Addr>
185  inline void operator=(const IndirectListBase<T, Addr>& list);
186 
187  //- Assign all entries to the given value
188  inline void operator=(const T& val);
189 
190  //- Assign all entries to zero
191  inline void operator=(const Foam::zero);
192 };
193 
194 
195 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
196 
197 } // End namespace Foam
198 
199 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
200 
201 #include "SubListI.H"
202 
203 // * * * * * * * * * * * * * * * Implementations * * * * * * * * * * * * * * //
204 
205 template<class Type>
207 Foam::UList<Type>::slice(const label pos, label len)
208 {
209  if (len < 0)
210  {
211  len = (this->size() - pos);
212  }
213  return SubList<Type>(*this, len, pos);
214 }
215 
216 
217 template<class Type>
219 Foam::UList<Type>::slice(const label pos, label len) const
220 {
221  if (len < 0)
222  {
223  len = (this->size() - pos);
224  }
225  return SubList<Type>(*this, len, pos);
226 }
227 
228 
229 template<class Type>
231 Foam::UList<Type>::slice(const labelRange& range)
232 {
233  return SubList<Type>(*this, range); // with range checking
234 }
235 
236 
237 template<class Type>
239 Foam::UList<Type>::slice(const labelRange& range) const
240 {
241  return SubList<Type>(*this, range); // with range checking
242 }
243 
244 
245 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
247 #endif
248 
249 // ************************************************************************* //
SubList< label > labelSubList
A SubList of labels.
Definition: SubList.H:55
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:54
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
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:118
Base for lists with indirect addressing, templated on the list contents type and the addressing type...
scalar range
dimensionedScalar pos(const dimensionedScalar &ds)
A List obtained as a section of another List.
Definition: SubList.H:50
SubList< T > slice(const label pos, label len=-1)
Return SubList slice (non-const access) - no range checking.
Definition: SubList.H:246
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: Scalar.H:258
label size() const noexcept
The number of elements in the container.
Definition: UList.H:671
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
const Vector< label > N(dict.get< Vector< label >>("N"))
static const SubList< T > & null()
Return a null SubList.
Definition: SubListI.H:27
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:50