SizeHelper32.cs

Gradually Resize

public class SizeHelper32
{
    private static readonly ulong[] SCurve =
    {
        1024, 1616, 2496, 3776, 5616, 8224, 11873, 16935, 23891, 33376, 46193, 63396, 86336, 116720, 156736, 209152, 277441, 365985, 480262, 627107, 815024, 1054544, 1358704,
        1743552, 2228834, 2838768, 3602962, 4557568, 5746578, 7223456, 9053008, 11313632, 14099984, 17526117, 21729223, 26874016, 33157888, 40817024, 50133527, 61443840, 75148560,
        91723952, 111735408, 135853168, 164870640, 199725776, 241525904, 291576640, 351415424, 422850471, 508005888, 609373904, 729875314, 872929328, 1042534194, 1243360240,
        1480857072, 1761377058, 2092317379, 2482283348, 4964566704, 9929133408, 19858266816, 39716533632, 79433067264, 158866134528, 317732269056, 635464538112, 1270929076224,
        2541858152448, 5083716304896, 10167432609792, 20334865219584, 40669730439168, 81339460878336, 162678921756672, 325357843513344, 650715687026688, 1301431374053376,
        2602862748106752, 5205725496213504, 10411450992427008, 20822901984854016, 41645803969708032, 83291607939416064, 166583215878832128, 333166431757664256, 666332863515328512,
        1332665727030657024, 2665331454061314048, 5330662908122628096, 10661325816245256192, 2875907558780960768, 5751815117561921536, 11503630235123843072, 4560516396538134528,
        9121032793076269056, 18242065586152538112, 18037387098595524608, 17628030123481497600, 16809316173253443584, 15171888272797335552, 11897032471885119488,
        5347320870060687360, 10694641740121374720, 2942539406533197824, 5885078813066395648, 11770157626132791296, 5093571178556030976, 10187142357112061952, 1927540640514572288,
        3855081281029144576, 7710162562058289152, 15420325124116578304, 12393906174523604992, 6341068275337658368, 12682136550675316736, 6917529027641081856, 13835058055282163712,
        9223372036854775808, 18446744073709551615
    };
    private readonly ulong limit = int.MaxValue;
    public ulong GetNewSize(ulong currentSize)
    {
        foreach (var v in SCurve)
        {
            if (v >= limit)
                return limit;
            if (v > currentSize)
                return v;
        }
        return limit;
    }
}

SizeHelper.cs

Non-Linear Array Size Adjustment Class

using System;
using System.Collections.Generic;
using Microsoft.VisualBasic.Devices;
public class SizeHelper<T>
{
    private static readonly FixedIntXPrimality bp = new FixedIntXPrimality(64);
    private readonly        int                AllocatedSizeLimit;
    public                  int[]              Curve;
    private                 int                Resizes;
    public SizeHelper()
    {
        var measure   = new MeasureSize<T>();
        var sizeOfOne = measure.GetByteSize();
        var am        = new ComputerInfo().AvailablePhysicalMemory;
        AllocatedSizeLimit = (int) ((long) am / sizeOfOne);
    }
    public int GetNewSize(int currentSize)
    {
        Resizes++;
        if (Curve == null)
            BuildCurve(currentSize);
        foreach (var v in Curve)
            if (v > currentSize)
                return v;
        var nv = GetNextValue(currentSize);
        return nv != -1 ? nv : int.MaxValue;
    }
    private int GetNextValue(int currentValue)
    {
        for (var value = currentValue | 1; value < AllocatedSizeLimit; value += 16384)
        {
            if (value < 0)
                break;
            if (bp.IsPrime(value))
                return value + 1;
        }
        return -1;
    }
    private void BuildCurve(int Size)
    {
        int Sizer(int oldSize)
        {
            try
            {
                oldSize = (int) (uint) oldSize;
                var log     = Math.Log(oldSize);
                var inv     = 1.0 / log * 4;
                var newSize = oldSize   * (1.0 + inv);
                return (int) (uint) newSize;
            }
            catch
            {
                return AllocatedSizeLimit;
            }
        }
        var curlst = new List<int>();
        var value  = Size | 1;
        do
        {
            value = Sizer(value);
            if (value < 0)
                break;
            if (value < AllocatedSizeLimit)
                curlst.Add(value);
        } while (value < AllocatedSizeLimit);
        Curve = curlst.ToArray();
        var dl   = new List<int>();
        var last = 0;
        for (var i = 0; i < Curve.Length; ++i)
        {
            if (i > 0)
                last = Curve[i - 1];
            var v = Curve[i];
            dl.Add(v - last);
        }
        var str = "";
        foreach (var v in dl)
            str += $"{v},";
    }
}