Commit 80710205 authored by Mark Olesen's avatar Mark Olesen
Browse files

ENH: make List output compile-time configurable (#1160)

- introduced a ListPolicy details to make the transition between
  a short list (space separated) and a long list (newline separated)
  more configurable.

  We suppress line breaks for commonly used types that often have
  short content: (word, wordRes, keyType).
parent bef508de
......@@ -46,6 +46,7 @@ See also
#include "HashOps.H"
#include "ListOps.H"
#include "SubList.H"
#include "ListPolicy.H"
#include <list>
#include <numeric>
......@@ -64,9 +65,22 @@ public:
using List<string>::List;
};
namespace Detail
{
namespace ListPolicy
{
// Override on a per-type basis
template<> struct short_length<short> : std::integral_constant<short,20> {};
} // End namespace ListPolicy
} // End namespace Detail
} // End namespace Foam
using namespace Foam;
template<class T, class ListType>
......@@ -92,6 +106,20 @@ void printMyString(const UList<string>& lst)
}
template<class T>
Ostream& printListOutputType(const char* what)
{
Info<< what
<< " (contiguous="
<< contiguous<T>() << " no_linebreak="
<< Detail::ListPolicy::no_linebreak<T>::value
<< " short_length="
<< Detail::ListPolicy::short_length<T>::value << ')';
return Info;
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
// Main program:
......@@ -251,23 +279,54 @@ int main(int argc, char *argv[])
Info<<"scalar identity:" << flatOutput(slist) << endl;
Info<< "labels (contiguous=" << contiguous<label>() << ")" << nl;
printListOutputType<label>("labels") << nl;
Info<< "normal: " << longLabelList << nl;
Info<< "flatOutput: " << flatOutput(longLabelList) << nl;
// Info<< "flatOutput(14): " << flatOutput(longLabelList, 14) << nl;
auto shrtList = ListOps::create<short>
(
longLabelList,
[](const label& val){ return val; }
);
printListOutputType<short>("short") << nl;
Info<< "normal: " << shrtList << nl;
stringList longStringList(12);
forAll(longStringList, i)
{
longStringList[i].resize(3, 'a' + i);
}
Info<< "string (contiguous=" << contiguous<string>() << ")" << nl;
printListOutputType<string>("string") << nl;
Info<< "normal: " << longStringList << nl;
Info<< "flatOutput: " << flatOutput(longStringList) << nl;
// contiguous longStringList[i].resize(3, 'a' + i);
auto wList = ListOps::create<word>
(
longStringList,
[](const std::string& val){ return val; }
);
printListOutputType<word>("word") << nl;
Info<< "normal: " << wList << nl;
// Shorten
longStringList.resize(8);
wList.resize(8);
Info<< "Test shorter lists" << nl;
printListOutputType<string>("string") << nl;
Info<< "normal: " << longStringList << nl;
printListOutputType<word>("word") << nl;
Info<< "normal: " << wList << nl;
}
// test SubList and labelRange
......
......@@ -3,7 +3,7 @@
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation
\\/ M anipulation | Copyright (C) 2017-2018 OpenCFD Ltd.
\\/ M anipulation | Copyright (C) 2017-2019 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
......@@ -400,10 +400,9 @@ public:
//- Clear list and read from stream
Istream& read(Istream& is);
//- Write the List, with line-breaks in ASCII if the list length
//- exceeds shortListLen.
//- Write List, with line-breaks in ASCII when length exceeds shortLen.
// Using '0' suppresses line-breaks entirely.
Ostream& writeList(Ostream& os, const label shortListLen=0) const;
Ostream& writeList(Ostream& os, const label shortLen=0) const;
//- Write as a dictionary entry with keyword
void writeEntry(const word& keyword, Ostream& os) const;
......@@ -492,15 +491,20 @@ public:
Istream& is,
PackedList<Width>& list
);
friend Ostream& operator<< <Width>
(
Ostream& os,
const PackedList<Width>& list
);
};
// * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
//- Write List to Ostream, as per UList::writeList() with default length.
// The default short-length is given by Detail::ListPolicy::short_length
template<unsigned Width>
Ostream& operator<<(Ostream& os, const PackedList<Width>& list)
{
return list.writeList(os, Detail::ListPolicy::short_length<void>::value);
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
} // End namespace Foam
......
......@@ -2,7 +2,7 @@
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2018 OpenCFD Ltd.
\\ / A nd | Copyright (C) 2018-2019 OpenCFD Ltd.
\\/ M anipulation |
-------------------------------------------------------------------------------
License
......@@ -206,7 +206,7 @@ template<unsigned Width>
Foam::Ostream& Foam::PackedList<Width>::writeList
(
Ostream& os,
const label shortListLen
const label shortLen
) const
{
const PackedList<Width>& list = *this;
......@@ -220,7 +220,7 @@ Foam::Ostream& Foam::PackedList<Width>::writeList
// Two or more entries, and all entries have identical values.
os << len << token::BEGIN_BLOCK << list[0] << token::END_BLOCK;
}
else if (!shortListLen || len <= shortListLen)
else if (!shortLen || len <= shortLen)
{
// Shorter list, or line-breaks suppressed
os << len << token::BEGIN_LIST;
......@@ -284,13 +284,6 @@ Foam::Istream& Foam::operator>>(Istream& is, PackedList<Width>& list)
}
template<unsigned Width>
Foam::Ostream& Foam::operator<<(Ostream& os, const PackedList<Width>& list)
{
return list.writeList(os, 10);
}
template<unsigned Width>
Foam::Ostream& Foam::operator<<
(
......
......@@ -2,7 +2,7 @@
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2018 OpenCFD Ltd.
\\ / A nd | Copyright (C) 2018-2019 OpenCFD Ltd.
\\/ M anipulation |
-------------------------------------------------------------------------------
License
......@@ -541,10 +541,9 @@ public:
// IO
//- Write the bitSet, with line-breaks in ASCII if the size
//- exceeds shortListLen.
//- Write bitSet, with line-breaks (ASCII) when length exceeds shortLen.
// Using '0' suppresses line-breaks entirely.
Ostream& writeList(Ostream& os, const label shortListLen=0) const;
Ostream& writeList(Ostream& os, const label shortLen=0) const;
//- Write as a dictionary entry with keyword
void writeEntry(const word& keyword, Ostream& os) const;
......@@ -561,7 +560,7 @@ public:
// Housekeeping
//- \deprecated(2018-04) compatibility name for PackedBoolList
//- Deprecated(2018-04) compatibility name for PackedBoolList
// \deprecated(2018-04) - use toc() method
inline labelList used() const { return toc(); }
......@@ -570,9 +569,13 @@ public:
// Global Operators
Ostream& operator<<(Ostream& os, const InfoProxy<bitSet>& info);
//- Write bitset to Ostream, as per bitSet::writeList() with default length
//- of 40 items.
Ostream& operator<<(Ostream& os, const bitSet& bitset);
//- Output bitset information
Ostream& operator<<(Ostream& os, const InfoProxy<bitSet>& info);
//- Bitwise-AND of two bitsets.
// See bitSet::operator&= for more details.
......
......@@ -2,7 +2,7 @@
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2018 OpenCFD Ltd.
\\ / A nd | Copyright (C) 2018-2019 OpenCFD Ltd.
\\/ M anipulation |
-------------------------------------------------------------------------------
License
......@@ -39,7 +39,7 @@ void Foam::bitSet::writeEntry(Ostream& os) const
Foam::Ostream& Foam::bitSet::writeList
(
Ostream& os,
const label shortListLen
const label shortLen
) const
{
const bitSet& list = *this;
......@@ -53,7 +53,7 @@ Foam::Ostream& Foam::bitSet::writeList
// Two or more entries, and all entries have identical values.
os << len << token::BEGIN_BLOCK << list[0] << token::END_BLOCK;
}
else if (!shortListLen || len <= shortListLen)
else if (!shortLen || len <= shortLen)
{
// Shorter list, or line-breaks suppressed
os << len << token::BEGIN_LIST;
......@@ -111,7 +111,7 @@ void Foam::bitSet::writeEntry
Foam::Ostream& Foam::operator<<(Ostream& os, const bitSet& bitset)
{
return bitset.writeList(os, 10);
return bitset.writeList(os, 40);
}
......
......@@ -3,7 +3,7 @@
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation
\\/ M anipulation | Copyright (C) 2016-2018 OpenCFD Ltd.
\\/ M anipulation | Copyright (C) 2016-2019 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
......@@ -362,7 +362,7 @@ Foam::HashSet<Key, Hash>::operator-=(const HashSet<Key, Hash>& rhs)
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
return tbl.writeKeys(os, Detail::ListPolicy::short_length<Key>::value);
}
......
......@@ -290,12 +290,12 @@ public:
// Writing
//- Write the unordered keys as a list, with line-breaks if list length
//- exceeds shortListLen.
//- Write unordered keys (list), with line-breaks
//- when length exceeds shortLen.
// Using '0' suppresses line-breaks entirely.
Ostream& writeList(Ostream& os, const label shortListLen=0) const
Ostream& writeList(Ostream& os, const label shortLen=0) const
{
return this->writeKeys(os, shortListLen);
return this->writeKeys(os, shortLen);
}
......
......@@ -870,10 +870,10 @@ public:
//- Print information
Ostream& printInfo(Ostream& os) const;
//- Write the unordered keys as a list, with line-breaks if list length
//- exceeds shortListLen.
//- Write unordered keys (list), with line-breaks
//- when length exceeds shortLen.
// Using '0' suppresses line-breaks entirely.
Ostream& writeKeys(Ostream& os, const label shortListLen=0) const;
Ostream& writeKeys(Ostream& os, const label shortLen=0) const;
// IOstream Operator
......
......@@ -115,7 +115,7 @@ template<class T, class Key, class Hash>
Foam::Ostream& Foam::HashTable<T, Key, Hash>::writeKeys
(
Ostream& os,
const label shortListLen
const label shortLen
) const
{
// Similar to UList::writeList version except the following:
......@@ -124,7 +124,11 @@ Foam::Ostream& Foam::HashTable<T, Key, Hash>::writeKeys
label i = this->size();
if (i <= 1 || !shortListLen || (i <= shortListLen))
if
(
(i <= 1 || !shortLen)
|| (i <= shortLen)
)
{
// Write size and start delimiter
os << i << token::BEGIN_LIST;
......
......@@ -294,10 +294,9 @@ public:
// IOstream operators
//- Write LList with line-breaks when its length exceeds
//- shortListLen.
//- Write LList with line-breaks when length exceeds shortLen.
// Using '0' suppresses line-breaks entirely.
Ostream& writeList(Ostream& os, const label shortListLen=0) const;
Ostream& writeList(Ostream& os, const label shortLen=0) const;
//- Read list from Istream
friend Istream& operator>> <LListBase, T>
......@@ -307,7 +306,7 @@ public:
);
//- Write LList to Ostream with line breaks,
//- as per writeList() with shortListLen=-1
//- as per writeList with shortLen=-1
friend Ostream& operator<< <LListBase, T>
(
Ostream& os,
......
......@@ -130,15 +130,15 @@ template<class LListBase, class T>
Foam::Ostream& Foam::LList<LListBase, T>::writeList
(
Ostream& os,
const label shortListLen
const label shortLen
) const
{
const label len = this->size();
if
(
len <= 1 || !shortListLen
|| (len <= shortListLen)
(len <= 1 || !shortLen)
|| (len <= shortLen)
)
{
// Size and start delimiter
......@@ -179,7 +179,7 @@ Foam::Ostream& Foam::LList<LListBase, T>::writeList
template<class LListBase, class T>
Foam::Ostream& Foam::operator<<(Ostream& os, const LList<LListBase, T>& lst)
{
return lst.writeList(os, -1); // always with line breaks
return lst.writeList(os, -1); // Always with line breaks
}
......
......@@ -178,13 +178,12 @@ public:
// IOstream operators
//- Write UILList with line-breaks when its length exceeds
//- shortListLen.
//- Write UILList with line-breaks when length exceeds shortLen.
// Using '0' suppresses line-breaks entirely.
Ostream& writeList(Ostream& os, const label shortListLen=0) const;
Ostream& writeList(Ostream& os, const label shortLen=0) const;
//- Write UILList to Ostream with line breaks,
//- as per writeList() with shortListLen=-1
//- as per writeList() with shortLen=-1
friend Ostream& operator<< <LListBase, T>
(
Ostream& os,
......
......@@ -33,15 +33,15 @@ template<class LListBase, class T>
Foam::Ostream& Foam::UILList<LListBase, T>::writeList
(
Ostream& os,
const label shortListLen
const label shortLen
) const
{
const label len = this->size();
if
(
len <= 1 || !shortListLen
|| (len <= shortListLen)
(len <= 1 || !shortLen)
|| (len <= shortLen)
)
{
// Size and start delimiter
......
......@@ -46,6 +46,7 @@ SourceFiles
#include "Swap.H"
#include "HashFwd.H"
#include "SLListFwd.H"
#include "ListPolicy.H"
#include <initializer_list>
#include <iterator>
......@@ -400,8 +401,7 @@ public:
//- Write the list as a dictionary entry with keyword
void writeEntry(const word& keyword, Ostream& os) const;
//- Write the list, with line-breaks in ASCII if the length
//- exceeds shortLen.
//- Write List, with line-breaks in ASCII when length exceeds shortLen.
// Using '0' suppresses line-breaks entirely.
Ostream& writeList(Ostream& os, const label shortLen=0) const;
......@@ -480,11 +480,12 @@ struct Hash<FixedList<T, N>>
// * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
//- Write to Ostream, as per FixedList::writeList() with shortLen=10
//- Write List to Ostream, as per FixedList::writeList() with default length.
// The default short-length is given by Detail::ListPolicy::short_length
template<class T, unsigned N>
Ostream& operator<<(Ostream& os, const FixedList<T, N>& list)
{
return list.writeList(os, 10);
return list.writeList(os, Detail::ListPolicy::short_length<T>::value);
}
......
......@@ -62,7 +62,7 @@ template<class T, unsigned N>
Foam::Ostream& Foam::FixedList<T, N>::writeList
(
Ostream& os,
const label shortListLen
const label shortLen
) const
{
const FixedList<T, N>& list = *this;
......@@ -77,8 +77,16 @@ Foam::Ostream& Foam::FixedList<T, N>::writeList
{
if
(
N <= 1 || !shortListLen
|| (N <= unsigned(shortListLen) && contiguous<T>())
(N <= 1 || !shortLen)
||
(
(N <= unsigned(shortLen))
&&
(
Detail::ListPolicy::no_linebreak<T>::value
|| contiguous<T>()
)
)
)
{
// Start delimiter
......
......@@ -3,7 +3,7 @@
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation
\\/ M anipulation | Copyright (C) 2017-2018 OpenCFD Ltd.
\\/ M anipulation | Copyright (C) 2017-2019 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
......@@ -50,7 +50,6 @@ namespace Foam
// Forward declarations
template<class T> class UIndirectList;
template<class T> Ostream& operator<<(Ostream&, const UIndirectList<T>&);
// Commonly required list types
typedef UIndirectList<bool> boolUIndList;
......@@ -320,21 +319,21 @@ public:
// Writing
//- Write the list, with line-breaks in ASCII if its length
//- exceeds shortListLen.
//- Write List, with line-breaks in ASCII when length exceeds shortLen.
// Using '0' suppresses line-breaks entirely.
Ostream& writeList(Ostream& os, const label shortListLen=0) const;
Ostream& writeList(Ostream& os, const label shortLen=0) const;
};
// Ostream operator
// * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
//- Write list to Ostream, as per writeList() with shortListLen=10
friend Ostream& operator<< <T>
(
Ostream& os,
const UIndirectList<T>& list
);
};
//- Write List to Ostream, as per UList::writeList() with default length.
// The default short-length is given by Detail::ListPolicy::short_length
template<class T>
Ostream& operator<<(Ostream& os, const UIndirectList<T>& list)
{
return list.writeList(os, Detail::ListPolicy::short_length<T>::value);
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
......
......@@ -3,7 +3,7 @@
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2011-2014 OpenFOAM Foundation
\\/ M anipulation | Copyright (C) 2016-2017 OpenCFD Ltd.
\\/ M anipulation | Copyright (C) 2016-2019 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
......@@ -34,7 +34,7 @@ template<class T>
Foam::Ostream& Foam::UIndirectList<T>::writeList
(
Ostream& os,
const label shortListLen
const label shortLen
) const
{
const UIndirectList<T>& list = *this;
......@@ -51,8 +51,16 @@ Foam::Ostream& Foam::UIndirectList<T>::writeList
}
else if
(
len <= 1 || !shortListLen
|| (len <= shortListLen && contiguous<T>())
(len <= 1 || !shortLen)
||
(
(len <= shortLen)
&&
(
Detail::ListPolicy::no_linebreak<T>::value
|| contiguous<T>()
)
)
)
{
// Size and start delimiter
......@@ -114,17 +122,4 @@ Foam::Ostream& Foam::UIndirectList<T>::writeList
}
// * * * * * * * * * * * * * * * Ostream Operator * * * * * * * * * * * * * //
template<class T>
Foam::Ostream& Foam::operator<<
(
Foam::Ostream& os,
const Foam::UIndirectList<T>& list
)
{
return list