Commit 8e63f084 authored by Mark Olesen's avatar Mark Olesen
Browse files

ENH: improve type consistency for HashTable iterators (#1563)

parent beebe4e2
......@@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2016 OpenFOAM Foundation
Copyright (C) 2016-2019 OpenCFD Ltd.
Copyright (C) 2016-2020 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
......@@ -246,14 +246,14 @@ inline Foam::label Foam::HashSet<Key, Hash>::unset
// * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
template<class Key, class Hash>
inline bool Foam::HashSet<Key, Hash>::operator()(const Key& key) const
inline bool Foam::HashSet<Key, Hash>::operator()(const Key& key) const noexcept
{
return this->found(key);
}
template<class Key, class Hash>
inline bool Foam::HashSet<Key, Hash>::operator[](const Key& key) const
inline bool Foam::HashSet<Key, Hash>::operator[](const Key& key) const noexcept
{
return this->found(key);
}
......@@ -353,6 +353,14 @@ Foam::HashSet<Key, Hash>::operator^=(const HashSet<Key, Hash>& rhs)
}
template<class Key, class Hash>
inline Foam::HashSet<Key, Hash>&
Foam::HashSet<Key, Hash>::operator+=(const HashSet<Key, Hash>& rhs)
{
return this->operator|=(rhs);
}
template<class Key, class Hash>
inline Foam::HashSet<Key, Hash>&
Foam::HashSet<Key, Hash>::operator-=(const HashSet<Key, Hash>& rhs)
......@@ -419,9 +427,9 @@ template<class Key, class Hash>
inline typename Foam::HashSet<Key, Hash>::iterator
Foam::HashSet<Key, Hash>::begin()
{
return HashTableCore::iterator_begin<iterator>
return iterator
(
static_cast<parent_type&>(*this)
static_cast<parent_type&>(*this).begin()
);
}
......@@ -430,9 +438,9 @@ template<class Key, class Hash>
inline typename Foam::HashSet<Key, Hash>::const_iterator
Foam::HashSet<Key, Hash>::begin() const
{
return HashTableCore::iterator_cbegin<const_iterator>
return const_iterator
(
static_cast<const parent_type&>(*this)
static_cast<const parent_type&>(*this).begin()
);
}
......@@ -441,34 +449,34 @@ template<class Key, class Hash>
inline typename Foam::HashSet<Key, Hash>::const_iterator
Foam::HashSet<Key, Hash>::cbegin() const
{
return HashTableCore::iterator_cbegin<const_iterator>
return const_iterator
(
static_cast<const parent_type&>(*this)
static_cast<const parent_type&>(*this).cbegin()
);
}
template<class Key, class Hash>
inline const typename Foam::HashSet<Key, Hash>::iterator&
Foam::HashSet<Key, Hash>::end()
inline typename Foam::HashSet<Key, Hash>::iterator
Foam::HashSet<Key, Hash>::end() noexcept
{
return HashTableCore::iterator_end<iterator>();
return iterator();
}
template<class Key, class Hash>
inline const typename Foam::HashSet<Key, Hash>::const_iterator&
Foam::HashSet<Key, Hash>::end() const
inline typename Foam::HashSet<Key, Hash>::const_iterator
Foam::HashSet<Key, Hash>::end() const noexcept
{
return HashTableCore::iterator_cend<const_iterator>();
return const_iterator();
}
template<class Key, class Hash>
inline const typename Foam::HashSet<Key, Hash>::const_iterator&
Foam::HashSet<Key, Hash>::cend() const
inline constexpr typename Foam::HashSet<Key, Hash>::const_iterator
Foam::HashSet<Key, Hash>::cend() const noexcept
{
return HashTableCore::iterator_cend<const_iterator>();
return const_iterator();
}
......
......@@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2016 OpenFOAM Foundation
Copyright (C) 2016-2019 OpenCFD Ltd.
Copyright (C) 2016-2020 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
......@@ -73,10 +73,9 @@ Description
namespace Foam
{
// Forward declarations
// Forward Declarations
template<class T> class MinMax;
/*---------------------------------------------------------------------------*\
Class HashSet Declaration
\*---------------------------------------------------------------------------*/
......@@ -115,12 +114,24 @@ public:
// Constructors
//- Construct null with default (128) table capacity
//- Default construct with default (128) table capacity
HashSet()
:
parent_type()
{}
//- Copy construct
HashSet(const this_type& rhs)
:
parent_type(rhs)
{}
//- Move construct
HashSet(this_type&& rhs)
:
parent_type(std::move(rhs))
{}
//- Construct given initial table capacity
explicit HashSet(const label size)
:
......@@ -128,7 +139,7 @@ public:
{}
//- Construct from Istream with default table capacity
HashSet(Istream& is)
explicit HashSet(Istream& is)
:
parent_type(is)
{}
......@@ -147,18 +158,6 @@ public:
//- Construct from an initializer list of Key
HashSet(std::initializer_list<Key> list);
//- Copy construct
HashSet(const this_type& hs)
:
parent_type(hs)
{}
//- Move construct
HashSet(this_type&& hs)
:
parent_type(std::move(hs))
{}
//- Construct from the keys of another HashTable,
//- the type of values held is arbitrary.
template<class AnyType, class AnyHash>
......@@ -169,7 +168,7 @@ public:
//- Same as found() - return true if key exists in the set.
// Method name compatibility with bitSet and boolList.
bool test(const Key& key) const
bool test(const Key& key) const noexcept
{
return found(key);
}
......@@ -291,13 +290,13 @@ public:
// STL iterators
iterator begin();
const_iterator begin() const;
const_iterator cbegin() const;
inline iterator begin();
inline const_iterator begin() const;
inline const_iterator cbegin() const;
const iterator& end();
const const_iterator& end() const;
const const_iterator& cend() const;
inline iterator end() noexcept;
inline const_iterator end() const noexcept;
inline constexpr const_iterator cend() const noexcept;
// Writing
......@@ -314,10 +313,10 @@ public:
// Member Operators
//- Return true if the entry exists, same as found()
inline bool operator()(const Key& key) const;
inline bool operator()(const Key& key) const noexcept;
//- Return true if the entry exists, same as found().
inline bool operator[](const Key& key) const;
inline bool operator[](const Key& key) const noexcept;
using parent_type::operator=;
......@@ -368,13 +367,10 @@ public:
//- Only retain unique entries (xor)
this_type& operator^=(const this_type& rhs);
//- Add entries to this HashSet
inline this_type& operator+=(const this_type& rhs)
{
return this->operator|=(rhs);
}
//- Add entries to this HashSet. Same as the '|=' operator
inline this_type& operator+=(const this_type& rhs);
//- Remove entries from this HashSet
//- Remove entries from this HashSet. Uses erase()
inline this_type& operator-=(const this_type& rhs);
......@@ -403,7 +399,6 @@ public:
//- Not applicable for HashSet
template<class BinaryPredicate>
label filterEntries(const BinaryPredicate&, const bool) = delete;
};
......
......@@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2016 OpenFOAM Foundation
Copyright (C) 2017-2019 OpenCFD Ltd.
Copyright (C) 2017-2020 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
......@@ -105,7 +105,6 @@ template<class T> class UList;
template<class T, unsigned N> class FixedList;
template<class T, class Key, class Hash> class HashTable;
/*---------------------------------------------------------------------------*\
Class HashTable Declaration
\*---------------------------------------------------------------------------*/
......@@ -171,10 +170,17 @@ public:
//- The third template parameter, the hash index method.
typedef Hash hasher;
//- Pointer type for storing into value_type objects.
// This type is usually 'value_type*'.
typedef T* pointer;
//- Reference to the stored value_type.
// This type is usually 'value_type&'.
typedef T& reference;
//- Const pointer type for the stored value_type.
typedef const T* const_pointer;
//- Const reference to the stored value_type.
typedef const T& const_reference;
......@@ -587,10 +593,10 @@ protected:
//- True if iterator points to an entry
// This can be used directly instead of comparing to end()
inline bool good() const;
inline bool good() const noexcept;
//- True if iterator points to an entry - same as good()
inline bool found() const;
inline bool found() const noexcept;
//- The key associated with the iterator
inline const Key& key() const;
......@@ -607,11 +613,11 @@ protected:
//- Compare hash-entry element pointers.
// Independent of const/non-const access
inline bool operator==(const Iterator<true>& iter) const;
inline bool operator!=(const Iterator<true>& iter) const;
template<bool Any>
inline bool operator==(const Iterator<Any>& iter) const noexcept;
inline bool operator==(const Iterator<false>& iter) const;
inline bool operator!=(const Iterator<false>& iter) const;
template<bool Any>
inline bool operator!=(const Iterator<Any>& iter) const noexcept;
protected:
......@@ -621,11 +627,11 @@ protected:
//- The selected entry.
// MUST be the first member for easy comparison between iterators
// and for reinterpret_cast from nullObject
// and to support reinterpret_cast from nullObject
node_type* entry_;
//- The hash-table container being iterated on.
// Using a pointer allows default bitwise copy/assignment
// Uses pointer for default copy/assignment
table_type* container_;
//- Index within the hash-table data.
......@@ -636,11 +642,11 @@ protected:
// Protected Constructors
//- Construct null (end iterator)
inline Iterator();
//- Default construct (end iterator)
inline constexpr Iterator() noexcept;
//- Construct from begin of hash-table
inline Iterator(bool, table_type* tbl);
inline explicit Iterator(table_type* tbl);
//- Construct by finding key in hash table
Iterator(table_type* tbl, const Key& key);
......@@ -651,10 +657,11 @@ protected:
//- Increment to the next position
inline void increment();
//- Permit an explicit cast to the other (const/non-const) searcher
inline explicit operator const Iterator<!Const>&() const
//- Permit explicit cast to the other (const/non-const) iterator
template<bool Any>
explicit operator const Iterator<Any>&() const
{
return *reinterpret_cast<const Iterator<!Const>*>(this);
return *reinterpret_cast<const Iterator<Any>*>(this);
}
};
......@@ -680,7 +687,9 @@ public:
using key_type = this_type::key_type;
using mapped_type = this_type::mapped_type;
using value_type = this_type::value_type;
using pointer = this_type::pointer;
using reference = this_type::reference;
using const_pointer = this_type::const_pointer;
using const_reference = this_type::const_reference;
......@@ -690,7 +699,7 @@ public:
iterator() = default;
//- Copy construct from similar access type
inline explicit iterator(const Iterator<false>& iter)
explicit iterator(const Iterator<false>& iter)
:
Iterator<false>(iter)
{}
......@@ -739,10 +748,11 @@ public:
using key_type = this_type::key_type;
using mapped_type = const this_type::mapped_type;
using value_type = const this_type::value_type;
using pointer = this_type::const_pointer;
using reference = this_type::const_reference;
// Constructors
// Generated Methods
//- Default construct (end iterator)
const_iterator() = default;
......@@ -750,23 +760,21 @@ public:
//- Copy construct
const_iterator(const const_iterator&) = default;
//- Copy construct from similar access type
inline explicit const_iterator(const Iterator<true>& iter)
:
Iterator<true>(iter)
{}
//- Copy assignment
const_iterator& operator=(const const_iterator&) = default;
//- Copy construct from dissimilar access type
inline explicit const_iterator(const Iterator<false>& iter)
// Constructors
//- Copy construct from any access type
template<bool Any>
const_iterator(const Iterator<Any>& iter)
:
Iterator<true>
(
static_cast<const Iterator<true>&>(iter)
)
Iterator<true>(static_cast<const Iterator<Any>&>(iter))
{}
//- Implicit conversion from dissimilar access type
inline const_iterator(const iterator& iter)
const_iterator(const iterator& iter)
:
const_iterator(reinterpret_cast<const const_iterator&>(iter))
{}
......@@ -790,9 +798,6 @@ public:
// Assignment
//- Copy assignment
const_iterator& operator=(const const_iterator&) = default;
// Allow assign from iterator to const_iterator
const_iterator& operator=(const iterator& iter)
{
......@@ -815,10 +820,17 @@ public:
public:
using value_type = this_type::key_type;
using pointer = const Key*;
using reference = const Key&;
//- Implicit conversion
inline key_iterator_base(const Iter& iter)
//- Default construct (end iterator)
constexpr key_iterator_base() noexcept
:
Iter()
{}
//- Copy construct with implicit conversion
explicit key_iterator_base(const Iter& iter)
:
Iter(iter)
{}
......@@ -851,8 +863,7 @@ public:
//- A const iterator begin/end pair for iterating over keys
const_iterator_pair<const_key_iterator, this_type> keys() const
{
return
const_iterator_pair<const_key_iterator,this_type>(*this);
return const_iterator_pair<const_key_iterator, this_type>(*this);
}
......@@ -867,14 +878,14 @@ public:
//- const_iterator set to the beginning of the HashTable
inline const_iterator cbegin() const;
//- iterator to signal the end for any HashTable
inline const iterator& end();
//- iterator to signal the end (for any HashTable)
inline iterator end() noexcept;
//- const_iterator to signal the end for any HashTable
inline const const_iterator& end() const;
//- const_iterator to signal the end (for any HashTable)
inline const_iterator end() const noexcept;
//- const_iterator to signal the end for any HashTable
inline const const_iterator& cend() const;
//- const_iterator to signal the end (for any HashTable)
inline constexpr const_iterator cend() const noexcept;
// Writing
......
......@@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2016 OpenFOAM Foundation
Copyright (C) 2017-2019 OpenCFD Ltd.
Copyright (C) 2017-2020 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
......@@ -62,12 +62,12 @@ struct HashTableCore
//- Return a canonical (power-of-two) of the requested size.
static label canonicalSize(const label requested_size);
//- Construct null
HashTableCore() = default;
//- Define template name and debug
//- Declare type-name (with debug switch)
ClassName("HashTable");
//- Default construct
HashTableCore() = default;
static_assert
(
sizeof(NullObject) >= sizeof(void*),
......@@ -75,34 +75,19 @@ struct HashTableCore
);
//- Factory method to create a non-const iterator begin
template<class IteratorType, class TableType>
inline static IteratorType iterator_begin(TableType& table);
//- Factory method to create a const iterator begin
template<class IteratorType, class TableType>
inline static IteratorType iterator_cbegin(const TableType& table);
//- Factory method to return an iterator end
// Simply reinterprets a NullObject as a hash-table iterator.
template<class IteratorType>
inline static const IteratorType& iterator_end();
//- Factory method to return an iterator cend
// Simply reinterprets a NullObject as a hash-table iterator.
template<class IteratorType>
inline static const IteratorType& iterator_cend();
//- Factory class for creating a begin/end pair for any const iterator.
template<class IteratorType, class TableType>
class const_iterator_pair
{
label size_;
const label size_;
IteratorType iter_;
public:
//- Default construct an empty pair
inline const_iterator_pair();
//- Construct begin/end pair for table
inline const_iterator_pair(const TableType& tbl);
label size() const noexcept { return size_; }
......@@ -111,8 +96,8 @@ struct HashTableCore
inline IteratorType begin() const;
inline IteratorType cbegin() const;
inline const IteratorType& end() const;
inline const IteratorType& cend() const;
inline IteratorType end() const;
inline IteratorType cend() const;
};
};
......
......@@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2017-2019 OpenCFD Ltd.
Copyright (C) 2017-2020 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
......@@ -25,43 +25,16 @@ License
\*---------------------------------------------------------------------------*/
// * * * * * * * * * * * * * * * helper methods * * * * * * * * * * * * * * //
template<class IteratorType, class TableType>
inline IteratorType Foam::HashTableCore::iterator_begin
(
TableType& table
)
{
return IteratorType(table.begin());
}
// * * * * * * * * * * * * * const iterator pair * * * * * * * * * * * * * * //
template<class IteratorType, class TableType>
inline IteratorType Foam::HashTableCore::iterator_cbegin
(
const TableType& table
)
{
return IteratorType(table.cbegin());
}
template<class IteratorType>
inline const IteratorType& Foam::HashTableCore::iterator_end()
{
return *reinterpret_cast<const IteratorType*>(nullObjectPtr);
}
template<class IteratorType>
inline const IteratorType& Foam::HashTableCore::iterator_cend()
{
return *reinterpret_cast<const IteratorType*>(nullObjectPtr);
}
inline Foam::HashTableCore::const_iterator_pair<IteratorType, TableType>
::const_iterator_pair()
:
size_(0),
iter_()
{}
// * * * * * * * * * * * * * const iterator pair * * * * * * * * * * * * * * //
template<class IteratorType, class TableType>
inline Foam::HashTableCore::const_iterator_pair<IteratorType, TableType>
......@@ -98,24 +71,24 @@ inline IteratorType Foam::HashTableCore::const_iterator_pair