ENH: support HashTable iterator pointer dereference

This commit is contained in:
Mark Olesen
2019-04-06 16:00:21 +02:00
committed by Andrew Heather
parent d4eb17a9ff
commit 39c91d8440
20 changed files with 300 additions and 115 deletions

View File

@ -2,7 +2,7 @@
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd |
\\ / A nd | Copyright (C) 2019 OpenCFD Ltd.
\\/ M anipulation |
-------------------------------------------------------------------------------
| Copyright (C) 2011-2016 OpenFOAM Foundation
@ -31,9 +31,53 @@ Description
#include "HashTable.H"
#include "HashPtrTable.H"
#include "Map.H"
#include "FlatOutput.H"
using namespace Foam;
// Simple wrapper for testing purposes
class Label
{
label data_;
public:
Label()
:
data_(0)
{}
Label(label val)
:
data_(val)
{}
~Label()
{
Info<<"delete label: " << data_ << endl;
}
// Some arbitrary non-const method (for testing)
label increment()
{
return ++data_;
}
// Some arbitrary method (for testing)
std::string info() const
{
return "boxed label=" + std::to_string(data_);
}
friend Ostream& operator<<(Ostream& os, const Label& val)
{
os << val.data_;
return os;
}
};
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
// Main program:
@ -46,7 +90,7 @@ int main(int argc, char *argv[])
};
Info<< "table1: " << table1 << nl
<< "toc: " << table1.toc() << endl;
<< "toc: " << flatOutput(table1.toc()) << nl;
HashTable<label, label, Hash<label>> table2
{
@ -56,31 +100,156 @@ int main(int argc, char *argv[])
};
Info<< "table2: " << table2 << nl
<< "toc: " << table2.toc() << endl;
<< "toc: " << flatOutput(table2.toc()) << nl;
Map<label> table3(1);
table3.transfer(table2);
Info<< "table2: " << table2 << nl
<< "toc: " << table2.toc() << endl;
<< "toc: " << flatOutput(table2.toc()) << nl;
Info<< "table3: " << table3 << nl
<< "toc: " << table3.toc() << endl;
<< "toc: " << flatOutput(table3.toc()) << nl;
Map<label> table4(std::move(table3));
Info<< "table3: " << table3 << nl
<< "toc: " << table3.toc() << endl;
<< "toc: " << flatOutput(table3.toc()) << nl;
Info<< "table4: " << table4 << nl
<< "toc: " << table4.toc() << endl;
<< "toc: " << flatOutput(table4.toc()) << nl;
HashPtrTable<label, Foam::string> ptable1(0);
ptable1.insert("kjhkjh", autoPtr<label>::New(10));
{
HashTable<Label, Foam::string> table1(0);
table1.insert("abc", 5);
table1.insert("def", 10);
table1.insert("ghi", 15);
table1.insert("jkl", 20);
Info<< "PtrTable toc: " << ptable1.toc() << endl;
Info<< nl << "Table toc: " << flatOutput(table1.toc()) << nl;
Info<< "End\n" << endl;
for (const word k : { "abc" })
{
const auto iter = table1.cfind(k);
if (iter.good())
{
Info<< "have " << k << nl
<< " info: " << (*iter).info() << nl
// Good: does not compile
// << " info: " << iter->info() << nl
;
}
auto iter2 = table1.find(k);
if (iter2.good())
{
Info<< "have " << k << nl
<< " incr: " << (*iter2).increment() << nl
// Good: does not compile
// << " incr: " << iter2->increment() << nl
;
}
}
}
{
HashPtrTable<Label> ptable1(0);
ptable1.insert("abc", autoPtr<Label>::New(5));
ptable1.insert("def", autoPtr<Label>::New(10));
ptable1.insert("ghi", autoPtr<Label>::New(15));
ptable1.insert("jkl", autoPtr<Label>::New(20));
Info<< nl << "PtrTable toc: " << flatOutput(ptable1.toc()) << nl;
for (const word k : { "abc" })
{
const auto iter = ptable1.cfind(k);
// Note: increment() changes contents of pointers,
// not the pointers themselves.
if (iter.good())
{
Info<< "have " << k << nl
<< " addr: " << long(*iter) << nl
<< " info: " << (*iter)->info() << nl
<< " info: " << iter->info() << nl
<< " incr: " << iter->increment() << nl
;
}
auto iter2 = ptable1.find(k);
if (iter2.good())
{
Info<< "have " << k << nl
<< " incr: " << (*iter2)->increment() << nl
<< " incr: " << iter2->increment() << nl
;
}
}
// Attempt the same again
HashTable<Label*> tableView1;
HashTable<const Label*> tableView2;
forAllConstIters(ptable1, iter)
{
tableView1.insert(iter.key(), iter.val());
tableView2.insert(iter.key(), iter.val());
}
Info<< nl << "Table<pointer> toc: "
<< flatOutput(tableView1.toc()) << nl;
for (const word k : { "abc" })
{
const auto iter1 = tableView1.cfind(k);
// Note that increment changes contents of the pointers
// not the table
if (iter1.good())
{
Info<< "have " << k << nl
<< " addr: " << long(*iter1) << nl
<< " info: " << (*iter1)->info() << nl
<< " info: " << iter1->info() << nl
<< " incr: " << iter1->increment() << nl
;
}
auto iter2 = tableView2.cfind(k);
if (iter2.good())
{
Info<< "have " << k << nl
<< " addr: " << long(*iter2) << nl
<< " info: " << (*iter2)->info() << nl
<< " info: " << iter2->info() << nl
// Good: does not compile
// << " incr: " << iter2->increment() << nl
;
}
auto iter3 = tableView2.find(k);
if (iter3.good())
{
Info<< "have " << k << nl
<< " addr: " << long(*iter3) << nl
<< " info: " << (*iter3)->info() << nl
<< " info: " << iter3->info() << nl
// Good: does not compile
// << " incr: " << iter3->increment() << nl
;
}
}
Info<< nl << "Ending scope" << nl;
}
Info<< "\nEnd\n" << endl;
return 0;
}