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
|
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
|
||||||
\\ / O peration |
|
\\ / O peration |
|
||||||
\\ / A nd | Copyright (C) 2011 OpenFOAM Foundation
|
\\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation
|
||||||
\\/ M anipulation |
|
\\/ M anipulation |
|
||||||
-------------------------------------------------------------------------------
|
-------------------------------------------------------------------------------
|
||||||
License
|
License
|
||||||
@ -30,40 +30,37 @@ License
|
|||||||
|
|
||||||
using namespace Foam;
|
using namespace Foam;
|
||||||
|
|
||||||
// use define so we can easily test other implementations
|
|
||||||
#define HASHTABLE_CLASS HashTable
|
|
||||||
|
|
||||||
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
|
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
|
||||||
// Main program:
|
// Main program:
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
HASHTABLE_CLASS<double> table1(13);
|
HashTable<scalar> table1
|
||||||
HASHTABLE_CLASS<double>::iterator iter;
|
{
|
||||||
|
{"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);
|
// Erase by key
|
||||||
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
|
|
||||||
table1.erase("aaw");
|
table1.erase("aaw");
|
||||||
|
|
||||||
// erase by iterator
|
// Erase by iterator
|
||||||
iter = table1.find("abs");
|
HashTable<scalar>::iterator iter = table1.find("abs");
|
||||||
table1.erase(iter);
|
table1.erase(iter);
|
||||||
|
|
||||||
Info<< "\ntable1 toc: " << table1.toc() << endl;
|
Info<< "\ntable1 toc: " << table1.toc() << endl;
|
||||||
Info<< "\ntable1 sortedToc: " << table1.sortedToc() << endl;
|
Info<< "\ntable1 sortedToc: " << table1.sortedToc() << endl;
|
||||||
table1.printInfo(Info)
|
table1.printInfo(Info)
|
||||||
<< "table1 [" << table1.size() << "] " << endl;
|
<< "table1 [" << table1.size() << "] " << endl;
|
||||||
forAllIter(HASHTABLE_CLASS<double>, table1, iter)
|
forAllIter(HashTable<scalar>, table1, iter)
|
||||||
{
|
{
|
||||||
Info<< iter.key() << " => " << iter() << nl;
|
Info<< iter.key() << " => " << iter() << nl;
|
||||||
}
|
}
|
||||||
@ -90,14 +87,14 @@ int main()
|
|||||||
{
|
{
|
||||||
OStringStream os;
|
OStringStream os;
|
||||||
os << table1;
|
os << table1;
|
||||||
HASHTABLE_CLASS<double> readTable(IStringStream(os.str())(), 100);
|
HashTable<scalar> readTable(IStringStream(os.str())(), 100);
|
||||||
|
|
||||||
Info<< "Istream constructor:" << readTable << endl;
|
Info<< "Istream constructor:" << readTable << endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
HASHTABLE_CLASS<double> table2(table1);
|
HashTable<scalar> table2(table1);
|
||||||
HASHTABLE_CLASS<double> table3(table1.xfer());
|
HashTable<scalar> table3(table1.xfer());
|
||||||
|
|
||||||
Info<< "\ncopy table1 -> table2" << nl
|
Info<< "\ncopy table1 -> table2" << nl
|
||||||
<< "transfer table1 -> table3 via the xfer() method" << nl;
|
<< "transfer table1 -> table3 via the xfer() method" << nl;
|
||||||
@ -107,7 +104,7 @@ int main()
|
|||||||
<< "\ntable3" << table3 << nl;
|
<< "\ntable3" << table3 << nl;
|
||||||
|
|
||||||
Info<< "\nerase table2 by iterator" << nl;
|
Info<< "\nerase table2 by iterator" << nl;
|
||||||
forAllIter(HASHTABLE_CLASS<double>, table2, iter)
|
forAllIter(HashTable<scalar>, table2, iter)
|
||||||
{
|
{
|
||||||
Info<< "erasing " << iter.key() << " => " << iter() << " ... ";
|
Info<< "erasing " << iter.key() << " => " << iter() << " ... ";
|
||||||
table2.erase(iter);
|
table2.erase(iter);
|
||||||
@ -128,7 +125,7 @@ int main()
|
|||||||
table3.printInfo(Info)
|
table3.printInfo(Info)
|
||||||
<< table3 << nl;
|
<< table3 << nl;
|
||||||
|
|
||||||
HASHTABLE_CLASS<double> table4;
|
HashTable<scalar> table4;
|
||||||
|
|
||||||
table4 = table3;
|
table4 = table3;
|
||||||
Info<< "\ncopy table3 -> table4 " << table4 << nl;
|
Info<< "\ncopy table3 -> table4 " << table4 << nl;
|
||||||
@ -145,7 +142,7 @@ int main()
|
|||||||
Info<< "removed an element - test table1 != table3 : "
|
Info<< "removed an element - test table1 != table3 : "
|
||||||
<< (table1 != table3) << nl;
|
<< (table1 != table3) << nl;
|
||||||
|
|
||||||
// insert a few things into table2
|
// Insert a few things into table2
|
||||||
table2.set("ada", 14.0);
|
table2.set("ada", 14.0);
|
||||||
table2.set("aeq", 15.0);
|
table2.set("aeq", 15.0);
|
||||||
table2.set("aaw", 16.0);
|
table2.set("aaw", 16.0);
|
||||||
|
|||||||
@ -37,19 +37,21 @@ using namespace Foam;
|
|||||||
|
|
||||||
int main(int argc, char *argv[])
|
int main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
HashTable<label, Foam::string> table1(0);
|
HashTable<label, Foam::string> table1
|
||||||
|
{
|
||||||
table1.insert("kjhk", 10);
|
{"kjhk", 10},
|
||||||
table1.insert("kjhk2", 12);
|
{"kjhk2", 12}
|
||||||
|
};
|
||||||
|
|
||||||
Info<< "table1: " << table1 << nl
|
Info<< "table1: " << table1 << nl
|
||||||
<< "toc: " << table1.toc() << endl;
|
<< "toc: " << table1.toc() << endl;
|
||||||
|
|
||||||
HashTable<label, label, Hash<label>> table2(10);
|
HashTable<label, label, Hash<label>> table2
|
||||||
|
{
|
||||||
table2.insert(3, 10);
|
{3, 10},
|
||||||
table2.insert(5, 12);
|
{5, 12},
|
||||||
table2.insert(7, 16);
|
{7, 16}
|
||||||
|
};
|
||||||
|
|
||||||
Info<< "table2: " << table2 << nl
|
Info<< "table2: " << table2 << nl
|
||||||
<< "toc: " << table2.toc() << endl;
|
<< "toc: " << table2.toc() << endl;
|
||||||
|
|||||||
@ -28,6 +28,7 @@ License
|
|||||||
|
|
||||||
#include "HashTable.H"
|
#include "HashTable.H"
|
||||||
#include "List.H"
|
#include "List.H"
|
||||||
|
#include "Tuple2.H"
|
||||||
|
|
||||||
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
|
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
|
||||||
|
|
||||||
@ -54,26 +55,14 @@ Foam::HashTable<T, Key, Hash>::HashTable(const label size)
|
|||||||
template<class T, class Key, class Hash>
|
template<class T, class Key, class Hash>
|
||||||
Foam::HashTable<T, Key, Hash>::HashTable(const HashTable<T, Key, Hash>& ht)
|
Foam::HashTable<T, Key, Hash>::HashTable(const HashTable<T, Key, Hash>& ht)
|
||||||
:
|
:
|
||||||
HashTableCore(),
|
HashTable<T, Key, Hash>(ht.tableSize_)
|
||||||
nElmts_(0),
|
|
||||||
tableSize_(ht.tableSize_),
|
|
||||||
table_(nullptr)
|
|
||||||
{
|
{
|
||||||
if (tableSize_)
|
|
||||||
{
|
|
||||||
table_ = new hashedEntry*[tableSize_];
|
|
||||||
|
|
||||||
for (label hashIdx = 0; hashIdx < tableSize_; hashIdx++)
|
|
||||||
{
|
|
||||||
table_[hashIdx] = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (const_iterator iter = ht.cbegin(); iter != ht.cend(); ++iter)
|
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>
|
template<class T, class Key, class Hash>
|
||||||
Foam::HashTable<T, Key, Hash>::HashTable
|
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 * * * * * * * * * * * * * * * //
|
// * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
|
||||||
|
|
||||||
template<class T, class Key, class Hash>
|
template<class T, class Key, class Hash>
|
||||||
|
|||||||
@ -49,6 +49,7 @@ SourceFiles
|
|||||||
#include "word.H"
|
#include "word.H"
|
||||||
#include "Xfer.H"
|
#include "Xfer.H"
|
||||||
#include "className.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 HashTable;
|
||||||
template<class T, class Key, class Hash> class HashPtrTable;
|
template<class T, class Key, class Hash> class HashPtrTable;
|
||||||
|
|
||||||
|
template<class Type1, class Type2>
|
||||||
|
class Tuple2;
|
||||||
|
|
||||||
template<class T, class Key, class Hash>
|
template<class T, class Key, class Hash>
|
||||||
Istream& operator>>(Istream&, HashTable<T, Key, Hash>&);
|
Istream& operator>>(Istream&, HashTable<T, Key, Hash>&);
|
||||||
|
|
||||||
@ -171,6 +175,7 @@ class HashTable
|
|||||||
//- Assign a new hashedEntry to a possibly already existing key
|
//- Assign a new hashedEntry to a possibly already existing key
|
||||||
bool set(const Key&, const T& newElmt, bool protect);
|
bool set(const Key&, const T& newElmt, bool protect);
|
||||||
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
// Forward declaration of iterators
|
// Forward declaration of iterators
|
||||||
@ -207,6 +212,9 @@ public:
|
|||||||
//- Construct by transferring the parameter contents
|
//- Construct by transferring the parameter contents
|
||||||
HashTable(const Xfer<HashTable<T, Key, Hash>>&);
|
HashTable(const Xfer<HashTable<T, Key, Hash>>&);
|
||||||
|
|
||||||
|
//- Construct from an initializer list
|
||||||
|
HashTable(std::initializer_list<Tuple2<Key, T>> lst);
|
||||||
|
|
||||||
|
|
||||||
//- Destructor
|
//- Destructor
|
||||||
~HashTable();
|
~HashTable();
|
||||||
|
|||||||
Reference in New Issue
Block a user