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 -------------------------------------------------------------------------------
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 #include "ListLoopM.H"
31 
32 // * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
33 
34 template<class T, unsigned N>
36 {
38  {
40  << "Invalid for non-contiguous data types"
41  << abort(FatalError);
42  }
44 }
45 
46 
47 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
48 
49 template<class T, unsigned N>
50 Foam::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 
71 template<class T, unsigned N>
72 Foam::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 
96 template<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 
108 template<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 
120 template<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 
132 template<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 
148 template<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 
168 template<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 
192 template<class T, unsigned N>
194 {
195  return !operator==(list);
196 }
197 
198 
199 template<class T, unsigned N>
201 {
202  return list.operator<(*this);
203 }
204 
205 
206 template<class T, unsigned N>
208 {
209  return !list.operator<(*this);
210 }
211 
212 
213 template<class T, unsigned N>
215 {
216  return !operator<(list);
217 }
218 
219 
220 // ************************************************************************* //
Foam::Swap
void Swap(DynamicList< T, SizeMinA > &a, DynamicList< T, SizeMinB > &b)
Definition: DynamicList.H:429
Foam::FixedList::swapLast
void swapLast(const label i)
Swap element with the last element.
Definition: FixedList.C:133
Foam::FixedList::operator<=
bool operator<=(const FixedList< T, N > &list) const
Return true if !(a > b). Takes linear time.
Definition: FixedList.C:207
Foam::FixedList::find
label find(const T &val, label pos=0) const
Find index of the first occurrence of the value.
Definition: FixedList.C:50
Foam::FixedList::operator!=
bool operator!=(const FixedList< T, N > &list) const
The opposite of the equality operation. Takes linear time.
Definition: FixedList.C:193
Foam::stringOps::lower
string lower(const std::string &s)
Return string copy transformed with std::tolower on each character.
Definition: stringOps.C:1184
Foam::FixedList::operator==
bool operator==(const FixedList< T, N > &list) const
Equality operation on FixedLists of the same type.
Definition: FixedList.C:149
Foam::FixedList::operator<
bool operator<(const FixedList< T, N > &list) const
Compare two FixedLists lexicographically. Takes linear time.
Definition: FixedList.C:169
Foam::operator==
tmp< faMatrix< Type > > operator==(const faMatrix< Type > &, const faMatrix< Type > &)
Foam::FixedList::rfind
label rfind(const T &val, label pos=-1) const
Find index of the last occurrence of the value.
Definition: FixedList.C:72
Foam::FixedList::byteSize
static std::streamsize byteSize()
Definition: FixedList.C:35
Foam::FatalError
error FatalError
Foam::FixedList::moveFirst
void moveFirst(const label i)
Move element to the first position.
Definition: FixedList.C:97
Foam::abort
errorManip< error > abort(error &err)
Definition: errorManip.H:144
T
const volScalarField & T
Definition: createFieldRefs.H:2
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:453
List_CONST_ACCESS
#define List_CONST_ACCESS(type, f, fp)
Definition: ListLoopM.H:43
Foam::FixedList::operator>
bool operator>(const FixedList< T, N > &list) const
Compare two FixedLists lexicographically. Takes linear time.
Definition: FixedList.C:200
Foam::FixedList::operator>=
bool operator>=(const FixedList< T, N > &list) const
Return true if !(a < b). Takes linear time.
Definition: FixedList.C:214
Foam::FixedList::swapFirst
void swapFirst(const label i)
Swap element with the first element.
Definition: FixedList.C:121
Foam::FixedList
A 1D vector of objects of type <T> with a fixed length <N>.
Definition: HashTable.H:104
Foam::operator<
bool operator<(const IOstreamOption::versionNumber &a, const IOstreamOption::versionNumber &b) noexcept
Version A older than B.
Definition: IOstreamOption.H:398
N
const Vector< label > N(dict.get< Vector< label >>("N"))
FixedList.H
Foam::stringOps::upper
string upper(const std::string &s)
Return string copy transformed with std::toupper on each character.
Definition: stringOps.C:1200
Foam::is_contiguous
A template class to specify that a data type can be considered as being contiguous in memory.
Definition: contiguous.H:75
ListLoopM.H
Macros for accessing List elements.
Foam::FixedList::moveLast
void moveLast(const label i)
Move element to the last position.
Definition: FixedList.C:109
Foam::pos
dimensionedScalar pos(const dimensionedScalar &ds)
Definition: dimensionedScalar.C:177