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-2021 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#include "ListLoopM.H"
31
32// * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
33
34template<class T, unsigned N>
36{
38 {
40 << "Invalid for non-contiguous data types"
41 << abort(FatalError);
42 }
44}
45
46
47// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
48
49template<class T, unsigned N>
50Foam::label Foam::FixedList<T, N>::find(const T& val, label pos) const
51{
52 if (pos >= 0)
53 {
54 List_CONST_ACCESS(T, *this, list);
55
56 while (pos < label(N))
57 {
58 if (list[pos] == val)
59 {
60 return pos;
61 }
62
63 ++pos;
64 }
65 }
66
67 return -1;
68}
69
70
71template<class T, unsigned N>
72Foam::label Foam::FixedList<T, N>::rfind(const T& val, label pos) const
73{
74 // pos == -1 has same meaning as std::string::npos - search from end
75 if (pos < 0 || pos >= label(N))
76 {
77 pos = label(N)-1;
78 }
79
80 List_CONST_ACCESS(T, *this, list);
81
82 while (pos >= 0)
83 {
84 if (list[pos] == val)
85 {
86 return pos;
87 }
88
89 --pos;
90 }
91
92 return -1;
93}
94
95
96template<class T, unsigned N>
98{
99 checkIndex(i);
100
101 for (label lower = 0; lower < i; ++lower)
102 {
103 Foam::Swap(v_[lower], v_[i]);
104 }
105}
106
107
108template<class T, unsigned N>
110{
111 checkIndex(i);
112
113 for (label upper = label(N-1); upper > i; --upper)
114 {
115 Foam::Swap(v_[i], v_[upper]);
116 }
117}
118
119
120template<class T, unsigned N>
122{
123 checkIndex(i);
124
125 if (i > 0)
126 {
127 Foam::Swap(v_[0], v_[i]);
128 }
129}
130
131
132template<class T, unsigned N>
134{
135 checkIndex(i);
136
137 const label upper = label(N-1);
138
139 if (i < upper)
140 {
141 Foam::Swap(v_[i], v_[upper]);
142 }
143}
144
145
146// * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
147
148template<class T, unsigned N>
150{
151 List_CONST_ACCESS(T, *this, lhs);
152 List_CONST_ACCESS(T, (list), rhs);
153
154 // List sizes are identical by definition (template parameter)
155 for (unsigned i = 0; i < N; ++i)
156 {
157 if (!(lhs[i] == rhs[i]))
158 {
159 return false;
160 }
161 }
162
163 // Contents appear to be identical.
164 return true;
165}
166
167
168template<class T, unsigned N>
170{
171 List_CONST_ACCESS(T, *this, lhs);
172 List_CONST_ACCESS(T, (list), rhs);
173
174 // List sizes are identical by definition (template parameter)
175 for (unsigned i=0; i<N; ++i)
176 {
177 if (lhs[i] < rhs[i])
178 {
179 return true;
180 }
181 else if (rhs[i] < lhs[i])
182 {
183 return false;
184 }
185 }
186
187 // Contents appear to be identical.
188 return false;
189}
190
191
192template<class T, unsigned N>
194{
195 return !operator==(list);
196}
197
198
199template<class T, unsigned N>
201{
202 return list.operator<(*this);
203}
204
205
206template<class T, unsigned N>
208{
209 return !list.operator<(*this);
210}
211
212
213template<class T, unsigned N>
215{
216 return !operator<(list);
217}
218
219
220// ************************************************************************* //
Macros for accessing List elements.
#define List_CONST_ACCESS(type, f, fp)
Definition: ListLoopM.H:43
A 1D vector of objects of type <T> with a fixed length <N>.
Definition: FixedList.H:81
void moveLast(const label i)
Move element to the last position.
Definition: FixedList.C:109
void swapLast(const label i)
Swap element with the last element.
Definition: FixedList.C:133
bool operator>=(const FixedList< T, N > &list) const
Return true if !(a < b). Takes linear time.
Definition: FixedList.C:214
void swapFirst(const label i)
Swap element with the first element.
Definition: FixedList.C:121
bool operator<(const FixedList< T, N > &list) const
Compare two FixedLists lexicographically. Takes linear time.
Definition: FixedList.C:169
label rfind(const T &val, label pos=-1) const
Find index of the last occurrence of the value.
Definition: FixedList.C:72
static std::streamsize byteSize()
Definition: FixedList.C:35
bool operator<=(const FixedList< T, N > &list) const
Return true if !(a > b). Takes linear time.
Definition: FixedList.C:207
label find(const T &val, label pos=0) const
Find index of the first occurrence of the value.
Definition: FixedList.C:50
void moveFirst(const label i)
Move element to the first position.
Definition: FixedList.C:97
bool operator>(const FixedList< T, N > &list) const
Compare two FixedLists lexicographically. Takes linear time.
Definition: FixedList.C:200
friend bool operator!=(const refineCell &rc1, const refineCell &rc2)
Definition: refineCell.H:106
friend bool operator==(const refineCell &rc1, const refineCell &rc2)
Definition: refineCell.H:97
const volScalarField & T
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:453
dimensionedScalar pos(const dimensionedScalar &ds)
void Swap(DynamicList< T, SizeMinA > &a, DynamicList< T, SizeMinB > &b)
Definition: DynamicList.H:408
tmp< faMatrix< Type > > operator==(const faMatrix< Type > &, const faMatrix< Type > &)
errorManip< error > abort(error &err)
Definition: errorManip.H:144
bool operator<(const IOstreamOption::versionNumber &a, const IOstreamOption::versionNumber &b) noexcept
Version A older than B.
error FatalError
A template class to specify that a data type can be considered as being contiguous in memory.
Definition: contiguous.H:78
const Vector< label > N(dict.get< Vector< label > >("N"))