RecyclableSortedList added
This commit is contained in:
1 parent
81e064af68
commit
fa3e78139e
5 files changed
+214
-21
No files matched your search
@@ -15,10 +15,17 @@
|
|||||||
<Description>Recyclable.Collections project is an open source framework for operating dynamic lists at performance close to raw arrays. It aims at providing high performance with minimal memory footprint. It implements IList<T>'s interface and is targeted as direct replacements of List<T>, SortableList<T>, PriorityQueue<T> and similar.
|
<Description>Recyclable.Collections project is an open source framework for operating dynamic lists at performance close to raw arrays. It aims at providing high performance with minimal memory footprint. It implements IList<T>'s interface and is targeted as direct replacements of List<T>, SortableList<T>, PriorityQueue<T> and similar.
|
||||||
|
|
||||||
The project & source code is free for educational, open-source & personal use.</Description>
|
The project & source code is free for educational, open-source & personal use.</Description>
|
||||||
|
<ProjectUrl>https://github.com/mlemanczyk/Recyclable.Collections</ProjectUrl>
|
||||||
|
<RepositoryUrl>https://github.com/mlemanczyk/Recyclable.Collections</RepositoryUrl>
|
||||||
|
<Copyright>Marek Lemańczyk 2023</Copyright>
|
||||||
|
<LicenseUrl>https://github.com/mlemanczyk/Recyclable.Collections</LicenseUrl>
|
||||||
|
<Readme>/README.md</Readme>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Compile Remove="RecyclableSortedList.cs" />
|
<None Include="..\README.md" Link="README.md">
|
||||||
|
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||||
|
</None>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
@@ -3,8 +3,6 @@ using System.Collections;
|
|||||||
|
|
||||||
namespace Recyclable.Collections
|
namespace Recyclable.Collections
|
||||||
{
|
{
|
||||||
public delegate void ItemAdded<T>(long index, T item);
|
|
||||||
|
|
||||||
public class RecyclableList<T> : IDisposable, IList<T>
|
public class RecyclableList<T> : IDisposable, IList<T>
|
||||||
{
|
{
|
||||||
private static readonly IEqualityComparer<T> _comparer = EqualityComparer<T>.Default;
|
private static readonly IEqualityComparer<T> _comparer = EqualityComparer<T>.Default;
|
||||||
@@ -13,9 +11,20 @@ namespace Recyclable.Collections
|
|||||||
private readonly List<T[]> _arrays = new();
|
private readonly List<T[]> _arrays = new();
|
||||||
private bool _disposedValue;
|
private bool _disposedValue;
|
||||||
private long _capacity;
|
private long _capacity;
|
||||||
|
protected uint _isUpdating;
|
||||||
|
|
||||||
private ItemAdded<T>? _itemAdded;
|
protected virtual void BlockUpdateEvent()
|
||||||
public event ItemAdded<T> ItemAdded { add => _itemAdded += value; remove => _itemAdded -= value; }
|
{
|
||||||
|
_isUpdating++;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected virtual void UnblockUpdateEvent()
|
||||||
|
{
|
||||||
|
if (_isUpdating > 0)
|
||||||
|
{
|
||||||
|
_isUpdating--;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private static IEnumerable<T> EnumerateElements(List<T[]> arrays, int chunkSize, long totalCount)
|
private static IEnumerable<T> EnumerateElements(List<T[]> arrays, int chunkSize, long totalCount)
|
||||||
{
|
{
|
||||||
@@ -58,9 +67,17 @@ namespace Recyclable.Collections
|
|||||||
public RecyclableList(IEnumerable<T> source, int blockSize = RecyclableDefaults.BlockSize)
|
public RecyclableList(IEnumerable<T> source, int blockSize = RecyclableDefaults.BlockSize)
|
||||||
{
|
{
|
||||||
_blockSize = blockSize;
|
_blockSize = blockSize;
|
||||||
foreach (var item in source)
|
BlockUpdateEvent();
|
||||||
|
try
|
||||||
{
|
{
|
||||||
Add(item);
|
foreach (var item in source)
|
||||||
|
{
|
||||||
|
Add(item);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
UnblockUpdateEvent();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -71,7 +88,10 @@ namespace Recyclable.Collections
|
|||||||
set
|
set
|
||||||
{
|
{
|
||||||
_arrays[(int)(index / _blockSize)][index % _blockSize] = value;
|
_arrays[(int)(index / _blockSize)][index % _blockSize] = value;
|
||||||
_itemAdded?.Invoke(index, value);
|
if (!IsUpdating)
|
||||||
|
{
|
||||||
|
ListUpdated();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -84,6 +104,7 @@ namespace Recyclable.Collections
|
|||||||
public int Count => LongCount <= int.MaxValue ? (int)LongCount : int.MaxValue;
|
public int Count => LongCount <= int.MaxValue ? (int)LongCount : int.MaxValue;
|
||||||
public long LongCount { get; protected set; }
|
public long LongCount { get; protected set; }
|
||||||
public bool IsReadOnly { get; } = false;
|
public bool IsReadOnly { get; } = false;
|
||||||
|
public bool IsUpdating => _isUpdating > 0;
|
||||||
|
|
||||||
public void Add(T item)
|
public void Add(T item)
|
||||||
{
|
{
|
||||||
@@ -95,15 +116,28 @@ namespace Recyclable.Collections
|
|||||||
}
|
}
|
||||||
|
|
||||||
var newIndex = LongCount;
|
var newIndex = LongCount;
|
||||||
this[newIndex] = item;
|
// We don't want this[newIndex] set to raise the update event, because
|
||||||
// Don't merge these 2 lines, otherwise we'll have increased count
|
// the count would be wrong. We need to increase it, first.
|
||||||
// before the item is actually added.
|
BlockUpdateEvent();
|
||||||
LongCount++;
|
try
|
||||||
_itemAdded?.Invoke(newIndex, item);
|
{
|
||||||
|
this[newIndex] = item;
|
||||||
|
LongCount++;
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
UnblockUpdateEvent();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!IsUpdating)
|
||||||
|
{
|
||||||
|
ListUpdated();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Clear()
|
public void Clear()
|
||||||
{
|
{
|
||||||
|
BlockUpdateEvent();
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
for (var arrayIdx = 0; arrayIdx < _arrays.Count; arrayIdx++)
|
for (var arrayIdx = 0; arrayIdx < _arrays.Count; arrayIdx++)
|
||||||
@@ -121,9 +155,16 @@ namespace Recyclable.Collections
|
|||||||
}
|
}
|
||||||
finally
|
finally
|
||||||
{
|
{
|
||||||
// Make sure the list is cleared so that we no longer use any of the arrays
|
try
|
||||||
LongCount = 0;
|
{
|
||||||
_arrays.Clear();
|
// Make sure the list is cleared so that we no longer use any of the arrays
|
||||||
|
LongCount = 0;
|
||||||
|
_arrays.Clear();
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
UnblockUpdateEvent();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -203,6 +244,10 @@ namespace Recyclable.Collections
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected virtual void ListUpdated()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
// override finalizer only if 'Dispose(bool disposing)' has code to free unmanaged resources
|
// override finalizer only if 'Dispose(bool disposing)' has code to free unmanaged resources
|
||||||
// ~RecyclableList()
|
// ~RecyclableList()
|
||||||
// {
|
// {
|
||||||
@@ -216,5 +261,19 @@ namespace Recyclable.Collections
|
|||||||
Dispose(disposing: true);
|
Dispose(disposing: true);
|
||||||
GC.SuppressFinalize(this);
|
GC.SuppressFinalize(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void BeginUpdate() => _isUpdating++;
|
||||||
|
|
||||||
|
public void EndUpdate()
|
||||||
|
{
|
||||||
|
if (_isUpdating > 0)
|
||||||
|
{
|
||||||
|
_isUpdating--;
|
||||||
|
if (_isUpdating == 0)
|
||||||
|
{
|
||||||
|
ListUpdated();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,43 @@
|
|||||||
|
namespace Recyclable.Collections
|
||||||
|
{
|
||||||
|
public class RecyclableSortedList<TKey, TValue> : RecyclableList<(TKey Key, TValue Value)>
|
||||||
|
where TKey : notnull
|
||||||
|
{
|
||||||
|
private static readonly IComparer<(TKey, TValue)> _comparer = new FrequencyTupleComparer<TKey, TValue>();
|
||||||
|
private readonly SystemRandomNumberGenerator _randomNumberGenerator = new SystemRandomNumberGenerator();
|
||||||
|
|
||||||
|
protected override void ListUpdated()
|
||||||
|
{
|
||||||
|
base.ListUpdated();
|
||||||
|
QuickSort();
|
||||||
|
}
|
||||||
|
|
||||||
|
public RecyclableSortedList() : base()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public RecyclableSortedList(Dictionary<TKey, TValue> source, int blockSize = RecyclableDefaults.BlockSize)
|
||||||
|
: this(source.Select(x => (x.Key, x.Value)), blockSize)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public RecyclableSortedList(IEnumerable<(TKey Key, TValue Value)> source, int blockSize = RecyclableDefaults.BlockSize)
|
||||||
|
: base(source, blockSize)
|
||||||
|
{
|
||||||
|
QuickSort();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void QuickSort()
|
||||||
|
{
|
||||||
|
_isUpdating++;
|
||||||
|
try
|
||||||
|
{
|
||||||
|
this.QuickSort(_comparer, _randomNumberGenerator);
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
_isUpdating--;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
<Project Sdk="Microsoft.NET.Sdk">
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<TargetFramework>net6.0</TargetFramework>
|
<TargetFramework>net6.0</TargetFramework>
|
||||||
@@ -8,10 +8,6 @@
|
|||||||
<IsPackable>false</IsPackable>
|
<IsPackable>false</IsPackable>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
|
||||||
<Compile Remove="RecyclableSortedListTests.cs" />
|
|
||||||
</ItemGroup>
|
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="FluentAssertions" Version="6.8.0" />
|
<PackageReference Include="FluentAssertions" Version="6.8.0" />
|
||||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.4.1" />
|
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.4.1" />
|
||||||
|
|||||||
@@ -0,0 +1,88 @@
|
|||||||
|
using FluentAssertions;
|
||||||
|
using Recyclable.Collections;
|
||||||
|
|
||||||
|
namespace Recyclable.CollectionsTests
|
||||||
|
{
|
||||||
|
[TestClass()]
|
||||||
|
public class RecyclableSortedListTests
|
||||||
|
{
|
||||||
|
private static readonly (int, string)[] _testData = new[] { (5, "a"), (4, "d"), (3, "c"), (2, "b"), (1, "a") };
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public void ShouldBeSortedWhenCreated()
|
||||||
|
{
|
||||||
|
// Act
|
||||||
|
using var sortedList = new RecyclableSortedList<int, string>(_testData);
|
||||||
|
|
||||||
|
// Validate
|
||||||
|
_ = sortedList.Should().HaveCount(_testData.Length)
|
||||||
|
.And.BeEquivalentTo(_testData)
|
||||||
|
.And.BeInAscendingOrder((item) => item.Key);
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public void ShouldBeEmptyWhenNotInitialized()
|
||||||
|
{
|
||||||
|
// Act
|
||||||
|
using var sortedList = new RecyclableSortedList<int, string>();
|
||||||
|
|
||||||
|
// Validate
|
||||||
|
_ = sortedList.Should().BeEmpty();
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public void ShouldBeSortedWhenInitialized()
|
||||||
|
{
|
||||||
|
// Act
|
||||||
|
using var sortedList = new RecyclableSortedList<int, string>()
|
||||||
|
{
|
||||||
|
_testData[0],
|
||||||
|
_testData[1],
|
||||||
|
_testData[2],
|
||||||
|
_testData[3],
|
||||||
|
_testData[4]
|
||||||
|
};
|
||||||
|
|
||||||
|
// Validate
|
||||||
|
_ = sortedList.Should().NotBeEmpty()
|
||||||
|
.And.BeInAscendingOrder(x => x.Key)
|
||||||
|
.And.BeEquivalentTo(_testData);
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public void ShouldBeSortedWhenUpdateEnds()
|
||||||
|
{
|
||||||
|
// Prepare
|
||||||
|
using var sortedList = new RecyclableSortedList<int, string>();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
sortedList.BeginUpdate();
|
||||||
|
foreach (var testItem in _testData)
|
||||||
|
{
|
||||||
|
sortedList.Add(testItem);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Validate
|
||||||
|
_ = sortedList.Should().BeInDescendingOrder(x => x.Key);
|
||||||
|
|
||||||
|
// Act
|
||||||
|
sortedList.EndUpdate();
|
||||||
|
|
||||||
|
// Validate
|
||||||
|
_ = sortedList.Should().BeInAscendingOrder(x => x.Key);
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public void ShouldBeEmptyAfterClear()
|
||||||
|
{
|
||||||
|
// Prepare
|
||||||
|
using var sortedList = new RecyclableSortedList<int, string>(_testData);
|
||||||
|
|
||||||
|
// Act
|
||||||
|
sortedList.Clear();
|
||||||
|
|
||||||
|
// Validate
|
||||||
|
_ = sortedList.Should().BeEmpty();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in new issue
Block a user