diff --git a/applications/test/HashTable/Test-hashTable.C b/applications/test/HashTable/Test-hashTable.C index 87449a585d6a4d3a87b776da4ebd48c8328ed385..e0f65bd2819a39ea0a2742a954da2109d35c35f0 100644 --- a/applications/test/HashTable/Test-hashTable.C +++ b/applications/test/HashTable/Test-hashTable.C @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | - \\ / A nd | Copyright (C) 2011 OpenFOAM Foundation + \\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation \\/ M anipulation | ------------------------------------------------------------------------------- License @@ -30,40 +30,37 @@ License using namespace Foam; -// use define so we can easily test other implementations -#define HASHTABLE_CLASS HashTable - // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // // Main program: int main() { - HASHTABLE_CLASS<double> table1(13); - HASHTABLE_CLASS<double>::iterator iter; - - table1.insert("aaa", 1.0); - table1.insert("aba", 2.0); - table1.insert("aca", 3.0); - table1.insert("ada", 4.0); - table1.insert("aeq", 5.0); - table1.insert("aaw", 6.0); - table1.insert("abs", 7.0); - table1.insert("acr", 8.0); - table1.insert("adx", 9.0); - table1.insert("aec", 10.0); - - // erase by key + HashTable<scalar> table1 + { + {"aaa", 1.0}, + {"aba", 2.0}, + {"aca", 3.0}, + {"ada", 4.0}, + {"aeq", 5.0}, + {"aaw", 6.0}, + {"abs", 7.0}, + {"acr", 8.0}, + {"adx", 9.0}, + {"aec", 10.0} + }; + + // Erase by key table1.erase("aaw"); - // erase by iterator - iter = table1.find("abs"); + // Erase by iterator + HashTable<scalar>::iterator iter = table1.find("abs"); table1.erase(iter); Info<< "\ntable1 toc: " << table1.toc() << endl; Info<< "\ntable1 sortedToc: " << table1.sortedToc() << endl; table1.printInfo(Info) << "table1 [" << table1.size() << "] " << endl; - forAllIter(HASHTABLE_CLASS<double>, table1, iter) + forAllIter(HashTable<scalar>, table1, iter) { Info<< iter.key() << " => " << iter() << nl; } @@ -90,14 +87,14 @@ int main() { OStringStream os; os << table1; - HASHTABLE_CLASS<double> readTable(IStringStream(os.str())(), 100); + HashTable<scalar> readTable(IStringStream(os.str())(), 100); Info<< "Istream constructor:" << readTable << endl; } - HASHTABLE_CLASS<double> table2(table1); - HASHTABLE_CLASS<double> table3(table1.xfer()); + HashTable<scalar> table2(table1); + HashTable<scalar> table3(table1.xfer()); Info<< "\ncopy table1 -> table2" << nl << "transfer table1 -> table3 via the xfer() method" << nl; @@ -107,7 +104,7 @@ int main() << "\ntable3" << table3 << nl; Info<< "\nerase table2 by iterator" << nl; - forAllIter(HASHTABLE_CLASS<double>, table2, iter) + forAllIter(HashTable<scalar>, table2, iter) { Info<< "erasing " << iter.key() << " => " << iter() << " ... "; table2.erase(iter); @@ -128,7 +125,7 @@ int main() table3.printInfo(Info) << table3 << nl; - HASHTABLE_CLASS<double> table4; + HashTable<scalar> table4; table4 = table3; Info<< "\ncopy table3 -> table4 " << table4 << nl; @@ -145,7 +142,7 @@ int main() Info<< "removed an element - test table1 != table3 : " << (table1 != table3) << nl; - // insert a few things into table2 + // Insert a few things into table2 table2.set("ada", 14.0); table2.set("aeq", 15.0); table2.set("aaw", 16.0); diff --git a/applications/test/HashTable2/Test-HashTable2.C b/applications/test/HashTable2/Test-HashTable2.C index 0fb7b3a94252952c060f7ba2ded6c58c9b836454..537d0986c831cabcd34c74f30421ada3659e40bc 100644 --- a/applications/test/HashTable2/Test-HashTable2.C +++ b/applications/test/HashTable2/Test-HashTable2.C @@ -37,19 +37,21 @@ using namespace Foam; int main(int argc, char *argv[]) { - HashTable<label, Foam::string> table1(0); - - table1.insert("kjhk", 10); - table1.insert("kjhk2", 12); + HashTable<label, Foam::string> table1 + { + {"kjhk", 10}, + {"kjhk2", 12} + }; Info<< "table1: " << table1 << nl << "toc: " << table1.toc() << endl; - HashTable<label, label, Hash<label>> table2(10); - - table2.insert(3, 10); - table2.insert(5, 12); - table2.insert(7, 16); + HashTable<label, label, Hash<label>> table2 + { + {3, 10}, + {5, 12}, + {7, 16} + }; Info<< "table2: " << table2 << nl << "toc: " << table2.toc() << endl; diff --git a/src/OpenFOAM/containers/HashTables/HashTable/HashTable.C b/src/OpenFOAM/containers/HashTables/HashTable/HashTable.C index f665e7ca71bca11954ec3eaeaf8bc9026bd3ebb4..5da26ea5298b3bda46c6ab9fa3732a9c20cfef3d 100644 --- a/src/OpenFOAM/containers/HashTables/HashTable/HashTable.C +++ b/src/OpenFOAM/containers/HashTables/HashTable/HashTable.C @@ -28,6 +28,7 @@ License #include "HashTable.H" #include "List.H" +#include "Tuple2.H" // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // @@ -54,27 +55,15 @@ Foam::HashTable<T, Key, Hash>::HashTable(const label size) template<class T, class Key, class Hash> Foam::HashTable<T, Key, Hash>::HashTable(const HashTable<T, Key, Hash>& ht) : - HashTableCore(), - nElmts_(0), - tableSize_(ht.tableSize_), - table_(nullptr) + HashTable<T, Key, Hash>(ht.tableSize_) { - if (tableSize_) + for (const_iterator iter = ht.cbegin(); iter != ht.cend(); ++iter) { - table_ = new hashedEntry*[tableSize_]; - - for (label hashIdx = 0; hashIdx < tableSize_; hashIdx++) - { - table_[hashIdx] = 0; - } - - for (const_iterator iter = ht.cbegin(); iter != ht.cend(); ++iter) - { - insert(iter.key(), *iter); - } + insert(iter.key(), *iter); } } + template<class T, class Key, class Hash> Foam::HashTable<T, Key, Hash>::HashTable ( @@ -90,6 +79,21 @@ Foam::HashTable<T, Key, Hash>::HashTable } +template<class T, class Key, class Hash> +Foam::HashTable<T, Key, Hash>::HashTable +( + std::initializer_list<Tuple2<Key, T>> lst +) +: + HashTable<T, Key, Hash>(lst.size()) +{ + for (const Tuple2<Key, T>& pair : lst) + { + insert(pair.first(), pair.second()); + } +} + + // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * // template<class T, class Key, class Hash> diff --git a/src/OpenFOAM/containers/HashTables/HashTable/HashTable.H b/src/OpenFOAM/containers/HashTables/HashTable/HashTable.H index f2137687385dd8ef423e18072aa35869beb9c1e6..4ef49f1a21121f891f1731179cd84364cbba9c2a 100644 --- a/src/OpenFOAM/containers/HashTables/HashTable/HashTable.H +++ b/src/OpenFOAM/containers/HashTables/HashTable/HashTable.H @@ -49,6 +49,7 @@ SourceFiles #include "word.H" #include "Xfer.H" #include "className.H" +#include <initializer_list> // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // @@ -62,6 +63,9 @@ template<class T> class UList; 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> Istream& operator>>(Istream&, HashTable<T, Key, Hash>&); @@ -171,6 +175,7 @@ class HashTable //- Assign a new hashedEntry to a possibly already existing key bool set(const Key&, const T& newElmt, bool protect); + public: // Forward declaration of iterators @@ -207,6 +212,9 @@ public: //- Construct by transferring the parameter contents HashTable(const Xfer<HashTable<T, Key, Hash>>&); + //- Construct from an initializer list + HashTable(std::initializer_list<Tuple2<Key, T>> lst); + //- Destructor ~HashTable();