ENH: simpler handling of dictionary start/end line numbers

STYLE: use front(), back(), push_front(), push_back() methods
This commit is contained in:
Mark Olesen 2023-07-20 13:41:09 +02:00
parent 7cae3b9660
commit 76efcba4c7
10 changed files with 51 additions and 70 deletions

View File

@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2017 OpenFOAM Foundation
Copyright (C) 2015-2022 OpenCFD Ltd.
Copyright (C) 2015-2023 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -102,8 +102,8 @@ Foam::dictionary::dictionary
if (e.keyword().isPattern())
{
patterns_.prepend(&e);
regexps_.prepend(autoPtr<regExp>::New(e.keyword()));
patterns_.push_front(&e);
regexps_.push_front(autoPtr<regExp>::New(e.keyword()));
}
}
}
@ -124,8 +124,8 @@ Foam::dictionary::dictionary
if (e.keyword().isPattern())
{
patterns_.prepend(&e);
regexps_.prepend(autoPtr<regExp>::New(e.keyword()));
patterns_.push_front(&e);
regexps_.push_front(autoPtr<regExp>::New(e.keyword()));
}
}
}
@ -204,23 +204,23 @@ const Foam::dictionary& Foam::dictionary::topDict() const
Foam::label Foam::dictionary::startLineNumber() const
{
if (size())
{
return first()->startLineNumber();
}
return -1;
return
(
IDLList<entry>::empty()
? -1
: IDLList<entry>::front()->startLineNumber()
);
}
Foam::label Foam::dictionary::endLineNumber() const
{
if (size())
{
return last()->endLineNumber();
}
return -1;
return
(
IDLList<entry>::empty()
? -1
: IDLList<entry>::back()->endLineNumber()
);
}
@ -674,8 +674,8 @@ Foam::entry* Foam::dictionary::add(entry* entryPtr, bool mergeEntry)
if (entryPtr->keyword().isPattern())
{
patterns_.prepend(entryPtr);
regexps_.prepend(autoPtr<regExp>::New(entryPtr->keyword()));
patterns_.push_front(entryPtr);
regexps_.push_front(autoPtr<regExp>::New(entryPtr->keyword()));
}
return entryPtr; // now an entry in the dictionary
@ -698,12 +698,12 @@ Foam::entry* Foam::dictionary::add(entry* entryPtr, bool mergeEntry)
entryPtr->name() =
fileName::concat(name(), entryPtr->keyword(), '.');
parent_type::append(entryPtr);
parent_type::push_back(entryPtr);
if (entryPtr->keyword().isPattern())
{
patterns_.prepend(entryPtr);
regexps_.prepend(autoPtr<regExp>::New(entryPtr->keyword()));
patterns_.push_front(entryPtr);
regexps_.push_front(autoPtr<regExp>::New(entryPtr->keyword()));
}
return entryPtr; // now an entry in the dictionary

View File

@ -57,23 +57,13 @@ Foam::dictionaryEntry::dictionaryEntry
Foam::label Foam::dictionaryEntry::startLineNumber() const
{
if (size())
{
return first()->startLineNumber();
}
return -1;
return dictionary::startLineNumber();
}
Foam::label Foam::dictionaryEntry::endLineNumber() const
{
if (size())
{
return last()->endLineNumber();
}
return -1;
return dictionary::endLineNumber();
}

View File

@ -179,17 +179,22 @@ void Foam::dictionary::writeEntry(const keyType& kw, Ostream& os) const
void Foam::dictionary::writeEntries(Ostream& os, const bool extraNewLine) const
{
// Add extra new line separation between entries
// for "top-level" dictionaries
const bool addLine = (extraNewLine && parent() == dictionary::null);
bool separator = false;
for (const entry& e : *this)
{
// Write entry
os << e;
// Add extra new line between entries for "top-level" dictionaries,
// but not after the last entry (looks ugly).
if (extraNewLine && parent() == dictionary::null && e != *last())
if (separator)
{
os << nl;
}
separator = addLine;
// Write entry
os << e;
// Check stream before going to next entry.
if (!os.good())

View File

@ -32,16 +32,14 @@ License
// * * * * * * * * * * * * * * * Local Functions * * * * * * * * * * * * * * //
// File-scope: The dictionary size without the "FoamFile" entry
static Foam::label realSize(const Foam::dictionary& dict)
static inline Foam::label realSize(const Foam::dictionary& dict)
{
if (dict.size() < 1 || dict.first()->keyword() != "FoamFile")
{
return dict.size();
}
else
{
return dict.size() - 1;
}
return
(
(dict.empty() || (dict.front()->keyword() != "FoamFile"))
? dict.size()
: dict.size() - 1
);
}

View File

@ -680,8 +680,8 @@ bool Foam::dictionary::changeKeyword
if (newKeyword.isPattern())
{
patterns_.prepend(iter());
regexps_.prepend(autoPtr<regExp>::New(newKeyword));
patterns_.push_front(iter());
regexps_.push_front(autoPtr<regExp>::New(newKeyword));
}
return true;

View File

@ -49,7 +49,7 @@ Foam::functionEntry::readStringList(Istream& is)
// - treated like list with a single entry
list.resize(1);
iss >> list.first();
iss >> list.front();
}
else
{

View File

@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2016 OpenFOAM Foundation
Copyright (C) 2017-2021 OpenCFD Ltd.
Copyright (C) 2017-2023 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -265,26 +265,14 @@ Foam::primitiveEntry::primitiveEntry(const keyType& key, const ITstream& is)
Foam::label Foam::primitiveEntry::startLineNumber() const
{
const tokenList& tokens = *this;
if (tokens.size())
{
return tokens.first().lineNumber();
}
return -1;
return (tokens.empty() ? -1 : tokens.front().lineNumber());
}
Foam::label Foam::primitiveEntry::endLineNumber() const
{
const tokenList& tokens = *this;
if (tokens.size())
{
return tokens.last().lineNumber();
}
return -1;
return (tokens.empty() ? -1 : tokens.back().lineNumber());
}

View File

@ -231,7 +231,7 @@ void Foam::expressions::exprResultDelayed::storeValue
{
// Replace value
storedValues_.last().second() = settingResult_;
storedValues_.back().second() = settingResult_;
}
}

View File

@ -736,7 +736,7 @@ void Foam::Field<Type>::writeEntry(const word& keyword, Ostream& os) const
if (is_contiguous<Type>::value && List<Type>::uniform())
{
os << word("uniform") << token::SPACE << this->first();
os << word("uniform") << token::SPACE << List<Type>::front();
}
else
{

View File

@ -565,7 +565,7 @@ Foam::word Foam::fileName::component
{
if (cmpt == std::string::npos)
{
return parsed.last().str();
return parsed.back().str();
}
else if (cmpt < parsed.size())
{