ENH: additional HashTable emplace/insert/set methods (#1286)

- support move insert/set and emplace insertion.

  These adjustments can be used for improved memory efficiency, and
  allow hash tables of non-copyable objects (eg, std::unique_ptr).

- extend special HashTable output treatment to include pointer-like
  objects such as autoPtr and unique_ptr.

ENH: HashTable::at() method with checking. Fatal if entry does not exist.
This commit is contained in:
Mark Olesen
2019-05-06 08:34:39 +02:00
committed by Andrew Heather
parent e30dc962b3
commit ac317699d8
11 changed files with 227 additions and 70 deletions

View File

@ -2,7 +2,7 @@
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2017-2018 OpenCFD Ltd.
\\ / A nd | Copyright (C) 2017-2019 OpenCFD Ltd.
\\/ M anipulation |
-------------------------------------------------------------------------------
| Copyright (C) 2011 OpenFOAM Foundation
@ -25,9 +25,9 @@ License
Description
\*---------------------------------------------------------------------------*/
#include <memory>
#include <iostream>
#include "autoPtr.H"
#include "HashPtrTable.H"
@ -56,14 +56,14 @@ void printTable(const HashPtrTable<T>& table)
Info<< ")" << endl;
// Values only, with for-range
// Iterate across values, with for-range
Info<< "values (";
for (auto val : table)
for (const auto& ptr : table)
{
Info<< ' ';
if (val)
if (ptr)
{
Info<< *val;
Info<< *ptr;
}
else
{
@ -86,8 +86,27 @@ int main()
myTable.set("natlog", new double(2.718282));
myTable.insert("sqrt2", autoPtr<double>::New(1.414214));
HashTable<std::unique_ptr<double>, word, string::hash> myTable1;
myTable1.set("abc", std::unique_ptr<double>(new double(42.1)));
myTable1.set("pi", std::unique_ptr<double>(new double(3.14159)));
myTable1.set("natlog", std::unique_ptr<double>(new double(2.718282)));
HashTable<autoPtr<double>, word, string::hash> myTable2;
myTable2.set("abc", autoPtr<double>(new double(42.1)));
myTable2.set("pi", autoPtr<double>(new double(3.14159)));
myTable2.set("natlog", autoPtr<double>(new double(2.718282)));
myTable2.insert("sqrt2", autoPtr<double>::New(1.414214));
// Info<< myTable << endl;
printTable(myTable);
Info<< myTable2 << nl;
auto iter2 = myTable2.find("pi");
Info<<"PI: " << **iter2 << nl;
HashPtrTable<double> copy(myTable);