Compare commits
1 Commits
master
...
feature-st
Author | SHA1 | Date | |
---|---|---|---|
|
775d0b277b |
@ -5,7 +5,7 @@
|
||||
\\ / A nd | www.openfoam.com
|
||||
\\/ M anipulation |
|
||||
-------------------------------------------------------------------------------
|
||||
Copyright (C) 2022 OpenCFD Ltd.
|
||||
Copyright (C) 2022-2023 OpenCFD Ltd.
|
||||
-------------------------------------------------------------------------------
|
||||
License
|
||||
This file is part of OpenFOAM.
|
||||
@ -75,6 +75,16 @@ void testBroadcast(List<T>& values)
|
||||
}
|
||||
|
||||
|
||||
template<class T>
|
||||
void testBroadcast(std::vector<T>& values)
|
||||
{
|
||||
Info<< nl << "is_contiguous:" << is_contiguous<T>::value << endl;
|
||||
Pout<< "pre-broadcast: " << flatOutput(values) << endl;
|
||||
Pstream::broadcast(values);
|
||||
Pout<< "post-broadcast: " << flatOutput(values) << endl;
|
||||
}
|
||||
|
||||
|
||||
void testBroadcast(bitSet& values)
|
||||
{
|
||||
Pout<< "pre-broadcast: "
|
||||
@ -135,6 +145,20 @@ int main(int argc, char *argv[])
|
||||
testBroadcast(values);
|
||||
}
|
||||
|
||||
{
|
||||
std::vector<word> values;
|
||||
if (Pstream::master())
|
||||
{
|
||||
values.resize(UPstream::nProcs());
|
||||
|
||||
for (decltype(values.size()) i=0; i < values.size(); ++i)
|
||||
{
|
||||
values[i] = "vector_" + Foam::name(i);
|
||||
}
|
||||
}
|
||||
testBroadcast(values);
|
||||
}
|
||||
|
||||
{
|
||||
vector values(vector::uniform(-1));
|
||||
if (Pstream::master())
|
||||
|
@ -91,6 +91,9 @@ public:
|
||||
//- Construct from UList, the entire size
|
||||
inline explicit SubList(const UList<T>& list) noexcept;
|
||||
|
||||
//- Construct from std::vector, the entire size
|
||||
inline explicit SubList(const std::vector<T>& list) noexcept;
|
||||
|
||||
//- Construct from FixedList, the entire size
|
||||
template<unsigned N>
|
||||
inline explicit SubList(const FixedList<T, N>& list);
|
||||
|
@ -49,6 +49,16 @@ inline Foam::SubList<T>::SubList
|
||||
{}
|
||||
|
||||
|
||||
template<class T>
|
||||
inline Foam::SubList<T>::SubList
|
||||
(
|
||||
const std::vector<T>& list
|
||||
) noexcept
|
||||
:
|
||||
UList<T>(const_cast<T*>(list.data()), label(list.size()))
|
||||
{}
|
||||
|
||||
|
||||
template<class T>
|
||||
template<unsigned N>
|
||||
inline Foam::SubList<T>::SubList
|
||||
|
@ -676,7 +676,11 @@ Ostream& operator<<(Ostream& os, const UList<T>& list)
|
||||
return list.writeList(os, Detail::ListPolicy::short_length<T>::value);
|
||||
}
|
||||
|
||||
//- Write std::vector to Ostream. ASCII only, no line-breaks
|
||||
//- Read std::vector contents from Istream
|
||||
template<class T>
|
||||
Istream& operator>>(Istream& is, std::vector<T>& list);
|
||||
|
||||
//- Write std::vector to Ostream (via UList)
|
||||
template<class T>
|
||||
Ostream& operator<<(Ostream& os, const std::vector<T>& list);
|
||||
|
||||
|
@ -26,36 +26,183 @@ License
|
||||
\*---------------------------------------------------------------------------*/
|
||||
|
||||
#include "UList.H"
|
||||
#include "Istream.H"
|
||||
#include "Ostream.H"
|
||||
#include "contiguous.H"
|
||||
#include "token.H"
|
||||
#include <vector>
|
||||
|
||||
// * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
|
||||
|
||||
template<class T>
|
||||
Foam::Ostream& Foam::operator<<(Ostream& os, const std::vector<T>& list)
|
||||
Foam::Istream& Foam::operator>>(Istream& is, std::vector<T>& list)
|
||||
{
|
||||
auto iter = list.cbegin();
|
||||
const auto last = list.cend();
|
||||
is.fatalCheck(FUNCTION_NAME);
|
||||
|
||||
// Write ascii list contents, no line breaks
|
||||
token tok(is);
|
||||
|
||||
os << label(list.size()) << token::BEGIN_LIST;
|
||||
is.fatalCheck("Istream >> std::vector<T> : reading first token");
|
||||
|
||||
// Contents
|
||||
if (iter != last)
|
||||
if (tok.isCompound())
|
||||
{
|
||||
os << *iter;
|
||||
// No compound handling ...
|
||||
|
||||
for (++iter; (iter != last); (void)++iter)
|
||||
list.clear(); // Clear old contents
|
||||
FatalIOErrorInFunction(is)
|
||||
<< "Support for compoundToken - not implemented" << nl
|
||||
<< exit(FatalIOError);
|
||||
}
|
||||
else if (tok.isLabel())
|
||||
{
|
||||
// Label: could be int(..), int{...} or just a plain '0'
|
||||
|
||||
const label len = tok.labelToken();
|
||||
|
||||
// Resize to length required
|
||||
list.resize(len);
|
||||
|
||||
if (is.format() == IOstreamOption::BINARY && is_contiguous<T>::value)
|
||||
{
|
||||
os << token::SPACE << *iter;
|
||||
// Binary and contiguous
|
||||
|
||||
if (len)
|
||||
{
|
||||
Detail::readContiguous<T>
|
||||
(
|
||||
is,
|
||||
reinterpret_cast<char*>(list.data()), // data_bytes()
|
||||
std::streamsize(list.size())*sizeof(T) // size_bytes()
|
||||
);
|
||||
|
||||
is.fatalCheck
|
||||
(
|
||||
"Istream >> std::vector<T> : "
|
||||
"reading binary block"
|
||||
);
|
||||
}
|
||||
}
|
||||
else if (std::is_same<char, T>::value)
|
||||
{
|
||||
// Special treatment for char data (binary I/O only)
|
||||
const auto oldFmt = is.format(IOstreamOption::BINARY);
|
||||
|
||||
if (len)
|
||||
{
|
||||
// read(...) includes surrounding start/end delimiters
|
||||
is.read
|
||||
(
|
||||
reinterpret_cast<char*>(list.data()), // data_bytes()
|
||||
std::streamsize(list.size())*sizeof(T) // size_bytes()
|
||||
);
|
||||
|
||||
is.fatalCheck
|
||||
(
|
||||
"Istream >> std::vector<char> : "
|
||||
"reading binary block"
|
||||
);
|
||||
}
|
||||
|
||||
is.format(oldFmt);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Begin of contents marker
|
||||
const char delimiter = is.readBeginList("List");
|
||||
|
||||
if (len)
|
||||
{
|
||||
if (delimiter == token::BEGIN_LIST)
|
||||
{
|
||||
auto iter = list.begin();
|
||||
const auto last = list.end();
|
||||
|
||||
// Contents
|
||||
for (/*nil*/; (iter != last); (void)++iter)
|
||||
{
|
||||
is >> *iter;
|
||||
|
||||
is.fatalCheck
|
||||
(
|
||||
"Istream >> std::vector<char> : "
|
||||
"reading entry"
|
||||
);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// Uniform content (delimiter == token::BEGIN_BLOCK)
|
||||
|
||||
T elem;
|
||||
is >> elem;
|
||||
|
||||
is.fatalCheck
|
||||
(
|
||||
"Istream >> std::vector<char> : "
|
||||
"reading the single entry"
|
||||
);
|
||||
|
||||
// Fill with the value
|
||||
list.assign(list.size(), elem);
|
||||
}
|
||||
}
|
||||
|
||||
// End of contents marker
|
||||
is.readEndList("List");
|
||||
}
|
||||
}
|
||||
else if (tok.isPunctuation(token::BEGIN_LIST))
|
||||
{
|
||||
// "(...)" : read as bracketed list
|
||||
|
||||
os << token::END_LIST;
|
||||
// Slightly sub-optimal since it has intermediate resizing,
|
||||
// however don't expect this as input very often.
|
||||
|
||||
os.check(FUNCTION_NAME);
|
||||
list.clear(); // Clear addressing, leave storage intact (probably)
|
||||
|
||||
is >> tok;
|
||||
is.fatalCheck(FUNCTION_NAME);
|
||||
|
||||
while (!tok.isPunctuation(token::END_LIST))
|
||||
{
|
||||
is.putBack(tok);
|
||||
|
||||
// C++17
|
||||
// is >> list.emplace_back();
|
||||
|
||||
// C++11
|
||||
list.emplace_back();
|
||||
is >> list.back();
|
||||
|
||||
is.fatalCheck
|
||||
(
|
||||
"Istream >> std::vector<char> : "
|
||||
"reading entry"
|
||||
);
|
||||
|
||||
is >> tok;
|
||||
is.fatalCheck(FUNCTION_NAME);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
list.clear(); // Clear old contents
|
||||
|
||||
FatalIOErrorInFunction(is)
|
||||
<< "incorrect first token, expected <int> or '(', found "
|
||||
<< tok.info() << nl
|
||||
<< exit(FatalIOError);
|
||||
}
|
||||
|
||||
return is;
|
||||
}
|
||||
|
||||
|
||||
template<class T>
|
||||
Foam::Ostream& Foam::operator<<(Ostream& os, const std::vector<T>& list)
|
||||
{
|
||||
// Use UList for output
|
||||
UList<T> proxy(const_cast<T*>(list.data()), label(list.size()));
|
||||
os << proxy;
|
||||
return os;
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user