DLPermutationI.H
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2026 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 \*---------------------------------------------------------------------------*/
25 
26 #include "DLPermutation.H"
27 
28 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
29 
30 template<class LabelList>
32 (
33  const DLPermutation<LabelList>& p,
34  const label i
35 )
36 :
37  p_(p),
38  i_(i)
39 {}
40 
41 
42 template<class LabelList>
44 (
45  const List<label>& permutation
46 )
47 :
48  head_(-1),
49  next_(permutation.size(), -1),
50  tail_(-1),
51  previous_(permutation.size(), -1)
52 {
53  if (permutation.empty()) return;
54 
55  head_ = permutation.first();
56  for (label i = 1; i < permutation.size(); ++ i)
57  {
58  next_[permutation[i - 1]] = permutation[i];
59  }
60  next_[permutation.last()] = -1;
61 
62  tail_ = permutation.last();
63  for (label i = permutation.size() - 2; i >= 0; -- i)
64  {
65  previous_[permutation[i + 1]] = permutation[i];
66  }
67  previous_[permutation.first()] = -2;
68 }
69 
70 
71 template<class LabelList>
73 (
74  const label n,
76 )
77 :
78  DLPermutation<LabelList>(rndGen.permutation(n))
79 {}
80 
81 
82 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
83 
84 template<class LabelList>
85 inline Foam::label
87 {
88  return i_;
89 }
90 
91 
92 template<class LabelList>
95 (
96  const const_iterator& iter
97 )
98 {
99  i_ = iter.i_;
100  return *this;
101 }
102 
103 
104 template<class LabelList>
107 {
108  i_ = p_.next_[i_];
109  return *this;
110 }
111 
112 
113 template<class LabelList>
116 {
117  const_iterator iter(*this);
118  this->operator++();
119  return iter;
120 }
121 
122 
123 template<class LabelList>
126 {
127  i_ = p_.previous_[i_];
128  return *this;
129 }
130 
131 
132 template<class LabelList>
135 {
136  const_iterator iter(*this);
137  this->operator--();
138  return iter;
139 }
140 
141 
142 template<class LabelList>
144 (
145  const const_iterator& iter
146 ) const
147 {
148  return i_ == iter.i_;
149 }
150 
151 
152 template<class LabelList>
154 (
155  const const_iterator& iter
156 ) const
157 {
158  return !(*this == iter);
159 }
160 
161 
162 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
163 
164 template<class LabelList>
167 {
168  if (i_ == -1)
169  {
170  return const_iterator(p_, p_.head_);
171  }
172 
173  const_iterator iter(*this);
174  ++ iter;
175  return iter;
176 }
177 
178 
179 template<class LabelList>
182 {
183  if (i_ == -1)
184  {
185  return const_iterator(p_, p_.tail_);
186  }
187 
188  const_iterator iter(*this);
189  -- iter;
190  return iter;
191 }
192 
193 
194 template<class LabelList>
196 (
197  const bool reverse
198 )
199 {
200  List<label> result(next_.size());
201 
202  if (!reverse)
203  {
204  label i = 0;
206  {
207  result[i ++] = *iter;
208  }
209  }
210  else
211  {
212  label i = result.size();
214  {
215  result[-- i] = *iter;
216  }
217  }
218 
219  return result;
220 }
221 
222 
223 template<class LabelList>
226 {
227  return const_iterator(*this, head_);
228 }
229 
230 
231 template<class LabelList>
234 {
235  return const_iterator(*this, -1);
236 }
237 
238 
239 template<class LabelList>
242 {
243  return const_iterator(*this, head_);
244 }
245 
246 
247 template<class LabelList>
250 {
251  return const_iterator(*this, -1);
252 }
253 
254 
255 template<class LabelList>
258 {
259  return const_iterator(*this, tail_);
260 }
261 
262 
263 template<class LabelList>
266 {
267  return const_iterator(*this, -2);
268 }
269 
270 
271 template<class LabelList>
274 {
275  return const_iterator(*this, tail_);
276 }
277 
278 
279 template<class LabelList>
282 {
283  return const_iterator(*this, -2);
284 }
285 
286 
287 template<class LabelList>
289 {
290  const label i = next_.size();
291 
292  next_.append(-1);
293  previous_.append(tail_);
294  next_[tail_] = i;
295  tail_ = i;
296 }
297 
298 
299 template<class LabelList>
301 {
302  const label i = next_.size();
303 
304  next_.append(head_);
305  previous_.append(-2);
306  previous_[head_] = i;
307  head_ = i;
308 }
309 
310 
311 template<class LabelList>
313 {
314  if (i == head_) return;
315 
316  const label i0 = previous_[i];
317  const label i1 = next_[i];
318 
319  (i1 != -1 ? previous_[i1] : tail_) = previous_[i];
320  previous_[i] = -2;
321  previous_[head_] = i;
322 
323  next_[i0] = next_[i];
324  next_[i] = head_;
325  head_ = i;
326 }
327 
328 
329 template<class LabelList>
331 {
332  if (i == tail_) return;
333 
334  const label i0 = previous_[i];
335  const label i1 = next_[i];
336 
337  (i0 != -2 ? next_[i0] : head_) = next_[i];
338  next_[i] = -1;
339  next_[tail_] = i;
340 
341  previous_[i1] = previous_[i];
342  previous_[i] = tail_;
343  tail_ = i;
344 }
345 
346 
347 // ************************************************************************* //
#define forAllConstIterReverse(Container, container, iter)
label n
#define forAllConstIter(Container, container, iter)
Iterate across all elements in the container object of type.
Definition: UList.H:492
A bi-directional constant iterator.
Definition: DLPermutation.H:73
const_iterator & operator--()
Decrement.
label operator*() const
Dereference.
const_iterator & operator++()
Increment.
const_iterator next() const
Return the next iterator without incrementing.
const_iterator(const DLPermutation &, const label)
Construct at an index within a permutation.
const_iterator previous() const
Return the previous iterator without incrementing.
A permutation stored in the same manner as a doubly-linked list in order to facilitate reordering in ...
Definition: DLPermutation.H:51
void sink(const label i)
Sink a given index to the end of the permutation.
const_iterator rbegin() const
Return the starting reverse iterator.
const_iterator cbegin() const
Return the starting forward iterator.
const_iterator rend() const
Return the ending reverse iterator.
const_iterator crbegin() const
Return the starting reverse iterator.
const_iterator begin() const
Return the starting forward iterator.
const_iterator crend() const
Return the ending reverse iterator.
List< label > permutation(const bool reverse)
Return the permutation.
DLPermutation(const List< label > &permutation)
Construct for a given permutation.
const_iterator cend() const
Return the ending forward iterator.
const_iterator end() const
Return the ending forward iterator.
void append()
Add a new index to the end of the permutation.
void prepend()
Add a new index to the start of the permutation.
void raise(const label i)
Raise a given index to the start of the permutation.
void size(const label)
Override size to be inconsistent with allocated storage.
Definition: ListI.H:164
T & first()
Return the first element of the list.
Definition: UListI.H:114
bool empty() const
Return true if the UList is empty (ie, size() is zero)
Definition: UListI.H:325
T & last()
Return the last element of the list.
Definition: UListI.H:128
Random number generator.
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:59
void reverse(UList< T > &, const label n)
Definition: UListI.H:334
randomGenerator rndGen(653213)
volScalarField & p