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) 2011-2015 OpenFOAM Foundation
9  Copyright (C) 2018-2019 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 \*---------------------------------------------------------------------------*/
28 
29 // * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * //
30 
31 template<class T, class Addr>
32 template<class ListType>
33 inline void Foam::IndirectListBase<T, Addr>::copyList(const ListType& rhs)
34 {
35  const label len = addr_.size();
36 
37  if (len != rhs.size())
38  {
40  << "Addressing and list of addressed elements "
41  "have different sizes: " << len << " " << rhs.size()
42  << abort(FatalError);
43  }
44 
45  // Or std::copy(rhs.cbegin(), rhs.cend(), this->begin());
46  for (label i = 0; i < len; ++i)
47  {
48  values_[addr_[i]] = rhs[i];
49  }
50 }
51 
52 
53 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
54 
55 template<class T, class Addr>
57 (
58  const UList<T>& values,
59  const Addr& addr
60 )
61 :
62  values_(const_cast<UList<T>&>(values)),
63  addr_(addr)
64 {}
65 
66 
67 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
68 
69 template<class T, class Addr>
71 {
72  const label len = this->size();
73 
74  if (!len)
75  {
76  return false;
77  }
78 
79  const T& val = (*this)[0];
80 
81  for (label i=1; i<len; ++i)
82  {
83  if (val != (*this)[i])
84  {
85  return false;
86  }
87  }
88 
89  return true;
90 }
91 
92 
93 template<class T, class Addr>
95 (
96  const T& val,
97  const label pos
98 ) const
99 {
100  return this->find(val, pos) != -1;
101 }
102 
103 
104 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
105 
106 template<class T, class Addr>
107 inline Foam::List<T>
109 {
110  const label len = addr_.size();
111 
112  List<T> result(len);
113 
114  // Or std::copy(this->cbegin(), this->cend(), result.begin());
115  for (label i = 0; i < len; ++i)
116  {
117  result[i] = values_[addr_[i]];
118  }
119 
120  return result;
121 }
122 
123 
124 template<class T, class Addr>
126 {
127  return values_[addr_[i]];
128 }
129 
130 
131 template<class T, class Addr>
132 inline const T&
134 {
135  return values_[addr_[i]];
136 }
137 
138 
139 template<class T, class Addr>
141 {
142  // Or std::fill(this->begin(), this->end(), val);
143  for (const label idx : addr_)
144  {
145  values_[idx] = val;
146  }
147 }
148 
149 
150 template<class T, class Addr>
152 {
153  // Or std::fill(this->begin(), this->end(), Zero);
154  for (const label idx : addr_)
155  {
156  values_[idx] = Zero;
157  }
158 }
159 
160 
161 template<class T, class Addr>
163 (
164  const UList<T>& rhs
165 )
166 {
167  this->copyList(rhs);
168 }
169 
170 
171 template<class T, class Addr>
173 (
174  const IndirectListBase<T, Addr>& rhs
175 )
176 {
177  this->copyList(rhs);
178 }
179 
180 
181 template<class T, class Addr>
182 template<class AnyAddr>
184 (
186 )
187 {
188  this->copyList(rhs);
189 }
190 
191 
192 // ************************************************************************* //
Foam::val
label ListType::const_reference val
Definition: ListOps.H:407
Foam::IndirectListBase::operator()
List< T > operator()() const
Return the addressed elements as a List.
Definition: IndirectListBaseI.H:108
Foam::IndirectListBase::found
bool found(const T &val, const label start=0) const
True if the value if found in the list. Linear search.
Definition: IndirectListBaseI.H:95
Foam::IndirectListBase::copyList
void copyList(const ListType &rhs)
Copy values The number of elements in the list.
Definition: IndirectListBaseI.H:33
Foam::Zero
static constexpr const zero Zero
Global zero.
Definition: zero.H:128
Foam::HashTableOps::values
List< T > values(const HashTable< T, Key, Hash > &tbl, const bool doSort=false)
List of values from HashTable, optionally sorted.
Definition: HashOps.H:149
Foam::IndirectListBase::operator[]
T & operator[](const label i)
Non-const access to an element in the list.
Definition: IndirectListBaseI.H:125
Foam::label
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:62
Foam::IndirectListBase::IndirectListBase
IndirectListBase()=delete
No null construct.
Foam::FatalError
error FatalError
Foam::ListOps::find
label find(const ListType &input, const UnaryPredicate &pred, const label start=0)
Find index of the first occurrence that satisfies the predicate.
Foam::abort
errorManip< error > abort(error &err)
Definition: errorManip.H:137
T
const volScalarField & T
Definition: createFieldRefs.H:2
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:355
Foam::IndirectListBase::uniform
bool uniform() const
True if all entries have identical values, and list is non-empty.
Definition: IndirectListBaseI.H:70
Foam::List
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: HashTable.H:102
Foam::UList
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:103
Foam::IndirectListBase::operator=
void operator=(const T &val)
Assign all addressed elements to the given value.
Definition: IndirectListBaseI.H:140
Foam::IndirectListBase
Base for lists with indirect addressing, templated on the list contents type and the addressing type....
Definition: IndirectListBase.H:57
Foam::zero
A class representing the concept of 0 (zero), which can be used to avoid manipulating objects that ar...
Definition: zero.H:61
Foam::pos
dimensionedScalar pos(const dimensionedScalar &ds)
Definition: dimensionedScalar.C:177