From 21e7ce8f42fd130aae151fb4205e8b49198a036e Mon Sep 17 00:00:00 2001 From: Mark Olesen Date: Thu, 17 Nov 2022 15:12:11 +0100 Subject: [PATCH] STYLE: place HashTable trivial methods in the header (reduce clutter) --- .../test/IOobjectList/Test-IOobjectList.C | 41 ++-------- .../HashTables/HashPtrTable/HashPtrTable.C | 10 --- .../HashTables/HashPtrTable/HashPtrTable.H | 12 +-- .../HashTables/HashPtrTable/HashPtrTableI.H | 10 +++ .../containers/HashTables/HashSet/HashSet.H | 4 +- .../HashTables/HashTable/HashTable.H | 81 ++++++++++--------- .../HashTables/HashTable/HashTableCore.H | 6 +- .../HashTables/HashTable/HashTableIterI.H | 61 +------------- src/OpenFOAM/containers/HashTables/Map/Map.H | 6 +- .../containers/HashTables/PtrMap/PtrMap.H | 4 +- src/OpenFOAM/db/IOobjectList/IOobjectList.C | 25 +++--- .../db/IOobjectList/IOobjectListTemplates.C | 32 +++++--- 12 files changed, 115 insertions(+), 177 deletions(-) diff --git a/applications/test/IOobjectList/Test-IOobjectList.C b/applications/test/IOobjectList/Test-IOobjectList.C index dc46bc710b..2e1e261a2f 100644 --- a/applications/test/IOobjectList/Test-IOobjectList.C +++ b/applications/test/IOobjectList/Test-IOobjectList.C @@ -240,13 +240,8 @@ int main(int argc, char *argv[]) ); argList::addBoolOption ( - "copy-append", - "test move append lists (requires -filter)" - ); - argList::addBoolOption - ( - "move-append", - "test move append lists (requires -filter)" + "merge", + "test merging lists (requires -filter)" ); // timeSelector::addOptions(); @@ -261,16 +256,10 @@ int main(int argc, char *argv[]) Info<<"limit names: " << matcher << nl; } - if (args.found("copy-append") && matcher.empty()) + if (args.found("merge") && matcher.empty()) { FatalError - << nl << "The -copy-append test also requires -filter" << nl - << exit(FatalError); - } - if (args.found("move-append") && matcher.empty()) - { - FatalError - << nl << "The -move-append test also requires -filter" << nl + << nl << "The -merge test also requires -filter" << nl << exit(FatalError); } @@ -327,13 +316,9 @@ int main(int argc, char *argv[]) // On last time if (timeI == timeDirs.size()-1) { - if (args.found("copy-append")) + if (args.found("merge")) { - Info<< nl << "Test move append" << nl; - } - else if (args.found("move-append")) - { - Info<< nl << "Test move append" << nl; + Info<< nl << "Test merge" << nl; } else { @@ -349,18 +334,8 @@ int main(int argc, char *argv[]) Info<< "==target==" << nl; reportDetail(objects); Info<< "==source==" << nl; reportDetail(other); - if (args.found("copy-append")) - { - objects.append(other); - - Info<< nl << "After copy-append" << nl; - } - else - { - objects.append(std::move(other)); - - Info<< nl << "After move-append" << nl; - } + objects.merge(std::move(other)); + Info<< nl << "After merge" << nl; Info<< "==target==" << nl; reportDetail(objects); Info<< "==source==" << nl; reportDetail(other); diff --git a/src/OpenFOAM/containers/HashTables/HashPtrTable/HashPtrTable.C b/src/OpenFOAM/containers/HashTables/HashPtrTable/HashPtrTable.C index eedbc26416..f9b7c10fb4 100644 --- a/src/OpenFOAM/containers/HashTables/HashPtrTable/HashPtrTable.C +++ b/src/OpenFOAM/containers/HashTables/HashPtrTable/HashPtrTable.C @@ -57,16 +57,6 @@ Foam::HashPtrTable::HashPtrTable } -template -Foam::HashPtrTable::HashPtrTable -( - HashPtrTable&& rhs -) -: - parent_type(std::move(rhs)) -{} - - // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * // template diff --git a/src/OpenFOAM/containers/HashTables/HashPtrTable/HashPtrTable.H b/src/OpenFOAM/containers/HashTables/HashPtrTable/HashPtrTable.H index f3eca79cc9..50175e0887 100644 --- a/src/OpenFOAM/containers/HashTables/HashPtrTable/HashPtrTable.H +++ b/src/OpenFOAM/containers/HashTables/HashPtrTable/HashPtrTable.H @@ -99,6 +99,12 @@ public: //- Construct given initial table capacity inline explicit HashPtrTable(const label size); + //- Copy construct, making a copy of each element + HashPtrTable(const this_type& rhs); + + //- Move construct + inline HashPtrTable(this_type&& rhs); + //- Construct from Istream using given Istream constructor class template HashPtrTable(Istream& is, const INew& inew); @@ -109,12 +115,6 @@ public: //- Construct from dictionary with default dictionary constructor class explicit HashPtrTable(const dictionary& dict); - //- Copy construct, making a copy of each element - HashPtrTable(const this_type& rhs); - - //- Move construct - HashPtrTable(this_type&& rhs); - //- Destructor ~HashPtrTable(); diff --git a/src/OpenFOAM/containers/HashTables/HashPtrTable/HashPtrTableI.H b/src/OpenFOAM/containers/HashTables/HashPtrTable/HashPtrTableI.H index 9213091085..50f29c4415 100644 --- a/src/OpenFOAM/containers/HashTables/HashPtrTable/HashPtrTableI.H +++ b/src/OpenFOAM/containers/HashTables/HashPtrTable/HashPtrTableI.H @@ -38,6 +38,16 @@ inline Foam::HashPtrTable::HashPtrTable(const label size) {} +template +inline Foam::HashPtrTable::HashPtrTable +( + HashPtrTable&& rhs +) +: + parent_type(std::move(rhs)) +{} + + // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // template diff --git a/src/OpenFOAM/containers/HashTables/HashSet/HashSet.H b/src/OpenFOAM/containers/HashTables/HashSet/HashSet.H index fc6ba60074..042211fc19 100644 --- a/src/OpenFOAM/containers/HashTables/HashSet/HashSet.H +++ b/src/OpenFOAM/containers/HashTables/HashSet/HashSet.H @@ -62,8 +62,8 @@ Description \*---------------------------------------------------------------------------*/ -#ifndef HashSet_H -#define HashSet_H +#ifndef Foam_HashSet_H +#define Foam_HashSet_H #include "HashTable.H" #include "IndirectList.H" diff --git a/src/OpenFOAM/containers/HashTables/HashTable/HashTable.H b/src/OpenFOAM/containers/HashTables/HashTable/HashTable.H index 337effa745..4f2f17b49d 100644 --- a/src/OpenFOAM/containers/HashTables/HashTable/HashTable.H +++ b/src/OpenFOAM/containers/HashTables/HashTable/HashTable.H @@ -6,7 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011-2016 OpenFOAM Foundation - Copyright (C) 2017-2021 OpenCFD Ltd. + Copyright (C) 2017-2022 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -273,7 +273,7 @@ public: //- Find and return a hashed entry. FatalError if it does not exist. inline const T& at(const Key& key) const; - //- Return true if hashed entry is found in table + //- True if hashed key is found in table inline bool found(const Key& key) const; //- Find and return an iterator set at the hashed entry @@ -630,39 +630,7 @@ protected: >::type; - // Member Functions - - //- True if iterator points to an entry - // This can be used directly instead of comparing to end() - inline bool good() const noexcept; - - //- True if iterator points to an entry - same as good() - inline bool found() const noexcept; - - //- The key associated with the iterator - inline const Key& key() const; - - //- Write the (key, val) pair - inline Ostream& print(Ostream& os) const; - - - // Member Operators - - //- True if iterator points to an entry - // This can be used directly instead of comparing to end() - explicit inline operator bool() const noexcept; - - //- Compare hash-entry element pointers. - // Independent of const/non-const access - template - inline bool operator==(const Iterator& iter) const noexcept; - - template - inline bool operator!=(const Iterator& iter) const noexcept; - - protected: - friend class HashTable; // For begin/find constructors // Protected Data @@ -680,10 +648,13 @@ protected: // to mark the position. label index_; + // Friendship with HashTable, for begin/find constructors + friend class HashTable; + // Protected Constructors - //- Default construct (end iterator) + //- Default construct. Also the same as the end iterator inline constexpr Iterator() noexcept; //- Construct from begin of hash-table @@ -704,9 +675,47 @@ protected: { return *reinterpret_cast*>(this); } - }; + public: + + // Member Functions + + //- True if iterator points to an entry + // This can be used directly instead of comparing to end() + bool good() const noexcept { return entry_; } + + //- True if iterator points to an entry - same as good() + bool found() const noexcept { return entry_; } + + //- The key associated with the iterator + const Key& key() const { return entry_->key(); } + + //- Write the (key, val) pair + inline Ostream& print(Ostream& os) const; + + + // Member Operators + + //- True if iterator points to an entry + // This can be used directly instead of comparing to end() + explicit operator bool() const noexcept { return entry_; } + + //- Compare hash-entry element pointers. + // Independent of const/non-const access + template + bool operator==(const Iterator& iter) const noexcept + { + return (entry_ == iter.entry_); + } + + template + bool operator!=(const Iterator& iter) const noexcept + { + return (entry_ != iter.entry_); + } + }; + //- Low-level entry erasure using iterator internals. // This invalidates the iterator until the next ++ operation. // \return True if the corresponding entry existed and was removed diff --git a/src/OpenFOAM/containers/HashTables/HashTable/HashTableCore.H b/src/OpenFOAM/containers/HashTables/HashTable/HashTableCore.H index e483ee1783..aa5377ad19 100644 --- a/src/OpenFOAM/containers/HashTables/HashTable/HashTableCore.H +++ b/src/OpenFOAM/containers/HashTables/HashTable/HashTableCore.H @@ -36,8 +36,8 @@ SourceFiles \*---------------------------------------------------------------------------*/ -#ifndef HashTableCore_H -#define HashTableCore_H +#ifndef Foam_HashTableCore_H +#define Foam_HashTableCore_H #include "label.H" #include "uLabel.H" @@ -66,7 +66,7 @@ struct HashTableCore ClassName("HashTable"); //- Default construct - HashTableCore() = default; + HashTableCore() noexcept = default; static_assert ( diff --git a/src/OpenFOAM/containers/HashTables/HashTable/HashTableIterI.H b/src/OpenFOAM/containers/HashTables/HashTable/HashTableIterI.H index 07be2b62a8..ab36301c5f 100644 --- a/src/OpenFOAM/containers/HashTables/HashTable/HashTableIterI.H +++ b/src/OpenFOAM/containers/HashTables/HashTable/HashTableIterI.H @@ -5,7 +5,7 @@ \\ / A nd | www.openfoam.com \\/ M anipulation | ------------------------------------------------------------------------------- - Copyright (C) 2017-2020 OpenCFD Ltd. + Copyright (C) 2017-2022 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -109,32 +109,6 @@ Foam::HashTable::Iterator::increment() } -template -template -inline bool -Foam::HashTable::Iterator::good() const noexcept -{ - return entry_; -} - - -template -template -inline bool -Foam::HashTable::Iterator::found() const noexcept -{ - return entry_; -} - - -template -template -inline const Key& Foam::HashTable::Iterator::key() const -{ - return entry_->key(); -} - - template template inline Foam::Ostream& Foam::HashTable::Iterator::print @@ -150,39 +124,6 @@ inline Foam::Ostream& Foam::HashTable::Iterator::print } -template -template -inline Foam::HashTable::Iterator::operator -bool() const noexcept -{ - return entry_; -} - - -template -template -template -inline bool Foam::HashTable::Iterator::operator== -( - const Iterator& iter -) const noexcept -{ - return entry_ == iter.entry_; -} - - -template -template -template -inline bool Foam::HashTable::Iterator::operator!= -( - const Iterator& iter -) const noexcept -{ - return entry_ != iter.entry_; -} - - // * * * * * * * * * * * * * * * * STL iterator * * * * * * * * * * * * * * // template diff --git a/src/OpenFOAM/containers/HashTables/Map/Map.H b/src/OpenFOAM/containers/HashTables/Map/Map.H index 406151539d..fe10faf945 100644 --- a/src/OpenFOAM/containers/HashTables/Map/Map.H +++ b/src/OpenFOAM/containers/HashTables/Map/Map.H @@ -40,8 +40,8 @@ See also \*---------------------------------------------------------------------------*/ -#ifndef Map_H -#define Map_H +#ifndef Foam_Map_H +#define Foam_Map_H #include "HashTable.H" @@ -103,7 +103,7 @@ public: parent_type(std::move(map)) {} - //- Construct from an initializer list + //- Construct from pairs of values Map(std::initializer_list> map) : parent_type(map) diff --git a/src/OpenFOAM/containers/HashTables/PtrMap/PtrMap.H b/src/OpenFOAM/containers/HashTables/PtrMap/PtrMap.H index 46963c08f0..2313fd5205 100644 --- a/src/OpenFOAM/containers/HashTables/PtrMap/PtrMap.H +++ b/src/OpenFOAM/containers/HashTables/PtrMap/PtrMap.H @@ -35,8 +35,8 @@ See also \*---------------------------------------------------------------------------*/ -#ifndef PtrMap_H -#define PtrMap_H +#ifndef Foam_PtrMap_H +#define Foam_PtrMap_H #include "HashPtrTable.H" diff --git a/src/OpenFOAM/db/IOobjectList/IOobjectList.C b/src/OpenFOAM/db/IOobjectList/IOobjectList.C index 3a035b9f47..05729db4fb 100644 --- a/src/OpenFOAM/db/IOobjectList/IOobjectList.C +++ b/src/OpenFOAM/db/IOobjectList/IOobjectList.C @@ -189,23 +189,28 @@ const Foam::IOobject* Foam::IOobjectList::cfindObject const word& objName ) const { - const_iterator iter = cfind(objName); + // Like HashPtrTable::get(), or lookup() with a nullptr + const IOobject* io = nullptr; - if (iter.found()) + const const_iterator iter(cfind(objName)); + if (iter.good()) { - if (IOobject::debug) + io = iter.val(); + } + + if (IOobject::debug) + { + if (io) { InfoInFunction << "Found " << objName << endl; } - - return iter.val(); - } - else if (IOobject::debug) - { - InfoInFunction << "Could not find " << objName << endl; + else + { + InfoInFunction << "Could not find " << objName << endl; + } } - return nullptr; + return io; } diff --git a/src/OpenFOAM/db/IOobjectList/IOobjectListTemplates.C b/src/OpenFOAM/db/IOobjectList/IOobjectListTemplates.C index 87c807ba65..515e964715 100644 --- a/src/OpenFOAM/db/IOobjectList/IOobjectListTemplates.C +++ b/src/OpenFOAM/db/IOobjectList/IOobjectListTemplates.C @@ -308,30 +308,38 @@ const Foam::IOobject* Foam::IOobjectList::cfindObject const word& objName ) const { - const_iterator iter = cfind(objName); + // Like HashPtrTable::get(), or lookup() with a nullptr + const IOobject* io = nullptr; - if (iter.found()) + const const_iterator iter(cfind(objName)); + if (iter.good()) { - const IOobject* io = iter.val(); + io = iter.val(); + } - if (io->isHeaderClass()) + if (IOobject::debug) + { + if (io) { - if (IOobject::debug) + if (io->isHeaderClass()) { InfoInFunction << "Found " << objName << endl; } - - return io; + else + { + InfoInFunction << "Found " << objName + << " with different type" << endl; + } } - else if (IOobject::debug) + else { - InfoInFunction - << "Found " << objName << " of different type" << endl; + InfoInFunction << "Could not find " << objName << endl; } } - else if (IOobject::debug) + + if (io && io->isHeaderClass()) { - InfoInFunction << "Could not find " << objName << endl; + return io; } return nullptr;