mirror of
https://develop.openfoam.com/Development/openfoam.git
synced 2025-11-28 03:28:01 +00:00
HashTable / StaticHashTable changes
StaticHashTable: - erase(iterator&) now actually alters the iterator and iterator++() handles it properly - clear() also sets count to zero - operator=(const StaticHashTable&) doesn't crash after a previous transfer - operator(), operator==() and operator!=() added HashTable: - operator=(const HashTable&) gets tableSize if required, eg, after a previous transfer) HashSet / Map - add xfer<...> constructor for underlying HashTable
This commit is contained in:
@ -24,90 +24,122 @@ License
|
||||
|
||||
\*---------------------------------------------------------------------------*/
|
||||
|
||||
#include <iostream>
|
||||
#include "HashTable.H"
|
||||
#include "IOstreams.H"
|
||||
#include "IStringStream.H"
|
||||
#include "OStringStream.H"
|
||||
|
||||
using namespace Foam;
|
||||
|
||||
// use define so we can easily test other implementations
|
||||
#define HASHTABLE_CLASS HashTable
|
||||
|
||||
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
|
||||
// Main program:
|
||||
|
||||
int main()
|
||||
{
|
||||
//for (;;)
|
||||
HASHTABLE_CLASS<double> table1(100);
|
||||
|
||||
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);
|
||||
|
||||
table1.erase("aaw");
|
||||
table1.erase("abs");
|
||||
|
||||
Info<< "\ntable1 toc: " << table1.toc() << endl;
|
||||
Info<< "\ntable1 [" << table1.size() << "] " << endl;
|
||||
forAllIter(HASHTABLE_CLASS<double>, table1, iter)
|
||||
{
|
||||
HashTable<double> myTable(100);
|
||||
|
||||
myTable.insert("aaa", 1.0);
|
||||
myTable.insert("aba", 2.0);
|
||||
myTable.insert("aca", 3.0);
|
||||
myTable.insert("ada", 4.0);
|
||||
myTable.insert("aeq", 5.0);
|
||||
myTable.insert("aaw", 6.0);
|
||||
myTable.insert("abs", 7.0);
|
||||
myTable.insert("acr", 8.0);
|
||||
myTable.insert("adx", 9.0);
|
||||
myTable.insert("aec", 10.0);
|
||||
|
||||
myTable.erase("aaw");
|
||||
myTable.erase("abs");
|
||||
|
||||
std::cerr << myTable.find("aaa")() << '\n';
|
||||
std::cerr << myTable.find("aba")() << '\n';
|
||||
std::cerr << myTable.find("aca")() << '\n';
|
||||
std::cerr << myTable.find("ada")() << '\n';
|
||||
std::cerr << myTable.find("aeq")() << '\n';
|
||||
//std::cerr << myTable.find("aaw")() << '\n';
|
||||
//std::cerr << myTable.find("abs")() << '\n';
|
||||
std::cerr << myTable.find("acr")() << '\n';
|
||||
std::cerr << myTable.find("adx")() << '\n';
|
||||
std::cerr << myTable.find("aec")() << '\n';
|
||||
|
||||
std::cerr << "\nprint table\n" << std::endl;
|
||||
|
||||
for
|
||||
(
|
||||
HashTable<double>::iterator iter = myTable.begin();
|
||||
iter != myTable.end();
|
||||
++iter
|
||||
)
|
||||
{
|
||||
std::cerr << *iter << '\n';
|
||||
Info<< iter.key() << " => " << iter() << nl;
|
||||
}
|
||||
|
||||
std::cerr << "\nprint table\n" << std::endl;
|
||||
table1.set("acr", 108);
|
||||
table1.set("adx", 109);
|
||||
table1.set("aec", 100);
|
||||
table1("aaw") -= 1000;
|
||||
table1("aeq") += 1000;
|
||||
|
||||
Info<< "\noverwrote some values table1: " << table1 << endl;
|
||||
|
||||
Info<< "\ntest find:" << endl;
|
||||
Info<< table1.find("aaa")() << nl
|
||||
<< table1.find("aba")() << nl
|
||||
<< table1.find("aca")() << nl
|
||||
<< table1.find("ada")() << nl
|
||||
<< table1.find("aeq")() << nl
|
||||
<< table1.find("acr")() << nl
|
||||
<< table1.find("adx")() << nl
|
||||
<< table1.find("aec")() << nl
|
||||
<< table1["aaa"] << nl;
|
||||
|
||||
forAllIter(HashTable<double>, myTable, iter)
|
||||
{
|
||||
std::cerr << *iter << '\n';
|
||||
OStringStream os;
|
||||
os << table1;
|
||||
HASHTABLE_CLASS<double> readTable(IStringStream(os.str())(), 100);
|
||||
|
||||
Info<< "Istream constructor:" << readTable << endl;
|
||||
}
|
||||
|
||||
std::cerr << "\ncopy of table\n" << std::endl;
|
||||
|
||||
HashTable<double> myTable2;
|
||||
myTable2 = myTable;
|
||||
HASHTABLE_CLASS<double> table2(table1);
|
||||
HASHTABLE_CLASS<double> table3(table1.transfer());
|
||||
|
||||
forAllConstIter(HashTable<double>, myTable2, iter2)
|
||||
Info<< "\ncopy table1 -> table2" << nl
|
||||
<< "transfer table1 -> table3 via the transfer() method" << nl;
|
||||
|
||||
Info<< "\ntable1" << table1 << nl
|
||||
<< "\ntable2" << table1 << nl
|
||||
<< "\ntable3" << table3 << nl;
|
||||
|
||||
Info<< "\ndelete table2" << nl;
|
||||
forAllIter(HASHTABLE_CLASS<double>, table2, iter)
|
||||
{
|
||||
std::cerr << *iter2 << '\n';
|
||||
Info<< "deleting " << iter.key() << " => " << iter() << " ... ";
|
||||
table2.erase(iter);
|
||||
Info<< "deleted" << endl;
|
||||
}
|
||||
|
||||
std::cerr << "\ndelete entries\n" << std::endl;
|
||||
Info<< "\ntable1" << table1 << nl
|
||||
<< "\ntable2" << table2 << nl
|
||||
<< "\ntable3" << table3 << nl;
|
||||
|
||||
forAllIter(HashTable<double>, myTable, iter)
|
||||
{
|
||||
std::cerr << "deleting " << *iter << '\n';
|
||||
myTable.erase(iter);
|
||||
std::cerr << "deleted\n";
|
||||
}
|
||||
table3.resize(1);
|
||||
Info<< "\nresize(1) table3" << table3 << nl;
|
||||
|
||||
forAllConstIter(HashTable<double>, myTable, iter)
|
||||
{
|
||||
std::cerr << *iter << '\n';
|
||||
}
|
||||
}
|
||||
table3.resize(10000);
|
||||
Info<< "\nresize(10000) table3" << table3 << nl;
|
||||
|
||||
std::cerr << "\nBye.\n";
|
||||
HASHTABLE_CLASS<double> table4;
|
||||
|
||||
table4 = table3;
|
||||
Info<< "\ncopy table3 -> table4 " << table4 << nl;
|
||||
|
||||
Info<< "\nclear table4 ... ";
|
||||
table4.clear();
|
||||
Info<< "[" << table4.size() << "] " << table4 << nl;
|
||||
|
||||
table1 = table3;
|
||||
Info<< "\ncopy table3 -> table1 (previously transferred)" << table1 << nl;
|
||||
|
||||
Info<< "test table1 == table3 : " << (table1 == table3) << nl;
|
||||
table1.erase(table1.begin());
|
||||
Info<< "removed an element - test table1 != table3 : "
|
||||
<< (table1 != table3) << nl;
|
||||
|
||||
Info<< "\nclearStorage table3 ... ";
|
||||
table3.clearStorage();
|
||||
Info<< table3 << nl;
|
||||
|
||||
Info<< "\nDone\n";
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user