From f8c58bdd5c6580ed2e21a57a4e9ea5d9e0b7d318 Mon Sep 17 00:00:00 2001 From: Mark Olesen Date: Tue, 2 May 2017 01:58:38 +0200 Subject: [PATCH] ENH: HashSet iterators operator* now return Key. - much more useful than returning nil. Can now use with a for range: for (auto i : myLabelHashSet) { ... } --- applications/test/HashSet/Test-hashSet.C | 6 ++ .../containers/HashTables/HashSet/HashSet.C | 48 ++++++++++ .../containers/HashTables/HashSet/HashSet.H | 93 ++++++++++++++++--- .../HashTables/HashTable/HashTable.H | 5 +- 4 files changed, 134 insertions(+), 18 deletions(-) diff --git a/applications/test/HashSet/Test-hashSet.C b/applications/test/HashSet/Test-hashSet.C index 1534065a46..fdfecaa08c 100644 --- a/applications/test/HashSet/Test-hashSet.C +++ b/applications/test/HashSet/Test-hashSet.C @@ -206,6 +206,12 @@ int main(int argc, char *argv[]) Info<< "added " << added << " from " << someLst.size() << endl; Info<< "setD : " << flatOutput(setD) << endl; + Info<< "setD for-range()" << nl; + + for (auto i : setD) + { + Info << i << endl; + } return 0; } diff --git a/src/OpenFOAM/containers/HashTables/HashSet/HashSet.C b/src/OpenFOAM/containers/HashTables/HashSet/HashSet.C index 796069c109..fb33e0e2c7 100644 --- a/src/OpenFOAM/containers/HashTables/HashSet/HashSet.C +++ b/src/OpenFOAM/containers/HashTables/HashSet/HashSet.C @@ -319,6 +319,54 @@ Foam::operator^ return out; } +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +template +inline typename Foam::HashSet::iterator +Foam::HashSet::begin() +{ + return iterator(this->ParentClass::begin()); +} + +template +inline typename Foam::HashSet::const_iterator +Foam::HashSet::begin() const +{ + return const_iterator(this->ParentClass::cbegin()); +} + +template +inline typename Foam::HashSet::const_iterator +Foam::HashSet::cbegin() const +{ + return this->begin(); +} + +template +inline const typename Foam::HashSet::iterator& +Foam::HashSet::end() +{ + using iter_type = typename HashSet::iterator; + return HashTableCore::endIteratorRef(); +} + +template +inline const typename Foam::HashSet::const_iterator& +Foam::HashSet::end() const +{ + using iter_type = typename HashSet::const_iterator; + return HashTableCore::endIteratorRef(); +} + +template +inline const typename Foam::HashSet::const_iterator& +Foam::HashSet::cend() const +{ + using iter_type = typename HashSet::const_iterator; + return HashTableCore::endIteratorRef(); +} + + // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // #endif diff --git a/src/OpenFOAM/containers/HashTables/HashSet/HashSet.H b/src/OpenFOAM/containers/HashTables/HashSet/HashSet.H index c6229c4e3f..f7f780fe71 100644 --- a/src/OpenFOAM/containers/HashTables/HashSet/HashSet.H +++ b/src/OpenFOAM/containers/HashTables/HashSet/HashSet.H @@ -3,7 +3,7 @@ \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | \\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation - \\/ M anipulation | Copyright (C) 2016 OpenCFD Ltd. + \\/ M anipulation | Copyright (C) 2016-2017 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -52,6 +52,13 @@ Description namespace Foam { +// Forward declaration of friend functions and operators +template class HashSet; + +template +Ostream& operator<<(Ostream& os, const HashSet& tbl); + + /*---------------------------------------------------------------------------*\ Class HashSet Declaration \*---------------------------------------------------------------------------*/ @@ -61,6 +68,8 @@ class HashSet : public HashTable { + typedef HashTable ParentClass; + // Private Member Functions //- Insert values, using begin/end iterators. @@ -83,22 +92,18 @@ class HashSet public: - using iterator = typename HashTable::iterator; - using const_iterator = typename HashTable::const_iterator; - - // Constructors //- Construct given initial size HashSet(const label size = 128) : - HashTable(size) + ParentClass(size) {} //- Construct from Istream HashSet(Istream& is) : - HashTable(is) + ParentClass(is) {} //- Construct from UList of Key @@ -114,13 +119,13 @@ public: //- Construct as copy HashSet(const HashSet& hs) : - HashTable(hs) + ParentClass(hs) {} //- Construct by transferring the parameter contents HashSet(const Xfer>& hs) : - HashTable(hs) + ParentClass(hs) {} //- Construct by transferring the parameter contents @@ -142,7 +147,7 @@ public: //- Insert a new entry bool insert(const Key& key) { - return HashTable::insert(key, nil()); + return this->ParentClass::insert(key, nil()); } //- Insert keys from the list of Key @@ -186,29 +191,79 @@ public: //- Unset the specified key - same as erase bool unset(const Key& key) { - return HashTable::erase(key); + return this->ParentClass::erase(key); } //- Unset the listed keys - same as erase label unset(const UList& lst) { - return HashTable::erase(lst); + return this->ParentClass::erase(lst); } //- Unset the listed keys - same as erase template label unset(const FixedList& lst) { - return HashTable::erase(lst); + return this->ParentClass::erase(lst); } //- Unset the listed keys - same as erase label unset(std::initializer_list lst) { - return HashTable::erase(lst); + return this->ParentClass::erase(lst); } + // STL iterator + + //- An STL-conforming iterator + struct iterator + : + public ParentClass::iterator + { + using parent = typename ParentClass::iterator; + + //- Implicit conversion + iterator(const parent& iter) + : + parent(iter) + {} + + //- Return the key + auto operator*() const -> const Key& + { + return this->key(); + } + }; + + //- An STL-conforming const_iterator + struct const_iterator + : + public ParentClass::const_iterator + { + using parent = typename ParentClass::const_iterator; + + //- Implicit conversion + const_iterator(const parent& iter) + : + parent(iter) + {} + + //- Return the key + auto operator*() const -> const Key& + { + return this->key(); + } + }; + + auto begin() -> iterator; + auto begin() const -> const_iterator; + auto cbegin() const -> const_iterator; + auto end() -> const iterator&; + auto end() const -> const const_iterator&; + auto cend() const -> const const_iterator&; + + // Writing //- Write the unordered keys as a list, with line-breaks if list length @@ -260,6 +315,16 @@ public: //- Remove entries listed in the given HashSet from this HashSet void operator-=(const HashSet& rhs); + + + // IOstream Operator + + friend Ostream& operator<< + ( + Ostream& os, + const HashSet& tbl + ); + }; diff --git a/src/OpenFOAM/containers/HashTables/HashTable/HashTable.H b/src/OpenFOAM/containers/HashTables/HashTable/HashTable.H index f90b0a6a26..adc3f0aa74 100644 --- a/src/OpenFOAM/containers/HashTables/HashTable/HashTable.H +++ b/src/OpenFOAM/containers/HashTables/HashTable/HashTable.H @@ -64,11 +64,8 @@ namespace Foam template class List; template class UList; template class FixedList; +template class Tuple2; template class HashTable; -template class HashPtrTable; - -template -class Tuple2; template Istream& operator>>(Istream& is, HashTable& L);