From 915e8c9f35791cc852a1be866be865c725202f26 Mon Sep 17 00:00:00 2001
From: Mark Olesen <Mark.Olesen@esi-group.com>
Date: Thu, 25 Jan 2018 12:13:16 +0100
Subject: [PATCH] ENH: remove Foam::Swap specializations for HashSet, HashTable

- without these will use the normal move construct + move assign.
  This is similarly efficient, but avoids the inadvertently having the
  incorrect Swap being used for derived classes.

STYLE: remove unused xfer methods for HashTable, HashSet

- unneeded since move construct and move assignment are possible
---
 .../test/HashTable1/Test-HashTable1.C         |  9 +-
 .../containers/HashTables/HashSet/HashSet.C   | 35 ++-----
 .../containers/HashTables/HashSet/HashSet.H   | 15 +--
 .../HashTables/HashTable/HashTable.C          | 99 +++++++++----------
 .../HashTables/HashTable/HashTable.H          | 30 ++----
 .../HashTables/HashTable/HashTableI.H         | 13 ---
 .../containers/Identifiers/Keyed/Keyed.H      | 12 +--
 .../containers/Identifiers/Keyed/KeyedI.H     | 11 +--
 .../polyMesh/globalMeshData/globalPoints.C    |  8 +-
 9 files changed, 84 insertions(+), 148 deletions(-)

diff --git a/applications/test/HashTable1/Test-HashTable1.C b/applications/test/HashTable1/Test-HashTable1.C
index 82dd7151d65..26171deee04 100644
--- a/applications/test/HashTable1/Test-HashTable1.C
+++ b/applications/test/HashTable1/Test-HashTable1.C
@@ -98,11 +98,12 @@ int main()
     }
 
 
-    HashTable<scalar> table2(table1);
-    HashTable<scalar> table3(std::move(table1));
+    HashTable<scalar> table2(table1); // Copy
+    HashTable<scalar> table3(std::move(table1)); // Move
 
-    Info<< "\ncopy table1 -> table2" << nl
-        << "transfer table1 -> table3 via the xfer() method" << nl;
+    Info<< nl
+        << "copy table1 -> table2" << nl
+        << "move table1 -> table3" << nl;
 
     Info<< "\ntable1" << table1 << nl
         << "\ntable2" << table2 << nl
diff --git a/src/OpenFOAM/containers/HashTables/HashSet/HashSet.C b/src/OpenFOAM/containers/HashTables/HashSet/HashSet.C
index 575dda79030..9fc7370d103 100644
--- a/src/OpenFOAM/containers/HashTables/HashSet/HashSet.C
+++ b/src/OpenFOAM/containers/HashTables/HashSet/HashSet.C
@@ -170,28 +170,27 @@ inline bool Foam::HashSet<Key, Hash>::operator[](const Key& key) const
 
 
 template<class Key, class Hash>
-void Foam::HashSet<Key, Hash>::operator=(const UList<Key>& lst)
+void Foam::HashSet<Key, Hash>::operator=(const UList<Key>& rhs)
 {
-    assignMultiple(lst.begin(), lst.end(), 2*lst.size());
+    assignMultiple(rhs.begin(), rhs.end(), 2*rhs.size());
 }
 
 
 template<class Key, class Hash>
 template<unsigned Size>
-void Foam::HashSet<Key, Hash>::operator=(const FixedList<Key, Size>& lst)
+void Foam::HashSet<Key, Hash>::operator=(const FixedList<Key, Size>& rhs)
 {
-    assignMultiple(lst.begin(), lst.end(), 2*lst.size());
+    assignMultiple(rhs.begin(), rhs.end(), 2*rhs.size());
 }
 
 
 template<class Key, class Hash>
-void Foam::HashSet<Key, Hash>::operator=(std::initializer_list<Key> lst)
+void Foam::HashSet<Key, Hash>::operator=(std::initializer_list<Key> rhs)
 {
-    assignMultiple(lst.begin(), lst.end(), 2*lst.size());
+    assignMultiple(rhs.begin(), rhs.end(), 2*rhs.size());
 }
 
 
-
 template<class Key, class Hash>
 bool Foam::HashSet<Key, Hash>::operator==(const HashSet<Key, Hash>& rhs) const
 {
@@ -283,24 +282,10 @@ Foam::Ostream& Foam::operator<<(Ostream& os, const HashSet<Key, Hash>& tbl)
 }
 
 
-// * * * * * * * * * * * * * * * Global Functions  * * * * * * * * * * * * * //
-
-template<class Key, class Hash>
-inline void Foam::Swap
-(
-    HashSet<Key, Hash>& a,
-    HashSet<Key, Hash>& b
-)
-{
-    a.swap(b);
-}
-
-
 /* * * * * * * * * * * * * * * * Global Operators  * * * * * * * * * * * * * */
 
 template<class Key, class Hash>
-Foam::HashSet<Key, Hash>
-Foam::operator|
+Foam::HashSet<Key, Hash> Foam::operator|
 (
     const HashSet<Key, Hash>& hash1,
     const HashSet<Key, Hash>& hash2
@@ -313,8 +298,7 @@ Foam::operator|
 
 
 template<class Key, class Hash>
-Foam::HashSet<Key, Hash>
-Foam::operator&
+Foam::HashSet<Key, Hash> Foam::operator&
 (
     const HashSet<Key, Hash>& hash1,
     const HashSet<Key, Hash>& hash2
@@ -327,8 +311,7 @@ Foam::operator&
 
 
 template<class Key, class Hash>
-Foam::HashSet<Key, Hash>
-Foam::operator^
+Foam::HashSet<Key, Hash> Foam::operator^
 (
     const HashSet<Key, Hash>& hash1,
     const HashSet<Key, Hash>& hash2
diff --git a/src/OpenFOAM/containers/HashTables/HashSet/HashSet.H b/src/OpenFOAM/containers/HashTables/HashSet/HashSet.H
index abf1731ceef..da024138b6d 100644
--- a/src/OpenFOAM/containers/HashTables/HashSet/HashSet.H
+++ b/src/OpenFOAM/containers/HashTables/HashSet/HashSet.H
@@ -334,10 +334,10 @@ public:
 
         //- Assignment from a FixedList of keys
         template<unsigned Size>
-        void operator=(const FixedList<Key, Size>& lst);
+        void operator=(const FixedList<Key, Size>& rhs);
 
         //- Assignment from an initializer list of keys
-        void operator=(std::initializer_list<Key> lst);
+        void operator=(std::initializer_list<Key> rhs);
 
 
     // Logical and set operations
@@ -372,17 +372,6 @@ public:
 };
 
 
-// Global Functions
-
-// Exchange contents of HashSets - see HashSet::swap().
-template<class Key, class Hash>
-inline void Swap
-(
-    HashSet<Key, Hash>& a,
-    HashSet<Key, Hash>& b
-);
-
-
 // Global Operators
 
 //- Combine entries from HashSets
diff --git a/src/OpenFOAM/containers/HashTables/HashTable/HashTable.C b/src/OpenFOAM/containers/HashTables/HashTable/HashTable.C
index e4344770abf..e96d1311e95 100644
--- a/src/OpenFOAM/containers/HashTables/HashTable/HashTable.C
+++ b/src/OpenFOAM/containers/HashTables/HashTable/HashTable.C
@@ -100,11 +100,16 @@ Foam::HashTable<T, Key, Hash>::HashTable(const HashTable<T, Key, Hash>& ht)
 
 
 template<class T, class Key, class Hash>
-Foam::HashTable<T, Key, Hash>::HashTable(HashTable<T, Key, Hash>&& ht)
+Foam::HashTable<T, Key, Hash>::HashTable(HashTable<T, Key, Hash>&& rhs)
 :
-    HashTable<T, Key, Hash>(0)
+    HashTableCore(),
+    size_(rhs.size_),
+    capacity_(rhs.capacity_),
+    table_(rhs.table_)
 {
-    transfer(ht);
+    rhs.size_ = 0;
+    rhs.capacity_ = 0;
+    rhs.table_ = nullptr;
 }
 
 
@@ -116,9 +121,9 @@ Foam::HashTable<T, Key, Hash>::HashTable
 :
     HashTable<T, Key, Hash>(2*lst.size())
 {
-    for (const auto& pair : lst)
+    for (const auto& keyval : lst)
     {
-        insert(pair.first, pair.second);
+        insert(keyval.first, keyval.second);
     }
 }
 
@@ -141,25 +146,25 @@ Foam::HashTable<T, Key, Hash>::~HashTable()
 template<class T, class Key, class Hash>
 Foam::List<Key> Foam::HashTable<T, Key, Hash>::toc() const
 {
-    List<Key> keyLst(size_);
+    List<Key> list(size_);
     label count = 0;
 
     for (const_iterator iter = cbegin(); iter != cend(); ++iter)
     {
-        keyLst[count++] = iter.key();
+        list[count++] = iter.key();
     }
 
-    return keyLst;
+    return list;
 }
 
 
 template<class T, class Key, class Hash>
 Foam::List<Key> Foam::HashTable<T, Key, Hash>::sortedToc() const
 {
-    List<Key> keyLst(this->toc());
-    Foam::sort(keyLst);
+    List<Key> list(this->toc());
+    Foam::sort(list);
 
-    return keyLst;
+    return list;
 }
 
 
@@ -170,10 +175,10 @@ Foam::List<Key> Foam::HashTable<T, Key, Hash>::sortedToc
     const Compare& comp
 ) const
 {
-    List<Key> keyLst(this->toc());
-    Foam::sort(keyLst, comp);
+    List<Key> list(this->toc());
+    Foam::sort(list, comp);
 
-    return keyLst;
+    return list;
 }
 
 
@@ -185,21 +190,21 @@ Foam::List<Key> Foam::HashTable<T, Key, Hash>::tocKeys
     const bool invert
 ) const
 {
-    List<Key> keyLst(size_);
+    List<Key> list(size_);
     label count = 0;
 
     for (const_iterator iter = cbegin(); iter != cend(); ++iter)
     {
         if ((pred(iter.key()) ? !invert : invert))
         {
-            keyLst[count++] = iter.key();
+            list[count++] = iter.key();
         }
     }
 
-    keyLst.setSize(count);
-    Foam::sort(keyLst);
+    list.setSize(count);
+    Foam::sort(list);
 
-    return keyLst;
+    return list;
 }
 
 
@@ -211,21 +216,21 @@ Foam::List<Key> Foam::HashTable<T, Key, Hash>::tocValues
     const bool invert
 ) const
 {
-    List<Key> keyLst(size_);
+    List<Key> list(size_);
     label count = 0;
 
     for (const_iterator iter = cbegin(); iter != cend(); ++iter)
     {
         if ((pred(iter.object()) ? !invert : invert))
         {
-            keyLst[count++] = iter.key();
+            list[count++] = iter.key();
         }
     }
 
-    keyLst.setSize(count);
-    Foam::sort(keyLst);
+    list.setSize(count);
+    Foam::sort(list);
 
-    return keyLst;
+    return list;
 }
 
 
@@ -237,21 +242,21 @@ Foam::List<Key> Foam::HashTable<T, Key, Hash>::tocEntries
     const bool invert
 ) const
 {
-    List<Key> keyLst(size_);
+    List<Key> list(size_);
     label count = 0;
 
     for (const_iterator iter = cbegin(); iter != cend(); ++iter)
     {
         if ((pred(iter.key(), iter.object()) ? !invert : invert))
         {
-            keyLst[count++] = iter.key();
+            list[count++] = iter.key();
         }
     }
 
-    keyLst.setSize(count);
-    Foam::sort(keyLst);
+    list.setSize(count);
+    Foam::sort(list);
 
-    return keyLst;
+    return list;
 }
 
 
@@ -647,33 +652,19 @@ void Foam::HashTable<T, Key, Hash>::clearStorage()
 
 
 template<class T, class Key, class Hash>
-void Foam::HashTable<T, Key, Hash>::swap(HashTable<T, Key, Hash>& ht)
+void Foam::HashTable<T, Key, Hash>::swap(HashTable<T, Key, Hash>& rhs)
 {
-    Foam::Swap(size_, ht.size_);
-    Foam::Swap(capacity_, ht.capacity_);
-    Foam::Swap(table_, ht.table_);
+    Foam::Swap(size_, rhs.size_);
+    Foam::Swap(capacity_, rhs.capacity_);
+    Foam::Swap(table_, rhs.table_);
 }
 
 
 template<class T, class Key, class Hash>
-void Foam::HashTable<T, Key, Hash>::transfer(HashTable<T, Key, Hash>& ht)
+void Foam::HashTable<T, Key, Hash>::transfer(HashTable<T, Key, Hash>& rhs)
 {
-    // As per destructor
-    if (table_)
-    {
-        clear();
-        delete[] table_;
-    }
-
-    size_ = ht.size_;
-    ht.size_ = 0;
-
-    capacity_ = ht.capacity_;
-    ht.capacity_ = 0;
-
-    table_ = ht.table_;
-    ht.table_ = nullptr;
-
+    clear();
+    swap(rhs);
 }
 
 
@@ -794,22 +785,22 @@ void Foam::HashTable<T, Key, Hash>::operator=
 template<class T, class Key, class Hash>
 void Foam::HashTable<T, Key, Hash>::operator=
 (
-    std::initializer_list<std::pair<Key, T>> lst
+    std::initializer_list<std::pair<Key, T>> rhs
 )
 {
     // Could be zero-sized from a previous transfer()
     if (!capacity_)
     {
-        resize(2*lst.size());
+        resize(2*rhs.size());
     }
     else
     {
         clear();
     }
 
-    for (const auto& pair : lst)
+    for (const auto& keyval : rhs)
     {
-        insert(pair.first, pair.second);
+        insert(keyval.first, keyval.second);
     }
 }
 
diff --git a/src/OpenFOAM/containers/HashTables/HashTable/HashTable.H b/src/OpenFOAM/containers/HashTables/HashTable/HashTable.H
index 35621df78de..922fe917ced 100644
--- a/src/OpenFOAM/containers/HashTables/HashTable/HashTable.H
+++ b/src/OpenFOAM/containers/HashTables/HashTable/HashTable.H
@@ -336,11 +336,11 @@ public:
         //- Construct from Istream with default table capacity
         HashTable(Istream& is, const label size = 128);
 
-        //- Construct as copy
+        //- Copy construct
         HashTable(const this_type& ht);
 
         //- Move construct
-        HashTable(this_type&& ht);
+        HashTable(this_type&& rhs);
 
         //- Construct from an initializer list
         HashTable(std::initializer_list<std::pair<Key, T>> lst);
@@ -591,12 +591,11 @@ public:
         //  Equivalent to clear() followed by resize(0)
         void clearStorage();
 
-        //- Swap contents of the argument table into this table
-        void swap(HashTable<T, Key, Hash>& ht);
+        //- Swap contents into this table
+        void swap(HashTable<T, Key, Hash>& rhs);
 
-        //- Transfer the contents of the argument table into this table
-        //  and annul the argument table.
-        void transfer(HashTable<T, Key, Hash>& ht);
+        //- Transfer contents into this table.
+        void transfer(HashTable<T, Key, Hash>& rhs);
 
 
     // Member Operators
@@ -615,11 +614,11 @@ public:
         //- Return existing entry or insert a new entry.
         inline T& operator()(const Key& key, const T& deflt);
 
-        //- Assignment
+        //- Copy assignment
         void operator=(const HashTable<T, Key, Hash>& rhs);
 
-        //- Assignment from an initializer list
-        void operator=(std::initializer_list<std::pair<Key, T>> lst);
+        //- Copy assignment from an initializer list
+        void operator=(std::initializer_list<std::pair<Key, T>> rhs);
 
         //- Move assign
         void operator=(HashTable<T, Key, Hash>&& rhs);
@@ -973,17 +972,6 @@ public:
 };
 
 
-// Global Functions
-
-// Exchange contents of hash tables - see HashTable::swap().
-template<class T, class Key, class Hash>
-inline void Swap
-(
-    HashTable<T, Key, Hash>& a,
-    HashTable<T, Key, Hash>& b
-);
-
-
 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
 
 } // End namespace Foam
diff --git a/src/OpenFOAM/containers/HashTables/HashTable/HashTableI.H b/src/OpenFOAM/containers/HashTables/HashTable/HashTableI.H
index 0ac2749278d..424d3db57de 100644
--- a/src/OpenFOAM/containers/HashTables/HashTable/HashTableI.H
+++ b/src/OpenFOAM/containers/HashTables/HashTable/HashTableI.H
@@ -218,17 +218,4 @@ inline T& Foam::HashTable<T, Key, Hash>::operator()
 }
 
 
-// * * * * * * * * * * * * * * * Global Functions  * * * * * * * * * * * * * //
-
-template<class T, class Key, class Hash>
-inline void Foam::Swap
-(
-    HashTable<T, Key, Hash>& a,
-    HashTable<T, Key, Hash>& b
-)
-{
-    a.swap(b);
-}
-
-
 // ************************************************************************* //
diff --git a/src/OpenFOAM/containers/Identifiers/Keyed/Keyed.H b/src/OpenFOAM/containers/Identifiers/Keyed/Keyed.H
index df695531de1..c7cdb3ce97c 100644
--- a/src/OpenFOAM/containers/Identifiers/Keyed/Keyed.H
+++ b/src/OpenFOAM/containers/Identifiers/Keyed/Keyed.H
@@ -71,14 +71,14 @@ public:
         //- Add labels to a list of values
         inline static List<Keyed<T>> createList
         (
-            const List<T>&,
+            const UList<T>& lst,
             const label key=0
         );
 
         //- Add labels to a list of values
         inline static List<Keyed<T>> createList
         (
-            const List<T>&,
+            const UList<T>& lst,
             const labelUList& keys
         );
 
@@ -88,14 +88,14 @@ public:
         //- Construct null
         inline Keyed();
 
-        //- Construct as a copy of item, with a key
+        //- Copy construct item, with a key
         inline Keyed(const T& item, const label key=0);
 
-        //- Construct by transferring the item, with a key
-        inline Keyed(const Xfer<T>& item, const label key=0);
+        //- Move construct item, with a key
+        inline Keyed(T&& item, const label key=0);
 
         //- Construct from Istream
-        inline Keyed(Istream&);
+        inline Keyed(Istream& is);
 
 
     // Member Functions
diff --git a/src/OpenFOAM/containers/Identifiers/Keyed/KeyedI.H b/src/OpenFOAM/containers/Identifiers/Keyed/KeyedI.H
index 59f49406871..25598ea1c76 100644
--- a/src/OpenFOAM/containers/Identifiers/Keyed/KeyedI.H
+++ b/src/OpenFOAM/containers/Identifiers/Keyed/KeyedI.H
@@ -25,8 +25,6 @@ License
 
 #include "IOstreams.H"
 
-// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
-
 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
 
 template<class T>
@@ -45,9 +43,9 @@ inline Foam::Keyed<T>::Keyed(const T& item, const label key)
 
 
 template<class T>
-inline Foam::Keyed<T>::Keyed(const Xfer<T>& item, const label key)
+inline Foam::Keyed<T>::Keyed(T&& item, const label key)
 :
-    T(item),
+    T(std::move(item)),
     key_(key)
 {}
 
@@ -76,7 +74,7 @@ inline Foam::label& Foam::Keyed<T>::key()
 
 template<class T>
 inline Foam::List<Foam::Keyed<T>>
-Foam::Keyed<T>::createList(const List<T>& lst, const label key)
+Foam::Keyed<T>::createList(const UList<T>& lst, const label key)
 {
     List<Keyed<T>> newList(lst.size());
 
@@ -90,7 +88,7 @@ Foam::Keyed<T>::createList(const List<T>& lst, const label key)
 
 template<class T>
 inline Foam::List<Foam::Keyed<T>>
-Foam::Keyed<T>::createList(const List<T>& lst, const labelUList& keys)
+Foam::Keyed<T>::createList(const UList<T>& lst, const labelUList& keys)
 {
     if (lst.size() != keys.size())
     {
@@ -141,6 +139,5 @@ inline Foam::Ostream& Foam::operator<<(Ostream& os, const Keyed<T>& item)
     return os;
 }
 
-// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
 
 // ************************************************************************* //
diff --git a/src/OpenFOAM/meshes/polyMesh/globalMeshData/globalPoints.C b/src/OpenFOAM/meshes/polyMesh/globalMeshData/globalPoints.C
index 94fb8a3f4a2..f32a551c514 100644
--- a/src/OpenFOAM/meshes/polyMesh/globalMeshData/globalPoints.C
+++ b/src/OpenFOAM/meshes/polyMesh/globalMeshData/globalPoints.C
@@ -732,14 +732,14 @@ void Foam::globalPoints::remove
     // Save old ones.
     Map<label> oldMeshToProcPoint(std::move(meshToProcPoint_));
     meshToProcPoint_.resize(oldMeshToProcPoint.size());
-    DynamicList<labelPairList> oldProcPoints(procPoints_.xfer());
+    DynamicList<labelPairList> oldProcPoints(std::move(procPoints_));
     procPoints_.setCapacity(oldProcPoints.size());
 
     // Go through all equivalences
-    forAllConstIter(Map<label>, oldMeshToProcPoint, iter)
+    forAllConstIters(oldMeshToProcPoint, iter)
     {
-        label localPointi = iter.key();
-        const labelPairList& pointInfo = oldProcPoints[iter()];
+        const label localPointi = iter.key();
+        const labelPairList& pointInfo = oldProcPoints[iter.object()];
 
         if (pointInfo.size() == 2)
         {
-- 
GitLab