mirror of
https://develop.openfoam.com/Development/openfoam.git
synced 2025-11-28 03:28:01 +00:00
HashTable: Added C++11 initializer_list constructor
e.g.
HashTable<label, string> table1
{
{"kjhk", 10},
{"kjhk2", 12}
};
HashTable<label, label, Hash<label>> table2
{
{3, 10},
{5, 12},
{7, 16}
};
This commit is contained in:
@ -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;
|
||||
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}
|
||||
};
|
||||
|
||||
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
|
||||
// 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);
|
||||
|
||||
@ -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;
|
||||
|
||||
@ -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>
|
||||
|
||||
@ -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();
|
||||
|
||||
Reference in New Issue
Block a user