ENH: HashSet iterators operator* now return Key.

- much more useful than returning nil. Can now use with a for range:

    for (auto i : myLabelHashSet)
    {
        ...
    }
This commit is contained in:
Mark Olesen
2017-05-02 01:58:38 +02:00
parent 8f75bfbed5
commit f8c58bdd5c
4 changed files with 134 additions and 18 deletions

View File

@ -206,6 +206,12 @@ int main(int argc, char *argv[])
Info<< "added " << added << " from " << someLst.size() << endl; Info<< "added " << added << " from " << someLst.size() << endl;
Info<< "setD : " << flatOutput(setD) << endl; Info<< "setD : " << flatOutput(setD) << endl;
Info<< "setD for-range()" << nl;
for (auto i : setD)
{
Info << i << endl;
}
return 0; return 0;
} }

View File

@ -319,6 +319,54 @@ Foam::operator^
return out; return out;
} }
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
template<class Key, class Hash>
inline typename Foam::HashSet<Key, Hash>::iterator
Foam::HashSet<Key, Hash>::begin()
{
return iterator(this->ParentClass::begin());
}
template<class Key, class Hash>
inline typename Foam::HashSet<Key, Hash>::const_iterator
Foam::HashSet<Key, Hash>::begin() const
{
return const_iterator(this->ParentClass::cbegin());
}
template<class Key, class Hash>
inline typename Foam::HashSet<Key, Hash>::const_iterator
Foam::HashSet<Key, Hash>::cbegin() const
{
return this->begin();
}
template<class Key, class Hash>
inline const typename Foam::HashSet<Key, Hash>::iterator&
Foam::HashSet<Key, Hash>::end()
{
using iter_type = typename HashSet<Key, Hash>::iterator;
return HashTableCore::endIteratorRef<iter_type>();
}
template<class Key, class Hash>
inline const typename Foam::HashSet<Key, Hash>::const_iterator&
Foam::HashSet<Key, Hash>::end() const
{
using iter_type = typename HashSet<Key, Hash>::const_iterator;
return HashTableCore::endIteratorRef<iter_type>();
}
template<class Key, class Hash>
inline const typename Foam::HashSet<Key, Hash>::const_iterator&
Foam::HashSet<Key, Hash>::cend() const
{
using iter_type = typename HashSet<Key, Hash>::const_iterator;
return HashTableCore::endIteratorRef<iter_type>();
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
#endif #endif

View File

@ -3,7 +3,7 @@
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | \\ / O peration |
\\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation \\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation
\\/ M anipulation | Copyright (C) 2016 OpenCFD Ltd. \\/ M anipulation | Copyright (C) 2016-2017 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -52,6 +52,13 @@ Description
namespace Foam namespace Foam
{ {
// Forward declaration of friend functions and operators
template<class Key, class Hash> class HashSet;
template<class Key, class Hash>
Ostream& operator<<(Ostream& os, const HashSet<Key, Hash>& tbl);
/*---------------------------------------------------------------------------*\ /*---------------------------------------------------------------------------*\
Class HashSet Declaration Class HashSet Declaration
\*---------------------------------------------------------------------------*/ \*---------------------------------------------------------------------------*/
@ -61,6 +68,8 @@ class HashSet
: :
public HashTable<nil, Key, Hash> public HashTable<nil, Key, Hash>
{ {
typedef HashTable<nil, Key, Hash> ParentClass;
// Private Member Functions // Private Member Functions
//- Insert values, using begin/end iterators. //- Insert values, using begin/end iterators.
@ -83,22 +92,18 @@ class HashSet
public: public:
using iterator = typename HashTable<nil, Key, Hash>::iterator;
using const_iterator = typename HashTable<nil, Key, Hash>::const_iterator;
// Constructors // Constructors
//- Construct given initial size //- Construct given initial size
HashSet(const label size = 128) HashSet(const label size = 128)
: :
HashTable<nil, Key, Hash>(size) ParentClass(size)
{} {}
//- Construct from Istream //- Construct from Istream
HashSet(Istream& is) HashSet(Istream& is)
: :
HashTable<nil, Key, Hash>(is) ParentClass(is)
{} {}
//- Construct from UList of Key //- Construct from UList of Key
@ -114,13 +119,13 @@ public:
//- Construct as copy //- Construct as copy
HashSet(const HashSet<Key, Hash>& hs) HashSet(const HashSet<Key, Hash>& hs)
: :
HashTable<nil, Key, Hash>(hs) ParentClass(hs)
{} {}
//- Construct by transferring the parameter contents //- Construct by transferring the parameter contents
HashSet(const Xfer<HashSet<Key, Hash>>& hs) HashSet(const Xfer<HashSet<Key, Hash>>& hs)
: :
HashTable<nil, Key, Hash>(hs) ParentClass(hs)
{} {}
//- Construct by transferring the parameter contents //- Construct by transferring the parameter contents
@ -142,7 +147,7 @@ public:
//- Insert a new entry //- Insert a new entry
bool insert(const Key& key) bool insert(const Key& key)
{ {
return HashTable<nil, Key, Hash>::insert(key, nil()); return this->ParentClass::insert(key, nil());
} }
//- Insert keys from the list of Key //- Insert keys from the list of Key
@ -186,29 +191,79 @@ public:
//- Unset the specified key - same as erase //- Unset the specified key - same as erase
bool unset(const Key& key) bool unset(const Key& key)
{ {
return HashTable<nil, Key, Hash>::erase(key); return this->ParentClass::erase(key);
} }
//- Unset the listed keys - same as erase //- Unset the listed keys - same as erase
label unset(const UList<Key>& lst) label unset(const UList<Key>& lst)
{ {
return HashTable<nil, Key, Hash>::erase(lst); return this->ParentClass::erase(lst);
} }
//- Unset the listed keys - same as erase //- Unset the listed keys - same as erase
template<unsigned Size> template<unsigned Size>
label unset(const FixedList<Key, Size>& lst) label unset(const FixedList<Key, Size>& lst)
{ {
return HashTable<nil, Key, Hash>::erase(lst); return this->ParentClass::erase(lst);
} }
//- Unset the listed keys - same as erase //- Unset the listed keys - same as erase
label unset(std::initializer_list<Key> lst) label unset(std::initializer_list<Key> lst)
{ {
return HashTable<nil, Key, Hash>::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 // Writing
//- Write the unordered keys as a list, with line-breaks if list length //- 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 //- Remove entries listed in the given HashSet from this HashSet
void operator-=(const HashSet<Key, Hash>& rhs); void operator-=(const HashSet<Key, Hash>& rhs);
// IOstream Operator
friend Ostream& operator<< <Key, Hash>
(
Ostream& os,
const HashSet<Key, Hash>& tbl
);
}; };

View File

@ -64,11 +64,8 @@ namespace Foam
template<class T> class List; template<class T> class List;
template<class T> class UList; template<class T> class UList;
template<class T, unsigned Size> class FixedList; template<class T, unsigned Size> class FixedList;
template<class T1, class T2> class Tuple2;
template<class T, class Key, class Hash> class HashTable; template<class T, class Key, class Hash> class HashTable;
template<class T, class Key, class Hash> class HashPtrTable;
template<class Type1, class Type2>
class Tuple2;
template<class T, class Key, class Hash> template<class T, class Key, class Hash>
Istream& operator>>(Istream& is, HashTable<T, Key, Hash>& L); Istream& operator>>(Istream& is, HashTable<T, Key, Hash>& L);