diff --git a/Recyclable.Collections/GlobalSuppressions.cs b/Recyclable.Collections/GlobalSuppressions.cs new file mode 100644 index 0000000..67f632b --- /dev/null +++ b/Recyclable.Collections/GlobalSuppressions.cs @@ -0,0 +1,8 @@ +// This file is used by Code Analysis to maintain SuppressMessage +// attributes that are applied to this project. +// Project-level suppressions either have no target or are given +// a specific target and scoped to a namespace, type, member, etc. + +using System.Diagnostics.CodeAnalysis; + +[assembly: SuppressMessage("Style", "IDE0032:Use auto property", Justification = "", Scope = "member", Target = "~F:Recyclable.Collections.RecyclableArrayList`1._count")] diff --git a/Recyclable.Collections/RecyclableArrayList.cs b/Recyclable.Collections/RecyclableArrayList.cs index 0c492cd..b2eef03 100644 --- a/Recyclable.Collections/RecyclableArrayList.cs +++ b/Recyclable.Collections/RecyclableArrayList.cs @@ -16,7 +16,7 @@ namespace Recyclable.Collections throw new ArgumentOutOfRangeException("index"); } - //[MethodImpl(MethodImplOptions.AggressiveInlining)] + [MethodImpl(MethodImplOptions.AggressiveInlining)] protected static T[] SetNewLength(in T[]? source, in int newSize) { ArrayPool arrayPool = _arrayPool; @@ -36,9 +36,8 @@ namespace Recyclable.Collections return newArray; case false: - Span sourceSpan = new(source); + var sourceSpan = new Span(source)[..newSize]; Span newArraySpan = new(newArray); - sourceSpan = sourceSpan[..newSize]; sourceSpan.CopyTo(newArraySpan); source!.ReturnToPool(arrayPool); return newArray; @@ -118,7 +117,7 @@ namespace Recyclable.Collections } } - private int _count; + protected int _count; public int Count { get => _count; @@ -147,12 +146,14 @@ namespace Recyclable.Collections _ = EnsureCapacity(targetCapacity); } + var targetSpan = new Span(_memory)[_count..]; Span itemsSpan = new(items); - Span targetSpan = new(_memory); - for (int sourceItemIdx = 0, targetItemIdx = _count; sourceItemIdx < sourceItemsCount;) - { - targetSpan[targetItemIdx++] = itemsSpan[sourceItemIdx++]; - } + itemsSpan.CopyTo(targetSpan); + + //for (int sourceItemIdx = 0, targetItemIdx = _count; sourceItemIdx < sourceItemsCount;) + //{ + // targetSpan[targetItemIdx++] = itemsSpan[sourceItemIdx++]; + //} _count = targetCapacity; } @@ -166,12 +167,33 @@ namespace Recyclable.Collections _ = EnsureCapacity(targetCapacity); } - Span targetSpan = new(_memory); - for (int sourceItemIdx = 0, targetItemIdx = _count; sourceItemIdx < sourceItemsCount;) + items.CopyTo(_memory, _count); + //Span targetSpan = new(_memory); + //for (int sourceItemIdx = 0, targetItemIdx = _count; sourceItemIdx < sourceItemsCount;) + //{ + // targetSpan[targetItemIdx++] = items[sourceItemIdx++]; + //} + + _count = targetCapacity; + } + + public void AddRange(in RecyclableArrayList items) + { + var sourceItemsCount = items._count; + var targetCapacity = _count + sourceItemsCount; + if (_capacity < targetCapacity) { - targetSpan[targetItemIdx++] = items[sourceItemIdx++]; + _ = EnsureCapacity(targetCapacity); } + var targetSpan = new Span(_memory)[_count..]; + Span itemsSpan = new(items._memory); + itemsSpan.CopyTo(targetSpan); + //for (int sourceItemIdx = 0, targetItemIdx = _count; sourceItemIdx < sourceItemsCount;) + //{ + // targetSpan[targetItemIdx++] = itemsSpan[sourceItemIdx++]; + //} + // _count = targetCapacity; } @@ -273,10 +295,10 @@ namespace Recyclable.Collections public void Dispose() { - if (Capacity > 0) + if (_capacity > 0) { Clear(); - Capacity = 0; + _capacity = 0; _memory.ReturnToPool(_arrayPool); GC.SuppressFinalize(this); }