DLPermutation.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 Class
25  Foam::DLPermutation
26 
27 Description
28  A permutation stored in the same manner as a doubly-linked list in order to
29  facilitate reordering in constant time
30 
31 SourceFiles
32  DLPermutationI.H
33 
34 \*---------------------------------------------------------------------------*/
35 
36 #ifndef DLPermutation_H
37 #define DLPermutation_H
38 
39 #include "randomGenerator.H"
40 
41 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
42 
43 namespace Foam
44 {
45 
46 /*---------------------------------------------------------------------------*\
47  Class DLPermutation Declaration
48 \*---------------------------------------------------------------------------*/
49 
50 template<class LabelList>
51 class DLPermutation
52 {
53  // Private Data
54 
55  //- The first index
56  label head_;
57 
58  //- For every index, the next index
59  LabelList next_;
60 
61  //- The last index
62  label tail_;
63 
64  //- For every index, the previous index
65  LabelList previous_;
66 
67 
68 public:
69 
70  // Public Classes
71 
72  //- A bi-directional constant iterator
73  class const_iterator
74  {
75  // Private Data
76 
77  //- Reference to the permutation
78  const DLPermutation& p_;
79 
80  //- The current index
81  label i_;
82 
83 
84  public:
85 
86  // Constructors
87 
88  //- Construct at an index within a permutation
89  inline const_iterator(const DLPermutation&, const label);
90 
91  //- Use the default copy constructor
92  inline const_iterator(const const_iterator&) = default;
93 
94 
95  // Member Operators
96 
97  //- Dereference
98  inline label operator*() const;
99 
100  //- Assignment
101  inline const_iterator& operator=(const const_iterator& iter);
102 
103  //- Increment
104  inline const_iterator& operator++();
105 
106  //- Increment
107  inline const_iterator operator++(int);
108 
109  //- Decrement
110  inline const_iterator& operator--();
111 
112  //- Decrement
113  inline const_iterator operator--(int);
114 
115  //- Equality comparison
116  inline bool operator==(const const_iterator& iter) const;
117 
118  //- Inequality comparison
119  inline bool operator!=(const const_iterator& iter) const;
120 
121 
122  // Member Functions
123 
124  //- Return the next iterator without incrementing
125  inline const_iterator next() const;
126 
127  //- Return the previous iterator without incrementing
128  inline const_iterator previous() const;
129  };
130 
131 
132  // Constructors
133 
134  //- Construct for a given permutation
135  inline DLPermutation(const List<label>& permutation);
136 
137  //- Construct a random permutation
138  inline DLPermutation(const label n, randomGenerator& rndGen);
139 
140 
141  // Member Functions
142 
143  //- Return the permutation
144  inline List<label> permutation(const bool reverse);
145 
146  //- Return the starting forward iterator
147  inline const_iterator begin() const;
148 
149  //- Return the ending forward iterator
150  inline const_iterator end() const;
151 
152  //- Return the starting forward iterator
153  inline const_iterator cbegin() const;
154 
155  //- Return the ending forward iterator
156  inline const_iterator cend() const;
157 
158  //- Return the starting reverse iterator
159  inline const_iterator rbegin() const;
160 
161  //- Return the ending reverse iterator
162  inline const_iterator rend() const;
163 
164  //- Return the starting reverse iterator
165  inline const_iterator crbegin() const;
166 
167  //- Return the ending reverse iterator
168  inline const_iterator crend() const;
169 
170  //- Add a new index to the end of the permutation
171  void append();
172 
173  //- Add a new index to the start of the permutation
174  void prepend();
175 
176  //- Raise a given index to the start of the permutation
177  void raise(const label i);
178 
179  //- Sink a given index to the end of the permutation
180  void sink(const label i);
181 
182  // !!! More operations are possible; e.g., swap two elements or rotate
183  // a sub-section. Implement as necessary.
184 };
185 
186 
187 //- Define the type of a permutation based on a list
189 
190 
191 //- Define the type of a permutation based on a dynamic list
193 
194 
195 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
196 
197 } // End namespace Foam
198 
199 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
200 
201 #define forAllConstIterReverse(Container,container,iter) \
202  for \
203  ( \
204  Container::const_iterator iter = (container).rbegin(); \
205  iter != (container).rend(); \
206  -- iter \
207  )
208 
209 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
210 
211 #include "DLPermutationI.H"
212 
213 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
214 
215 #endif
216 
217 // ************************************************************************* //
label n
A bi-directional constant iterator.
Definition: DLPermutation.H:73
const_iterator & operator--()
Decrement.
bool operator!=(const const_iterator &iter) const
Inequality comparison.
label operator*() const
Dereference.
const_iterator & operator=(const const_iterator &iter)
Assignment.
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.
bool operator==(const const_iterator &iter) const
Equality comparison.
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.
Random number generator.
Namespace for OpenFOAM.
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
DLPermutation< DynamicList< label > > dynamicDLPermutation
Define the type of a permutation based on a dynamic list.
DLPermutation< List< label > > dLPermutation
Define the type of a permutation based on a list.
randomGenerator rndGen(653213)