From b8a0706e72a7f74b7d1d20f465401929ff30c85e Mon Sep 17 00:00:00 2001 From: Mark Olesen Date: Tue, 1 Apr 2025 10:56:23 +0200 Subject: [PATCH] ENH: add ListPolicy::reserve_size() helper (related to #3348) - central way to calculate reverse sizes for dynamic containers. For example; reserve_size<16, 2>(len, cap); // min-size=16, ratio=2 reserve_size<16, 3, 2>(len, cap); // min-size=16, ratio=1.5 replaces this type of code that was used in several places: max(SizeMin, max(len, label(2*capacity_))); The caller will have already checked (len < cap) before deciding to make this call. ENH: updates for DynamicList/DynamicField handling - add reserve_exact() method, which is like reserve() but without any extra sizing heuristics - add DynamicField 'reuse' constructors, consistent with Field constructors - sync allocated size before list destruction. This may help when using aligned allocation strategies. --- applications/test/CircularBuffer/Make/files | 2 +- ...rcularBuffer.C => Test-CircularBuffer.cxx} | 0 applications/test/List/Test-List.cxx | 38 +++++++ .../containers/Bits/PackedList/PackedList.H | 5 + .../containers/Bits/PackedList/PackedListI.H | 21 +++- .../containers/Buffers/CircularBuffer.C | 9 +- .../containers/Buffers/CircularBufferI.H | 4 +- .../Lists/DynamicList/DynamicList.H | 12 ++- .../Lists/DynamicList/DynamicListI.H | 27 ++++- .../Lists/DynamicList/DynamicListIO.C | 2 +- src/OpenFOAM/containers/Lists/List/List.C | 4 +- src/OpenFOAM/containers/Lists/List/ListIO.C | 2 +- .../Lists/ListOps/ListOpsTemplates.C | 8 +- .../containers/Lists/policy/ListPolicy.H | 57 +++++++++++ .../PtrLists/PtrDynList/PtrDynList.H | 11 ++- .../PtrLists/PtrDynList/PtrDynListI.H | 32 ++++-- .../db/IOstreams/memory/memoryStreamBuffer.H | 2 + .../fields/Fields/DynamicField/DynamicField.H | 23 ++++- .../Fields/DynamicField/DynamicFieldI.H | 98 ++++++++++++++++++- .../surfaceFeatures/surfaceFeatures.C | 17 ++-- 20 files changed, 334 insertions(+), 40 deletions(-) rename applications/test/CircularBuffer/{Test-CircularBuffer.C => Test-CircularBuffer.cxx} (100%) diff --git a/applications/test/CircularBuffer/Make/files b/applications/test/CircularBuffer/Make/files index 72687d6670..c0cbca5498 100644 --- a/applications/test/CircularBuffer/Make/files +++ b/applications/test/CircularBuffer/Make/files @@ -1,3 +1,3 @@ -Test-CircularBuffer.C +Test-CircularBuffer.cxx EXE = $(FOAM_USER_APPBIN)/Test-CircularBuffer diff --git a/applications/test/CircularBuffer/Test-CircularBuffer.C b/applications/test/CircularBuffer/Test-CircularBuffer.cxx similarity index 100% rename from applications/test/CircularBuffer/Test-CircularBuffer.C rename to applications/test/CircularBuffer/Test-CircularBuffer.cxx diff --git a/applications/test/List/Test-List.cxx b/applications/test/List/Test-List.cxx index 9b747a5395..96571f642c 100644 --- a/applications/test/List/Test-List.cxx +++ b/applications/test/List/Test-List.cxx @@ -140,8 +140,46 @@ int main(int argc, char *argv[]) argList::addBoolOption("ListList", "Test list of list functionality"); argList::addBoolOption("flag"); + argList::addBoolOption("reserve", "Test ListPolicy for reserve_size"); + #include "setRootCase.H" + if (args.found("reserve")) + { + using namespace Foam::ListPolicy; + + using control = std::pair; + + for + ( + const auto& tup : + { + control{ 10, 5 }, + control{ 20, 25 } + } + ) + { + const auto [len, capacity] = tup; + + Info<< "test " << tup << nl; + + auto size = reserve_size<16,2>(len, capacity); + Info<< " => " << size << " (ratio 2)" << nl; + + size = reserve_size<16,3,2>(len, capacity); + Info<< " => " << size << " (ratio 3/2)" << nl; + + size = reserve_size<16,13,8>(len, capacity); + Info<< " => " << size << " (ratio " << (13.0/8) << ')' << nl; + + size = reserve_size<16,25,16>(len, capacity); + Info<< " => " << size << " (ratio " << (25.0/16) << ')' << nl; + } + + Info<< nl << "\nEnd" << endl; + return 0; + } + { List