Loading...
Searching...
No Matches
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-2025 OpenCFD Ltd.
10-------------------------------------------------------------------------------
11License
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
33template<class T, unsigned N>
34std::streamsize Foam::FixedList<T, N>::byteSize()
35{
36 if constexpr (!is_contiguous_v<T>)
37 {
39 << "Invalid for non-contiguous data types"
41 }
43}
44
45
46// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
47
48template<class T, unsigned N>
49Foam::label Foam::FixedList<T, N>::find(const T& val) const
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
56template<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
92template<class T, unsigned N>
93Foam::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;
111
112 return -1;
113}
114
115
116template<class T, unsigned N>
117void Foam::FixedList<T, N>::moveFirst(const label i)
118{
119 checkIndex(i);
120
121 for (label lower = 0; lower < i; ++lower)
123 Foam::Swap(v_[lower], v_[i]);
124 }
125}
126
127
128template<class T, unsigned N>
129void Foam::FixedList<T, N>::moveLast(const label i)
130{
131 checkIndex(i);
132
133 for (label upper = label(N-1); upper > i; --upper)
135 Foam::Swap(v_[i], v_[upper]);
136 }
137}
138
139
140template<class T, unsigned N>
141void Foam::FixedList<T, N>::swapFirst(const label i)
142{
143 checkIndex(i);
144
145 if (i > 0)
147 Foam::Swap(v_[0], v_[i]);
148 }
149}
150
151
152template<class T, unsigned N>
153void 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]);
163}
164
165
166// * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
167
168template<class T, unsigned N>
169bool Foam::FixedList<T, N>::operator==(const FixedList<T, N>& list) const
170{
171 // Can dispatch with
172 // - std::execution::par_unseq
173 // - std::execution::unseq
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
182template<class T, unsigned N>
184{
185 // List sizes are identical by definition (template parameter)
186
187 // Can dispatch with
188 // - std::execution::par_unseq
189 // - std::execution::unseq
190 return std::lexicographical_compare
191 (
192 this->cbegin(), this->cend(),
193 list.cbegin(), list.cend()
194 );
195}
196
197
198template<class T, unsigned N>
200{
201 return !operator==(list);
202}
203
204
205template<class T, unsigned N>
207{
208 return list.operator<(*this);
209}
210
211
212template<class T, unsigned N>
214{
215 return !list.operator<(*this);
216}
217
218
219template<class T, unsigned N>
221{
222 return !operator<(list);
223}
224
225
226// ************************************************************************* //
A 1D vector of objects of type <T> with a fixed length <N>.
Definition FixedList.H:73
const_iterator cend() const noexcept
Return const_iterator to end traversing the constant FixedList.
Definition FixedListI.H:542
void moveLast(const label i)
Move element to the last position.
Definition FixedList.C:122
void swapLast(const label i)
Swap element with the last element.
Definition FixedList.C:146
bool operator>=(const FixedList< T, N > &list) const
Return true if !(a < b). Takes linear time.
Definition FixedList.C:213
static std::streamsize size_bytes() noexcept
Number of contiguous bytes for the list data,.
Definition FixedListI.H:144
const_iterator cbegin() const noexcept
Return const_iterator to begin traversing the constant FixedList.
Definition FixedListI.H:494
void checkIndex(const label i) const
Check index is within valid range [0,N).
Definition FixedListI.H:264
void swapFirst(const label i)
Swap element with the first element.
Definition FixedList.C:134
bool operator<(const FixedList< T, N > &list) const
Compare two FixedLists lexicographically. Takes linear time.
Definition FixedList.C:176
label rfind(const T &val, label pos=-1) const
Find index of the last occurrence of the value.
Definition FixedList.C:86
static std::streamsize byteSize()
Number of contiguous bytes for the list data, runtime FatalError if type is not contiguous.
Definition FixedList.C:27
bool operator<=(const FixedList< T, N > &list) const
Return true if !(a > b). Takes linear time.
Definition FixedList.C:206
bool operator!=(const FixedList< T, N > &list) const
The opposite of the equality operation. Takes linear time.
Definition FixedList.C:192
void moveFirst(const label i)
Move element to the first position.
Definition FixedList.C:110
bool operator==(const FixedList< T, N > &list) const
Equality operation on FixedLists of the same type.
Definition FixedList.C:162
FixedList()=default
Default construct.
bool operator>(const FixedList< T, N > &list) const
Compare two FixedLists lexicographically. Takes linear time.
Definition FixedList.C:199
label find(const T &val) const
Find index of the first occurrence of the value.
Definition FixedList.C:42
const volScalarField & T
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition error.H:600
dimensionedScalar pos(const dimensionedScalar &ds)
void Swap(DynamicList< T, SizeMinA > &a, DynamicList< T, SizeMinB > &b)
Exchange contents of lists - see DynamicList::swap().
errorManip< error > abort(error &err)
Definition errorManip.H:139
bool operator<(const IOstreamOption::versionNumber &a, const IOstreamOption::versionNumber &b) noexcept
Version A older than B.
error FatalError
Error stream (stdout output on all processes), with additional 'FOAM FATAL ERROR' header text and sta...
constexpr bool is_contiguous_v
The is_contiguous value of Type (after stripping of qualifiers).
Definition contiguous.H:77
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
const Vector< label > N(dict.get< Vector< label > >("N"))