Stroika Library 3.0d16
 
Loading...
Searching...
No Matches
ReserveTweaks.h
1/*
2 * Copyright(c) Sophist Solutions, Inc. 1990-2025. All rights reserved
3 */
4#ifndef _Stroika_Foundation_Containers_Support_ReserveTweaks_h_
5#define _Stroika_Foundation_Containers_Support_ReserveTweaks_h_ 1
6
7#include "Stroika/Foundation/StroikaPreComp.h"
8
9#include <cstddef>
10#include <optional>
11
12namespace Stroika::Foundation::Containers::Support::ReserveTweaks {
13
14 using namespace std;
15
16 constexpr size_t kDefaultMinChunkSize = 16;
17
18 /**
19 * \brief Compute the best 'capacity' to use for the given targetSize of the container.
20 *
21 * Adjust the given target (required) capacity to grow in geometric rate (so ln N reallocs), and use the argument chunkSize to
22 * make sure the number of entries is a multiple of that chunk size.
23 *
24 * \post capacityResult >= targetSize;
25 *
26 * \note the targetSize, minChunk and returned capacity are in units of elements, not bytes. eltSizeInBytes is measured in bytes.
27 */
28 constexpr size_t GetScaledUpCapacity (size_t targetSize, size_t eltSizeInBytes = 1, size_t minChunk = kDefaultMinChunkSize);
29
30 /**
31 * \brief Comupute the best 'capacity' (using GetScaledUpCapacity) to use for the given container adding N elements to its given size .
32 *
33 * @see GetScaledUpCapacity
34 */
35 template <typename CONTAINER>
36 constexpr optional<size_t> GetScaledUpCapacity4AddN (const CONTAINER& c, size_t addN, size_t minChunk = kDefaultMinChunkSize);
37
38 /**
39 * \brief use @see GetScaledUpCapacity () to automatically optimally adjust the capacity (reserve) on the given container before adding N elements to it.
40 *
41 * For the given container (which supports capacity/reserve/size APIs) - figure how much to adjust the capacity
42 * for the given container (using @see ScaleUpCapacity) and call reseve() to perform that adjustment.
43 */
44 template <typename CONTAINER>
45 void Reserve4AddN (CONTAINER& c, size_t n, size_t minChunk = kDefaultMinChunkSize);
46
47 /**
48 * \brief use @see GetScaledUpCapacity () to automatically optimally adjust the capacity (reserve) on the given container before adding one element to it.
49 *
50 * For the given container (which supports capacity/reserve/size APIs) - figure how much to adjust the capacity
51 * for the given container (using @see ScaleUpCapacity) and call reseve() to perform that adjustment.
52 */
53 template <typename CONTAINER>
54 void Reserve4Add1 (CONTAINER& c, size_t minChunk = kDefaultMinChunkSize);
55
56}
57
58/*
59 ********************************************************************************
60 ***************************** Implementation Details ***************************
61 ********************************************************************************
62 */
63#include "ReserveTweaks.inl"
64
65#endif /*_Stroika_Foundation_Containers_Support_ReserveTweaks_h_*/
STL namespace.