Loading...
Searching...
No Matches
IOobjectListTemplates.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) 2018-2025 OpenCFD Ltd.
9-------------------------------------------------------------------------------
10License
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\*---------------------------------------------------------------------------*/
27
29#include "predicates.H"
30
31// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
32
33// Templated implementation for classes()
34template<class MatchPredicate>
35Foam::HashTable<Foam::wordHashSet> Foam::IOobjectList::classesImpl
36(
37 const IOobjectList& list,
38 const MatchPredicate& matchName
39)
40{
42 summary.reserve(16); // Relatively few types
43
44 // Summary (key,val) = (class-name, object-names)
45 forAllConstIters(list, iter)
46 {
47 const word& key = iter.key();
48 const IOobject* io = iter.val();
49
50 if (matchName(key))
51 {
52 // Create entry (if needed) and insert
53 summary(io->headerClassName()).insert(key);
54 }
55 }
57 return summary;
58}
59
60
61// Templated implementation for count()
62template<class MatchPredicate1, class MatchPredicate2>
63Foam::label Foam::IOobjectList::countImpl
64(
65 const IOobjectList& list,
66 const MatchPredicate1& matchClass,
67 const MatchPredicate2& matchName
68)
69{
70 label count = 0;
71
72 forAllConstIters(list, iter)
73 {
74 const IOobject* io = iter.val();
75
76 if (matchClass(io->headerClassName()) && matchName(io->name()))
77 {
78 ++count;
79 }
80 }
82 return count;
83}
84
85
86// Templated implementation for count()
87template<class Type, class MatchPredicate>
88Foam::label Foam::IOobjectList::countTypeImpl
89(
90 const IOobjectList& list,
91 const MatchPredicate& matchName
92)
93{
94 label count = 0;
95
96 forAllConstIters(list, iter)
97 {
98 const IOobject* io = iter.val();
99
100 if (io->isHeaderClass<Type>() && matchName(io->name()))
101 {
102 ++count;
103 }
104 }
106 return count;
107}
108
109
110// Templated implementation for names(), sortedNames()
111template<class MatchPredicate1, class MatchPredicate2>
112Foam::wordList Foam::IOobjectList::namesImpl
113(
114 const IOobjectList& list,
115 const MatchPredicate1& matchClass,
116 const MatchPredicate2& matchName,
117 const bool doSort
118)
119{
120 wordList objNames(list.size());
121
122 label count = 0;
123 forAllConstIters(list, iter)
124 {
125 const word& key = iter.key();
126 const IOobject* io = iter.val();
127
128 if (matchClass(io->headerClassName()) && matchName(key))
129 {
130 objNames[count] = key;
131 ++count;
132 }
133 }
134
135 objNames.resize(count);
136
137 if (doSort)
138 {
139 Foam::sort(objNames);
140 }
142 return objNames;
143}
144
145
146// Templated implementation for names(), sortedNames()
147template<class Type, class MatchPredicate>
148Foam::wordList Foam::IOobjectList::namesTypeImpl
149(
150 const IOobjectList& list,
151 const MatchPredicate& matchName,
152 const bool doSort
153)
154{
155 wordList objNames(list.size());
156
157 label count = 0;
158 forAllConstIters(list, iter)
159 {
160 const word& key = iter.key();
161 const IOobject* io = iter.val();
162
163 if (io->isHeaderClass<Type>() && matchName(key))
164 {
165 objNames[count] = key;
166 ++count;
167 }
168 }
169
170 objNames.resize(count);
171
172 if (doSort)
173 {
174 Foam::sort(objNames);
175 }
176
177 return objNames;
178}
179
180
181// Templated implementation for csorted(), csorted()
182template<class Type, class MatchPredicate>
184Foam::IOobjectList::objectsTypeImpl
185(
186 const IOobjectList& list,
187 const MatchPredicate& matchName,
188 const bool doSort
189)
190{
191 UPtrList<const IOobject> result(list.size());
192
193 label count = 0;
194 forAllConstIters(list, iter)
195 {
196 const word& key = iter.key();
197 const IOobject* io = iter.val();
198
199 if (io->isHeaderClass<Type>() && matchName(key))
200 {
201 result.set(count, io);
202 ++count;
203 }
204 }
205
206 result.resize(count);
207
208 if (doSort)
209 {
210 Foam::sort(result, nameOp<IOobject>()); // Sort by object name()
211 }
213 return result;
214}
215
216
217// Templated implementation for lookup()
218template<class MatchPredicate>
219Foam::IOobjectList Foam::IOobjectList::lookupImpl
220(
221 const IOobjectList& list,
222 const MatchPredicate& matchName
223)
224{
225 IOobjectList results(list.size());
226
227 forAllConstIters(list, iter)
228 {
229 const word& key = iter.key();
230 const IOobject* io = iter.val();
231
232 if (matchName(key))
233 {
234 if (IOobject::debug)
235 {
236 InfoInFunction << "Found " << key << endl;
237 }
238
239 results.set(key, new IOobject(*io));
240 }
241 }
243 return results;
244}
245
246
247// Templated implementation for lookupClass()
248template<class MatchPredicate1, class MatchPredicate2>
249Foam::IOobjectList Foam::IOobjectList::lookupClassImpl
250(
251 const IOobjectList& list,
252 const MatchPredicate1& matchClass,
253 const MatchPredicate2& matchName
254)
255{
256 IOobjectList results(list.size());
257
258 forAllConstIters(list, iter)
259 {
260 const word& key = iter.key();
261 const IOobject* io = iter.val();
262
263 if (matchClass(io->headerClassName()) && matchName(key))
264 {
265 if (IOobject::debug)
266 {
267 InfoInFunction << "Found " << key << endl;
268 }
269
270 results.set(key, new IOobject(*io));
271 }
272 }
274 return results;
275}
276
277
278// Templated implementation for lookupClass()
279template<class Type, class MatchPredicate>
280Foam::IOobjectList Foam::IOobjectList::lookupClassTypeImpl
281(
282 const IOobjectList& list,
283 const MatchPredicate& matchName
284)
285{
286 IOobjectList results(list.size());
287
288 forAllConstIters(list, iter)
289 {
290 const word& key = iter.key();
291 const IOobject* io = iter.val();
292
293 if (io->isHeaderClass<Type>() && matchName(key))
294 {
295 if (IOobject::debug)
296 {
297 InfoInFunction << "Found " << key << endl;
298 }
299
300 results.set(key, new IOobject(*io));
301 }
302 }
303
304 return results;
305}
306
307
308// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
309
310template<class Type>
312(
313 const word& objName
314) const
315{
316 // Like HashPtrTable::get(), or lookup() with a nullptr
317 const IOobject* io = nullptr;
318
319 if (objName.empty())
320 {
321 return nullptr;
322 }
323 else if (auto iter = cfind(objName); iter.good())
324 {
325 io = iter.val();
326 }
327
328 if (IOobject::debug)
329 {
330 if (io)
331 {
332 if (io->isHeaderClass<Type>())
333 {
334 InfoInFunction << "Found " << objName << endl;
335 }
336 else
337 {
338 InfoInFunction << "Found " << objName
339 << " with different type" << endl;
340 }
341 }
342 else
343 {
344 InfoInFunction << "Could not find " << objName << endl;
345 }
346 }
347
348 if (io && io->isHeaderClass<Type>())
349 {
350 return io;
352
353 return nullptr;
354}
355
356
357template<class Type>
359(
360 const word& objName
361) const
362{
363 return cfindObject<Type>(objName);
364}
365
366
367template<class Type>
369{
370 return const_cast<IOobject*>(cfindObject<Type>(objName));
371}
372
373
374template<class Type>
376{
377 return const_cast<IOobject*>(cfindObject<Type>(objName));
378}
379
380
381template<class MatchPredicate>
383(
384 const MatchPredicate& matchName
385) const
386{
387 return lookupImpl(*this, matchName);
388}
389
390
391template<class MatchPredicate>
393(
394 const MatchPredicate& matchClass
395) const
396{
397 return lookupClassImpl(*this, matchClass, predicates::always());
398}
399
400
401template<class MatchPredicate1, class MatchPredicate2>
403(
404 const MatchPredicate1& matchClass,
405 const MatchPredicate2& matchName
406) const
407{
408 return lookupClassImpl(*this, matchClass, matchName);
409}
410
411
412template<class Type>
414{
415 return lookupClassTypeImpl<Type>(*this, predicates::always());
416}
417
418
419template<class Type, class MatchPredicate>
421(
422 const MatchPredicate& matchName
423) const
425 return lookupClassImpl<Type>(*this, matchName);
426}
427
428
429template<class MatchPredicate>
432(
433 const MatchPredicate& matchName
434) const
435{
436 return classesImpl(*this, matchName);
437}
438
439
440template<class MatchPredicate>
442(
443 const MatchPredicate& matchClass
444) const
445{
446 return countImpl(*this, matchClass, predicates::always());
447}
448
449
450template<class MatchPredicate1, class MatchPredicate2>
452(
453 const MatchPredicate1& matchClass,
454 const MatchPredicate2& matchName
455) const
456{
457 return countImpl(*this, matchClass, matchName);
458}
459
460
461template<class Type>
462Foam::label Foam::IOobjectList::count() const
463{
464 return countTypeImpl<Type>(*this, predicates::always());
465}
466
467
468template<class Type, class MatchPredicate>
470(
471 const MatchPredicate& matchName
472) const
473{
474 return countTypeImpl<Type>(*this, matchName);
475}
477
478
479// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
480
481template<class Type>
484{
485 // doSort = false
486 return objectsTypeImpl<Type>(*this, predicates::always(), false);
487}
488
489
490template<class Type>
493{
494 // doSort = true
495 return objectsTypeImpl<Type>(*this, predicates::always(), true);
496}
497
498
499template<class Type>
501Foam::IOobjectList::csorted(const bool syncPar) const
502{
504 (
505 // doSort = true
506 objectsTypeImpl<Type>(*this, predicates::always(), true)
507 );
508
509 checkObjectOrder(list, syncPar);
511 return list;
512}
513
514
515template<class Type, class MatchPredicate>
518(
519 const MatchPredicate& matchName
520) const
521{
522 // doSort = false
523 return objectsTypeImpl<Type>(*this, matchName, false);
524}
525
526
527template<class Type, class MatchPredicate>
530(
531 const MatchPredicate& matchName
532) const
533{
534 // doSort = true
535 return objectsTypeImpl<Type>(*this, matchName, true);
536}
537
538
539template<class Type, class MatchPredicate>
542(
543 const MatchPredicate& matchName,
544 const bool syncPar
545) const
546{
548 (
549 // doSort = true
550 objectsTypeImpl<Type>(*this, matchName, true)
551 );
552
553 checkObjectOrder(list, syncPar);
554
555 return list;
556}
557
558
559// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
560
561template<class MatchPredicate>
563(
564 const MatchPredicate& matchClass
565) const
567 // doSort = false
568 return namesImpl(*this, matchClass, predicates::always(), false);
569}
570
571
572template<class MatchPredicate>
574(
575 const MatchPredicate& matchClass,
576 const bool syncPar
577) const
578{
579 return sortedNames(matchClass, syncPar);
580}
581
582
583template<class MatchPredicate1, class MatchPredicate2>
585(
586 const MatchPredicate1& matchClass,
587 const MatchPredicate2& matchName
588) const
590 // doSort = false
591 return namesImpl(*this, matchClass, matchName, false);
592}
593
594
595template<class MatchPredicate1, class MatchPredicate2>
597(
598 const MatchPredicate1& matchClass,
599 const MatchPredicate2& matchName,
600 const bool syncPar
601) const
602{
603 return sortedNames(matchClass, matchName, syncPar);
604}
605
606
607template<class Type>
610 // doSort = false
611 return namesTypeImpl<Type>(*this, predicates::always(), false);
612}
613
614
615template<class Type>
617{
618 return sortedNames<Type>(syncPar);
619}
620
621
622template<class Type, class MatchPredicate>
624(
625 const MatchPredicate& matchName
626) const
628 // doSort = false
629 return namesTypeImpl<Type>(*this, matchName, false);
630}
631
632
633template<class Type, class MatchPredicate>
635(
636 const MatchPredicate& matchName,
637 const bool syncPar
638) const
639{
640 return sortedNames<Type>(matchName, syncPar);
641}
642
643
644// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
645
646template<class MatchPredicate>
648(
649 const MatchPredicate& matchClass
650) const
651{
652 return namesImpl(*this, matchClass, predicates::always(), true);
653}
654
655
656template<class MatchPredicate>
658(
659 const MatchPredicate& matchClass,
660 const bool syncPar
661) const
662{
663 wordList objNames
664 (
665 namesImpl(*this, matchClass, predicates::always(), true)
666 );
668 checkNameOrder(objNames, syncPar);
669 return objNames;
670}
671
672
673template<class MatchPredicate1, class MatchPredicate2>
675(
676 const MatchPredicate1& matchClass,
677 const MatchPredicate2& matchName
678) const
679{
680 return namesImpl(*this, matchClass, matchName, true);
681}
682
683
684template<class MatchPredicate1, class MatchPredicate2>
686(
687 const MatchPredicate1& matchClass,
688 const MatchPredicate2& matchName,
689 const bool syncPar
690) const
691{
692 wordList objNames(namesImpl(*this, matchClass, matchName, true));
694 checkNameOrder(objNames, syncPar);
695 return objNames;
696}
697
698
699template<class Type>
701{
702 return namesTypeImpl<Type>(*this, predicates::always(), true);
703}
704
705
706template<class Type>
707Foam::wordList Foam::IOobjectList::sortedNames(const bool syncPar) const
708{
709 wordList objNames(namesTypeImpl<Type>(*this, predicates::always(), true));
711 checkNameOrder(objNames, syncPar);
712 return objNames;
713}
714
715
716template<class Type, class MatchPredicate>
718(
719 const MatchPredicate& matchName
720) const
721{
722 return namesTypeImpl<Type>(*this, matchName, true);
723}
724
725
726template<class Type, class MatchPredicate>
728(
729 const MatchPredicate& matchName,
730 const bool syncPar
731) const
732{
733 wordList objNames(namesTypeImpl<Type>(*this, matchName, true));
734
735 checkNameOrder(objNames, syncPar);
736 return objNames;
737}
738
739
740// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
741
742template<class UnaryPredicate>
744(
745 const UnaryPredicate& pred,
746 const bool pruning
747)
748{
749// This is like
750// return HashPtrTable<IOobject>::filterValues
751// (
752// [&](const IOobject* io){ return pred(io->headerClassName()); },
753// pruning
754// );
755// which is really
756// return HashTable<IOobject*>::filterValues
757//
758// except that it does not leak
759
760 label changed = 0;
761
762 for (iterator iter = begin(); iter != end(); ++iter)
763 {
764 // Matches? either prune (pruning) or keep (!pruning)
765 if
766 (
767 (pred(iter.val()->headerClassName()) ? pruning : !pruning)
768 && erase(iter)
769 )
770 {
771 ++changed;
772 }
774
775 return changed;
776}
777
778
779template<class UnaryPredicate>
781(
782 const UnaryPredicate& pred,
783 const bool pruning
784)
785{
786// This is like
787// return HashPtrTable<IOobject>::filterKeys(pred, pruning);
788// which is really
789// return HashTable<IOobject*>::filterKeys(pred, pruning);
790//
791// except that it does not leak
792
793 label changed = 0;
794
795 for (iterator iter = begin(); iter != end(); ++iter)
796 {
797 // Matches? either prune (pruning) or keep (!pruning)
798 if
799 (
800 (pred(iter.key()) ? pruning : !pruning)
801 && erase(iter)
802 )
803 {
804 ++changed;
805 }
807
808 return changed;
809}
810
811
812template<class Type>
814{
815 wordList objNames(namesTypeImpl<Type>(*this, predicates::always(), false));
816
817 syncNames(objNames);
818 return objNames;
819}
820
821
822// ************************************************************************* //
bool erase(iterator &iter)
bool set(const Key &key, T *ptr)
Assign a new entry, overwrites existing.
Forward iterator with non-const access.
Definition HashTable.H:1043
A HashTable similar to std::unordered_map.
Definition HashTable.H:124
iterator begin()
iterator set to the beginning of the HashTable
const_iterator cfind(const Key &key) const
Find and return an const_iterator set at the hashed entry.
Definition HashTableI.H:113
void reserve(label numEntries)
Reserve space for at least the specified number of elements (not the number of buckets) and regenerat...
Definition HashTable.C:729
bool insert(const Key &key, const T &obj)
Copy insert a new entry, not overwriting existing entries.
Definition HashTableI.H:152
label size() const noexcept
The number of elements in table.
Definition HashTable.H:358
iterator end() noexcept
iterator to signal the end (for any HashTable)
List of IOobjects with searching and retrieving facilities. Implemented as a HashTable,...
const IOobject * findObject(const word &objName) const
Return const pointer to the object found by name.
IOobjectList lookup(const MatchPredicate &matchName) const
The list of IOobjects that have a matching object name.
wordList sortedNames() const
The sorted names of the IOobjects.
label filterClasses(const UnaryPredicate &pred, const bool pruning=false)
Filter to retain or prune given classes.
const IOobject * cfindObject(const word &objName) const
Return const pointer to the object found by name.
IOobject * getObject(const word &objName) const
Return non-const pointer to the object found by name, using a const-cast to have it behave like a mut...
HashTable< wordHashSet > classes() const
A summary hash of classes used and their associated object names.
UPtrList< const IOobject > cobjects() const
The unsorted list of IOobjects with headerClassName == Type::typeName.
IOobjectList() noexcept=default
Default construct: empty without allocation (capacity=0).
label filterObjects(const UnaryPredicate &pred, const bool pruning=false)
Filter to retain or prune given object names.
UPtrList< const IOobject > csorted() const
The sorted list of IOobjects with headerClassName == Type::typeName.
wordList allNames() const
The sorted names of all objects (synchronised across processors).
label count() const
The number of objects with headerClassName == Type::typeName.
IOobjectList lookupClass() const
The list of IOobjects with headerClassName == Type::typeName.
wordList names() const
The unsorted names of the IOobjects.
label count(const char *clsName) const
The number of objects of the given headerClassName.
Defines the attributes of an object for which implicit objectRegistry management is supported,...
Definition IOobject.H:191
void resize(const label len)
Adjust allocated size of list.
Definition ListI.H:153
A list of pointers to objects of type <T>, without allocation/deallocation management of the pointers...
Definition UPtrList.H:101
const T * set(const label i) const
Return const pointer to element (can be nullptr), or nullptr for out-of-range access (ie,...
Definition UPtrList.H:366
void resize(const label newLen)
Change the size of the list. Any new entries are nullptr.
Definition UPtrListI.H:251
A class for handling words, derived from Foam::string.
Definition word.H:66
const auto & io
#define InfoInFunction
Report an information message using Foam::Info.
List< word > wordList
List of word.
Definition fileName.H:60
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition Ostream.H:519
void sort(UList< T > &list)
Sort the list.
Definition UList.C:283
srcOptions erase("case")
#define forAllConstIters(container, iter)
Iterate across all elements of the container object with const access.
Definition stdFoam.H:235
Extract name (as a word) from an object, typically using its name() method.
Definition word.H:341
Unary and binary predicates that always return true, useful for templating.
Definition predicates.H:54