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-2021 OpenCFD Ltd.
9 -------------------------------------------------------------------------------
10 License
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/>.
25 
26 \*---------------------------------------------------------------------------*/
27 
28 // * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * //
29 
30 template<class T, class Addr>
31 template<class ListType>
32 inline 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 
63 template<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 
77 template<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 
101 template<class T, class Addr>
103 (
104  const T& val,
105  label pos
106 ) const
107 {
108  return (this->find(val, pos) >= 0);
109 }
110 
111 
112 template<class T, class Addr>
113 inline Foam::label Foam::IndirectListBase<T, Addr>::fcIndex(const label i) const
114 {
115  return (i == addr_.size()-1 ? 0 : i+1);
116 }
117 
118 
119 template<class T, class Addr>
120 inline Foam::label Foam::IndirectListBase<T, Addr>::rcIndex(const label i) const
121 {
122  return (i ? i-1 : addr_.size()-1);
123 }
124 
125 
126 template<class T, class Addr>
127 inline const T& Foam::IndirectListBase<T, Addr>::first() const
128 {
129  return values_[addr_.first()];
130 }
131 
132 template<class T, class Addr>
134 {
135  return values_[addr_.first()];
136 }
137 
138 
139 template<class T, class Addr>
140 inline const T& Foam::IndirectListBase<T, Addr>::last() const
141 {
142  return values_[addr_.last()];
143 }
144 
145 template<class T, class Addr>
147 {
148  return values_[addr_.last()];
149 }
150 
151 
152 template<class T, class Addr>
153 inline const T& Foam::IndirectListBase<T, Addr>::fcValue(const label i) const
154 {
155  return values_[this->fcIndex(i)];
156 }
157 
158 
159 template<class T, class Addr>
161 {
162  return values_[this->fcIndex(i)];
163 }
164 
165 
166 template<class T, class Addr>
167 inline const T& Foam::IndirectListBase<T, Addr>::rcValue(const label i) const
168 {
169  return values_[this->rcIndex(i)];
170 }
171 
172 
173 template<class T, class Addr>
174 inline T& Foam::IndirectListBase<T, Addr>::rcValue(const label i)
175 {
176  return values_[this->rcIndex(i)];
177 }
178 
179 
180 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
181 
182 template<class T, class Addr>
183 inline Foam::List<T>
185 {
186  const label len = addr_.size();
187 
188  List<T> result(len);
189 
190  // Or std::copy(this->cbegin(), this->cend(), result.begin());
191  for (label i = 0; i < len; ++i)
192  {
193  result[i] = values_[addr_[i]];
194  }
195 
196  return result;
197 }
198 
199 
200 template<class T, class Addr>
201 inline T& Foam::IndirectListBase<T, Addr>::operator[](const label i)
202 {
203  return values_[addr_[i]];
204 }
205 
206 
207 template<class T, class Addr>
208 inline const T&
210 {
211  return values_[addr_[i]];
212 }
213 
214 
215 template<class T, class Addr>
216 inline void Foam::IndirectListBase<T, Addr>::operator=(const T& val)
217 {
218  // Or std::fill(this->begin(), this->end(), val);
219  for (const label idx : addr_)
220  {
221  values_[idx] = val;
222  }
223 }
224 
225 
226 template<class T, class Addr>
228 {
229  // Or std::fill(this->begin(), this->end(), Zero);
230  for (const label idx : addr_)
231  {
232  values_[idx] = Zero;
233  }
234 }
235 
236 
237 template<class T, class Addr>
239 (
240  const UList<T>& rhs
241 )
242 {
243  this->copyList(rhs);
244 }
245 
246 
247 template<class T, class Addr>
249 (
250  const IndirectListBase<T, Addr>& rhs
251 )
252 {
253  this->copyList(rhs);
254 }
255 
256 
257 template<class T, class Addr>
258 template<class AnyAddr>
260 (
262 )
263 {
264  this->copyList(rhs);
265 }
266 
267 
268 // ************************************************************************* //
Foam::IndirectListBase::operator()
List< T > operator()() const
Definition: IndirectListBaseI.H:177
Foam::IndirectListBase::copyList
void copyList(const ListType &rhs)
Definition: IndirectListBaseI.H:25
Foam::Zero
static constexpr const zero Zero
Definition: zero.H:131
Foam::HashTableOps::values
List< T > values(const HashTable< T, Key, Hash > &tbl, const bool doSort=false)
Definition: HashOps.H:164
Foam::IndirectListBase::rcIndex
label rcIndex(const label i) const
Definition: IndirectListBaseI.H:113
Foam::IndirectListBase::first
const T & first() const
Definition: IndirectListBaseI.H:120
Foam::IndirectListBase::last
const T & last() const
Definition: IndirectListBaseI.H:133
Foam::IndirectListBase::operator[]
T & operator[](const label i)
Definition: IndirectListBaseI.H:194
Foam::IndirectListBase::fcIndex
label fcIndex(const label i) const
Definition: IndirectListBaseI.H:106
Foam::IndirectListBase::rcValue
const T & rcValue(const label i) const
Definition: IndirectListBaseI.H:160
Foam::IndirectListBase::IndirectListBase
IndirectListBase()=delete
Foam::FatalError
error FatalError
Foam::ListOps::find
label find(const ListType &input, const UnaryPredicate &pred, const label start=0)
Foam::abort
errorManip< error > abort(error &err)
Definition: errorManip.H:139
T
const volScalarField & T
Definition: createFieldRefs.H:2
Foam::IndirectListBase::fcValue
const T & fcValue(const label i) const
Definition: IndirectListBaseI.H:146
FatalErrorInFunction
#define FatalErrorInFunction
Definition: error.H:465
Foam::IndirectListBase::uniform
bool uniform() const
Definition: IndirectListBaseI.H:71
Foam::List< T >
Foam::UList< T >
Foam::IndirectListBase::operator=
void operator=(const T &val)
Definition: IndirectListBaseI.H:209
Foam::IndirectListBase
Base for lists with indirect addressing, templated on the list contents type and the addressing type....
Definition: IndirectListBase.H:52
Foam::IndirectListBase::found
bool found(const T &val, label pos=0) const
Definition: IndirectListBaseI.H:96
Foam::zero
A class representing the concept of 0 (zero) that can be used to avoid manipulating objects known to ...
Definition: zero.H:58
Foam::pos
dimensionedScalar pos(const dimensionedScalar &ds)
Definition: dimensionedScalar.C:170