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 (reference to a nullObject).
77  //- Behaves like an empty SubList.
78  static const SubList<T>& null() noexcept
79  {
80  return NullObjectRef<SubList<T>>();
81  }
82 
83 
84  // Generated Methods
85 
86  //- Default construct, zero-sized and nullptr
87  SubList() noexcept = default;
88 
89  //- Copy construct, shallow copy
90  SubList(const SubList<T>&) noexcept = default;
91 
92 
93  // Constructors
94 
95  //- Construct from UList, the entire size
96  inline explicit SubList(const UList<T>& list) noexcept;
97 
98  //- Construct from FixedList, the entire size
99  template<unsigned N>
100  inline explicit SubList(const FixedList<T, N>& list) noexcept;
101 
102  //- Construct from UList and sub-list size, start at 0
103  inline SubList
104  (
105  const UList<T>& list,
106  const label len
107  );
108 
109  //- Construct from UList, sub-list size and start index
110  inline SubList
111  (
112  const UList<T>& list,
113  const label len,
114  const label start
115  );
116 
117  //- Construct from UList and a (start,size) range.
118  // The range is subsetted with the list size itself to ensure that the
119  // result always addresses a valid section of the list.
120  inline SubList
121  (
122  const UList<T>& list,
123  const labelRange& range
124  );
125 
126  //- Construct from UList and a (start,size) range,
127  //- but bypassing run-time range checking.
128  inline SubList
129  (
130  const labelRange& range,
131  const UList<T>& list
132  );
133 
134 
135  // Member Functions
136 
137  //- Reset to zero-sized and nullptr
138  inline UList<T>& reset(std::nullptr_t) noexcept;
139 
140  //- Reset to use entire UList
141  inline UList<T>& reset(const UList<T>& list) noexcept;
142 
143  //- Reset to use UList with sub-list size, start at 0
144  inline UList<T>& reset
145  (
146  const UList<T>& list,
147  const label len
148  );
149 
150  //- Reset to use UList with sub-list size and start index
151  inline UList<T>& reset
152  (
153  const UList<T>& list,
154  const label len,
155  const label start
156  );
157 
158  //- Reset to use UList with a (start,size) range.
159  // The range is subsetted with the list size itself to ensure that the
160  // result always addresses a valid section of the list.
161  inline UList<T>& reset
162  (
163  const UList<T>& list,
164  const labelRange& range
165  );
166 
167  //- Reset to use UList with a (start,size) range, but bypassing
168  //- run-time range checking.
169  inline UList<T>& reset
170  (
171  const labelRange& range,
172  const UList<T>& list
173  );
174 
175 
176  // Member Operators
177 
178  //- Allow cast to a const List<T>&
179  inline operator const Foam::List<T>&() const;
180 
181  //- Copy assign entries from given sub-list. Sizes must match!
182  inline void operator=(const SubList<T>& list);
183 
184  //- Copy assign entries from given list. 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=(const 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 Foam::UList<Type>::slice(const label pos, label len)
212 {
213  if (len < 0)
214  {
215  len = (this->size() - pos);
216  }
217  return SubList<Type>(*this, len, pos);
218 }
219 
220 
221 template<class Type>
223 Foam::UList<Type>::slice(const label pos, label len) const
224 {
225  if (len < 0)
226  {
227  len = (this->size() - pos);
228  }
229  return SubList<Type>(*this, len, pos);
230 }
231 
232 
233 template<class Type>
235 Foam::UList<Type>::slice(const labelRange& range)
236 {
237  return SubList<Type>(*this, range); // with range checking
238 }
239 
240 
241 template<class Type>
243 Foam::UList<Type>::slice(const labelRange& range) const
244 {
245  return SubList<Type>(*this, range); // with range checking
246 }
247 
248 
249 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
251 #endif
252 
253 // ************************************************************************* //
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:109
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:250
static const SubList< T > & null() noexcept
Return a null SubList (reference to a nullObject). Behaves like an empty SubList. ...
Definition: SubList.H:75
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:680
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
const Vector< label > N(dict.get< Vector< label >>("N"))
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