RecyclableBlockList renamed to RecyclableArrayList for clarity. First set of optimizations and implementation.
This commit is contained in:
1 parent
8147c4ecbf
commit
880abd605e
16 files changed
+1040
-800
No files matched your search
@@ -84,7 +84,7 @@ namespace Recyclable.Collections.Benchmarks
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
[Benchmark(Baseline = true)]
|
//[Benchmark(Baseline = true)]
|
||||||
public void Comparer()
|
public void Comparer()
|
||||||
{
|
{
|
||||||
TestWithComparer(_comparer);
|
TestWithComparer(_comparer);
|
||||||
@@ -99,7 +99,7 @@ namespace Recyclable.Collections.Benchmarks
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
[Benchmark]
|
//[Benchmark]
|
||||||
public void Func()
|
public void Func()
|
||||||
{
|
{
|
||||||
TestWithFunc((x, y) => (x - y) switch
|
TestWithFunc((x, y) => (x - y) switch
|
||||||
@@ -110,7 +110,7 @@ namespace Recyclable.Collections.Benchmarks
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
[Benchmark]
|
//[Benchmark]
|
||||||
public void StaticFunc()
|
public void StaticFunc()
|
||||||
{
|
{
|
||||||
TestWithFunc(static (x, y) => (x - y) switch
|
TestWithFunc(static (x, y) => (x - y) switch
|
||||||
@@ -130,7 +130,7 @@ namespace Recyclable.Collections.Benchmarks
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
[Benchmark]
|
//[Benchmark]
|
||||||
public void Delegate()
|
public void Delegate()
|
||||||
{
|
{
|
||||||
TestWithDelegate<long>((x, y) => (x - y) switch
|
TestWithDelegate<long>((x, y) => (x - y) switch
|
||||||
@@ -141,7 +141,7 @@ namespace Recyclable.Collections.Benchmarks
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
[Benchmark]
|
//[Benchmark]
|
||||||
public void ListOfFuncs()
|
public void ListOfFuncs()
|
||||||
{
|
{
|
||||||
const long x = 2, y = 5;
|
const long x = 2, y = 5;
|
||||||
@@ -153,7 +153,7 @@ namespace Recyclable.Collections.Benchmarks
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
[Benchmark]
|
//[Benchmark]
|
||||||
public void ListOfLocalFuncs()
|
public void ListOfLocalFuncs()
|
||||||
{
|
{
|
||||||
const long x = 2, y = 5;
|
const long x = 2, y = 5;
|
||||||
@@ -165,7 +165,7 @@ namespace Recyclable.Collections.Benchmarks
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
[Benchmark]
|
//[Benchmark]
|
||||||
public void ListOfComparers()
|
public void ListOfComparers()
|
||||||
{
|
{
|
||||||
const long x = 2, y = 5;
|
const long x = 2, y = 5;
|
||||||
@@ -177,7 +177,7 @@ namespace Recyclable.Collections.Benchmarks
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
[Benchmark]
|
//[Benchmark]
|
||||||
public void ListOfClasses()
|
public void ListOfClasses()
|
||||||
{
|
{
|
||||||
const long x = 2, y = 5;
|
const long x = 2, y = 5;
|
||||||
@@ -189,7 +189,7 @@ namespace Recyclable.Collections.Benchmarks
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
[Benchmark]
|
//[Benchmark]
|
||||||
public void ListOfStructs()
|
public void ListOfStructs()
|
||||||
{
|
{
|
||||||
const long x = 2, y = 5;
|
const long x = 2, y = 5;
|
||||||
@@ -201,7 +201,7 @@ namespace Recyclable.Collections.Benchmarks
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
[Benchmark]
|
//[Benchmark]
|
||||||
public void ListOfBoxedStructs()
|
public void ListOfBoxedStructs()
|
||||||
{
|
{
|
||||||
const long x = 2, y = 5;
|
const long x = 2, y = 5;
|
||||||
@@ -213,7 +213,7 @@ namespace Recyclable.Collections.Benchmarks
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
[Benchmark]
|
//[Benchmark]
|
||||||
public void ListOfUnboxedStructs()
|
public void ListOfUnboxedStructs()
|
||||||
{
|
{
|
||||||
const long x = 2, y = 5;
|
const long x = 2, y = 5;
|
||||||
@@ -225,7 +225,7 @@ namespace Recyclable.Collections.Benchmarks
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
[Benchmark]
|
//[Benchmark]
|
||||||
public void ListOfUnboxedReadOnlyStructs()
|
public void ListOfUnboxedReadOnlyStructs()
|
||||||
{
|
{
|
||||||
const long x = 2, y = 5;
|
const long x = 2, y = 5;
|
||||||
@@ -237,7 +237,7 @@ namespace Recyclable.Collections.Benchmarks
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
[Benchmark]
|
//[Benchmark]
|
||||||
public void ListOfDelegates()
|
public void ListOfDelegates()
|
||||||
{
|
{
|
||||||
const long x = 2, y = 5;
|
const long x = 2, y = 5;
|
||||||
|
|||||||
@@ -1,279 +0,0 @@
|
|||||||
using BenchmarkDotNet.Attributes;
|
|
||||||
|
|
||||||
namespace Recyclable.Collections.Benchmarks
|
|
||||||
{
|
|
||||||
[MemoryDiagnoser]
|
|
||||||
public class ListVsLinkedListBenchmark
|
|
||||||
{
|
|
||||||
//[Params(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 128, 256, 512, 1024, 2048, 4096, 8192, 1_000_000)]
|
|
||||||
public int TestObjectCount = 1_000_000;
|
|
||||||
public int BlockSize => TestObjectCount switch
|
|
||||||
{
|
|
||||||
0 => 1,
|
|
||||||
> 0 and <= 10 => TestObjectCount,
|
|
||||||
_ => TestObjectCount / 10
|
|
||||||
};
|
|
||||||
|
|
||||||
private object[]? _testObjects;
|
|
||||||
|
|
||||||
public ListVsLinkedListBenchmark()
|
|
||||||
{
|
|
||||||
_ = TestObjects;
|
|
||||||
}
|
|
||||||
|
|
||||||
private object[] TestObjects => _testObjects ??= Enumerable.Range(1, TestObjectCount).Select(x => new object()).ToArray();
|
|
||||||
|
|
||||||
private static void DoNothing<T>(T obj)
|
|
||||||
{
|
|
||||||
_ = obj;
|
|
||||||
}
|
|
||||||
|
|
||||||
[Benchmark]
|
|
||||||
public void Array()
|
|
||||||
{
|
|
||||||
var data = TestObjects;
|
|
||||||
var dataCount = TestObjectCount;
|
|
||||||
var list = new object[dataCount];
|
|
||||||
for (var i = 0; i < dataCount; i++)
|
|
||||||
{
|
|
||||||
list[i] = data[i];
|
|
||||||
}
|
|
||||||
|
|
||||||
for (var i = 0; i < list.LongLength; i++)
|
|
||||||
{
|
|
||||||
DoNothing(list[i]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
[Benchmark]
|
|
||||||
public void RecyclableBlockList()
|
|
||||||
{
|
|
||||||
var data = TestObjects;
|
|
||||||
var dataCount = TestObjectCount;
|
|
||||||
using var list = new RecyclableBlockList<object>(initialCapacity: dataCount);
|
|
||||||
for (var i = 0; i < dataCount; i++)
|
|
||||||
{
|
|
||||||
list.Add(data[i]);
|
|
||||||
}
|
|
||||||
|
|
||||||
for (var i = 0; i < list.LongCount; i++)
|
|
||||||
{
|
|
||||||
DoNothing(list[i]);
|
|
||||||
}
|
|
||||||
|
|
||||||
list.Clear();
|
|
||||||
}
|
|
||||||
|
|
||||||
[Benchmark(Baseline = true)]
|
|
||||||
public void RecyclableList()
|
|
||||||
{
|
|
||||||
var data = TestObjects;
|
|
||||||
var dataCount = TestObjectCount;
|
|
||||||
using var list = new RecyclableList<object>(BlockSize, initialCapacity: dataCount);
|
|
||||||
for (var i = 0; i < dataCount; i++)
|
|
||||||
{
|
|
||||||
list.Add(data[i]);
|
|
||||||
}
|
|
||||||
|
|
||||||
for (var i = 0; i < list.LongCount; i++)
|
|
||||||
{
|
|
||||||
DoNothing(list[i]);
|
|
||||||
}
|
|
||||||
|
|
||||||
list.Clear();
|
|
||||||
}
|
|
||||||
|
|
||||||
[Benchmark]
|
|
||||||
public void List()
|
|
||||||
{
|
|
||||||
var data = TestObjects;
|
|
||||||
var dataCount = TestObjectCount;
|
|
||||||
var list = new List<object>(capacity: dataCount);
|
|
||||||
for (var i = 0; i < dataCount; i++)
|
|
||||||
{
|
|
||||||
list.Add(data[i]);
|
|
||||||
}
|
|
||||||
|
|
||||||
for (var i = 0; i < list.Count; i++)
|
|
||||||
{
|
|
||||||
DoNothing(list[i]);
|
|
||||||
}
|
|
||||||
|
|
||||||
list.Clear();
|
|
||||||
}
|
|
||||||
|
|
||||||
//[Benchmark]
|
|
||||||
public void LinkedList()
|
|
||||||
{
|
|
||||||
var data = TestObjects;
|
|
||||||
var dataCount = TestObjectCount;
|
|
||||||
var list = new LinkedList<object>();
|
|
||||||
for (var i = 0; i < dataCount; i++)
|
|
||||||
{
|
|
||||||
_ = list.AddLast(data[i]);
|
|
||||||
}
|
|
||||||
|
|
||||||
foreach (var node in list)
|
|
||||||
{
|
|
||||||
DoNothing(node);
|
|
||||||
}
|
|
||||||
|
|
||||||
list.Clear();
|
|
||||||
}
|
|
||||||
|
|
||||||
//[Benchmark]
|
|
||||||
public void RecyclableQueue()
|
|
||||||
{
|
|
||||||
var data = TestObjects;
|
|
||||||
var dataCount = TestObjectCount;
|
|
||||||
var list = new RecyclableQueue<object>(BlockSize);
|
|
||||||
for (var i = 0; i < dataCount; i++)
|
|
||||||
{
|
|
||||||
list.Enqueue(data[i]);
|
|
||||||
}
|
|
||||||
|
|
||||||
while (list.LongCount > 0)
|
|
||||||
{
|
|
||||||
DoNothing(list.Dequeue());
|
|
||||||
}
|
|
||||||
|
|
||||||
list.Clear();
|
|
||||||
}
|
|
||||||
|
|
||||||
//[Benchmark]
|
|
||||||
public void Queue()
|
|
||||||
{
|
|
||||||
var data = TestObjects;
|
|
||||||
var dataCount = TestObjectCount;
|
|
||||||
var list = new Queue<object>(capacity: dataCount);
|
|
||||||
for (var i = 0; i < dataCount; i++)
|
|
||||||
{
|
|
||||||
list.Enqueue(data[i]);
|
|
||||||
}
|
|
||||||
|
|
||||||
while (list.Count > 0)
|
|
||||||
{
|
|
||||||
DoNothing(list.Dequeue());
|
|
||||||
}
|
|
||||||
|
|
||||||
list.Clear();
|
|
||||||
}
|
|
||||||
|
|
||||||
//[Benchmark]
|
|
||||||
public void PriorityQueue()
|
|
||||||
{
|
|
||||||
var data = TestObjects;
|
|
||||||
var dataCount = TestObjectCount;
|
|
||||||
var list = new PriorityQueue<object, long>(initialCapacity: dataCount);
|
|
||||||
for (var i = 0; i < dataCount; i++)
|
|
||||||
{
|
|
||||||
list.Enqueue(data[i], i);
|
|
||||||
}
|
|
||||||
|
|
||||||
while (list.Count > 0)
|
|
||||||
{
|
|
||||||
DoNothing(list.Dequeue());
|
|
||||||
}
|
|
||||||
|
|
||||||
list.Clear();
|
|
||||||
}
|
|
||||||
|
|
||||||
//[Benchmark]
|
|
||||||
public void RecyclableSortedList_InUpdateMode()
|
|
||||||
{
|
|
||||||
var data = TestObjects;
|
|
||||||
var dataCount = TestObjectCount;
|
|
||||||
var list = new RecyclableSortedList<long, object>(BlockSize);
|
|
||||||
list.BeginUpdate();
|
|
||||||
for (var i = 0; i < dataCount; i++)
|
|
||||||
{
|
|
||||||
list.Add(i, data[i]);
|
|
||||||
}
|
|
||||||
|
|
||||||
list.EndUpdate();
|
|
||||||
|
|
||||||
for (var i = 0; i < list.LongCount; i++)
|
|
||||||
{
|
|
||||||
DoNothing(list[i]);
|
|
||||||
}
|
|
||||||
|
|
||||||
list.Clear();
|
|
||||||
}
|
|
||||||
|
|
||||||
//[Benchmark]
|
|
||||||
public void RecyclableSortedList()
|
|
||||||
{
|
|
||||||
var data = TestObjects;
|
|
||||||
var dataCount = TestObjectCount;
|
|
||||||
var list = new RecyclableSortedList<long, object>(BlockSize);
|
|
||||||
for (var i = 0; i < dataCount; i++)
|
|
||||||
{
|
|
||||||
list.Add(i, data[i]);
|
|
||||||
}
|
|
||||||
|
|
||||||
for (var i = 0; i < list.LongCount; i++)
|
|
||||||
{
|
|
||||||
DoNothing(list[i]);
|
|
||||||
}
|
|
||||||
|
|
||||||
list.Clear();
|
|
||||||
}
|
|
||||||
|
|
||||||
//[Benchmark]
|
|
||||||
public void SortedList()
|
|
||||||
{
|
|
||||||
var data = TestObjects;
|
|
||||||
var dataCount = TestObjectCount;
|
|
||||||
var list = new SortedList<long, object>();
|
|
||||||
for (var i = 0; i < dataCount; i++)
|
|
||||||
{
|
|
||||||
list.Add(i, data[i]);
|
|
||||||
}
|
|
||||||
|
|
||||||
for (var i = 0; i < list.Count; i++)
|
|
||||||
{
|
|
||||||
DoNothing(list[i]);
|
|
||||||
}
|
|
||||||
|
|
||||||
list.Clear();
|
|
||||||
}
|
|
||||||
|
|
||||||
//[Benchmark]
|
|
||||||
public void RecyclableStack()
|
|
||||||
{
|
|
||||||
var data = TestObjects;
|
|
||||||
var dataCount = TestObjectCount;
|
|
||||||
var list = new RecyclableStack<object>(BlockSize);
|
|
||||||
for (var i = 0; i < dataCount; i++)
|
|
||||||
{
|
|
||||||
list.Push(data[i]);
|
|
||||||
}
|
|
||||||
|
|
||||||
while (list.LongCount > 0)
|
|
||||||
{
|
|
||||||
DoNothing(list.Pop());
|
|
||||||
}
|
|
||||||
|
|
||||||
list.Clear();
|
|
||||||
}
|
|
||||||
|
|
||||||
//[Benchmark]
|
|
||||||
public void Stack()
|
|
||||||
{
|
|
||||||
var data = TestObjects;
|
|
||||||
var dataCount = TestObjectCount;
|
|
||||||
var list = new Stack<object>();
|
|
||||||
for (var i = 0; i < dataCount; i++)
|
|
||||||
{
|
|
||||||
list.Push(data[i]);
|
|
||||||
}
|
|
||||||
|
|
||||||
while (list.Count > 0)
|
|
||||||
{
|
|
||||||
DoNothing(list.Pop());
|
|
||||||
}
|
|
||||||
|
|
||||||
list.Clear();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,262 @@
|
|||||||
|
namespace Recyclable.Collections.Benchmarks
|
||||||
|
{
|
||||||
|
public class BenchmarkBase
|
||||||
|
{
|
||||||
|
//[Params(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 128, 256, 512, 1024, 2048, 4096, 8192, 1_000_000)]
|
||||||
|
public const int TestObjectCount = 1_000_000;
|
||||||
|
public static int BlockSize = TestObjectCount switch
|
||||||
|
{
|
||||||
|
0 => 1,
|
||||||
|
> 0 and <= 10 => TestObjectCount,
|
||||||
|
_ => TestObjectCount / 10
|
||||||
|
};
|
||||||
|
|
||||||
|
protected object[]? _testObjects;
|
||||||
|
protected object[] TestObjects => _testObjects ??= Enumerable.Range(1, TestObjectCount).Select(x => new object()).ToArray();
|
||||||
|
|
||||||
|
protected static void DoNothing<T>(T obj)
|
||||||
|
{
|
||||||
|
_ = obj;
|
||||||
|
}
|
||||||
|
|
||||||
|
public BenchmarkBase()
|
||||||
|
{
|
||||||
|
_ = TestObjects;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//[MemoryDiagnoser]
|
||||||
|
//public class ListVsLinkedListBenchmarks
|
||||||
|
//{
|
||||||
|
// [Benchmark]
|
||||||
|
// public void RecyclableBlockList()
|
||||||
|
// {
|
||||||
|
// var data = TestObjects;
|
||||||
|
// var dataCount = TestObjectCount;
|
||||||
|
// using var list = new RecyclableBlockList<object>(initialCapacity: dataCount);
|
||||||
|
// for (long i = 0; i < dataCount; i++)
|
||||||
|
// {
|
||||||
|
// list.Add(data[i]);
|
||||||
|
// }
|
||||||
|
|
||||||
|
// for (long i = 0; i < list.LongCount; i++)
|
||||||
|
// {
|
||||||
|
// DoNothing(list[i]);
|
||||||
|
// }
|
||||||
|
|
||||||
|
// list.Clear();
|
||||||
|
// }
|
||||||
|
|
||||||
|
// [Benchmark(Baseline = true)]
|
||||||
|
// public void RecyclableList()
|
||||||
|
// {
|
||||||
|
// var data = TestObjects;
|
||||||
|
// long dataCount = TestObjectCount;
|
||||||
|
// using var list = new RecyclableList<object>(BlockSize, totalItemsCount: dataCount);
|
||||||
|
// for (long i = 0; i < dataCount; i++)
|
||||||
|
// {
|
||||||
|
// list.Add(data[i]);
|
||||||
|
// }
|
||||||
|
|
||||||
|
// for (long i = 0; i < list.LongCount; i++)
|
||||||
|
// {
|
||||||
|
// DoNothing(list[i]);
|
||||||
|
// }
|
||||||
|
|
||||||
|
// list.Clear();
|
||||||
|
// }
|
||||||
|
|
||||||
|
// //[Benchmark]
|
||||||
|
// public void List()
|
||||||
|
// {
|
||||||
|
// var data = TestObjects;
|
||||||
|
// var dataCount = TestObjectCount;
|
||||||
|
// var list = new List<object>(capacity: dataCount);
|
||||||
|
// for (var i = 0; i < dataCount; i++)
|
||||||
|
// {
|
||||||
|
// list.Add(data[i]);
|
||||||
|
// }
|
||||||
|
|
||||||
|
// for (var i = 0; i < list.Count; i++)
|
||||||
|
// {
|
||||||
|
// DoNothing(list[i]);
|
||||||
|
// }
|
||||||
|
|
||||||
|
// list.Clear();
|
||||||
|
// }
|
||||||
|
|
||||||
|
// //[Benchmark]
|
||||||
|
// public void LinkedList()
|
||||||
|
// {
|
||||||
|
// var data = TestObjects;
|
||||||
|
// var dataCount = TestObjectCount;
|
||||||
|
// var list = new LinkedList<object>();
|
||||||
|
// for (var i = 0; i < dataCount; i++)
|
||||||
|
// {
|
||||||
|
// _ = list.AddLast(data[i]);
|
||||||
|
// }
|
||||||
|
|
||||||
|
// foreach (var node in list)
|
||||||
|
// {
|
||||||
|
// DoNothing(node);
|
||||||
|
// }
|
||||||
|
|
||||||
|
// list.Clear();
|
||||||
|
// }
|
||||||
|
|
||||||
|
// //[Benchmark]
|
||||||
|
// public void RecyclableQueue()
|
||||||
|
// {
|
||||||
|
// var data = TestObjects;
|
||||||
|
// var dataCount = TestObjectCount;
|
||||||
|
// var list = new RecyclableQueue<object>(BlockSize);
|
||||||
|
// for (var i = 0; i < dataCount; i++)
|
||||||
|
// {
|
||||||
|
// list.Enqueue(data[i]);
|
||||||
|
// }
|
||||||
|
|
||||||
|
// while (list.LongCount > 0)
|
||||||
|
// {
|
||||||
|
// DoNothing(list.Dequeue());
|
||||||
|
// }
|
||||||
|
|
||||||
|
// list.Clear();
|
||||||
|
// }
|
||||||
|
|
||||||
|
// //[Benchmark]
|
||||||
|
// public void Queue()
|
||||||
|
// {
|
||||||
|
// var data = TestObjects;
|
||||||
|
// var dataCount = TestObjectCount;
|
||||||
|
// var list = new Queue<object>(capacity: dataCount);
|
||||||
|
// for (var i = 0; i < dataCount; i++)
|
||||||
|
// {
|
||||||
|
// list.Enqueue(data[i]);
|
||||||
|
// }
|
||||||
|
|
||||||
|
// while (list.Count > 0)
|
||||||
|
// {
|
||||||
|
// DoNothing(list.Dequeue());
|
||||||
|
// }
|
||||||
|
|
||||||
|
// list.Clear();
|
||||||
|
// }
|
||||||
|
|
||||||
|
// //[Benchmark]
|
||||||
|
// public void PriorityQueue()
|
||||||
|
// {
|
||||||
|
// var data = TestObjects;
|
||||||
|
// var dataCount = TestObjectCount;
|
||||||
|
// var list = new PriorityQueue<object, long>(initialCapacity: dataCount);
|
||||||
|
// for (var i = 0; i < dataCount; i++)
|
||||||
|
// {
|
||||||
|
// list.Enqueue(data[i], i);
|
||||||
|
// }
|
||||||
|
|
||||||
|
// while (list.Count > 0)
|
||||||
|
// {
|
||||||
|
// DoNothing(list.Dequeue());
|
||||||
|
// }
|
||||||
|
|
||||||
|
// list.Clear();
|
||||||
|
// }
|
||||||
|
|
||||||
|
// //[Benchmark]
|
||||||
|
// public void RecyclableSortedList_InUpdateMode()
|
||||||
|
// {
|
||||||
|
// var data = TestObjects;
|
||||||
|
// var dataCount = TestObjectCount;
|
||||||
|
// var list = new RecyclableSortedList<long, object>(BlockSize);
|
||||||
|
// list.BeginUpdate();
|
||||||
|
// for (var i = 0; i < dataCount; i++)
|
||||||
|
// {
|
||||||
|
// list.Add(i, data[i]);
|
||||||
|
// }
|
||||||
|
|
||||||
|
// list.EndUpdate();
|
||||||
|
|
||||||
|
// for (var i = 0; i < list.LongCount; i++)
|
||||||
|
// {
|
||||||
|
// DoNothing(list[i]);
|
||||||
|
// }
|
||||||
|
|
||||||
|
// list.Clear();
|
||||||
|
// }
|
||||||
|
|
||||||
|
// //[Benchmark]
|
||||||
|
// public void RecyclableSortedList()
|
||||||
|
// {
|
||||||
|
// var data = TestObjects;
|
||||||
|
// var dataCount = TestObjectCount;
|
||||||
|
// var list = new RecyclableSortedList<long, object>(BlockSize);
|
||||||
|
// for (var i = 0; i < dataCount; i++)
|
||||||
|
// {
|
||||||
|
// list.Add(i, data[i]);
|
||||||
|
// }
|
||||||
|
|
||||||
|
// for (var i = 0; i < list.LongCount; i++)
|
||||||
|
// {
|
||||||
|
// DoNothing(list[i]);
|
||||||
|
// }
|
||||||
|
|
||||||
|
// list.Clear();
|
||||||
|
// }
|
||||||
|
|
||||||
|
// //[Benchmark]
|
||||||
|
// public void SortedList()
|
||||||
|
// {
|
||||||
|
// var data = TestObjects;
|
||||||
|
// var dataCount = TestObjectCount;
|
||||||
|
// var list = new SortedList<long, object>();
|
||||||
|
// for (var i = 0; i < dataCount; i++)
|
||||||
|
// {
|
||||||
|
// list.Add(i, data[i]);
|
||||||
|
// }
|
||||||
|
|
||||||
|
// for (var i = 0; i < list.Count; i++)
|
||||||
|
// {
|
||||||
|
// DoNothing(list[i]);
|
||||||
|
// }
|
||||||
|
|
||||||
|
// list.Clear();
|
||||||
|
// }
|
||||||
|
|
||||||
|
// //[Benchmark]
|
||||||
|
// public void RecyclableStack()
|
||||||
|
// {
|
||||||
|
// var data = TestObjects;
|
||||||
|
// var dataCount = TestObjectCount;
|
||||||
|
// var list = new RecyclableStack<object>(BlockSize);
|
||||||
|
// for (var i = 0; i < dataCount; i++)
|
||||||
|
// {
|
||||||
|
// list.Push(data[i]);
|
||||||
|
// }
|
||||||
|
|
||||||
|
// while (list.LongCount > 0)
|
||||||
|
// {
|
||||||
|
// DoNothing(list.Pop());
|
||||||
|
// }
|
||||||
|
|
||||||
|
// list.Clear();
|
||||||
|
// }
|
||||||
|
|
||||||
|
// //[Benchmark]
|
||||||
|
// public void Stack()
|
||||||
|
// {
|
||||||
|
// var data = TestObjects;
|
||||||
|
// var dataCount = TestObjectCount;
|
||||||
|
// var list = new Stack<object>();
|
||||||
|
// for (var i = 0; i < dataCount; i++)
|
||||||
|
// {
|
||||||
|
// list.Push(data[i]);
|
||||||
|
// }
|
||||||
|
|
||||||
|
// while (list.Count > 0)
|
||||||
|
// {
|
||||||
|
// DoNothing(list.Pop());
|
||||||
|
// }
|
||||||
|
|
||||||
|
// list.Clear();
|
||||||
|
// }
|
||||||
|
//}
|
||||||
|
}
|
||||||
@@ -1,8 +1,14 @@
|
|||||||
using BenchmarkDotNet.Running;
|
using BenchmarkDotNet.Running;
|
||||||
using Recyclable.Collections.Benchmarks;
|
using Recyclable.Collections.Benchmarks;
|
||||||
|
|
||||||
BenchmarkRunner.Run<ListVsLinkedListBenchmark>();
|
//BenchmarkSwitcher.FromAssembly(typeof(Program).Assembly).Run(new[] { "-f*" });
|
||||||
|
|
||||||
|
BenchmarkRunner.Run<RecyclableCollectionsBenchmarks>();
|
||||||
|
|
||||||
|
//var test = new RecyclableCollectionsBenchmarks();
|
||||||
|
|
||||||
|
|
||||||
//BenchmarkRunner.Run<DelegateVsComparerBenchmark>();
|
//BenchmarkRunner.Run<DelegateVsComparerBenchmark>();
|
||||||
|
|
||||||
//var tests = new ListVsLinkedListBenchmark();
|
//var tests = new ListVsLinkedListBenchmark();
|
||||||
//tests.RecyclableBlockList();
|
//tests.RecyclableList();
|
||||||
@@ -0,0 +1,52 @@
|
|||||||
|
using BenchmarkDotNet.Attributes;
|
||||||
|
|
||||||
|
namespace Recyclable.Collections.Benchmarks
|
||||||
|
{
|
||||||
|
[MemoryDiagnoser]
|
||||||
|
public partial class RecyclableCollectionsBenchmarks : BenchmarkBase
|
||||||
|
{
|
||||||
|
private readonly object[] _testArray;
|
||||||
|
|
||||||
|
//[Benchmark(Baseline = true)]
|
||||||
|
public void Array_Create_WithCapacity()
|
||||||
|
{
|
||||||
|
DoNothing(new object[TestObjectCount]);
|
||||||
|
}
|
||||||
|
|
||||||
|
//[Benchmark]
|
||||||
|
public void Array_GetItem()
|
||||||
|
{
|
||||||
|
var data = _testArray;
|
||||||
|
var dataCount = TestObjectCount;
|
||||||
|
for (var i = 0; i < dataCount; i++)
|
||||||
|
{
|
||||||
|
DoNothing(data[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//[Benchmark]
|
||||||
|
public void Array_SetItem()
|
||||||
|
{
|
||||||
|
var data = _testArray;
|
||||||
|
var dataCount = TestObjectCount;
|
||||||
|
for (var i = 0; i < dataCount; i++)
|
||||||
|
{
|
||||||
|
data[i] = TestObjects[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//[Benchmark]
|
||||||
|
public void Array_Count()
|
||||||
|
{
|
||||||
|
var data = _testArray;
|
||||||
|
DoNothing(data.Length);
|
||||||
|
}
|
||||||
|
|
||||||
|
//[Benchmark]
|
||||||
|
public void Array_LongCount()
|
||||||
|
{
|
||||||
|
var data = _testArray;
|
||||||
|
DoNothing(data.LongLength);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,83 @@
|
|||||||
|
using BenchmarkDotNet.Attributes;
|
||||||
|
|
||||||
|
namespace Recyclable.Collections.Benchmarks
|
||||||
|
{
|
||||||
|
public partial class RecyclableCollectionsBenchmarks : BenchmarkBase
|
||||||
|
{
|
||||||
|
private readonly List<object> _testList;
|
||||||
|
|
||||||
|
[Benchmark(Baseline = true)]
|
||||||
|
public void List_Create()
|
||||||
|
{
|
||||||
|
var list = new List<object>();
|
||||||
|
DoNothing(list);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Benchmark]
|
||||||
|
public void List_Create_0Capacity()
|
||||||
|
{
|
||||||
|
var list = new List<object>(0);
|
||||||
|
DoNothing(list);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Benchmark]
|
||||||
|
public void List_Create_WithCapacity()
|
||||||
|
{
|
||||||
|
var list = new List<object>(TestObjectCount);
|
||||||
|
DoNothing(list);
|
||||||
|
}
|
||||||
|
|
||||||
|
//[Benchmark]
|
||||||
|
public void List_Add()
|
||||||
|
{
|
||||||
|
var data = TestObjects;
|
||||||
|
var dataCount = TestObjectCount;
|
||||||
|
var list = new List<object>();
|
||||||
|
for (var i = 0; i < dataCount; i++)
|
||||||
|
{
|
||||||
|
list.Add(data[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//[Benchmark]
|
||||||
|
public void List_Add_WithCapacity()
|
||||||
|
{
|
||||||
|
var data = TestObjects;
|
||||||
|
var dataCount = TestObjectCount;
|
||||||
|
var list = new List<object>(dataCount);
|
||||||
|
for (var i = 0; i < dataCount; i++)
|
||||||
|
{
|
||||||
|
list.Add(data[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//[Benchmark]
|
||||||
|
public void List_GetItem()
|
||||||
|
{
|
||||||
|
var data = _testList;
|
||||||
|
var dataCount = TestObjectCount;
|
||||||
|
for (var i = 0; i < dataCount; i++)
|
||||||
|
{
|
||||||
|
DoNothing(data[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//[Benchmark]
|
||||||
|
public void List_SetItem()
|
||||||
|
{
|
||||||
|
var data = _testList;
|
||||||
|
var dataCount = TestObjectCount;
|
||||||
|
for (var i = 0; i < dataCount; i++)
|
||||||
|
{
|
||||||
|
data[i] = TestObjects[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//[Benchmark]
|
||||||
|
public void List_Count()
|
||||||
|
{
|
||||||
|
var data = _testList;
|
||||||
|
DoNothing(data.Count);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
+97
@@ -0,0 +1,97 @@
|
|||||||
|
using BenchmarkDotNet.Attributes;
|
||||||
|
|
||||||
|
namespace Recyclable.Collections.Benchmarks
|
||||||
|
{
|
||||||
|
public partial class RecyclableCollectionsBenchmarks : BenchmarkBase
|
||||||
|
{
|
||||||
|
private readonly RecyclableArrayList<object> _testRecyclableArrayList;
|
||||||
|
|
||||||
|
public RecyclableCollectionsBenchmarks()
|
||||||
|
{
|
||||||
|
_testRecyclableArrayList = new(TestObjects, TestObjectCount);
|
||||||
|
_testRecyclableList = new(TestObjects, TestObjectCount);
|
||||||
|
_testArray = TestObjects.ToArray();
|
||||||
|
_testList = new(TestObjects);
|
||||||
|
}
|
||||||
|
|
||||||
|
~RecyclableCollectionsBenchmarks()
|
||||||
|
{
|
||||||
|
_testRecyclableArrayList?.Dispose();
|
||||||
|
_testRecyclableList?.Dispose();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Benchmark]
|
||||||
|
public void RecyclableArrayList_Create()
|
||||||
|
{
|
||||||
|
using var list = new RecyclableArrayList<object>();
|
||||||
|
DoNothing(list);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Benchmark]
|
||||||
|
public void RecyclableArrayList_Create_0Capacity()
|
||||||
|
{
|
||||||
|
using var list = new RecyclableArrayList<object>(0);
|
||||||
|
DoNothing(list);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Benchmark]
|
||||||
|
public void RecyclableArrayList_Create_WithCapacity()
|
||||||
|
{
|
||||||
|
using var list = new RecyclableArrayList<object>(TestObjectCount);
|
||||||
|
DoNothing(list);
|
||||||
|
}
|
||||||
|
|
||||||
|
//[Benchmark]
|
||||||
|
public void RecyclableArrayList_Add()
|
||||||
|
{
|
||||||
|
var data = TestObjects;
|
||||||
|
var dataCount = TestObjectCount;
|
||||||
|
using var list = new RecyclableArrayList<object>();
|
||||||
|
for (var i = 0; i < dataCount; i++)
|
||||||
|
{
|
||||||
|
list.Add(data[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//[Benchmark]
|
||||||
|
public void RecyclableArrayList_Add_WithCapacity()
|
||||||
|
{
|
||||||
|
var data = TestObjects;
|
||||||
|
var dataCount = TestObjectCount;
|
||||||
|
using var list = new RecyclableArrayList<object>(dataCount);
|
||||||
|
for (var i = 0; i < dataCount; i++)
|
||||||
|
{
|
||||||
|
list.Add(data[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//[Benchmark]
|
||||||
|
public void RecyclableArrayList_GetItem()
|
||||||
|
{
|
||||||
|
var data = _testRecyclableArrayList;
|
||||||
|
var dataCount = TestObjectCount;
|
||||||
|
for (var i = 0; i < dataCount; i++)
|
||||||
|
{
|
||||||
|
DoNothing(data[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//[Benchmark]
|
||||||
|
public void RecyclableArrayList_SetItem()
|
||||||
|
{
|
||||||
|
var data = _testRecyclableArrayList;
|
||||||
|
var dataCount = TestObjectCount;
|
||||||
|
for (var i = 0; i < dataCount; i++)
|
||||||
|
{
|
||||||
|
data[i] = TestObjects[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//[Benchmark]
|
||||||
|
public void RecyclableArrayList_Count()
|
||||||
|
{
|
||||||
|
var data = _testRecyclableArrayList;
|
||||||
|
DoNothing(data.Count);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,90 @@
|
|||||||
|
using BenchmarkDotNet.Attributes;
|
||||||
|
|
||||||
|
namespace Recyclable.Collections.Benchmarks
|
||||||
|
{
|
||||||
|
public partial class RecyclableCollectionsBenchmarks : BenchmarkBase
|
||||||
|
{
|
||||||
|
private readonly RecyclableList<object> _testRecyclableList;
|
||||||
|
|
||||||
|
[Benchmark]
|
||||||
|
public void RecyclableList_Create()
|
||||||
|
{
|
||||||
|
using var list = new RecyclableList<object>();
|
||||||
|
DoNothing(list);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Benchmark]
|
||||||
|
public void RecyclableList_Create_0Capacity()
|
||||||
|
{
|
||||||
|
using var list = new RecyclableList<object>(totalItemsCount: 0);
|
||||||
|
DoNothing(list);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Benchmark]
|
||||||
|
public void RecyclableList_Create_WithCapacity()
|
||||||
|
{
|
||||||
|
using var list = new RecyclableList<object>(totalItemsCount: TestObjectCount);
|
||||||
|
DoNothing(list);
|
||||||
|
}
|
||||||
|
|
||||||
|
//[Benchmark]
|
||||||
|
public void RecyclableList_Add()
|
||||||
|
{
|
||||||
|
var data = TestObjects;
|
||||||
|
var dataCount = TestObjectCount;
|
||||||
|
using var list = new RecyclableList<object>();
|
||||||
|
for (var i = 0; i < dataCount; i++)
|
||||||
|
{
|
||||||
|
list.Add(data[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//[Benchmark]
|
||||||
|
public void RecyclableList_Add_WithCapacity()
|
||||||
|
{
|
||||||
|
var data = TestObjects;
|
||||||
|
var dataCount = TestObjectCount;
|
||||||
|
using var list = new RecyclableList<object>(dataCount);
|
||||||
|
for (var i = 0; i < dataCount; i++)
|
||||||
|
{
|
||||||
|
list.Add(data[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//[Benchmark]
|
||||||
|
public void RecyclableList_GetItem()
|
||||||
|
{
|
||||||
|
var data = _testRecyclableList;
|
||||||
|
var dataCount = TestObjectCount;
|
||||||
|
for (var i = 0; i < dataCount; i++)
|
||||||
|
{
|
||||||
|
DoNothing(data[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//[Benchmark]
|
||||||
|
public void RecyclableList_SetItem()
|
||||||
|
{
|
||||||
|
var data = _testRecyclableList;
|
||||||
|
var dataCount = TestObjectCount;
|
||||||
|
for (var i = 0; i < dataCount; i++)
|
||||||
|
{
|
||||||
|
data[i] = TestObjects[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//[Benchmark]
|
||||||
|
public void RecyclableList_Count()
|
||||||
|
{
|
||||||
|
var data = _testRecyclableList;
|
||||||
|
DoNothing(data.Count);
|
||||||
|
}
|
||||||
|
|
||||||
|
//[Benchmark]
|
||||||
|
public void RecyclableList_LongCount()
|
||||||
|
{
|
||||||
|
var data = _testRecyclableList;
|
||||||
|
DoNothing(data.LongCount);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -7,10 +7,10 @@ namespace Recyclable.Collections
|
|||||||
public static class ListExtensions
|
public static class ListExtensions
|
||||||
{
|
{
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
public static bool Contains<T>(this RecyclableBlockList<T[]> arrays, T item) => arrays.Any(x => x.Contains(item));
|
public static bool Contains<T>(this RecyclableArrayList<T[]> arrays, T item) => arrays.Any(x => x.Contains(item));
|
||||||
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
public static void CopyTo<T>(this RecyclableBlockList<T[]> arrays, long startingIndex, int blockSize, int lastBlockSize, T[] destinationArray, int destinationArrayIndex)
|
public static void CopyTo<T>(this RecyclableArrayList<T[]> arrays, long startingIndex, int blockSize, int lastBlockSize, T[] destinationArray, int destinationArrayIndex)
|
||||||
{
|
{
|
||||||
Span<T> arrayMemory = destinationArray.AsSpan();
|
Span<T> arrayMemory = destinationArray.AsSpan();
|
||||||
arrayMemory = arrayMemory[destinationArrayIndex..];
|
arrayMemory = arrayMemory[destinationArrayIndex..];
|
||||||
@@ -49,7 +49,7 @@ namespace Recyclable.Collections
|
|||||||
}
|
}
|
||||||
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
public static IEnumerable<T> Enumerate<T>(this RecyclableBlockList<T[]> arrays, int chunkSize, long totalCount)
|
public static IEnumerable<T> Enumerate<T>(this RecyclableArrayList<T[]> arrays, int chunkSize, long totalCount)
|
||||||
{
|
{
|
||||||
long currentCount = 0;
|
long currentCount = 0;
|
||||||
for (var arrayIdx = 0; (arrayIdx < arrays.Count) && (currentCount < totalCount); arrayIdx++)
|
for (var arrayIdx = 0; (arrayIdx < arrays.Count) && (currentCount < totalCount); arrayIdx++)
|
||||||
@@ -104,7 +104,7 @@ namespace Recyclable.Collections
|
|||||||
}
|
}
|
||||||
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
public static long LongIndexOf<T>(this RecyclableBlockList<T[]> arrays, int blockSize, T item, IEqualityComparer<T> comparer)
|
public static long LongIndexOf<T>(this RecyclableArrayList<T[]> arrays, int blockSize, T item, IEqualityComparer<T> comparer)
|
||||||
{
|
{
|
||||||
for (var arrayIdx = 0; arrayIdx < arrays.Count; arrayIdx++)
|
for (var arrayIdx = 0; arrayIdx < arrays.Count; arrayIdx++)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -0,0 +1,220 @@
|
|||||||
|
using System.Collections;
|
||||||
|
|
||||||
|
namespace Recyclable.Collections
|
||||||
|
{
|
||||||
|
public class RecyclableArrayList<T> : IEnumerable<T>, IDisposable
|
||||||
|
{
|
||||||
|
private static readonly IEqualityComparer<T> _equalityComparer = EqualityComparer<T>.Default;
|
||||||
|
|
||||||
|
protected T[] _memory;
|
||||||
|
|
||||||
|
protected static T[] SetNewLength(in T[]? source, in int newSize)
|
||||||
|
{
|
||||||
|
var sourceLength = source?.Length ?? 0;
|
||||||
|
T[] newArray;
|
||||||
|
newArray = newSize.RentArrayFromPool<T>();
|
||||||
|
switch (newSize >= sourceLength)
|
||||||
|
{
|
||||||
|
case true:
|
||||||
|
if (sourceLength > 0)
|
||||||
|
{
|
||||||
|
Memory<T> sourceMemory = new(source);
|
||||||
|
Memory<T> newArrayMemory = new(newArray);
|
||||||
|
sourceMemory.CopyTo(newArrayMemory);
|
||||||
|
source!.ReturnToPool();
|
||||||
|
}
|
||||||
|
|
||||||
|
return newArray;
|
||||||
|
|
||||||
|
case false:
|
||||||
|
Span<T> sourceSpan = source.AsSpan();
|
||||||
|
Span<T> newArraySpan = newArray.AsSpan();
|
||||||
|
sourceSpan = sourceSpan[..newSize];
|
||||||
|
sourceSpan.CopyTo(newArraySpan);
|
||||||
|
source!.ReturnToPool();
|
||||||
|
return newArray;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void EnsureCapacity(in int requestedCapacity)
|
||||||
|
{
|
||||||
|
int newCapacity;
|
||||||
|
switch (_capacity > 0)
|
||||||
|
{
|
||||||
|
case true:
|
||||||
|
newCapacity = _capacity;
|
||||||
|
while (newCapacity < requestedCapacity)
|
||||||
|
{
|
||||||
|
newCapacity *= 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
break;
|
||||||
|
|
||||||
|
case false:
|
||||||
|
newCapacity = requestedCapacity;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
_memory = SetNewLength(_memory, newCapacity);
|
||||||
|
_capacity = _memory.Length;
|
||||||
|
}
|
||||||
|
|
||||||
|
#pragma warning disable CS8618 // _memory will be initialized when the 1st item is added
|
||||||
|
public RecyclableArrayList()
|
||||||
|
#pragma warning restore CS8618
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
#pragma warning disable CS8618 // _memory will be initialized when the 1st item is added
|
||||||
|
public RecyclableArrayList(int initialCapacity)
|
||||||
|
#pragma warning restore CS8618
|
||||||
|
{
|
||||||
|
// _memory will be initialized when the 1st item is added
|
||||||
|
if (initialCapacity > 0)
|
||||||
|
{
|
||||||
|
_memory = SetNewLength(_memory, initialCapacity);
|
||||||
|
_capacity = initialCapacity;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public RecyclableArrayList(IEnumerable<T> source, int initialCapacity = RecyclableDefaults.Capacity)
|
||||||
|
{
|
||||||
|
_memory = SetNewLength(_memory, initialCapacity);
|
||||||
|
_capacity = initialCapacity;
|
||||||
|
|
||||||
|
foreach (var item in source)
|
||||||
|
{
|
||||||
|
Add(item);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public T this[int index]
|
||||||
|
{
|
||||||
|
get => _memory[index];
|
||||||
|
set => _memory[index] = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
internal int _capacity;
|
||||||
|
public int Capacity
|
||||||
|
{
|
||||||
|
get => _capacity;
|
||||||
|
set
|
||||||
|
{
|
||||||
|
if (_capacity != value)
|
||||||
|
{
|
||||||
|
_memory = SetNewLength(_memory, value);
|
||||||
|
_capacity = value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public int Count { get; set; }
|
||||||
|
public bool IsReadOnly { get; } = false;
|
||||||
|
|
||||||
|
public void Add(in T item)
|
||||||
|
{
|
||||||
|
int requestedCapacity = Count + 1;
|
||||||
|
if (_capacity < requestedCapacity)
|
||||||
|
{
|
||||||
|
EnsureCapacity(requestedCapacity);
|
||||||
|
}
|
||||||
|
|
||||||
|
_memory[Count] = item;
|
||||||
|
Count++;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Clear()
|
||||||
|
{
|
||||||
|
Count = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool Contains(T item) => _memory.Contains(item);
|
||||||
|
|
||||||
|
public void CopyTo(T[] array, int arrayIndex) => _memory.CopyTo(array, arrayIndex);
|
||||||
|
|
||||||
|
protected static IEnumerable<T> Enumerate(RecyclableArrayList<T> list)
|
||||||
|
{
|
||||||
|
for (var i = 0; i < list.Count; i++)
|
||||||
|
{
|
||||||
|
yield return list._memory[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public int IndexOf(T itemToFind)
|
||||||
|
{
|
||||||
|
var memory = _memory;
|
||||||
|
int itemCount = Count;
|
||||||
|
var equalityComparer = _equalityComparer;
|
||||||
|
for (var itemIdx = 0; itemIdx < itemCount; itemIdx++)
|
||||||
|
{
|
||||||
|
var item = memory[itemIdx];
|
||||||
|
if (equalityComparer.Equals(item, itemToFind))
|
||||||
|
{
|
||||||
|
return itemIdx;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Insert(int index, T item)
|
||||||
|
{
|
||||||
|
int requestedCapacity = Count + 1;
|
||||||
|
if (Capacity < requestedCapacity)
|
||||||
|
{
|
||||||
|
EnsureCapacity(requestedCapacity);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (var toMoveIdx = Count - 1; toMoveIdx >= index; toMoveIdx--)
|
||||||
|
{
|
||||||
|
_memory[toMoveIdx + 1] = _memory[toMoveIdx];
|
||||||
|
}
|
||||||
|
|
||||||
|
_memory[index] = item;
|
||||||
|
Count++;
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool Remove(T itemToRemove)
|
||||||
|
{
|
||||||
|
var itemIdx = IndexOf(itemToRemove);
|
||||||
|
if (itemIdx >= 0)
|
||||||
|
{
|
||||||
|
RemoveAt(itemIdx);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
public void RemoveAt(int index)
|
||||||
|
{
|
||||||
|
int oldCount = Count;
|
||||||
|
int oldCountMinus1 = oldCount - 1;
|
||||||
|
if (index == oldCountMinus1)
|
||||||
|
{
|
||||||
|
Count--;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (index >= oldCount)
|
||||||
|
{
|
||||||
|
throw new ArgumentOutOfRangeException(nameof(index));
|
||||||
|
}
|
||||||
|
|
||||||
|
_memory.CopyItems(index, oldCountMinus1, ref _memory);
|
||||||
|
Count--;
|
||||||
|
}
|
||||||
|
|
||||||
|
public IEnumerator<T> GetEnumerator() => Enumerate(this).GetEnumerator();
|
||||||
|
IEnumerator IEnumerable.GetEnumerator() => Enumerate(this).GetEnumerator();
|
||||||
|
|
||||||
|
public void Dispose()
|
||||||
|
{
|
||||||
|
if (Capacity > 0)
|
||||||
|
{
|
||||||
|
Clear();
|
||||||
|
Capacity = 0;
|
||||||
|
_memory.ReturnToPool();
|
||||||
|
GC.SuppressFinalize(this);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,327 +0,0 @@
|
|||||||
using System.Collections;
|
|
||||||
using System.Runtime.CompilerServices;
|
|
||||||
|
|
||||||
namespace Recyclable.Collections
|
|
||||||
{
|
|
||||||
public class RecyclableBlockList<T> : IEnumerable<T>, IDisposable
|
|
||||||
{
|
|
||||||
private static readonly IEqualityComparer<T> _equalityComparer = EqualityComparer<T>.Default;
|
|
||||||
private bool _disposedValue;
|
|
||||||
|
|
||||||
|
|
||||||
private void CopyItems(long startingIndex, long count, ref T[] destArray, long offset = 1)
|
|
||||||
{
|
|
||||||
for (long destItemIndex = startingIndex, sourceItemIndex = offset; destItemIndex < startingIndex + count; destItemIndex++, sourceItemIndex++)
|
|
||||||
{
|
|
||||||
destArray[destItemIndex] = _memory[sourceItemIndex];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
||||||
private static T GetItem(in T[] arrays, in long index) => arrays[index];
|
|
||||||
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
||||||
private void InternalRemoveAt(long index)
|
|
||||||
{
|
|
||||||
long oldCount = LongCount;
|
|
||||||
long oldCountMinus1 = oldCount - 1;
|
|
||||||
if (index == oldCountMinus1)
|
|
||||||
{
|
|
||||||
LongCount--;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (index >= oldCount)
|
|
||||||
{
|
|
||||||
throw new ArgumentOutOfRangeException(nameof(index));
|
|
||||||
}
|
|
||||||
|
|
||||||
CopyItems(index, oldCountMinus1, ref _memory);
|
|
||||||
|
|
||||||
LongCount--;
|
|
||||||
}
|
|
||||||
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
||||||
private static long LongIndexOf(in T[] memory, in long itemCount, T? itemToFind, IEqualityComparer<T> equalityComparer)
|
|
||||||
{
|
|
||||||
for (var itemIdx = 0; itemIdx < itemCount; itemIdx++)
|
|
||||||
{
|
|
||||||
var item = memory[itemIdx];
|
|
||||||
if (equalityComparer.Equals(item, itemToFind))
|
|
||||||
{
|
|
||||||
return itemIdx;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
||||||
private static void SetItem(in T[] arrays, in long index, in T value) => arrays[index] = value;
|
|
||||||
|
|
||||||
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
||||||
private T[] SetNewLength(in long newSize)
|
|
||||||
{
|
|
||||||
if (newSize == _capacity)
|
|
||||||
{
|
|
||||||
return _memory;
|
|
||||||
}
|
|
||||||
|
|
||||||
var source = _memory;
|
|
||||||
var sourceLength = source.LongLength;
|
|
||||||
var is32bit = newSize <= int.MaxValue;
|
|
||||||
T[] newArray;
|
|
||||||
if (is32bit)
|
|
||||||
{
|
|
||||||
var newSize32bit = (int)newSize;
|
|
||||||
newArray = newSize32bit.RentArrayFromPool<T>();
|
|
||||||
if (newSize32bit >= sourceLength)
|
|
||||||
{
|
|
||||||
if (sourceLength > 0)
|
|
||||||
{
|
|
||||||
Memory<T> sourceMemory = new(source);
|
|
||||||
Memory<T> newArrayMemory = new(newArray);
|
|
||||||
sourceMemory.CopyTo(newArrayMemory);
|
|
||||||
source.ReturnToPool();
|
|
||||||
}
|
|
||||||
|
|
||||||
return newArray;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
Span<T> sourceSpan = source.AsSpan();
|
|
||||||
Span<T> newArraySpan = newArray.AsSpan();
|
|
||||||
sourceSpan = sourceSpan[..newSize32bit];
|
|
||||||
sourceSpan.CopyTo(newArraySpan);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
newArray = newSize.RentArrayFromPool<T>();
|
|
||||||
if (newSize >= sourceLength)
|
|
||||||
{
|
|
||||||
source.CopyTo(newArray, 0);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
CopyItems(0, newArray.LongLength, ref newArray, 0);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
source.ReturnToPool();
|
|
||||||
return newArray;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected T[] _memory = Array.Empty<T>();
|
|
||||||
|
|
||||||
public RecyclableBlockList(long initialCapacity = RecyclableDefaults.Capacity)
|
|
||||||
{
|
|
||||||
_memory = SetNewLength(initialCapacity);
|
|
||||||
_capacity = initialCapacity;
|
|
||||||
}
|
|
||||||
|
|
||||||
public RecyclableBlockList(IEnumerable<T> source, int initialCapacity = RecyclableDefaults.Capacity)
|
|
||||||
{
|
|
||||||
_memory = SetNewLength(initialCapacity);
|
|
||||||
_capacity = initialCapacity;
|
|
||||||
|
|
||||||
foreach (var item in source)
|
|
||||||
{
|
|
||||||
Add(item);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public T this[long index]
|
|
||||||
{
|
|
||||||
get => _memory[index];
|
|
||||||
set => _memory[index] = value;
|
|
||||||
}
|
|
||||||
|
|
||||||
public T this[int index]
|
|
||||||
{
|
|
||||||
get => _memory[index];
|
|
||||||
set => _memory[index] = value;
|
|
||||||
}
|
|
||||||
|
|
||||||
private long _capacity;
|
|
||||||
public long Capacity
|
|
||||||
{
|
|
||||||
get => _capacity;
|
|
||||||
set
|
|
||||||
{
|
|
||||||
if (_capacity != value)
|
|
||||||
{
|
|
||||||
_memory = SetNewLength(value);
|
|
||||||
_capacity = value;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
public int Count => (int)LongCount;
|
|
||||||
public long LongCount { get; set; }
|
|
||||||
public bool IsReadOnly { get; } = false;
|
|
||||||
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
||||||
public void Add(in T item)
|
|
||||||
{
|
|
||||||
//EnsureCapacity(LongCount + 1);
|
|
||||||
long requestedCapacity = LongCount + 1;
|
|
||||||
if (_capacity < requestedCapacity)
|
|
||||||
{
|
|
||||||
long newCapacity;
|
|
||||||
switch (_capacity > 0)
|
|
||||||
{
|
|
||||||
case true:
|
|
||||||
newCapacity = _capacity * 2;
|
|
||||||
while (newCapacity < requestedCapacity)
|
|
||||||
{
|
|
||||||
newCapacity *= 2;
|
|
||||||
}
|
|
||||||
|
|
||||||
break;
|
|
||||||
|
|
||||||
case false:
|
|
||||||
newCapacity = requestedCapacity;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
_memory = SetNewLength(newCapacity);
|
|
||||||
_capacity = newCapacity;
|
|
||||||
}
|
|
||||||
|
|
||||||
_memory[LongCount] = item;
|
|
||||||
LongCount++;
|
|
||||||
}
|
|
||||||
|
|
||||||
//[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
||||||
//public void EnsureCapacity(long requestedCapacity)
|
|
||||||
//{
|
|
||||||
// if (Capacity < requestedCapacity)
|
|
||||||
// {
|
|
||||||
// long newCapacity;
|
|
||||||
// switch (Capacity > 0)
|
|
||||||
// {
|
|
||||||
// case true:
|
|
||||||
// newCapacity = Capacity * 2;
|
|
||||||
// while (newCapacity < requestedCapacity)
|
|
||||||
// {
|
|
||||||
// newCapacity *= 2;
|
|
||||||
// }
|
|
||||||
|
|
||||||
// break;
|
|
||||||
|
|
||||||
// case false:
|
|
||||||
// newCapacity = requestedCapacity;
|
|
||||||
// break;
|
|
||||||
// }
|
|
||||||
|
|
||||||
// _memory = SetNewLength(newCapacity);
|
|
||||||
// _capacity = newCapacity;
|
|
||||||
// }
|
|
||||||
//}
|
|
||||||
|
|
||||||
public void Clear()
|
|
||||||
{
|
|
||||||
LongCount = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
public bool Contains(T item) => _memory.Contains(item);
|
|
||||||
|
|
||||||
public void CopyTo(T[] array, int arrayIndex) => _memory.CopyTo(array, arrayIndex);
|
|
||||||
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
||||||
public IEnumerable<T> Enumerate()
|
|
||||||
{
|
|
||||||
for (var i = 0; i < LongCount; i++)
|
|
||||||
{
|
|
||||||
yield return _memory[i];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public int IndexOf(T item) => (int)LongIndexOf(_memory, LongCount, item, _equalityComparer);
|
|
||||||
|
|
||||||
public void Insert(int index, T item)
|
|
||||||
{
|
|
||||||
long requestedCapacity = LongCount + 1;
|
|
||||||
if (Capacity < requestedCapacity)
|
|
||||||
{
|
|
||||||
long newCapacity;
|
|
||||||
switch (Capacity > 0)
|
|
||||||
{
|
|
||||||
case true:
|
|
||||||
newCapacity = Capacity * 2;
|
|
||||||
while (newCapacity < requestedCapacity)
|
|
||||||
{
|
|
||||||
newCapacity *= 2;
|
|
||||||
}
|
|
||||||
|
|
||||||
break;
|
|
||||||
|
|
||||||
case false:
|
|
||||||
newCapacity = requestedCapacity;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
_memory = SetNewLength(newCapacity);
|
|
||||||
_capacity = newCapacity;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (var toMoveIdx = LongCount - 1; toMoveIdx >= index; toMoveIdx--)
|
|
||||||
{
|
|
||||||
_memory[toMoveIdx + 1] = _memory[toMoveIdx];
|
|
||||||
}
|
|
||||||
|
|
||||||
_memory[index] = item;
|
|
||||||
LongCount++;
|
|
||||||
}
|
|
||||||
|
|
||||||
public long LongIndexOf(T item) => LongIndexOf(_memory, LongCount, item, _equalityComparer);
|
|
||||||
|
|
||||||
public bool Remove(T item)
|
|
||||||
{
|
|
||||||
var itemIdx = LongIndexOf(item);
|
|
||||||
if (itemIdx >= 0)
|
|
||||||
{
|
|
||||||
RemoveAt(itemIdx);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void RemoveAt(int index) => InternalRemoveAt(index);
|
|
||||||
public void RemoveAt(long index) => InternalRemoveAt(index);
|
|
||||||
|
|
||||||
public IEnumerator<T> GetEnumerator() => Enumerate().GetEnumerator();
|
|
||||||
IEnumerator IEnumerable.GetEnumerator() => Enumerate().GetEnumerator();
|
|
||||||
|
|
||||||
protected virtual void Dispose(bool disposing)
|
|
||||||
{
|
|
||||||
if (!_disposedValue)
|
|
||||||
{
|
|
||||||
if (disposing)
|
|
||||||
{
|
|
||||||
Clear();
|
|
||||||
_memory.ReturnToPool();
|
|
||||||
Capacity = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
_disposedValue = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// override finalizer only if 'Dispose(bool disposing)' has code to free unmanaged resources
|
|
||||||
// ~RecyclableList()
|
|
||||||
// {
|
|
||||||
// // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
|
|
||||||
// Dispose(disposing: false);
|
|
||||||
// }
|
|
||||||
|
|
||||||
public void Dispose()
|
|
||||||
{
|
|
||||||
// Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
|
|
||||||
Dispose(disposing: true);
|
|
||||||
GC.SuppressFinalize(this);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -4,5 +4,6 @@
|
|||||||
{
|
{
|
||||||
public const int BlockSize = 10_240;
|
public const int BlockSize = 10_240;
|
||||||
public const int Capacity = 8;
|
public const int Capacity = 8;
|
||||||
|
public const int MinPooledArrayLength = 100;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,51 +1,102 @@
|
|||||||
using System.Collections;
|
using System.Buffers;
|
||||||
using System.Runtime.CompilerServices;
|
using System.Collections;
|
||||||
|
|
||||||
namespace Recyclable.Collections
|
namespace Recyclable.Collections
|
||||||
{
|
{
|
||||||
public class RecyclableList<T> : IDisposable, IList<T>
|
public class RecyclableList<T> : IDisposable, IList<T>
|
||||||
{
|
{
|
||||||
private static readonly IEqualityComparer<T> _equalityComparer = EqualityComparer<T>.Default;
|
private static readonly IEqualityComparer<T> _equalityComparer = EqualityComparer<T>.Default;
|
||||||
|
private const int _minPooledArraySize = 100;
|
||||||
|
private readonly int _blockSize;
|
||||||
|
private readonly RecyclableArrayList<T[]> _memoryBlocks;
|
||||||
|
|
||||||
private readonly RecyclableBlockList<T[]> _memoryBlocks;
|
private static void AddCapacity(RecyclableList<T> list, RecyclableArrayList<T[]> memoryBlocks, int additionalCapacity)
|
||||||
private bool _disposedValue;
|
|
||||||
|
|
||||||
public RecyclableList(int blockSize = RecyclableDefaults.BlockSize, long? initialCapacity = default)
|
|
||||||
{
|
{
|
||||||
BlockSize = blockSize;
|
T[] newMemoryBlock = RentArrayFromPool(additionalCapacity);
|
||||||
initialCapacity ??= 8 * BlockSize;
|
memoryBlocks.Add(newMemoryBlock);
|
||||||
int additionalArray = initialCapacity % BlockSize > 0 ? 1 : 0;
|
list.Capacity += additionalCapacity;
|
||||||
long targetCapacity = (initialCapacity.Value / BlockSize) + additionalArray;
|
|
||||||
_memoryBlocks = new(targetCapacity);
|
|
||||||
Capacity = initialCapacity.Value;
|
|
||||||
for (var blockIdx = 0; blockIdx < targetCapacity; blockIdx++)
|
|
||||||
{
|
|
||||||
_memoryBlocks.Add(BlockSize.RentArrayFromPool<T>());
|
|
||||||
}
|
|
||||||
|
|
||||||
//MemoryBlocks.LongCount = MemoryBlocks.Capacity;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public RecyclableList(IEnumerable<T> source, int blockSize = RecyclableDefaults.BlockSize, long? initialCapacity = default)
|
private static void AddRange(RecyclableList<T> destination, IEnumerable<T> source)
|
||||||
{
|
{
|
||||||
BlockSize = blockSize;
|
|
||||||
initialCapacity ??= 8 * BlockSize;
|
|
||||||
int additionalCapacity = initialCapacity % BlockSize > 0 ? 1 : 0;
|
|
||||||
long targetCapacity = (initialCapacity.Value / BlockSize) + additionalCapacity;
|
|
||||||
_memoryBlocks = new(targetCapacity);
|
|
||||||
Capacity = initialCapacity.Value;
|
|
||||||
for (var blockIdx = 0; blockIdx < targetCapacity; blockIdx++)
|
|
||||||
{
|
|
||||||
_memoryBlocks.Add(BlockSize.RentArrayFromPool<T>());
|
|
||||||
}
|
|
||||||
|
|
||||||
foreach (var item in source)
|
foreach (var item in source)
|
||||||
{
|
{
|
||||||
Add(item);
|
destination.Add(item);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public int BlockSize { get; }
|
private static void RemoveAt(RecyclableList<T> list, long index)
|
||||||
|
{
|
||||||
|
if (index == list.LongCount - 1)
|
||||||
|
{
|
||||||
|
list.LongCount--;
|
||||||
|
if (list.Capacity - list.LongCount == list._blockSize)
|
||||||
|
{
|
||||||
|
list.RemoveBlock(list.BlockCount - 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
throw new NotSupportedException("Only removal of the last element is supported");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static T[] RentArrayFromPool(int minSize) => (minSize >= _minPooledArraySize)
|
||||||
|
? ArrayPool<T>.Shared.Rent(minSize)
|
||||||
|
: new T[minSize];
|
||||||
|
|
||||||
|
private static void ReturnToPool(in T[] array)
|
||||||
|
{
|
||||||
|
if (array.LongLength is < _minPooledArraySize or > int.MaxValue)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
ArrayPool<T>.Shared.Return(array);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void RemoveBlock(RecyclableList<T> owner, RecyclableArrayList<T[]> memoryBlocks, int blockSize, int index)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
ReturnToPool(memoryBlocks[index]);
|
||||||
|
memoryBlocks.RemoveAt(index);
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
owner.Capacity -= blockSize;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static RecyclableArrayList<T[]> SetupMemoryBlocks(RecyclableList<T> owner, int blockSize, long? totalItemsCount)
|
||||||
|
{
|
||||||
|
totalItemsCount ??= 8 * blockSize;
|
||||||
|
int additionalArray = totalItemsCount % blockSize > 0 ? 1 : 0;
|
||||||
|
int memoryBlockCount = (int)(totalItemsCount.Value / blockSize).LimitTo(int.MaxValue) + additionalArray;
|
||||||
|
RecyclableArrayList<T[]> memoryBlocks = new(memoryBlockCount);
|
||||||
|
for (var blockIdx = 0; blockIdx < memoryBlockCount; blockIdx++)
|
||||||
|
{
|
||||||
|
memoryBlocks[blockIdx] = RentArrayFromPool(blockSize);
|
||||||
|
}
|
||||||
|
|
||||||
|
memoryBlocks.Count = memoryBlockCount;
|
||||||
|
owner.Capacity = totalItemsCount.Value;
|
||||||
|
return memoryBlocks;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public RecyclableList(int blockSize = RecyclableDefaults.BlockSize, long? totalItemsCount = default)
|
||||||
|
{
|
||||||
|
_blockSize = blockSize;
|
||||||
|
_memoryBlocks = SetupMemoryBlocks(this, blockSize, totalItemsCount);
|
||||||
|
}
|
||||||
|
|
||||||
|
public RecyclableList(IEnumerable<T> source, int blockSize = RecyclableDefaults.BlockSize, long? totalItemsCount = default)
|
||||||
|
{
|
||||||
|
_blockSize = blockSize;
|
||||||
|
_memoryBlocks = SetupMemoryBlocks(this, blockSize, totalItemsCount);
|
||||||
|
AddRange(this, source);
|
||||||
|
}
|
||||||
|
|
||||||
public T this[long index]
|
public T this[long index]
|
||||||
{
|
{
|
||||||
@@ -59,23 +110,22 @@ namespace Recyclable.Collections
|
|||||||
set => _memoryBlocks[index / _blockSize][index % _blockSize] = value;
|
set => _memoryBlocks[index / _blockSize][index % _blockSize] = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
public long Capacity { get; set; }
|
public long Capacity { get; protected set; }
|
||||||
public int Count => (int)LongCount;
|
public int Count => (int)LongCount;
|
||||||
public long LongCount { get; set; }
|
public long LongCount { get; set; }
|
||||||
public bool IsReadOnly { get; } = false;
|
public bool IsReadOnly { get; } = false;
|
||||||
public long BlockCount => _memoryBlocks.LongCount;
|
public int BlockCount => _memoryBlocks.Count;
|
||||||
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
||||||
public void Add(T item)
|
public void Add(T item)
|
||||||
{
|
{
|
||||||
if (LongCount == Capacity)
|
long oldCount = LongCount;
|
||||||
|
int blockSize = _blockSize;
|
||||||
|
if (oldCount == Capacity)
|
||||||
{
|
{
|
||||||
T[] newMemoryBlock = BlockSize.RentArrayFromPool<T>();
|
AddCapacity(this, _memoryBlocks, blockSize);
|
||||||
_memoryBlocks.Add(newMemoryBlock);
|
|
||||||
Capacity += BlockSize;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
memoryBlocks[oldCount / blockSize][oldCount % blockSize] = item;
|
_memoryBlocks[(int)(oldCount / blockSize)][oldCount % blockSize] = item;
|
||||||
LongCount++;
|
LongCount++;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -88,7 +138,7 @@ namespace Recyclable.Collections
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
RemoveBlock(_memoryBlocks.Count - 1);
|
RemoveBlock(this, _memoryBlocks, _blockSize, _memoryBlocks.Count - 1);
|
||||||
}
|
}
|
||||||
catch (Exception)
|
catch (Exception)
|
||||||
{
|
{
|
||||||
@@ -106,81 +156,29 @@ namespace Recyclable.Collections
|
|||||||
|
|
||||||
public bool Contains(T item) => _memoryBlocks.Contains(item);
|
public bool Contains(T item) => _memoryBlocks.Contains(item);
|
||||||
|
|
||||||
public void CopyTo(T[] array, int arrayIndex) => _memoryBlocks.CopyTo(0, BlockSize, (int)LongCount % BlockSize, array, arrayIndex);
|
public void CopyTo(T[] array, int arrayIndex) => _memoryBlocks.CopyTo(0, _blockSize, (int)LongCount % _blockSize, array, arrayIndex);
|
||||||
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
public IEnumerator<T> GetEnumerator() => _memoryBlocks.Enumerate(_blockSize, LongCount).GetEnumerator();
|
||||||
public IEnumerator<T> GetEnumerator() => _memoryBlocks.Enumerate(BlockSize, LongCount).GetEnumerator();
|
|
||||||
|
|
||||||
public int IndexOf(T item) => (int)_memoryBlocks.LongIndexOf(BlockSize, item, _equalityComparer);
|
public int IndexOf(T item) => (int)_memoryBlocks.LongIndexOf(_blockSize, item, _equalityComparer);
|
||||||
|
|
||||||
public void Insert(int index, T item) => throw new NotSupportedException();
|
public void Insert(int index, T item) => throw new NotSupportedException();
|
||||||
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
public long LongIndexOf(T item) => _memoryBlocks.LongIndexOf(_blockSize, item, _equalityComparer);
|
||||||
public long LongIndexOf(T item) => _memoryBlocks.LongIndexOf(BlockSize, item, _equalityComparer);
|
|
||||||
|
|
||||||
public bool Remove(T item) => throw new NotSupportedException();
|
public bool Remove(T item) => throw new NotSupportedException();
|
||||||
|
|
||||||
public void RemoveAt(int index) => RemoveAt(this, index);
|
public void RemoveAt(int index) => RemoveAt(this, index);
|
||||||
public void RemoveAt(long index) => RemoveAt(this, index);
|
public void RemoveAt(long index) => RemoveAt(this, index);
|
||||||
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
IEnumerator IEnumerable.GetEnumerator() => _memoryBlocks.Enumerate(_blockSize, LongCount).GetEnumerator();
|
||||||
private static void RemoveAt(RecyclableList<T> list, long index)
|
|
||||||
{
|
|
||||||
if (index == list.LongCount - 1)
|
|
||||||
{
|
|
||||||
list.LongCount--;
|
|
||||||
if (list.Capacity - list.LongCount == list.BlockSize)
|
|
||||||
{
|
|
||||||
list.RemoveBlock(list.BlockCount - 1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
throw new NotSupportedException("Only removal of the last element is supported");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
IEnumerator IEnumerable.GetEnumerator() => _memoryBlocks.Enumerate(BlockSize, LongCount).GetEnumerator();
|
|
||||||
|
|
||||||
protected virtual void Dispose(bool disposing)
|
|
||||||
{
|
|
||||||
if (!_disposedValue)
|
|
||||||
{
|
|
||||||
if (disposing)
|
|
||||||
{
|
|
||||||
Clear();
|
|
||||||
}
|
|
||||||
|
|
||||||
_disposedValue = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// override finalizer only if 'Dispose(bool disposing)' has code to free unmanaged resources
|
|
||||||
// ~RecyclableList()
|
|
||||||
// {
|
|
||||||
// // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
|
|
||||||
// Dispose(disposing: false);
|
|
||||||
// }
|
|
||||||
|
|
||||||
public void Dispose()
|
public void Dispose()
|
||||||
{
|
{
|
||||||
// Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
|
Clear();
|
||||||
Dispose(disposing: true);
|
|
||||||
GC.SuppressFinalize(this);
|
GC.SuppressFinalize(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void RemoveBlock(long index)
|
public void RemoveBlock(int index) => RemoveBlock(this, _memoryBlocks, _blockSize, index);
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
_memoryBlocks[index].ReturnToPool();
|
|
||||||
_memoryBlocks.RemoveAt(index);
|
|
||||||
}
|
|
||||||
finally
|
|
||||||
{
|
|
||||||
Capacity -= BlockSize;
|
|
||||||
LongCount -= BlockSize;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,5 +1,4 @@
|
|||||||
using System.Buffers;
|
using System.Buffers;
|
||||||
using System.Buffers;
|
|
||||||
using System.Runtime.CompilerServices;
|
using System.Runtime.CompilerServices;
|
||||||
|
|
||||||
namespace Recyclable.Collections
|
namespace Recyclable.Collections
|
||||||
@@ -8,18 +7,25 @@ namespace Recyclable.Collections
|
|||||||
{
|
{
|
||||||
private const int _minPooledArraySize = 100;
|
private const int _minPooledArraySize = 100;
|
||||||
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
public static void CopyItems<T>(this T[] memory, long startingIndex, long count, ref T[] destArray, long offset = 1)
|
||||||
|
{
|
||||||
|
for (long destItemIndex = startingIndex, sourceItemIndex = offset; destItemIndex < startingIndex + count; destItemIndex++, sourceItemIndex++)
|
||||||
|
{
|
||||||
|
destArray[destItemIndex] = memory[sourceItemIndex];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public static void CopyTo<T>(this RecyclableList<T[]> arrays, long startingIndex, int blockSize, int lastBlockSize, T[] destinationArray, int destinationArrayIndex)
|
public static void CopyTo<T>(this RecyclableList<T[]> arrays, long startingIndex, int blockSize, int lastBlockSize, T[] destinationArray, int destinationArrayIndex)
|
||||||
{
|
{
|
||||||
Span<T> arrayMemory = destinationArray.AsSpan();
|
Span<T> arrayMemory = destinationArray.AsSpan();
|
||||||
arrayMemory = arrayMemory[destinationArrayIndex..];
|
arrayMemory = arrayMemory[destinationArrayIndex..];
|
||||||
int startingArrayIndex = (int)(startingIndex / blockSize);
|
int startingArrayIndex = (int)(startingIndex / blockSize);
|
||||||
startingIndex %= blockSize;
|
startingIndex %= blockSize;
|
||||||
if (arrays.Count > 0)
|
if (arrays.LongCount > 0)
|
||||||
{
|
{
|
||||||
ReadOnlySpan<T> sourceMemory = arrays[0].AsSpan();
|
ReadOnlySpan<T> sourceMemory = arrays[0].AsSpan();
|
||||||
var maxToCopy = (int)Math.Min(blockSize - startingIndex, arrayMemory.Length);
|
var maxToCopy = (int)Math.Min(blockSize - startingIndex, arrayMemory.Length);
|
||||||
if (arrays.Count == 1)
|
if (arrays.LongCount == 1)
|
||||||
{
|
{
|
||||||
maxToCopy = (int)Math.Min(maxToCopy, lastBlockSize);
|
maxToCopy = (int)Math.Min(maxToCopy, lastBlockSize);
|
||||||
}
|
}
|
||||||
@@ -47,91 +53,122 @@ namespace Recyclable.Collections
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
//[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
public static bool Contains<T>(this RecyclableList<T[]> arrays, T item) => arrays.Any(x => x.Contains(item));
|
//public static bool Contains<T>(this RecyclableList<T[]> arrays, T item) => arrays.Any(x => x.Contains(item));
|
||||||
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
//public static IEnumerable<T> Enumerate<T>(this RecyclableList<T[]> arrays, int chunkSize, long totalCount)
|
||||||
public static IEnumerable<T> Enumerate<T>(this RecyclableList<T[]> arrays, int chunkSize, long totalCount)
|
//{
|
||||||
|
// long currentCount = 0;
|
||||||
|
// for (var arrayIdx = 0; arrayIdx < arrays.Count; arrayIdx++)
|
||||||
|
// {
|
||||||
|
// var array = arrays[arrayIdx];
|
||||||
|
// currentCount += chunkSize;
|
||||||
|
// switch (currentCount < totalCount)
|
||||||
|
// {
|
||||||
|
// case true:
|
||||||
|
// for (var valueIdx = 0; valueIdx < chunkSize; valueIdx++)
|
||||||
|
// {
|
||||||
|
// yield return array[valueIdx];
|
||||||
|
// }
|
||||||
|
|
||||||
|
// break;
|
||||||
|
|
||||||
|
// case false:
|
||||||
|
// var partialCount = (int)(totalCount % chunkSize);
|
||||||
|
// int maxCount = partialCount > 0 ? partialCount : chunkSize;
|
||||||
|
// for (var valueIdx = 0; valueIdx < maxCount; valueIdx++)
|
||||||
|
// {
|
||||||
|
// yield return array[valueIdx];
|
||||||
|
// }
|
||||||
|
|
||||||
|
// break;
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
//}
|
||||||
|
|
||||||
|
//[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
//public static T LimitTo<T>(this T value, T maxValue, IComparer<T> comparer)
|
||||||
|
// => comparer.Compare(value, maxValue) > 0 ? maxValue : value;
|
||||||
|
|
||||||
|
//[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
public static long LongIndexOf<T>(this RecyclableList<T> arrays, long itemsCount, T itemToFind, IEqualityComparer<T> comparer)
|
||||||
{
|
{
|
||||||
long currentCount = 0;
|
for (var itemIdx = 0L; itemIdx < itemsCount; itemIdx++)
|
||||||
for (var arrayIdx = 0; arrayIdx < arrays.Count; arrayIdx++)
|
|
||||||
{
|
{
|
||||||
var array = arrays[arrayIdx];
|
var item = arrays[itemIdx];
|
||||||
currentCount += chunkSize;
|
if (comparer.Equals(itemToFind, item))
|
||||||
switch (currentCount < totalCount)
|
|
||||||
{
|
{
|
||||||
case true:
|
return itemIdx;
|
||||||
for (var valueIdx = 0; valueIdx < chunkSize; valueIdx++)
|
|
||||||
{
|
|
||||||
yield return array[valueIdx];
|
|
||||||
}
|
|
||||||
|
|
||||||
break;
|
|
||||||
|
|
||||||
case false:
|
|
||||||
var partialCount = (int)(totalCount % chunkSize);
|
|
||||||
int maxCount = partialCount > 0 ? partialCount : chunkSize;
|
|
||||||
for (var valueIdx = 0; valueIdx < maxCount; valueIdx++)
|
|
||||||
{
|
|
||||||
yield return array[valueIdx];
|
|
||||||
}
|
|
||||||
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
itemIdx++;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
public static long LongIndexOf<T>(this RecyclableArrayList<T> arrays, int itemsCount, T itemToFind, IEqualityComparer<T> comparer)
|
||||||
public static T LimitTo<T>(this T value, T maxValue, IComparer<T> comparer)
|
|
||||||
=> comparer.Compare(value, maxValue) > 0 ? maxValue : value;
|
|
||||||
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
||||||
public static long LongIndexOf<T>(this RecyclableList<T[]> arrays, int blockSize, T item, IEqualityComparer<T> comparer)
|
|
||||||
{
|
{
|
||||||
for (var arrayIdx = 0; arrayIdx < arrays.Count; arrayIdx++)
|
for (var itemIdx = 0; itemIdx < itemsCount; itemIdx++)
|
||||||
{
|
{
|
||||||
var array = arrays[arrayIdx];
|
var item = arrays[itemIdx];
|
||||||
Span<T> arrayMemory = array.AsSpan();
|
if (comparer.Equals(itemToFind, item))
|
||||||
for (int memoryIdx = 0; memoryIdx < arrayMemory.Length; memoryIdx++)
|
|
||||||
{
|
{
|
||||||
if (comparer.Equals(arrayMemory[memoryIdx], item))
|
return itemIdx;
|
||||||
{
|
|
||||||
return (arrayIdx * (long)blockSize) + memoryIdx;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
//[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
public static RecyclableList<T> ToRecyclableList<T>(this IList<T> values, int blockSize = RecyclableDefaults.BlockSize) => new(values, blockSize);
|
//public static RecyclableList<T> ToRecyclableList<T>(this IList<T> values, int blockSize = RecyclableDefaults.BlockSize) => new(values, blockSize);
|
||||||
|
|
||||||
|
//[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
//public static RecyclableList<T> ToRecyclableList<T>(this IEnumerable<T> values, int blockSize = RecyclableDefaults.BlockSize) => new(values, blockSize);
|
||||||
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
public static RecyclableList<T> ToRecyclableList<T>(this IEnumerable<T> values, int blockSize = RecyclableDefaults.BlockSize) => new(values, blockSize);
|
public static T[] RentArrayFromPool<T>(this int minSize) => (minSize >= _minPooledArraySize)
|
||||||
|
? ArrayPool<T>.Shared.Rent(minSize)
|
||||||
|
: new T[minSize];
|
||||||
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
public static T[] RentArrayFromPool<T>(this int minSize) => RentArrayFromPool<T>((long)minSize);
|
public static T[] RentArrayFromPool<T>(this long minSize) => (minSize is >= _minPooledArraySize and <= int.MaxValue)
|
||||||
|
? ArrayPool<T>.Shared.Rent((int)minSize)
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
: new T[minSize];
|
||||||
public static T[] RentArrayFromPool<T>(this long minSize)
|
|
||||||
{
|
|
||||||
return minSize switch
|
|
||||||
{
|
|
||||||
< _minPooledArraySize => new T[minSize],
|
|
||||||
<= int.MaxValue => ArrayPool<T>.Shared.Rent((int)minSize),
|
|
||||||
_ => new T[minSize]
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
public static void ReturnToPool<T>(this T[] array)
|
public static void ReturnToPool<T>(this T[] array)
|
||||||
{
|
{
|
||||||
long arrayLength = array.LongLength;
|
if (array.LongLength is > _minPooledArraySize and <= int.MaxValue)
|
||||||
if (arrayLength is >= _minPooledArraySize and <= int.MaxValue)
|
|
||||||
{
|
{
|
||||||
ArrayPool<T>.Shared.Return(array);
|
ArrayPool<T>.Shared.Return(array);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//public static void AddRange<T>(this RecyclableList<T> destination, IEnumerable<T> source)
|
||||||
|
//{
|
||||||
|
// foreach (var item in source)
|
||||||
|
// {
|
||||||
|
// destination.Add(item);
|
||||||
|
// }
|
||||||
|
//}
|
||||||
|
|
||||||
|
public static long LimitTo(this long value, int limit)
|
||||||
|
=> (value <= limit) ? value : limit;
|
||||||
|
|
||||||
|
public static int IndexOf<T>(this T[] memory, int itemCount, T? itemToFind, IEqualityComparer<T> equalityComparer)
|
||||||
|
{
|
||||||
|
for (var itemIdx = 0; itemIdx < itemCount; itemIdx++)
|
||||||
|
{
|
||||||
|
var item = memory[itemIdx];
|
||||||
|
if (equalityComparer.Equals(item, itemToFind))
|
||||||
|
{
|
||||||
|
return itemIdx;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -8,7 +8,7 @@ namespace Recyclable.Collections
|
|||||||
private static readonly IEqualityComparer<T> _equalityComparer = EqualityComparer<T>.Default;
|
private static readonly IEqualityComparer<T> _equalityComparer = EqualityComparer<T>.Default;
|
||||||
|
|
||||||
private bool _disposedValue;
|
private bool _disposedValue;
|
||||||
private RecyclableBlockList<T[]> Memory { get; }
|
private RecyclableArrayList<T[]> Memory { get; }
|
||||||
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
private static T Dequeue(RecyclableQueue<T> queue)
|
private static T Dequeue(RecyclableQueue<T> queue)
|
||||||
|
|||||||
+13
-13
@@ -3,7 +3,7 @@ using Recyclable.Collections;
|
|||||||
|
|
||||||
namespace Recyclable.CollectionsTests
|
namespace Recyclable.CollectionsTests
|
||||||
{
|
{
|
||||||
public class RecyclableBlockListTests
|
public class RecyclableArrayListTests
|
||||||
{
|
{
|
||||||
private readonly static IEnumerable<int> _testData = Enumerable.Range(1, 20);
|
private readonly static IEnumerable<int> _testData = Enumerable.Range(1, 20);
|
||||||
|
|
||||||
@@ -12,13 +12,13 @@ namespace Recyclable.CollectionsTests
|
|||||||
{
|
{
|
||||||
// Prepare
|
// Prepare
|
||||||
var testData = _testData.ToArray();
|
var testData = _testData.ToArray();
|
||||||
var list = new RecyclableBlockList<int>();
|
var list = new RecyclableArrayList<int>();
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
foreach (var item in testData)
|
foreach (var item in testData)
|
||||||
{
|
{
|
||||||
list.Add(item);
|
list.Add(item);
|
||||||
_ = list.LongCount.Should().Be(item);
|
_ = list.Count.Should().Be(item);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Validate
|
// Validate
|
||||||
@@ -33,13 +33,13 @@ namespace Recyclable.CollectionsTests
|
|||||||
{
|
{
|
||||||
// Prepare
|
// Prepare
|
||||||
var testData = _testData.ToArray();
|
var testData = _testData.ToArray();
|
||||||
var list = new RecyclableBlockList<int>();
|
var list = new RecyclableArrayList<int>();
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
foreach (var item in testData)
|
foreach (var item in testData)
|
||||||
{
|
{
|
||||||
list.Insert(0, item);
|
list.Insert(0, item);
|
||||||
_ = list.LongCount.Should().Be(item);
|
_ = list.Count.Should().Be(item);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Validate
|
// Validate
|
||||||
@@ -54,7 +54,7 @@ namespace Recyclable.CollectionsTests
|
|||||||
{
|
{
|
||||||
// Prepare
|
// Prepare
|
||||||
var testData = _testData.ToArray();
|
var testData = _testData.ToArray();
|
||||||
var list = new RecyclableBlockList<int>(testData);
|
var list = new RecyclableArrayList<int>(testData);
|
||||||
|
|
||||||
// Act & Validate
|
// Act & Validate
|
||||||
foreach (var item in testData)
|
foreach (var item in testData)
|
||||||
@@ -72,7 +72,7 @@ namespace Recyclable.CollectionsTests
|
|||||||
{
|
{
|
||||||
// Prepare
|
// Prepare
|
||||||
var testData = _testData.ToArray();
|
var testData = _testData.ToArray();
|
||||||
var list = new RecyclableBlockList<int>(testData);
|
var list = new RecyclableArrayList<int>(testData);
|
||||||
|
|
||||||
// Act & Validate
|
// Act & Validate
|
||||||
for (var deleted = 1; deleted <= testData.LongLength; deleted++)
|
for (var deleted = 1; deleted <= testData.LongLength; deleted++)
|
||||||
@@ -94,7 +94,7 @@ namespace Recyclable.CollectionsTests
|
|||||||
var testData = _testData.ToArray();
|
var testData = _testData.ToArray();
|
||||||
|
|
||||||
// Act & Validate
|
// Act & Validate
|
||||||
var list = new RecyclableBlockList<int>(testData);
|
var list = new RecyclableArrayList<int>(testData);
|
||||||
for (var deleted = 1; deleted <= testData.LongLength; deleted++)
|
for (var deleted = 1; deleted <= testData.LongLength; deleted++)
|
||||||
{
|
{
|
||||||
// Act
|
// Act
|
||||||
@@ -112,7 +112,7 @@ namespace Recyclable.CollectionsTests
|
|||||||
{
|
{
|
||||||
// Prepare
|
// Prepare
|
||||||
var testData = _testData.ToArray();
|
var testData = _testData.ToArray();
|
||||||
var list = new RecyclableBlockList<int>(testData);
|
var list = new RecyclableArrayList<int>(testData);
|
||||||
|
|
||||||
// Act & Validate
|
// Act & Validate
|
||||||
for (var deleted = 1; deleted <= testData.LongLength; deleted++)
|
for (var deleted = 1; deleted <= testData.LongLength; deleted++)
|
||||||
@@ -135,13 +135,13 @@ namespace Recyclable.CollectionsTests
|
|||||||
{
|
{
|
||||||
// Prepare
|
// Prepare
|
||||||
var testData = _testData.ToArray();
|
var testData = _testData.ToArray();
|
||||||
var list = new RecyclableBlockList<int>(testData);
|
var list = new RecyclableArrayList<int>(testData);
|
||||||
|
|
||||||
// Act & Validate
|
// Act & Validate
|
||||||
for (var deleted = 1; deleted <= testData.LongLength; deleted++)
|
for (var deleted = 1; deleted <= testData.LongLength; deleted++)
|
||||||
{
|
{
|
||||||
// Act
|
// Act
|
||||||
list.RemoveAt(list.LongCount - 1);
|
list.RemoveAt(list.Count - 1);
|
||||||
|
|
||||||
// Validate
|
// Validate
|
||||||
_ = list.Should().HaveCount(testData.Length - deleted)
|
_ = list.Should().HaveCount(testData.Length - deleted)
|
||||||
@@ -160,7 +160,7 @@ namespace Recyclable.CollectionsTests
|
|||||||
var testData = _testData.ToArray();
|
var testData = _testData.ToArray();
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
var list = new RecyclableBlockList<int>(testData);
|
var list = new RecyclableArrayList<int>(testData);
|
||||||
|
|
||||||
// Validate
|
// Validate
|
||||||
_ = list.Should().HaveCount(testData.Length)
|
_ = list.Should().HaveCount(testData.Length)
|
||||||
@@ -173,7 +173,7 @@ namespace Recyclable.CollectionsTests
|
|||||||
{
|
{
|
||||||
// Prepare
|
// Prepare
|
||||||
var testData = _testData.ToArray();
|
var testData = _testData.ToArray();
|
||||||
var list = new RecyclableBlockList<int>(testData);
|
var list = new RecyclableArrayList<int>(testData);
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
var yieldedItems = new List<int>();
|
var yieldedItems = new List<int>();
|
||||||
Reference in new issue
Block a user