Loading...
Searching...
No Matches
IndirectListBaseI.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) 2018-2023 OpenCFD Ltd.
9-------------------------------------------------------------------------------
10License
11 This file is part of OpenFOAM.
12
13 OpenFOAM is free software: you can redistribute it and/or modify it
14 under the terms of the GNU General Public License as published by
15 the Free Software Foundation, either version 3 of the License, or
16 (at your option) any later version.
17
18 OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
19 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 for more details.
22
23 You should have received a copy of the GNU General Public License
24 along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26\*---------------------------------------------------------------------------*/
27
28// * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * //
29
30template<class T, class Addr>
31template<class ListType>
32inline void Foam::IndirectListBase<T, Addr>::copyList(const ListType& rhs)
33{
34 if
35 (
36 this
37 == reinterpret_cast<IndirectListBase<T,Addr>*>(const_cast<ListType*>(&rhs))
38 )
39 {
40 return; // Self-assignment is a no-op
41 }
42
43 const label len = addr_.size();
44
45 if (len != rhs.size())
46 {
48 << "Addressing and list of addressed elements "
49 "have different sizes: " << len << " " << rhs.size()
50 << abort(FatalError);
51 }
52
53 // Or std::copy(rhs.cbegin(), rhs.cend(), this->begin());
54 for (label i = 0; i < len; ++i)
55 {
56 values_[addr_[i]] = rhs[i];
57 }
58}
59
60
61// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
62
63template<class T, class Addr>
65(
66 const UList<T>& values,
67 const Addr& addr
68)
69:
70 values_(const_cast<UList<T>&>(values)),
71 addr_(addr)
72{}
73
74
75// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
76
77template<class T, class Addr>
79{
80 const label len = this->size();
81
82 if (!len)
83 {
84 return false;
85 }
86
87 const T& val = (*this)[0]; // first
88
89 for (label i = 1; i < len; ++i)
90 {
91 if (val != (*this)[i])
92 {
93 return false;
94 }
95 }
96
97 return true;
98}
99
100
101template<class T, class Addr>
103(
104 const T& val,
105 label pos,
106 label len
107) const
108{
109 return (this->find(val, pos, len) >= 0);
110}
111
112
113template<class T, class Addr>
114inline Foam::label Foam::IndirectListBase<T, Addr>::fcIndex(const label i) const
115{
116 return (i == addr_.size()-1 ? 0 : i+1);
117}
118
119
120template<class T, class Addr>
121inline Foam::label Foam::IndirectListBase<T, Addr>::rcIndex(const label i) const
122{
123 return (i ? i-1 : addr_.size()-1);
124}
125
126
127template<class T, class Addr>
128inline const T& Foam::IndirectListBase<T, Addr>::front() const
129{
130 return values_[addr_.front()];
131}
132
133template<class T, class Addr>
135{
136 return values_[addr_.front()];
137}
138
139
140template<class T, class Addr>
141inline const T& Foam::IndirectListBase<T, Addr>::back() const
142{
143 return values_[addr_.back()];
144}
145
146template<class T, class Addr>
148{
149 return values_[addr_.back()];
150}
151
152
153template<class T, class Addr>
154inline const T& Foam::IndirectListBase<T, Addr>::fcValue(const label i) const
155{
156 return values_[this->fcIndex(i)];
157}
158
159
160template<class T, class Addr>
162{
163 return values_[this->fcIndex(i)];
164}
165
166
167template<class T, class Addr>
168inline const T& Foam::IndirectListBase<T, Addr>::rcValue(const label i) const
169{
170 return values_[this->rcIndex(i)];
171}
172
173
174template<class T, class Addr>
175inline T& Foam::IndirectListBase<T, Addr>::rcValue(const label i)
176{
177 return values_[this->rcIndex(i)];
178}
180template<class T, class Addr>
182{
183 const label len = addr_.size();
185 List<T> result(len);
186
187 // Or std::copy(this->cbegin(), this->cend(), result.begin());
188 for (label i = 0; i < len; ++i)
189 {
190 result[i] = values_[addr_[i]];
191 }
192
193 return result;
195
196
197// * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
198
199template<class T, class Addr>
202 return values_[addr_[i]];
203}
204
206template<class T, class Addr>
207inline const T&
209{
210 return values_[addr_[i]];
212
213
214template<class T, class Addr>
215inline void Foam::IndirectListBase<T, Addr>::operator=(const T& val)
217 // Or std::fill(this->begin(), this->end(), val);
218 for (const label idx : addr_)
220 values_[idx] = val;
221 }
222}
223
224
225template<class T, class Addr>
227{
228 // Or std::fill(this->begin(), this->end(), Zero);
229 for (const label idx : addr_)
231 values_[idx] = Zero;
232 }
233}
234
235
236template<class T, class Addr>
238(
239 const UList<T>& rhs
241{
242 this->copyList(rhs);
243}
244
245
246template<class T, class Addr>
248(
250)
252 this->copyList(rhs);
253}
254
255
256template<class T, class Addr>
257template<class AnyAddr>
259(
261)
262{
263 this->copyList(rhs);
264}
265
266
267// ************************************************************************* //
Base for lists with indirect addressing, templated on the list contents type and the addressing type....
bool contains(const T &val, label pos=0, label len=-1) const
Is the value contained in the list?
void copyList(const ListType &rhs)
Deep copy values from the list.
label rcIndex(const label i) const
The reverse circular index. The previous index in the list which returns to the last at the beginning...
const T & front() const
The first element of the list.
void operator=(const T &val)
Assign all addressed elements to the given value.
const T & rcValue(const label i) const
Return reverse circular value (ie, previous value in the list).
label fcIndex(const label i) const
The forward circular index. The next index in the list which returns to the first at the end of the l...
const T & fcValue(const label i) const
Return forward circular value (ie, next value in the list).
IndirectListBase()=delete
No default construct.
const T & back() const
The last element of the list.
bool uniform() const
True if all entries have identical values, and list is non-empty.
List< T > list() const
Return the addressed elements as a List.
const UList< T > & values() const noexcept
The list of values (without addressing).
label find(const T &val, label pos=0, label len=-1) const
Find index of the first occurrence of the value.
label size() const noexcept
The number of elements in the list.
T & operator[](const label i)
Non-const access to an element in the list.
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition List.H:72
A 1D vector of objects of type <T>, where the size of the vector is known and can be used for subscri...
Definition UList.H:89
void size(const label n)
Older name for setAddressableSize.
Definition UList.H:118
A class representing the concept of 0 (zero) that can be used to avoid manipulating objects known to ...
Definition zero.H:58
const volScalarField & T
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition error.H:600
dimensionedScalar pos(const dimensionedScalar &ds)
errorManip< error > abort(error &err)
Definition errorManip.H:139
void rhs(fvMatrix< typename Expr::value_type > &m, const Expr &expression)
static constexpr const zero Zero
Global zero (0).
Definition zero.H:127
error FatalError
Error stream (stdout output on all processes), with additional 'FOAM FATAL ERROR' header text and sta...
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)