diff --git a/src/OpenFOAM/db/Time/instant/Instant.C b/src/OpenFOAM/db/Time/instant/Instant.C deleted file mode 100644 index c658e77818..0000000000 --- a/src/OpenFOAM/db/Time/instant/Instant.C +++ /dev/null @@ -1,121 +0,0 @@ -/*---------------------------------------------------------------------------*\ - ========= | - \\ / F ield | OpenFOAM: The Open Source CFD Toolbox - \\ / O peration | - \\ / A nd | www.openfoam.com - \\/ M anipulation | -------------------------------------------------------------------------------- - Copyright (C) 2018 OpenCFD Ltd. -------------------------------------------------------------------------------- -License - This file is part of OpenFOAM. - - OpenFOAM is free software: you can redistribute it and/or modify it - under the terms of the GNU General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - OpenFOAM is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License - for more details. - - You should have received a copy of the GNU General Public License - along with OpenFOAM. If not, see . - -\*---------------------------------------------------------------------------*/ - -#include "Instant.H" - -// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // - -template -Foam::Instant::Instant() -: - val_(0), - key_() -{} - - -template -Foam::Instant::Instant::Instant(scalar val, const T& key) -: - val_(val), - key_(key) -{} - - -template -Foam::Instant::Instant::Instant(scalar val, T&& key) -: - val_(val), - key_(std::move(key)) -{} - - -// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // - -template -bool Foam::Instant::equal(scalar val) const -{ - return ((val_ > val - SMALL) && (val_ < val + SMALL)); -} - - -template -template -bool Foam::Instant::equal(const Instant& other) const -{ - return this->equal(other.value()); -} - - -// * * * * * * * * * * * * * * * Global Operators * * * * * * * * * * * * * // - -template -bool Foam::operator==(const Instant& a, const Instant& b) -{ - return a.equal(b.value()); -} - - -template -bool Foam::operator!=(const Instant& a, const Instant& b) -{ - return !a.equal(b.value()); -} - - -template -bool Foam::operator<(const Instant& a, const Instant& b) -{ - return a.value() < b.value(); -} - - -template -bool Foam::operator>(const Instant& a, const Instant& b) -{ - return a.value() > b.value(); -} - - -// * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * // - -template -Foam::Istream& Foam::operator>>(Istream& is, Instant& inst) -{ - is >> inst.value() >> inst.name(); - return is; -} - - -template -Foam::Ostream& Foam::operator<<(Ostream& os, const Instant& inst) -{ - os << inst.value() << '\t' << inst.name(); - return os; -} - - -// ************************************************************************* // diff --git a/src/OpenFOAM/db/Time/instant/Instant.H b/src/OpenFOAM/db/Time/instant/Instant.H index 8c27aef27e..7d90291f6c 100644 --- a/src/OpenFOAM/db/Time/instant/Instant.H +++ b/src/OpenFOAM/db/Time/instant/Instant.H @@ -5,7 +5,7 @@ \\ / A nd | www.openfoam.com \\/ M anipulation | ------------------------------------------------------------------------------- - Copyright (C) 2018 OpenCFD Ltd. + Copyright (C) 2018-2022 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -27,19 +27,17 @@ Class Foam::Instant Description - A tuple of value and key. + A tuple of scalar value and key. The value often corresponds to a time value, thus the naming of the class. The key will usually be a time name or a file name etc. -SourceFiles - Instant.C - \*---------------------------------------------------------------------------*/ -#ifndef Instant_H -#define Instant_H +#ifndef Foam_Instant_H +#define Foam_Instant_H #include "scalar.H" +#include // std::move // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // @@ -64,28 +62,19 @@ class Instant public: - // Public classes + // Public Classes //- Less function for sorting struct less { - bool operator()(const Instant& a, const Instant& b) const + bool operator()(const Instant& a, const Instant& b) const noexcept { return a.value() < b.value(); } }; - // Constructors - - //- Construct null, with value = 0. - Instant(); - - //- Copy construct from components - Instant(scalar val, const T& key); - - //- Move construct from components - Instant(scalar val, T&& key); + // Generated Methods //- Copy construct Instant(const Instant&) = default; @@ -93,87 +82,132 @@ public: //- Move construct Instant(Instant&&) = default; - - // Member Functions - - //- The value (const access) - scalar value() const - { - return val_; - } - - //- The value (non-const access) - scalar& value() - { - return val_; - } - - //- The name/key (const access) - const T& name() const - { - return key_; - } - - //- The name/key (non-const access) - T& name() - { - return key_; - } - - //- True if values are equal (includes SMALL for rounding) - bool equal(scalar val) const; - - //- True if values are equal (includes SMALL for rounding) - template - bool equal(const Instant& other) const; - - - // Operators - //- Copy assignment Instant& operator=(const Instant&) = default; //- Move assignment Instant& operator=(Instant&&) = default; + + + // Constructors + + //- Default construct, with value = 0 and empty name + Instant() + : + val_(0), + key_() + {} + + //- Copy construct from components + Instant(scalar val, const T& key) + : + val_(val), + key_(key) + {} + + //- Move construct from components + Instant(scalar val, T&& key) + : + val_(val), + key_(std::move(key)) + {} + + + // Member Functions + + //- The value (const access) + scalar value() const noexcept + { + return val_; + } + + //- The value (non-const access) + scalar& value() noexcept + { + return val_; + } + + //- The name/key (const access) + const T& name() const noexcept + { + return key_; + } + + //- The name/key (non-const access) + T& name() noexcept + { + return key_; + } + + //- True if values are equal (includes SMALL for rounding) + bool equal(scalar val) const noexcept + { + return ((val_ > val - SMALL) && (val_ < val + SMALL)); + } + + //- True if values are equal (includes SMALL for rounding) + template + bool equal(const Instant& other) const noexcept + { + return (*this).equal(other.value()); + } }; -// IOstream Operators +// * * * * * * * * * * * * * * * Global Operators * * * * * * * * * * * * * // -template Istream& operator>>(Istream& is, Instant& inst); -template Ostream& operator<<(Ostream& os, const Instant& inst); - - -// Global Operators - -//- Compare instant values for equality template -bool operator==(const Instant& a, const Instant& b); +inline bool operator==(const Instant& a, const Instant& b) noexcept +{ + return a.equal(b.value()); +} -//- Compare instant values for inequality -template -bool operator!=(const Instant& a, const Instant& b); -//- Compare instant values for less-than template -bool operator<(const Instant& a, const Instant& b); +inline bool operator!=(const Instant& a, const Instant& b) noexcept +{ + return !a.equal(b.value()); +} + -//- Compare instant values for greater-than template -bool operator>(const Instant& a, const Instant& b); +inline bool operator<(const Instant& a, const Instant& b) noexcept +{ + return (a.value() < b.value()); +} + + +template +inline bool operator>(const Instant& a, const Instant& b) noexcept +{ + return (b.value() < a.value()); +} + + +// * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * // + +//- Read instant tuple from Istream +template +inline Istream& operator>>(Istream& is, Instant& inst) +{ + is >> inst.value() >> inst.name(); + return is; +} + + +//- Write instant tuple to Ostream +template +inline Ostream& operator<<(Ostream& os, const Instant& inst) +{ + os << inst.value() << '\t' << inst.name(); + return os; +} // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // } // End namespace Foam - -// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // - -#ifdef NoRepository - #include "Instant.C" -#endif - // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // #endif diff --git a/src/OpenFOAM/db/Time/instant/fileNameInstant.H b/src/OpenFOAM/db/Time/instant/fileNameInstant.H index ac7e4086ab..9ca7d54894 100644 --- a/src/OpenFOAM/db/Time/instant/fileNameInstant.H +++ b/src/OpenFOAM/db/Time/instant/fileNameInstant.H @@ -31,17 +31,20 @@ Description \*---------------------------------------------------------------------------*/ -#ifndef fileNameInstant_H -#define fileNameInstant_H +#ifndef Foam_fileNameInstant_H +#define Foam_fileNameInstant_H #include "fileName.H" -#include "Instant.H" +#include "instant.H" // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // namespace Foam { - typedef Instant fileNameInstant; + +typedef + Instant + fileNameInstant; } // End namespace Foam diff --git a/src/OpenFOAM/db/Time/instant/instant.C b/src/OpenFOAM/db/Time/instant/instant.C index 3c007ef387..5358ce9865 100644 --- a/src/OpenFOAM/db/Time/instant/instant.C +++ b/src/OpenFOAM/db/Time/instant/instant.C @@ -6,7 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011-2017 OpenFOAM Foundation - Copyright (C) 2018 OpenCFD Ltd. + Copyright (C) 2018-2022 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -28,7 +28,8 @@ License #include "instant.H" #include "Time.H" -#include +#include // std::atof +#include // std::move // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * // @@ -45,14 +46,18 @@ Foam::instant::instant(scalar timeValue) Foam::instant::instant(const word& timeName) : - Instant(std::atof(timeName.c_str()), timeName) -{} + Instant(0, timeName) +{ + value() = std::atof(name().c_str()); +} Foam::instant::instant(word&& timeName) : - Instant(std::atof(timeName.c_str()), std::move(timeName)) -{} + Instant(0, std::move(timeName)) +{ + value() = std::atof(name().c_str()); +} // ************************************************************************* // diff --git a/src/OpenFOAM/db/Time/instant/instant.H b/src/OpenFOAM/db/Time/instant/instant.H index b5e130046b..d4c05af601 100644 --- a/src/OpenFOAM/db/Time/instant/instant.H +++ b/src/OpenFOAM/db/Time/instant/instant.H @@ -6,7 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011-2017 OpenFOAM Foundation - Copyright (C) 2018 OpenCFD Ltd. + Copyright (C) 2018-2022 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -29,17 +29,18 @@ Class Description An instant of time. Contains the time value and name. + Uses Foam::Time when formatting the name. SourceFiles - instant.C + instants.C \*---------------------------------------------------------------------------*/ -#ifndef instant_H -#define instant_H +#ifndef Foam_instant_H +#define Foam_instant_H -#include "word.H" #include "Instant.H" +#include "word.H" // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // @@ -58,15 +59,13 @@ public: // Static Data Members + //- The type name (eg, for pTraits) static const char* const typeName; - // Constructors + // Generated Methods - //- Copy and move construct from components - using Instant::Instant; - - //- Construct null, with time-value = 0. + //- Default construct, with value = 0 and empty name instant() = default; //- Copy construct @@ -75,26 +74,29 @@ public: //- Move construct instant(instant&&) = default; - //- Construct from timeValue, auto generating the name - explicit instant(scalar timeValue); - - //- Construct from timeName, parsing timeName for a value - explicit instant(const word& timeName); - - //- Construct from timeName, parsing timeName for a value - explicit instant(word&& timeName); - - - // Operators - //- Copy assignment instant& operator=(const instant&) = default; //- Move assignment instant& operator=(instant&&) = default; + + // Constructors + + //- Inherit all constructors (eg, copy/move construct from components) + using Instant::Instant; + + //- Construct from timeValue, auto generating the name + explicit instant(scalar timeValue); + + //- Copy construct from timeName, parsing timeName for a value + explicit instant(const word& timeName); + + //- Move construct from timeName, parsing timeName for a value + explicit instant(word&& timeName); }; + // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // } // End namespace Foam diff --git a/src/OpenFOAM/db/Time/instant/instantList.H b/src/OpenFOAM/db/Time/instant/instantList.H index 91cdb70f1e..6a50b9b10e 100644 --- a/src/OpenFOAM/db/Time/instant/instantList.H +++ b/src/OpenFOAM/db/Time/instant/instantList.H @@ -31,8 +31,8 @@ Description \*---------------------------------------------------------------------------*/ -#ifndef instantList_H -#define instantList_H +#ifndef Foam_instantList_H +#define Foam_instantList_H #include "instant.H" #include "List.H" @@ -41,7 +41,11 @@ Description namespace Foam { - typedef List instantList; + +typedef + List + instantList; + } // End namespace Foam // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // diff --git a/src/OpenFOAM/global/fileOperations/masterUncollatedFileOperation/masterUncollatedFileOperation.C b/src/OpenFOAM/global/fileOperations/masterUncollatedFileOperation/masterUncollatedFileOperation.C index a2599f2fb2..d189af6e92 100644 --- a/src/OpenFOAM/global/fileOperations/masterUncollatedFileOperation/masterUncollatedFileOperation.C +++ b/src/OpenFOAM/global/fileOperations/masterUncollatedFileOperation/masterUncollatedFileOperation.C @@ -2234,9 +2234,9 @@ Foam::instantList Foam::fileOperations::masterUncollatedFileOperation::findTimes if (debug) { Pout<< "masterUncollatedFileOperation::findTimes :" - << " Found " << iter()->size() << " cached times" << endl; + << " Found " << iter.val()->size() << " cached times" << endl; } - return *iter(); + return *(iter.val()); } else { @@ -2252,19 +2252,20 @@ Foam::instantList Foam::fileOperations::masterUncollatedFileOperation::findTimes } Pstream::broadcast(times); //, comm_); - // Note: do we also cache if no times have been found since it might - // indicate a directory that is being filled later on ... - - instantList* tPtr = new instantList(std::move(times)); - - times_.set(directory, tPtr); - if (debug) { Pout<< "masterUncollatedFileOperation::findTimes :" - << " Caching times:" << *tPtr << nl + << " Caching times:" << times << nl << " for directory:" << directory << endl; } + + // Note: do we also cache if no times have been found since it might + // indicate a directory that is being filled later on ... + + auto* tPtr = new DynamicList(std::move(times)); + + times_.set(directory, tPtr); + return *tPtr; } } @@ -2280,33 +2281,40 @@ void Foam::fileOperations::masterUncollatedFileOperation::setTime return; } - // Mutable access to instantList for modification and sorting + // Mutable access to instant list for modification and sorting // - cannot use auto type deduction here - HashPtrTable::iterator iter = times_.find(tm.path()); + HashPtrTable>::iterator iter = times_.find(tm.path()); if (iter.found()) { - instantList& times = *iter(); + DynamicList& times = *(iter.val()); const instant timeNow(tm.value(), tm.timeName()); - // Exclude constant when checking and sorting - const label skipConst = + // The start index for checking and sorting (excluding "constant") + const label startIdx = ( - (!times.empty() && times[0].name() == tm.constant()) - ? 1 - : 0 + (times.empty() || times[0].name() != tm.constant()) + ? 0 + : 1 ); - if + // This routine always results in a sorted list of times, so first + // check if the new time is greater than the latest existing time. + // Can then simply append without extra searching or sorting + + if (times.size() <= startIdx || times.last() < timeNow) + { + times.append(timeNow); + } + else if ( findSortedIndex ( - SubList(times, times.size()-skipConst, skipConst), + SubList(times, times.size()-startIdx, startIdx), timeNow - ) - == -1 + ) < 0 ) { if (debug) @@ -2317,9 +2325,10 @@ void Foam::fileOperations::masterUncollatedFileOperation::setTime } times.append(timeNow); + SubList realTimes ( - times, times.size()-skipConst, skipConst + times, times.size()-startIdx, startIdx ); Foam::stableSort(realTimes); } diff --git a/src/OpenFOAM/global/fileOperations/masterUncollatedFileOperation/masterUncollatedFileOperation.H b/src/OpenFOAM/global/fileOperations/masterUncollatedFileOperation/masterUncollatedFileOperation.H index f1385f55b1..eb769d8b0c 100644 --- a/src/OpenFOAM/global/fileOperations/masterUncollatedFileOperation/masterUncollatedFileOperation.H +++ b/src/OpenFOAM/global/fileOperations/masterUncollatedFileOperation/masterUncollatedFileOperation.H @@ -6,7 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2017 OpenFOAM Foundation - Copyright (C) 2019-2021 OpenCFD Ltd. + Copyright (C) 2019-2022 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -65,6 +65,7 @@ Description #include "fileOperation.H" #include "OSspecific.H" #include "HashPtrTable.H" +#include "DynamicList.H" #include "List.H" #include "unthreadedInitialise.H" @@ -95,7 +96,7 @@ protected: const label myComm_; //- Cached times for a given directory - mutable HashPtrTable times_; + mutable HashPtrTable> times_; // Protected Operation Functors @@ -756,7 +757,7 @@ public: virtual void flush() const; //- Return cached times - const HashPtrTable& times() const + const HashPtrTable>& times() const noexcept { return times_; }