Commit 8f75bfbe authored by Mark OLESEN's avatar Mark OLESEN
Browse files

ENH: add HashTable::writeKeys and HashSet::writeList

- can use the hash-set writeList in combination with FlatOutput:

  Eg, flatOutput(myHashSet);
parent c0a50dc6
......@@ -3,7 +3,7 @@
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2011 OpenFOAM Foundation
\\/ M anipulation |
\\/ M anipulation | Copyright (C) 2017 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
......@@ -29,6 +29,7 @@ Description
#include "HashSet.H"
#include "Map.H"
#include "labelPairHashes.H"
#include "FlatOutput.H"
using namespace Foam;
......@@ -66,6 +67,8 @@ int main(int argc, char *argv[])
tableB.insert("value5", nil());
tableB.insert("value6", nil());
Info<< "tableA keys: "; tableA.writeKeys(Info) << endl;
Map<label> mapA
{
{ 1, 1 },
......@@ -149,7 +152,7 @@ int main(int argc, char *argv[])
labelHashSet setD(1);
setD.insert({11, 100, 49, 36, 2008});
Info<< "setD : " << setD << endl;
Info<< "setD : " << flatOutput(setD) << endl;
Info<< "setB == setC: " << (setB == setC) << endl;
Info<< "setC != setD: " << (setC != setD) << endl;
......@@ -188,7 +191,7 @@ int main(int argc, char *argv[])
Info<< "setD has no 11" << endl;
}
Info<< "setD : " << setD << endl;
Info<< "setD : " << flatOutput(setD) << endl;
// this doesn't work (yet?)
// setD[12] = true;
......@@ -201,7 +204,7 @@ int main(int argc, char *argv[])
label added = setD.set(someLst);
Info<< "added " << added << " from " << someLst.size() << endl;
Info<< "setD : " << setD << endl;
Info<< "setD : " << flatOutput(setD) << endl;
return 0;
......
......@@ -267,6 +267,15 @@ void Foam::HashSet<Key, Hash>::operator-=(const HashSet<Key, Hash>& rhs)
}
// * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
template<class Key, class Hash>
Foam::Ostream& Foam::operator<<(Ostream& os, const HashSet<Key, Hash>& tbl)
{
return tbl.writeList(os, 10); // 10=consistent with UList
}
/* * * * * * * * * * * * * * * * Global operators * * * * * * * * * * * * * */
template<class Key, class Hash>
......
......@@ -209,6 +209,16 @@ public:
}
// Writing
//- Write the unordered keys as a list, with line-breaks if list length
// exceeds shortListLen. Using '0' suppresses line-breaks entirely.
Ostream& writeList(Ostream& os, const label shortListLen=0) const
{
return this->writeKeys(os, shortListLen);
}
// Member Operators
//- Return true if the entry exists, same as found()
......
......@@ -261,9 +261,6 @@ public:
//- Return the table of contents as a sorted list
List<Key> sortedToc() const;
//- Print information
Ostream& printInfo(Ostream& os) const;
// Edit
......@@ -572,6 +569,15 @@ public:
};
// Writing
//- Print information
Ostream& printInfo(Ostream& os) const;
//- Write the unordered keys as a list, with line-breaks if list length
// exceeds shortListLen. Using '0' suppresses line-breaks entirely.
Ostream& writeKeys(Ostream& os, const label shortListLen=0) const;
// IOstream Operator
......
......@@ -54,8 +54,7 @@ Foam::HashTable<T, Key, Hash>::HashTable(Istream& is, const label size)
// * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * * //
template<class T, class Key, class Hash>
Foam::Ostream&
Foam::HashTable<T, Key, Hash>::printInfo(Ostream& os) const
Foam::Ostream& Foam::HashTable<T, Key, Hash>::printInfo(Ostream& os) const
{
label used = 0;
label maxChain = 0;
......@@ -90,6 +89,53 @@ Foam::HashTable<T, Key, Hash>::printInfo(Ostream& os) const
}
template<class T, class Key, class Hash>
Foam::Ostream& Foam::HashTable<T, Key, Hash>::writeKeys
(
Ostream& os,
const label shortListLen
) const
{
// Similar to UList::writeList version except the following:
// - the keys can never be uniform
// - never write in binary
label i = this->size();
if (i <= 1 || !shortListLen || (i <= shortListLen))
{
// Write size and start delimiter
os << i << token::BEGIN_LIST;
i = 0;
for (const_iterator iter = this->cbegin(); iter != this->cend(); ++iter)
{
if (i++) os << token::SPACE;
os << iter.key();
}
os << token::END_LIST; // End delimiter
}
else
{
// Write size and start delimiter
os << nl << i << nl << token::BEGIN_LIST << nl;
for (const_iterator iter = this->cbegin(); iter != this->cend(); ++iter)
{
os << iter.key() << nl;
}
os << token::END_LIST << nl; // End delimiter
}
// Check state of IOstream
os.check("HashSet<Key>::writeList(Ostream&)");
return os;
}
// * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
template<class T, class Key, class Hash>
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment