BiIndirectListI.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) 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 // * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
30 
31 template<class T>
32 inline Foam::label Foam::BiIndirectList<T>::posIndex(const label i)
33 {
34  return i;
35 }
36 
37 
38 template<class T>
39 inline Foam::label Foam::BiIndirectList<T>::negIndex(const label i)
40 {
41  return -i-1;
42 }
43 
44 
45 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
46 
47 template<class T>
49 (
50  const UList<T>& posList,
51  const UList<T>& negList,
52  const labelUList& addr
53 )
54 :
55  posList_(const_cast<UList<T>&>(posList)),
56  negList_(const_cast<UList<T>&>(negList)),
57  addressing_(addr)
58 {}
59 
60 
61 template<class T>
63 (
64  const UList<T>& posList,
65  const UList<T>& negList,
66  labelList&& addr
67 )
68 :
69  posList_(const_cast<UList<T>&>(posList)),
70  negList_(const_cast<UList<T>&>(negList)),
71  addressing_(std::move(addr))
72 {}
73 
74 
75 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
76 
77 template<class T>
78 inline Foam::label Foam::BiIndirectList<T>::size() const noexcept
79 {
80  return addressing_.size();
81 }
82 
83 
84 template<class T>
85 inline bool Foam::BiIndirectList<T>::empty() const noexcept
86 {
87  return addressing_.empty();
88 }
89 
90 
91 template<class T>
92 inline const Foam::UList<T>& Foam::BiIndirectList<T>::posList() const noexcept
93 {
94  return posList_;
95 }
96 
97 
98 template<class T>
99 inline const Foam::UList<T>& Foam::BiIndirectList<T>::negList() const noexcept
100 {
101  return negList_;
102 }
103 
104 
105 template<class T>
106 inline const Foam::labelList&
108 {
109  return addressing_;
110 }
111 
112 
113 template<class T>
115 (
116  const labelUList& addr
117 )
118 {
119  addressing_ = addr;
120 }
121 
122 
123 template<class T>
125 (
126  labelList&& addr
127 )
128 {
129  addressing_.transfer(addr);
130 }
131 
132 
133 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
134 
135 template<class T>
137 {
138  List<T> result(size());
139 
140  forAll(*this, i)
141  {
142  result[i] = operator[](i);
143  }
144 
145  return result;
146 }
147 
148 
149 template<class T>
151 {
152  const label index = addressing_[i];
153 
154  if (index >= 0)
155  {
156  return posList_[index];
157  }
158  else
159  {
160  return negList_[-index-1];
161  }
162 }
163 
164 
165 template<class T>
166 inline const T& Foam::BiIndirectList<T>::operator[](const label i) const
167 {
168  const label index = addressing_[i];
169 
170  if (index >= 0)
171  {
172  return posList_[index];
173  }
174  else
175  {
176  return negList_[-index-1];
177  }
178 }
179 
180 
181 template<class T>
183 {
184  if (addressing_.size() != ae.size())
185  {
187  << "Addressing and list of addressed elements "
188  "have different sizes: "
189  << addressing_.size() << " " << ae.size()
190  << abort(FatalError);
191  }
192 
193  forAll(addressing_, i)
194  {
195  operator[](i) = ae[i];
196  }
197 }
198 
199 
200 template<class T>
201 inline void Foam::BiIndirectList<T>::operator=(const T& val)
202 {
203  forAll(addressing_, i)
204  {
205  operator[](i) = val;
206  }
207 }
208 
209 
210 // ************************************************************************* //
Foam::BiIndirectList::empty
bool empty() const noexcept
True if the list is empty (ie, size() is zero).
Definition: BiIndirectListI.H:85
Foam::BiIndirectList::BiIndirectList
BiIndirectList(const UList< T > &posList, const UList< T > &negList, const labelUList &addr)
Construct given the complete lists and the addressing array.
Definition: BiIndirectListI.H:49
Foam::BiIndirectList::posIndex
static label posIndex(const label i)
Calculate index given whether index is into posList or negList.
Definition: BiIndirectListI.H:32
Foam::BiIndirectList::negList
const UList< T > & negList() const noexcept
Definition: BiIndirectListI.H:99
forAll
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:296
Foam::BiIndirectList::negIndex
static label negIndex(const label i)
Definition: BiIndirectListI.H:39
Foam::BiIndirectList::operator=
void operator=(const UList< T > &ae)
Assignment to UList of addressed elements.
Definition: BiIndirectListI.H:182
Foam::BiIndirectList::operator[]
T & operator[](const label i)
Return non-const access to an element.
Definition: BiIndirectListI.H:150
Foam::BiIndirectList::operator()
List< T > operator()() const
Return the addressed elements as a List.
Definition: BiIndirectListI.H:136
Foam::BiIndirectList::addressing
const labelList & addressing() const noexcept
Return the list addressing.
Definition: BiIndirectListI.H:107
Foam::FatalError
error FatalError
Foam::BiIndirectList::size
label size() const noexcept
The number of elements in the list.
Definition: BiIndirectListI.H:78
Foam::abort
errorManip< error > abort(error &err)
Definition: errorManip.H:144
T
const volScalarField & T
Definition: createFieldRefs.H:2
Foam::BiIndirectList::resetAddressing
void resetAddressing(const labelUList &addr)
Copy reset addressing.
Definition: BiIndirectListI.H:115
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:453
Foam::List< label >
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::BiIndirectList::posList
const UList< T > & posList() const noexcept
Definition: BiIndirectListI.H:92
Foam::UList::size
void size(const label n)
Older name for setAddressableSize.
Definition: UList.H:114