BUG: hashtable key_iterator ++ operator returning incorrect type

ENH: ensure std::distance works with hashtable iterators
This commit is contained in:
Mark Olesen
2017-05-14 16:58:47 +02:00
parent 0c53a815ed
commit 4b0d1632b6
3 changed files with 72 additions and 7 deletions

View File

@ -25,6 +25,8 @@ License
#include "HashTable.H" #include "HashTable.H"
#include "List.H" #include "List.H"
#include "SortableList.H"
#include "DynamicList.H"
#include "FlatOutput.H" #include "FlatOutput.H"
#include "IOstreams.H" #include "IOstreams.H"
#include "IStringStream.H" #include "IStringStream.H"
@ -194,7 +196,24 @@ int main()
// These do not yet work. Issues resolving the distance. // These do not yet work. Issues resolving the distance.
// //
// List<scalar> table1vals(table1.begin(), table1.end()); // List<scalar> table1vals(table1.begin(), table1.end());
// wordList table1keys(table1.begin(), table1.end());
{
Info<<"distance/size: "
<< std::distance(table1.begin(), table1.end())
<< "/" << table1.size()
<< " and "
<< std::distance(table1.keys().begin(), table1.keys().end())
<< "/" << table1.keys().size()
<< nl;
SortableList<word> sortKeys
// DynamicList<word> sortKeys
(
table1.keys().begin(),
table1.keys().end()
);
Info<<"sortKeys: " << flatOutput(sortKeys) << nl;
}
Info<< "\nFrom table1: " << flatOutput(table1.sortedToc()) << nl Info<< "\nFrom table1: " << flatOutput(table1.sortedToc()) << nl
<< "retain keys: " << flatOutput(table3.sortedToc()) << nl; << "retain keys: " << flatOutput(table3.sortedToc()) << nl;

View File

@ -61,6 +61,7 @@ SourceFiles
#include "nullObject.H" #include "nullObject.H"
#include <initializer_list> #include <initializer_list>
#include <iterator>
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
@ -179,10 +180,17 @@ public:
//- Type of values that the HashTable contains. //- Type of values that the HashTable contains.
typedef T value_type; typedef T value_type;
//- The type used for storing into value_type objects.
// This type is usually value_type&.
typedef T* pointer;
//- The type used for storing into value_type objects. //- The type used for storing into value_type objects.
// This type is usually value_type&. // This type is usually value_type&.
typedef T& reference; typedef T& reference;
//- The type used for reading from constant value_type objects.
typedef const T* const_pointer;
//- The type used for reading from constant value_type objects. //- The type used for reading from constant value_type objects.
typedef const T& const_reference; typedef const T& const_reference;
@ -441,6 +449,7 @@ protected:
// Public typedefs // Public typedefs
using table_type = this_type; using table_type = this_type;
using key_type = this_type::key_type; using key_type = this_type::key_type;
using iterator_category = std::forward_iterator_tag;
using difference_type = this_type::difference_type; using difference_type = this_type::difference_type;
private: private:
@ -518,16 +527,20 @@ public:
public WrappedIterator public WrappedIterator
{ {
public: public:
using value_type = this_type::key_type;
using pointer = const Key*;
using reference = const Key&; using reference = const Key&;
using difference_type = typename WrappedIterator::difference_type;
//- Implicit conversion //- Implicit conversion
inline key_iterator_base(const WrappedIterator& iter); inline key_iterator_base(const WrappedIterator& iter);
//- Return the key //- Return the key
inline reference operator*() const; inline reference operator*() const;
}; inline reference operator()() const;
inline key_iterator_base& operator++();
inline key_iterator_base operator++(int);
};
// STL iterator // STL iterator
@ -544,9 +557,9 @@ public:
// Public typedefs // Public typedefs
using table_type = this_type; using table_type = this_type;
using key_type = this_type::key_type; using value_type = this_type::value_type;
using pointer = this_type::pointer;
using reference = this_type::reference; using reference = this_type::reference;
using difference_type = typename iterator_base::difference_type;
// Constructors // Constructors
@ -592,9 +605,9 @@ public:
// Public typedefs // Public typedefs
using table_type = const this_type; using table_type = const this_type;
using key_type = this_type::key_type; using value_type = const this_type::value_type;
using pointer = this_type::const_pointer;
using reference = this_type::const_reference; using reference = this_type::const_reference;
using difference_type = typename iterator_base::difference_type;
// Constructors // Constructors

View File

@ -363,6 +363,39 @@ Foam::HashTable<T, Key, Hash>::key_iterator_base<WrappedIterator>
} }
template<class T, class Key, class Hash>
template<class WrappedIterator>
inline const Key&
Foam::HashTable<T, Key, Hash>::key_iterator_base<WrappedIterator>
::operator()() const
{
return this->key();
}
template<class T, class Key, class Hash>
template<class WrappedIterator>
inline Foam::HashTable<T, Key, Hash>::key_iterator_base<WrappedIterator>&
Foam::HashTable<T, Key, Hash>::key_iterator_base<WrappedIterator>
::operator++()
{
this->increment();
return *this;
}
template<class T, class Key, class Hash>
template<class WrappedIterator>
inline Foam::HashTable<T, Key, Hash>::key_iterator_base<WrappedIterator>
Foam::HashTable<T, Key, Hash>::key_iterator_base<WrappedIterator>
::operator++(int)
{
key_iterator_base old = *this;
this->increment();
return old;
}
// * * * * * * * * * * * * * * * * STL iterator * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * * STL iterator * * * * * * * * * * * * * * //
template<class T, class Key, class Hash> template<class T, class Key, class Hash>