PtrListOps.H
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) 2019-2021 OpenCFD Ltd.
9 -------------------------------------------------------------------------------
10 License
11  This file is part of OpenFOAM.
12 
13  OpenFOAM is free software: you can redistribute it and/or modify it
14  under the terms of the GNU General Public License as published by
15  the Free Software Foundation, either version 3 of the License, or
16  (at your option) any later version.
17 
18  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
19  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21  for more details.
22 
23  You should have received a copy of the GNU General Public License
24  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
25 
26 InNamespace
27  Foam
28 
29 Description
30  Functions to operate on Pointer Lists.
31 
32 Namespace
33  Foam::PtrListOps
34 
35 Description
36  Various utility functions to operate on Pointer Lists.
37 
38 SourceFiles
39  PtrListOpsTemplates.C
40 
41 \*---------------------------------------------------------------------------*/
42 
43 #ifndef PtrListOps_H
44 #define PtrListOps_H
45 
46 #include "predicates.H"
47 #include "PtrList.H"
48 #include "ListOps.H"
49 
50 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
51 
52 namespace Foam
53 {
54 
55 //- Return (stable) sort order for the list
56 template<class T>
57 labelList sortedOrder(const UPtrList<T>& input);
58 
59 //- Generate (stable) sort order for the list
60 template<class T>
61 void sortedOrder(const UPtrList<T>& input, labelList& order);
62 
63 //- Generate (stable) sort order for the list,
64 //- using the specified list compare predicate
65 template<class T, class ListComparePredicate>
66 void sortedOrder
67 (
68  const UPtrList<T>& input,
69  labelList& order,
70  const ListComparePredicate& comp
71 );
72 
73 
74 //- Inplace (stable) sorting of pointer list.
75 template<class T>
76 void sort(UPtrList<T>& list);
77 
78 //- Inplace (stable) sorting of pointer list.
79 template<class T, class Compare>
80 void sort(UPtrList<T>& list, const Compare& comp);
81 
82 //- Inplace shuffle of pointer list.
83 template<class T>
84 void shuffle(UPtrList<T>& list);
85 
86 
87 /*---------------------------------------------------------------------------*\
88  Namespace PtrListOps Declaration
89 \*---------------------------------------------------------------------------*/
90 
91 namespace PtrListOps
92 {
93 
94 // Public Classes
95 
96 //- A UPtrList compare binary predicate for normal sort.
97 // Null entries sort to the end
98 template<class T>
99 struct less
100 {
102 
103  less(const UPtrList<T>& list)
104  :
105  values(list)
106  {}
107 
108  bool operator()(const label a, const label b) const
109  {
110  const T* const ptr1 = values(a);
111  const T* const ptr2 = values(b);
112 
113  return (ptr1 && ptr2) ? (*ptr1 < *ptr2) : !ptr2;
114  }
115 };
116 
117 
118 //- A UPtrList compare binary predicate for reverse sort.
119 // Null entries sort to the end
120 template<class T>
121 struct greater
122 {
124 
125  greater(const UPtrList<T>& list)
126  :
127  values(list)
128  {}
129 
130  bool operator()(const label a, const label b) const
131  {
132  const T* const ptr1 = values(a);
133  const T* const ptr2 = values(b);
134 
135  return (ptr1 && ptr2) ? (*ptr2 < *ptr1) : !ptr2;
136  }
137 };
138 
139 
140 //- List of values generated by applying the access operation
141 //- to each list item.
142 //
143 // For example,
144 // \code
145 // PtrListOps::get(mesh.boundaryMesh(), nameOp<polyPatch>());
146 // \endcode
147 template<class ReturnType, class T, class AccessOp>
149 (
150  const UPtrList<T>& list,
151  const AccessOp& aop
152 );
153 
154 
155 //- List of names generated by calling \c name() for each list item
156 //- and filtered for matches
157 //
158 // For example,
159 // \code
160 // wordRes matches(...);
161 // PtrListOps::names(mesh.boundaryMesh(), matches);
162 //
163 // PtrListOps::names(mesh.boundaryMesh(), predicates::always());
164 // \endcode
165 template<class T, class UnaryMatchPredicate>
167 (
168  const UPtrList<T>& list,
169  const UnaryMatchPredicate& matcher
170 );
171 
172 
173 //- List of names generated by calling \c name() for each list item
174 //- no filtering (ie, predicates::always)
175 template<class T>
176 List<word> names(const UPtrList<T>& list);
177 
178 
179 //- Find first list item with 'name()' that matches, -1 on failure
180 template<class T, class UnaryMatchPredicate>
181 label firstMatching
182 (
183  const UPtrList<T>& list,
184  const UnaryMatchPredicate& matcher
185 );
186 
187 
188 //- Extract list indices for all items with 'name()' that matches
189 template<class T, class UnaryMatchPredicate>
191 (
192  const UPtrList<T>& list,
193  const UnaryMatchPredicate& matcher
194 );
195 
196 } // End namespace ListOps
197 
198 
199 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
200 
201 } // End namespace Foam
202 
203 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
204 
205 #ifdef NoRepository
206  #include "PtrListOpsTemplates.C"
207 #endif
208 
209 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
210 
211 #endif
212 
213 // ************************************************************************* //
Foam::labelList
List< label > labelList
A List of labels.
Definition: List.H:67
Foam::PtrListOps::findMatching
labelList findMatching(const UPtrList< T > &list, const UnaryMatchPredicate &matcher)
Extract list indices for all items with 'name()' that matches.
Foam::PtrListOps::less::less
less(const UPtrList< T > &list)
Definition: PtrListOps.H:103
Foam::PtrListOps::less
A UPtrList compare binary predicate for normal sort.
Definition: PtrListOps.H:99
Foam::PtrListOps::greater
A UPtrList compare binary predicate for reverse sort.
Definition: PtrListOps.H:121
Foam::constant::physicoChemical::b
const dimensionedScalar b
Wien displacement law constant: default SI units: [m.K].
Definition: createFields.H:27
Foam::T
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
Definition: FieldFieldFunctions.C:58
Foam::sort
void sort(UList< T > &a)
Definition: UList.C:261
Foam::PtrListOps::greater::values
const UPtrList< T > & values
Definition: PtrListOps.H:123
Foam::UPtrList
A list of pointers to objects of type <T>, without allocation/deallocation management of the pointers...
Definition: UPtrList.H:62
PtrListOpsTemplates.C
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::PtrListOps::less::values
const UPtrList< T > & values
Definition: PtrListOps.H:101
Foam::shuffle
void shuffle(UList< T > &a)
Definition: UList.C:289
Foam::List
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: BitOps.H:63
Foam::PtrListOps::firstMatching
label firstMatching(const UPtrList< T > &list, const UnaryMatchPredicate &matcher)
Find first list item with 'name()' that matches, -1 on failure.
Foam::PtrListOps::less::operator()
bool operator()(const label a, const label b) const
Definition: PtrListOps.H:108
Foam::input
static Istream & input(Istream &is, IntRange< T > &range)
Definition: IntRanges.C:55
Foam::PtrListOps::greater::operator()
bool operator()(const label a, const label b) const
Definition: PtrListOps.H:130
Foam::PtrListOps::get
List< ReturnType > get(const UPtrList< T > &list, const AccessOp &aop)
PtrList.H
Foam::PtrListOps::greater::greater
greater(const UPtrList< T > &list)
Definition: PtrListOps.H:125
predicates.H
ListOps.H
Various functions to operate on Lists.
Foam::sortedOrder
labelList sortedOrder(const UList< T > &input)
Return the (stable) sort order for the list.
Foam::PtrListOps::names
List< word > names(const UPtrList< T > &list, const UnaryMatchPredicate &matcher)