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-2020 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 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
33 
34 template<class T, unsigned N>
35 Foam::label Foam::FixedList<T, N>::find(const T& val, label pos) const
36 {
37  if (pos >= 0)
38  {
39  List_CONST_ACCESS(T, *this, list);
40 
41  while (pos < label(N))
42  {
43  if (list[pos] == val)
44  {
45  return pos;
46  }
47 
48  ++pos;
49  }
50  }
51 
52  return -1;
53 }
54 
55 
56 template<class T, unsigned N>
57 Foam::label Foam::FixedList<T, N>::rfind(const T& val, label pos) const
58 {
59  // pos == -1 has same meaning as std::string::npos - search from end
60  if (pos < 0 || pos >= label(N))
61  {
62  pos = label(N)-1;
63  }
64 
65  List_CONST_ACCESS(T, *this, list);
66 
67  while (pos >= 0)
68  {
69  if (list[pos] == val)
70  {
71  return pos;
72  }
73 
74  --pos;
75  }
76 
77  return -1;
78 }
79 
80 
81 template<class T, unsigned N>
83 {
84  checkIndex(i);
85 
86  for (label lower = 0; lower < i; ++lower)
87  {
88  Foam::Swap(v_[lower], v_[i]);
89  }
90 }
91 
92 
93 template<class T, unsigned N>
95 {
96  checkIndex(i);
97 
98  for (label upper = label(N-1); upper > i; --upper)
99  {
100  Foam::Swap(v_[i], v_[upper]);
101  }
102 }
103 
104 
105 template<class T, unsigned N>
107 {
108  checkIndex(i);
109 
110  if (i > 0)
111  {
112  Foam::Swap(v_[0], v_[i]);
113  }
114 }
115 
116 
117 template<class T, unsigned N>
119 {
120  checkIndex(i);
121 
122  const label upper = label(N-1);
123 
124  if (i < upper)
125  {
126  Foam::Swap(v_[i], v_[upper]);
127  }
128 }
129 
130 
131 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
132 
133 template<class T, unsigned N>
135 {
136  List_CONST_ACCESS(T, *this, lhs);
137  List_CONST_ACCESS(T, (list), rhs);
138 
139  // List sizes are identical by definition (template parameter)
140  for (unsigned i = 0; i < N; ++i)
141  {
142  if (!(lhs[i] == rhs[i]))
143  {
144  return false;
145  }
146  }
147 
148  // Contents appear to be identical.
149  return true;
150 }
151 
152 
153 template<class T, unsigned N>
155 {
156  List_CONST_ACCESS(T, *this, lhs);
157  List_CONST_ACCESS(T, (list), rhs);
158 
159  // List sizes are identical by definition (template parameter)
160  for (unsigned i=0; i<N; ++i)
161  {
162  if (lhs[i] < rhs[i])
163  {
164  return true;
165  }
166  else if (rhs[i] < lhs[i])
167  {
168  return false;
169  }
170  }
171 
172  // Contents appear to be identical.
173  return false;
174 }
175 
176 
177 template<class T, unsigned N>
179 {
180  return !operator==(list);
181 }
182 
183 
184 template<class T, unsigned N>
186 {
187  return list.operator<(*this);
188 }
189 
190 
191 template<class T, unsigned N>
193 {
194  return !list.operator<(*this);
195 }
196 
197 
198 template<class T, unsigned N>
200 {
201  return !operator<(list);
202 }
203 
204 
205 // * * * * * * * * * * * * * * * * IOStream operators * * * * * * * * * * * //
206 
207 #include "FixedListIO.C"
208 
209 // ************************************************************************* //
FixedListIO.C
Foam::FixedList::swapLast
void swapLast(const label i)
Swap element with the last element.
Definition: FixedList.C:118
Foam::FixedList::operator<=
bool operator<=(const FixedList< T, N > &list) const
Return true if !(a > b). Takes linear time.
Definition: FixedList.C:192
Foam::FixedList::find
label find(const T &val, label pos=0) const
Find index of the first occurrence of the value.
Definition: FixedList.C:35
Foam::FixedList::operator!=
bool operator!=(const FixedList< T, N > &list) const
The opposite of the equality operation. Takes linear time.
Definition: FixedList.C:178
Foam::Swap
void Swap(DynamicList< T, SizeMin1 > &a, DynamicList< T, SizeMin2 > &b)
Definition: DynamicListI.H:913
Foam::stringOps::lower
string lower(const std::string &s)
Return string copy transformed with std::tolower on each character.
Definition: stringOps.C:1186
Foam::FixedList::operator==
bool operator==(const FixedList< T, N > &list) const
Equality operation on FixedLists of the same type.
Definition: FixedList.C:134
Foam::FixedList::operator<
bool operator<(const FixedList< T, N > &list) const
Compare two FixedLists lexicographically. Takes linear time.
Definition: FixedList.C:154
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:57
Foam::FixedList::moveFirst
void moveFirst(const label i)
Move element to the first position.
Definition: FixedList.C:82
T
const volScalarField & T
Definition: createFieldRefs.H:2
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:185
Foam::FixedList::operator>=
bool operator>=(const FixedList< T, N > &list) const
Return true if !(a < b). Takes linear time.
Definition: FixedList.C:199
Foam::FixedList::swapFirst
void swapFirst(const label i)
Swap element with the first element.
Definition: FixedList.C:106
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:397
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:1202
ListLoopM.H
Macros for accessing List elements.
Foam::FixedList::moveLast
void moveLast(const label i)
Move element to the last position.
Definition: FixedList.C:94
Foam::pos
dimensionedScalar pos(const dimensionedScalar &ds)
Definition: dimensionedScalar.C:177