FixedList.C
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) 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 \*---------------------------------------------------------------------------*/
28 
29 #include "FixedList.H"
30 
31 // * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
32 
33 template<class T, unsigned N>
34 std::streamsize Foam::FixedList<T, N>::byteSize()
35 {
37  {
39  << "Invalid for non-contiguous data types"
40  << abort(FatalError);
41  }
43 }
44 
45 
46 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
47 
48 template<class T, unsigned N>
49 Foam::label Foam::FixedList<T, N>::find(const T& val) const
50 {
51  const auto iter = std::find(this->cbegin(), this->cend(), val);
52  return (iter != this->cend() ? label(iter - this->cbegin()) : label(-1));
53 }
54 
55 
56 template<class T, unsigned N>
58 (
59  const T& val,
60  label pos,
61  label len
62 ) const
63 {
64  if (pos >= 0 && pos < label(N))
65  {
66  // Change sub-length to (one-past) end position
67  // len == -1 (like std::string::npos) - search until end
68 
69  if (len > 0) len += pos;
70  if (len < 0 || len > label(N))
71  {
72  len = label(N);
73  }
74 
75  const auto iter = std::find
76  (
77  (this->cbegin() + pos),
78  (this->cbegin() + len),
79  val
80  );
81 
82  if (iter != (this->cbegin() + len))
83  {
84  return label(iter - this->cbegin());
85  }
86  }
87 
88  return -1;
89 }
90 
91 
92 template<class T, unsigned N>
93 Foam::label Foam::FixedList<T, N>::rfind(const T& val, label pos) const
94 {
95  // pos == -1 (like std::string::npos) - search from end
96 
97  if (pos < 0 || pos >= label(N))
98  {
99  pos = label(N)-1;
100  }
101 
102  while (pos >= 0)
103  {
104  if (this->v_[pos] == val)
105  {
106  return pos;
107  }
108 
109  --pos;
110  }
111 
112  return -1;
113 }
114 
115 
116 template<class T, unsigned N>
117 void Foam::FixedList<T, N>::moveFirst(const label i)
118 {
119  checkIndex(i);
120 
121  for (label lower = 0; lower < i; ++lower)
122  {
123  Foam::Swap(v_[lower], v_[i]);
124  }
125 }
126 
127 
128 template<class T, unsigned N>
129 void Foam::FixedList<T, N>::moveLast(const label i)
130 {
131  checkIndex(i);
132 
133  for (label upper = label(N-1); upper > i; --upper)
134  {
135  Foam::Swap(v_[i], v_[upper]);
136  }
137 }
138 
139 
140 template<class T, unsigned N>
141 void Foam::FixedList<T, N>::swapFirst(const label i)
142 {
143  checkIndex(i);
144 
145  if (i > 0)
146  {
147  Foam::Swap(v_[0], v_[i]);
148  }
149 }
150 
151 
152 template<class T, unsigned N>
153 void Foam::FixedList<T, N>::swapLast(const label i)
154 {
155  checkIndex(i);
156 
157  const label upper = label(N-1);
158 
159  if (i < upper)
160  {
161  Foam::Swap(v_[i], v_[upper]);
162  }
163 }
164 
165 
166 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
167 
168 template<class T, unsigned N>
169 bool Foam::FixedList<T, N>::operator==(const FixedList<T, N>& list) const
170 {
171  // Can dispatch with
172  // - std::execution::parallel_unsequenced_policy
173  // - std::execution::unsequenced_policy
174  return
175  (
176  // List sizes are identical by definition (template parameter)
177  std::equal(this->cbegin(), this->cend(), list.cbegin())
178  );
179 }
180 
181 
182 template<class T, unsigned N>
184 {
185  // List sizes are identical by definition (template parameter)
186 
187  // Can dispatch with
188  // - std::execution::parallel_unsequenced_policy
189  // - std::execution::unsequenced_policy
190  return std::lexicographical_compare
191  (
192  this->cbegin(), this->cend(),
193  list.cbegin(), list.cend()
194  );
195 }
196 
197 
198 template<class T, unsigned N>
200 {
201  return !operator==(list);
202 }
203 
204 
205 template<class T, unsigned N>
207 {
208  return list.operator<(*this);
209 }
210 
211 
212 template<class T, unsigned N>
214 {
215  return !list.operator<(*this);
216 }
217 
218 
219 template<class T, unsigned N>
221 {
222  return !operator<(list);
223 }
224 
225 
226 // ************************************************************************* //
label find(const ListType &input, const UnaryPredicate &pred, const label start=0)
Same as ListOps::find_if.
Definition: ListOps.H:795
bool operator!=(const FixedList< T, N > &list) const
The opposite of the equality operation. Takes linear time.
Definition: FixedList.C:192
A 1D vector of objects of type <T> with a fixed length <N>.
Definition: HashTable.H:107
error FatalError
Error stream (stdout output on all processes), with additional &#39;FOAM FATAL ERROR&#39; header text and sta...
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:598
void moveLast(const label i)
Move element to the last position.
Definition: FixedList.C:122
bool operator==(const FixedList< T, N > &list) const
Equality operation on FixedLists of the same type.
Definition: FixedList.C:162
static std::streamsize byteSize()
Number of contiguous bytes for the list data, runtime FatalError if type is not contiguous.
Definition: FixedList.C:27
string upper(const std::string &s)
Return string copy transformed with std::toupper on each character.
Definition: stringOps.C:1187
bool operator<(const IOstreamOption::versionNumber &a, const IOstreamOption::versionNumber &b) noexcept
Version A older than B.
bool equal(const Matrix< Form1, Type > &A, const Matrix< Form2, Type > &B, const bool verbose=false, const label maxDiffs=10, const scalar relTol=1e-5, const scalar absTol=1e-8)
Compare matrix elements for absolute or relative equality.
Definition: MatrixTools.C:27
dimensionedScalar pos(const dimensionedScalar &ds)
bool operator>(const FixedList< T, N > &list) const
Compare two FixedLists lexicographically. Takes linear time.
Definition: FixedList.C:199
void swapFirst(const label i)
Swap element with the first element.
Definition: FixedList.C:134
constexpr auto cend(const C &c) -> decltype(c.end())
Return const_iterator to the end of the container c.
Definition: stdFoam.H:223
errorManip< error > abort(error &err)
Definition: errorManip.H:139
constexpr auto cbegin(const C &c) -> decltype(c.begin())
Return const_iterator to the beginning of the container c.
Definition: stdFoam.H:190
bool operator<(const FixedList< T, N > &list) const
Compare two FixedLists lexicographically. Takes linear time.
Definition: FixedList.C:176
const volScalarField & T
const_iterator cbegin() const noexcept
Return const_iterator to begin traversing the constant FixedList.
Definition: FixedListI.H:498
const Vector< label > N(dict.get< Vector< label >>("N"))
label rfind(const T &val, label pos=-1) const
Find index of the last occurrence of the value.
Definition: FixedList.C:86
string lower(const std::string &s)
Return string copy transformed with std::tolower on each character.
Definition: stringOps.C:1171
label find(const T &val) const
Find index of the first occurrence of the value.
Definition: FixedList.C:42
A template class to specify that a data type can be considered as being contiguous in memory...
Definition: contiguous.H:70
void Swap(DynamicList< T, SizeMinA > &a, DynamicList< T, SizeMinB > &b)
Exchange contents of lists - see DynamicList::swap().
Definition: DynamicList.H:692
void moveFirst(const label i)
Move element to the first position.
Definition: FixedList.C:110
void swapLast(const label i)
Swap element with the last element.
Definition: FixedList.C:146
const_iterator cend() const noexcept
Return const_iterator to end traversing the constant FixedList.
Definition: FixedListI.H:522
tmp< faMatrix< Type > > operator==(const faMatrix< Type > &, const faMatrix< Type > &)
bool operator>=(const FixedList< T, N > &list) const
Return true if !(a < b). Takes linear time.
Definition: FixedList.C:213
bool operator<=(const FixedList< T, N > &list) const
Return true if !(a > b). Takes linear time.
Definition: FixedList.C:206