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.
This commit is contained in:
Mark Olesen
2025-04-01 10:56:23 +02:00
parent 7f062a8f5e
commit b8a0706e72
20 changed files with 334 additions and 40 deletions

View File

@ -1,3 +1,3 @@
Test-CircularBuffer.C
Test-CircularBuffer.cxx
EXE = $(FOAM_USER_APPBIN)/Test-CircularBuffer

View File

@ -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<label, label>;
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<label> ident(15);
Foam::identity(ident, 0);