IT.Collections.Factory 2.0.2

dotnet add package IT.Collections.Factory --version 2.0.2                
NuGet\Install-Package IT.Collections.Factory -Version 2.0.2                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="IT.Collections.Factory" Version="2.0.2" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add IT.Collections.Factory --version 2.0.2                
#r "nuget: IT.Collections.Factory, 2.0.2"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install IT.Collections.Factory as a Cake Addin
#addin nuget:?package=IT.Collections.Factory&version=2.0.2

// Install IT.Collections.Factory as a Cake Tool
#tool nuget:?package=IT.Collections.Factory&version=2.0.2                

IT.Collections.Factory

NuGet version (IT.Collections.Factory) NuGet pre version (IT.Collections.Factory)

Implementation of collections factory

Register factories

var registry = new EnumerableFactoryRegistry();
//var registry = new ConcurrentEnumerableFactoryRegistry();
registry.RegisterFactoriesDefault();

New empty list

var listFactory = registry.GetFactory<ListFactory>();
var list = listFactory.Empty<int>();
Assert.That(list.Capacity, Is.EqualTo(0));

New array and new list with capacity

var arrayFactory = registry.GetFactory<ArrayFactory>();
Assert.That(arrayFactory.Kind.IsFixed(), Is.True);
Assert.That(listFactory.Kind.IsFixed(), Is.False);

var array = arrayFactory.New<int>(3);
Assert.That(array.Length, Is.EqualTo(3));

list = listFactory.New<int>(4);
Assert.That(list.Capacity, Is.EqualTo(4));
Assert.That(list.Count, Is.EqualTo(0));

New LinkedList with IgnoreCapacity

var linkedListFactory = registry.GetFactory<LinkedListFactory>();

Assert.That(linkedListFactory.Kind.IsIgnoreCapacity(), Is.True);

var linkedList = linkedListFactory.New<int>(-1, tryAdd =>
{
    tryAdd(1);
    tryAdd(2);
    tryAdd(3);
});

Assert.That(linkedList.SequenceEqual(new[] { 1, 2, 3 }), Is.True);

New IReadOnlySet with comparer

#if NET6_0_OR_GREATER
    var roSetFactory = registry.GetFactory<IReadOnlySetFactory>();
    Assert.That(roSetFactory.Kind.IsUnique(), Is.True);
    Assert.That(roSetFactory.Kind.IsEquatable(), Is.True);

    var roSet = roSetFactory.New(2, tryAdd =>
    {
        Assert.That(tryAdd("Test"), Is.True);
        Assert.That(tryAdd("tEsT"), Is.False);
    }, StringComparer.OrdinalIgnoreCase.ToComparers());
    Assert.That(roSet.Count, Is.EqualTo(1));
#endif

New Stack from generic factory

var intStackFactory = registry.GetFactory<Stack<int>, int>();

Assert.That(intStackFactory.Kind.IsProxy(), Is.True);
Assert.That(intStackFactory.Kind.IsReverse(), Is.True);

var stack = intStackFactory.New(3, tryAdd =>
{
    tryAdd(3);
    tryAdd(2);
    tryAdd(1);
});

Assert.That(stack.SequenceEqual(new[] { 1, 2, 3 }), Is.True);

New collections with builder

IEnumerable<int> data = Enumerable.Range(5, 10);

//EnumerableKind: None
CheckFactory(data, registry.GetFactory<ListFactory>());

//EnumerableKind: IgnoreCapacity
CheckFactory(data, registry.GetFactory<LinkedListFactory>());

//EnumerableKind: Reverse
CheckFactory(data, registry.GetFactory<StackFactory>());

//EnumerableKind: IgnoreCapacity, Reverse, ThreadSafe
CheckFactory(data, registry.GetFactory<ConcurrentBagFactory>());

static void CheckFactory<T>(IEnumerable<T> data, IEnumerableFactory factory)
{
    IEnumerable<T> newEnumerable;

    var kind = factory.Kind;

    if (kind.IsThreadSafe())
    {
        var capacity = -1;

        if (!kind.IsIgnoreCapacity())
        {
            //allocation, need use ArrayPool
            var dataArray = data.ToArray();

            capacity = dataArray.Length;

            data = dataArray;
        }

        newEnumerable = factory.New<T, IEnumerable<T>>(capacity, BuildParallel, in data);

        Assert.That(newEnumerable.OrderBy(x => x).SequenceEqual(data), Is.True);
    }

    if (kind.IsIgnoreCapacity() && !kind.IsReverse())
    {
        newEnumerable = factory.New(-1, static (TryAdd<T> tryAdd, in IEnumerable<T> data) =>
        {
            foreach (var i in data) tryAdd(i);
        }, in data);
    }
    else
    {
        //allocation, need use ArrayPool
        var dataArray = data.ToArray();

        newEnumerable = factory.New<T, T[]>(dataArray.Length, 
            kind.IsReverse() ? BuildReverse : Build, in dataArray);
    }

    Assert.That(newEnumerable.SequenceEqual(data), Is.True);
}

static void Build<T>(TryAdd<T> tryAdd, in T[] data)
{
    for (int i = 0; i < data.Length; i++) tryAdd(data[i]);
}

static void BuildReverse<T>(TryAdd<T> tryAdd, in T[] data)
{
    for (int i = data.Length - 1; i >= 0; i--) tryAdd(data[i]);
}

static void BuildParallel<T>(TryAdd<T> tryAdd, in IEnumerable<T> data)
{
    Parallel.ForEach(data, i => tryAdd(i));
}
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 is compatible. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 is compatible. 
.NET Framework net461 is compatible.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 is compatible.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on IT.Collections.Factory:

Package Downloads
IT.Collections.Equatable.Factory

Implementation of collections factory for equatable

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
2.0.2 135 6/4/2024
2.0.1 816 10/23/2023
2.0.0 754 10/22/2023
2.0.0-pre4 110 10/20/2023
2.0.0-pre3 110 10/17/2023