Collection Enhancements
Collection Helpers are set of extension methods and classes aimed to improve System.Linq.Enumerable and .NET Collection types:
Read-only mapped view
Transformation of collection types can be done in lazy mode when item is converting on-demand without touching entire collection. Lazy converted collection is called mapped view.
There are several mapped views for different collection types:
- Read-only list view for lists and arrays
- Read-only collection view for generic collections without indexer support
- Read-only dictionary view for generic dictionaries
The following example demonstrates how to obtain read-only mapped view for the list:
using DotNext.Collections.Generic;
using System.Collections.Generic;
var list = new List<string>() { "1", "2", "3" };
var mappedList = list.Convert(int.Parse);
var first = mappedList[0]; //first == 1
ToArray
Extension method ToArray allows to convert arbitrary collection into array. Collection should implements ICollection<T>
or IReadOnlyCollection<T>
interface.
using System.Collections.Generic;
using DotNext.Collections.Generic;
ICollection<string> collection = new string[] { "a", "b", "c" };
var array = collection.ToArray(); //array = new [] {"a", "b", "c" }
Singleton list and set
The library provides optimized version of the list with the single element in it.
using DotNext.Collections.Generic;
IReadOnlyList<string> list = List.Singleton("a");
IReadOnlySet<string> set = Set.Singleton("a");
Collection items concatenation
Extension method ToString from class Collection allows to convert collection items into single plain string. Each item is separated by the specified delimiter.
using DotNext.Collections.Generic;
var array = new int[] {1, 2, 3};
var str = array.ToString(","); //str is 1,2,3
Iteration
Iteration in functional style is possible using extension method ForEach which is applicable to any type implementing interface IEnumerable<T>
.
using System;
using DotNext.Collections.Generic;
IEnumerable<string> list = new[] { "a", "b", "c" };
list.ForEach(item => Console.WriteLine(item));
List segments
Generic list from .NET standard library doesn't support range operator. Only one-dimensional arrays support ranges. To bridge this gap, .NEXT library contains ListSegment data type which has the same meaning as ArraySegment but for lists. This type allows to delimit section of a list. The section can be produced using Range syntax in C#:
using DotNext.Collections.Generic;
using System.Collections.Generic;
var list = new List<string> { "One", "Two", "Three" };
ListSegment<string> slice = list.Slice(1..);
slice[0] = string.Empty;
The delimited section allows to modify individual elements but doesn't support adding or removing elements.
Copy
Any collection implementing IEnumerable<T> interface can be copied to contiguous block of the memory rented from the pool.
using DotNext.Buffers;
using static DotNext.Collections.Generic.Sequence;
static IEnumerable<int> GetItems()
{
yield return 0;
yield return 1;
}
using MemoryOwner<int> copy = GetItems().Copy();
Now the elements can be accessed using the indexer of MemoryOwner<T> data type. The memory of the copied elements is rented from the pool. You can override memory allocation logic and pass custom MemoryAllocator<T> to Copy
method.
Generic range
Enumerable.Range from .NET library supports only arguments of type int. Set.Range method from .NEXT allows to construct ordered set from a range of numbers:
using DotNext.Collections.Generic;
IReadOnlySet<long> set = Set.Range<long, DisclosedEndpoint<long>, DisclosedEndpoint<long>>(0L, 3L); // [1, 2]
set = Set.Range<long, EnclosedEndpoint<long>, DisclosedEndpoint<long>>(0L, 3L); // [0, 1, 2]