diff --git a/src/Avalonia.Base/Collections/Pooled/ClearMode.cs b/src/Avalonia.Base/Collections/Pooled/ClearMode.cs
new file mode 100644
index 00000000000..6b6df303d4a
--- /dev/null
+++ b/src/Avalonia.Base/Collections/Pooled/ClearMode.cs
@@ -0,0 +1,37 @@
+namespace Avalonia.Collections.Pooled
+{
+ ///
+ /// This enum allows control over how data is treated when internal
+ /// arrays are returned to the ArrayPool. Be careful to understand
+ /// what each option does before using anything other than the default
+ /// of Auto.
+ ///
+ public enum ClearMode
+ {
+ ///
+ /// Auto
has different behavior depending on the host project's target framework.
+ /// .NET Core 2.1: Reference types and value types that contain reference types are cleared
+ /// when the internal arrays are returned to the pool. Value types that do not contain reference
+ /// types are not cleared when returned to the pool.
+ /// .NET Standard 2.0: All user types are cleared before returning to the pool, in case they
+ /// contain reference types.
+ /// For .NET Standard, Auto and Always have the same behavior.
+ ///
+ Auto = 0,
+ ///
+ /// The Always
setting has the effect of always clearing user types before returning to the pool.
+ /// This is the default behavior on .NET Standard.You might want to turn this on in a .NET Core project
+ /// if you were concerned about sensitive data stored in value types leaking to other pars of your application.
+ ///
+ Always = 1,
+ ///
+ /// Never
will cause pooled collections to never clear user types before returning them to the pool.
+ /// You might want to use this setting in a .NET Standard project when you know that a particular collection stores
+ /// only value types and you want the performance benefit of not taking time to reset array items to their default value.
+ /// Be careful with this setting: if used for a collection that contains reference types, or value types that contain
+ /// reference types, this setting could cause memory issues by making the garbage collector unable to clean up instances
+ /// that are still being referenced by arrays sitting in the ArrayPool.
+ ///
+ Never = 2
+ }
+}
diff --git a/src/Avalonia.Base/Collections/Pooled/ICollectionDebugView.cs b/src/Avalonia.Base/Collections/Pooled/ICollectionDebugView.cs
new file mode 100644
index 00000000000..2b15388a138
--- /dev/null
+++ b/src/Avalonia.Base/Collections/Pooled/ICollectionDebugView.cs
@@ -0,0 +1,31 @@
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the MIT license.
+// See the LICENSE file in the project root for more information.
+
+using System;
+using System.Collections.Generic;
+using System.Diagnostics;
+
+namespace Avalonia.Collections.Pooled
+{
+ internal sealed class ICollectionDebugView
+ {
+ private readonly ICollection _collection;
+
+ public ICollectionDebugView(ICollection collection)
+ {
+ _collection = collection ?? throw new ArgumentNullException(nameof(collection));
+ }
+
+ [DebuggerBrowsable(DebuggerBrowsableState.RootHidden)]
+ public T[] Items
+ {
+ get
+ {
+ T[] items = new T[_collection.Count];
+ _collection.CopyTo(items, 0);
+ return items;
+ }
+ }
+ }
+}
diff --git a/src/Avalonia.Base/Collections/Pooled/IReadOnlyPooledList.cs b/src/Avalonia.Base/Collections/Pooled/IReadOnlyPooledList.cs
new file mode 100644
index 00000000000..1a947db4b73
--- /dev/null
+++ b/src/Avalonia.Base/Collections/Pooled/IReadOnlyPooledList.cs
@@ -0,0 +1,18 @@
+using System;
+using System.Collections.Generic;
+
+namespace Avalonia.Collections.Pooled
+{
+ ///
+ /// Represents a read-only collection of pooled elements that can be accessed by index
+ ///
+ /// The type of elements in the read-only pooled list.
+
+ public interface IReadOnlyPooledList : IReadOnlyList
+ {
+ ///
+ /// Gets a for the items currently in the collection.
+ ///
+ ReadOnlySpan Span { get; }
+ }
+}
diff --git a/src/Avalonia.Base/Collections/Pooled/PooledList.cs b/src/Avalonia.Base/Collections/Pooled/PooledList.cs
new file mode 100644
index 00000000000..f0d6b292cc1
--- /dev/null
+++ b/src/Avalonia.Base/Collections/Pooled/PooledList.cs
@@ -0,0 +1,1531 @@
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the MIT license.
+// See the LICENSE file in the project root for more information.
+
+using System;
+using System.Buffers;
+using System.Collections;
+using System.Collections.Generic;
+using System.Collections.ObjectModel;
+using System.Diagnostics;
+using System.Runtime.CompilerServices;
+using System.Runtime.Serialization;
+using System.Threading;
+
+namespace Avalonia.Collections.Pooled
+{
+ ///
+ /// Implements a variable-size list that uses a pooled array to store the
+ /// elements. A PooledList has a capacity, which is the allocated length
+ /// of the internal array. As elements are added to a PooledList, the capacity
+ /// of the PooledList is automatically increased as required by reallocating the
+ /// internal array.
+ ///
+ ///
+ /// This class is based on the code for but it supports
+ /// and uses when allocating internal arrays.
+ ///
+ [DebuggerDisplay("Count = {Count}")]
+ [DebuggerTypeProxy(typeof(ICollectionDebugView<>))]
+ [Serializable]
+ public class PooledList : IList, IReadOnlyPooledList, IList, IDisposable, IDeserializationCallback
+ {
+ // internal constant copied from Array.MaxArrayLength
+ private const int MaxArrayLength = 0x7FEFFFFF;
+ private const int DefaultCapacity = 4;
+ private static readonly T[] s_emptyArray = Array.Empty();
+
+ [NonSerialized]
+ private ArrayPool _pool;
+ [NonSerialized]
+ private object _syncRoot;
+
+ private T[] _items; // Do not rename (binary serialization)
+ private int _size; // Do not rename (binary serialization)
+ private int _version; // Do not rename (binary serialization)
+ private readonly bool _clearOnFree;
+
+ #region Constructors
+
+ ///
+ /// Constructs a PooledList. The list is initially empty and has a capacity
+ /// of zero. Upon adding the first element to the list the capacity is
+ /// increased to DefaultCapacity, and then increased in multiples of two
+ /// as required.
+ ///
+ public PooledList() : this(ClearMode.Auto, ArrayPool.Shared) { }
+
+ ///
+ /// Constructs a PooledList. The list is initially empty and has a capacity
+ /// of zero. Upon adding the first element to the list the capacity is
+ /// increased to DefaultCapacity, and then increased in multiples of two
+ /// as required.
+ ///
+ public PooledList(ClearMode clearMode) : this(clearMode, ArrayPool.Shared) { }
+
+ ///
+ /// Constructs a PooledList. The list is initially empty and has a capacity
+ /// of zero. Upon adding the first element to the list the capacity is
+ /// increased to DefaultCapacity, and then increased in multiples of two
+ /// as required.
+ ///
+ public PooledList(ArrayPool customPool) : this(ClearMode.Auto, customPool) { }
+
+ ///
+ /// Constructs a PooledList. The list is initially empty and has a capacity
+ /// of zero. Upon adding the first element to the list the capacity is
+ /// increased to DefaultCapacity, and then increased in multiples of two
+ /// as required.
+ ///
+ public PooledList(ClearMode clearMode, ArrayPool customPool)
+ {
+ _items = s_emptyArray;
+ _pool = customPool ?? ArrayPool.Shared;
+ _clearOnFree = ShouldClear(clearMode);
+ }
+
+ ///
+ /// Constructs a List with a given initial capacity. The list is
+ /// initially empty, but will have room for the given number of elements
+ /// before any reallocations are required.
+ ///
+ public PooledList(int capacity) : this(capacity, ClearMode.Auto, ArrayPool.Shared) { }
+
+ ///
+ /// Constructs a List with a given initial capacity. The list is
+ /// initially empty, but will have room for the given number of elements
+ /// before any reallocations are required.
+ ///
+ public PooledList(int capacity, bool sizeToCapacity) : this(capacity, ClearMode.Auto, ArrayPool.Shared, sizeToCapacity) { }
+
+ ///
+ /// Constructs a List with a given initial capacity. The list is
+ /// initially empty, but will have room for the given number of elements
+ /// before any reallocations are required.
+ ///
+ public PooledList(int capacity, ClearMode clearMode) : this(capacity, clearMode, ArrayPool.Shared) { }
+
+ ///
+ /// Constructs a List with a given initial capacity. The list is
+ /// initially empty, but will have room for the given number of elements
+ /// before any reallocations are required.
+ ///
+ public PooledList(int capacity, ClearMode clearMode, bool sizeToCapacity) : this(capacity, clearMode, ArrayPool.Shared, sizeToCapacity) { }
+
+ ///
+ /// Constructs a List with a given initial capacity. The list is
+ /// initially empty, but will have room for the given number of elements
+ /// before any reallocations are required.
+ ///
+ public PooledList(int capacity, ArrayPool customPool) : this(capacity, ClearMode.Auto, customPool) { }
+
+ ///
+ /// Constructs a List with a given initial capacity. The list is
+ /// initially empty, but will have room for the given number of elements
+ /// before any reallocations are required.
+ ///
+ public PooledList(int capacity, ArrayPool customPool, bool sizeToCapacity) : this(capacity, ClearMode.Auto, customPool, sizeToCapacity) { }
+
+ ///
+ /// Constructs a List with a given initial capacity. The list is
+ /// initially empty, but will have room for the given number of elements
+ /// before any reallocations are required.
+ ///
+ public PooledList(int capacity, ClearMode clearMode, ArrayPool customPool) : this(capacity, clearMode, customPool, false) { }
+
+ ///
+ /// Constructs a List with a given initial capacity. The list is
+ /// initially empty, but will have room for the given number of elements
+ /// before any reallocations are required.
+ ///
+ /// If true, Count of list equals capacity. Depending on ClearMode, rented items may or may not hold dirty values.
+ public PooledList(int capacity, ClearMode clearMode, ArrayPool customPool, bool sizeToCapacity)
+ {
+ if (capacity < 0)
+ ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.capacity, ExceptionResource.ArgumentOutOfRange_NeedNonNegNum);
+
+ _pool = customPool ?? ArrayPool.Shared;
+ _clearOnFree = ShouldClear(clearMode);
+
+ if (capacity == 0)
+ {
+ _items = s_emptyArray;
+ }
+ else
+ {
+ _items = _pool.Rent(capacity);
+ }
+
+ if (sizeToCapacity)
+ {
+ _size = capacity;
+ if (clearMode != ClearMode.Never)
+ {
+ Array.Clear(_items, 0, _size);
+ }
+ }
+ }
+
+ ///
+ /// Constructs a PooledList, copying the contents of the given collection. The
+ /// size and capacity of the new list will both be equal to the size of the
+ /// given collection.
+ ///
+ public PooledList(T[] array) : this(array.AsSpan(), ClearMode.Auto, ArrayPool.Shared) { }
+
+ ///
+ /// Constructs a PooledList, copying the contents of the given collection. The
+ /// size and capacity of the new list will both be equal to the size of the
+ /// given collection.
+ ///
+ public PooledList(T[] array, ClearMode clearMode) : this(array.AsSpan(), clearMode, ArrayPool.Shared) { }
+
+ ///
+ /// Constructs a PooledList, copying the contents of the given collection. The
+ /// size and capacity of the new list will both be equal to the size of the
+ /// given collection.
+ ///
+ public PooledList(T[] array, ArrayPool customPool) : this(array.AsSpan(), ClearMode.Auto, customPool) { }
+
+ ///
+ /// Constructs a PooledList, copying the contents of the given collection. The
+ /// size and capacity of the new list will both be equal to the size of the
+ /// given collection.
+ ///
+ public PooledList(T[] array, ClearMode clearMode, ArrayPool customPool) : this(array.AsSpan(), clearMode, customPool) { }
+
+ ///
+ /// Constructs a PooledList, copying the contents of the given collection. The
+ /// size and capacity of the new list will both be equal to the size of the
+ /// given collection.
+ ///
+ public PooledList(ReadOnlySpan span) : this(span, ClearMode.Auto, ArrayPool.Shared) { }
+
+ ///
+ /// Constructs a PooledList, copying the contents of the given collection. The
+ /// size and capacity of the new list will both be equal to the size of the
+ /// given collection.
+ ///
+ public PooledList(ReadOnlySpan span, ClearMode clearMode) : this(span, clearMode, ArrayPool.Shared) { }
+
+ ///
+ /// Constructs a PooledList, copying the contents of the given collection. The
+ /// size and capacity of the new list will both be equal to the size of the
+ /// given collection.
+ ///
+ public PooledList(ReadOnlySpan span, ArrayPool customPool) : this(span, ClearMode.Auto, customPool) { }
+
+ ///
+ /// Constructs a PooledList, copying the contents of the given collection. The
+ /// size and capacity of the new list will both be equal to the size of the
+ /// given collection.
+ ///
+ public PooledList(ReadOnlySpan span, ClearMode clearMode, ArrayPool customPool)
+ {
+ _pool = customPool ?? ArrayPool.Shared;
+ _clearOnFree = ShouldClear(clearMode);
+
+ int count = span.Length;
+ if (count == 0)
+ {
+ _items = s_emptyArray;
+ }
+ else
+ {
+ _items = _pool.Rent(count);
+ span.CopyTo(_items);
+ _size = count;
+ }
+ }
+
+ ///
+ /// Constructs a PooledList, copying the contents of the given collection. The
+ /// size and capacity of the new list will both be equal to the size of the
+ /// given collection.
+ ///
+ public PooledList(IEnumerable collection) : this(collection, ClearMode.Auto, ArrayPool.Shared) { }
+
+ ///
+ /// Constructs a PooledList, copying the contents of the given collection. The
+ /// size and capacity of the new list will both be equal to the size of the
+ /// given collection.
+ ///
+ public PooledList(IEnumerable collection, ClearMode clearMode) : this(collection, clearMode, ArrayPool.Shared) { }
+
+ ///
+ /// Constructs a PooledList, copying the contents of the given collection. The
+ /// size and capacity of the new list will both be equal to the size of the
+ /// given collection.
+ ///
+ public PooledList(IEnumerable collection, ArrayPool customPool) : this(collection, ClearMode.Auto, customPool) { }
+
+ ///
+ /// Constructs a PooledList, copying the contents of the given collection. The
+ /// size and capacity of the new list will both be equal to the size of the
+ /// given collection.
+ ///
+ public PooledList(IEnumerable collection, ClearMode clearMode, ArrayPool customPool)
+ {
+ _pool = customPool ?? ArrayPool.Shared;
+ _clearOnFree = ShouldClear(clearMode);
+
+ switch (collection)
+ {
+ case null:
+ ThrowHelper.ThrowArgumentNullException(ExceptionArgument.collection);
+ break;
+
+ case ICollection c:
+ int count = c.Count;
+ if (count == 0)
+ {
+ _items = s_emptyArray;
+ }
+ else
+ {
+ _items = _pool.Rent(count);
+ c.CopyTo(_items, 0);
+ _size = count;
+ }
+ break;
+
+ default:
+ _size = 0;
+ _items = s_emptyArray;
+ using (var en = collection.GetEnumerator())
+ {
+ while (en.MoveNext())
+ Add(en.Current);
+ }
+ break;
+ }
+ }
+
+ #endregion
+
+ ///
+ /// Gets a for the items currently in the collection.
+ ///
+ public Span Span => _items.AsSpan(0, _size);
+
+ ///
+ ReadOnlySpan IReadOnlyPooledList.Span => Span;
+
+ ///
+ /// Gets and sets the capacity of this list. The capacity is the size of
+ /// the internal array used to hold items. When set, the internal
+ /// Memory of the list is reallocated to the given capacity.
+ /// Note that the return value for this property may be larger than the property was set to.
+ ///
+ public int Capacity
+ {
+ get => _items.Length;
+ set
+ {
+ if (value < _size)
+ {
+ ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.value, ExceptionResource.ArgumentOutOfRange_SmallCapacity);
+ }
+
+ if (value != _items.Length)
+ {
+ if (value > 0)
+ {
+ var newItems = _pool.Rent(value);
+ if (_size > 0)
+ {
+ Array.Copy(_items, newItems, _size);
+ }
+ ReturnArray();
+ _items = newItems;
+ }
+ else
+ {
+ ReturnArray();
+ _size = 0;
+ }
+ }
+ }
+ }
+
+ ///
+ /// Read-only property describing how many elements are in the List.
+ ///
+ public int Count => _size;
+
+ ///
+ /// Returns the ClearMode behavior for the collection, denoting whether values are
+ /// cleared from internal arrays before returning them to the pool.
+ ///
+ public ClearMode ClearMode => _clearOnFree ? ClearMode.Always : ClearMode.Never;
+
+ bool IList.IsFixedSize => false;
+
+ bool ICollection.IsReadOnly => false;
+
+ bool IList.IsReadOnly => false;
+
+ int ICollection.Count => _size;
+
+ bool ICollection.IsSynchronized => false;
+
+ // Synchronization root for this object.
+ object ICollection.SyncRoot
+ {
+ get
+ {
+ if (_syncRoot == null)
+ {
+ Interlocked.CompareExchange(ref _syncRoot, new object(), null);
+ }
+ return _syncRoot;
+ }
+ }
+
+ ///
+ /// Gets or sets the element at the given index.
+ ///
+ public T this[int index]
+ {
+ get
+ {
+ // Following trick can reduce the range check by one
+ if ((uint)index >= (uint)_size)
+ {
+ ThrowHelper.ThrowArgumentOutOfRange_IndexException();
+ }
+ return _items[index];
+ }
+
+ set
+ {
+ if ((uint)index >= (uint)_size)
+ {
+ ThrowHelper.ThrowArgumentOutOfRange_IndexException();
+ }
+ _items[index] = value;
+ _version++;
+ }
+ }
+
+ private static bool IsCompatibleObject(object value)
+ {
+ // Non-null values are fine. Only accept nulls if T is a class or Nullable.
+ // Note that default(T) is not equal to null for value types except when T is Nullable.
+ return ((value is T) || (value == null && default(T) == null));
+ }
+
+ object IList.this[int index]
+ {
+ get
+ {
+ return this[index];
+ }
+ set
+ {
+ ThrowHelper.IfNullAndNullsAreIllegalThenThrow(value, ExceptionArgument.value);
+
+ try
+ {
+ this[index] = (T)value;
+ }
+ catch (InvalidCastException)
+ {
+ ThrowHelper.ThrowWrongValueTypeArgumentException(value, typeof(T));
+ }
+ }
+ }
+
+ ///
+ /// Adds the given object to the end of this list. The size of the list is
+ /// increased by one. If required, the capacity of the list is doubled
+ /// before adding the new element.
+ ///
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ public void Add(T item)
+ {
+ _version++;
+ int size = _size;
+ if ((uint)size < (uint)_items.Length)
+ {
+ _size = size + 1;
+ _items[size] = item;
+ }
+ else
+ {
+ AddWithResize(item);
+ }
+ }
+
+ // Non-inline from List.Add to improve its code quality as uncommon path
+ [MethodImpl(MethodImplOptions.NoInlining)]
+ private void AddWithResize(T item)
+ {
+ int size = _size;
+ EnsureCapacity(size + 1);
+ _size = size + 1;
+ _items[size] = item;
+ }
+
+ int IList.Add(object item)
+ {
+ ThrowHelper.IfNullAndNullsAreIllegalThenThrow(item, ExceptionArgument.item);
+
+ try
+ {
+ Add((T)item);
+ }
+ catch (InvalidCastException)
+ {
+ ThrowHelper.ThrowWrongValueTypeArgumentException(item, typeof(T));
+ }
+
+ return Count - 1;
+ }
+
+ ///
+ /// Adds the elements of the given collection to the end of this list. If
+ /// required, the capacity of the list is increased to twice the previous
+ /// capacity or the new size, whichever is larger.
+ ///
+ public void AddRange(IEnumerable collection)
+ => InsertRange(_size, collection);
+
+ ///
+ /// Adds the elements of the given array to the end of this list. If
+ /// required, the capacity of the list is increased to twice the previous
+ /// capacity or the new size, whichever is larger.
+ ///
+ public void AddRange(T[] array)
+ => AddRange(array.AsSpan());
+
+ ///
+ /// Adds the elements of the given to the end of this list. If
+ /// required, the capacity of the list is increased to twice the previous
+ /// capacity or the new size, whichever is larger.
+ ///
+ public void AddRange(ReadOnlySpan span)
+ {
+ var newSpan = InsertSpan(_size, span.Length, false);
+ span.CopyTo(newSpan);
+ }
+
+ ///
+ /// Advances the by the number of items specified,
+ /// increasing the capacity if required, then returns a Span representing
+ /// the set of items to be added, allowing direct writes to that section
+ /// of the collection.
+ ///
+ /// The number of items to add.
+ public Span AddSpan(int count)
+ => InsertSpan(_size, count);
+
+ public ReadOnlyCollection AsReadOnly()
+ => new ReadOnlyCollection(this);
+
+ ///
+ /// Searches a section of the list for a given element using a binary search
+ /// algorithm.
+ ///
+ ///
+ /// Elements of the list are compared to the search value using
+ /// the given IComparer interface. If comparer is null, elements of
+ /// the list are compared to the search value using the IComparable
+ /// interface, which in that case must be implemented by all elements of the
+ /// list and the given search value. This method assumes that the given
+ /// section of the list is already sorted; if this is not the case, the
+ /// result will be incorrect.
+ ///
+ /// The method returns the index of the given value in the list. If the
+ /// list does not contain the given value, the method returns a negative
+ /// integer. The bitwise complement operator (~) can be applied to a
+ /// negative result to produce the index of the first element (if any) that
+ /// is larger than the given search value. This is also the index at which
+ /// the search value should be inserted into the list in order for the list
+ /// to remain sorted.
+ ///
+ public int BinarySearch(int index, int count, T item, IComparer comparer)
+ {
+ if (index < 0)
+ ThrowHelper.ThrowIndexArgumentOutOfRange_NeedNonNegNumException();
+ if (count < 0)
+ ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.count, ExceptionResource.ArgumentOutOfRange_NeedNonNegNum);
+ if (_size - index < count)
+ ThrowHelper.ThrowArgumentException(ExceptionResource.Argument_InvalidOffLen);
+
+ return Array.BinarySearch(_items, index, count, item, comparer);
+ }
+
+ ///
+ /// Searches the list for a given element using a binary search
+ /// algorithm. If the item implements
+ /// then that is used for comparison, otherwise is used.
+ ///
+ public int BinarySearch(T item)
+ => BinarySearch(0, Count, item, null);
+
+ ///
+ /// Searches the list for a given element using a binary search
+ /// algorithm. If the item implements
+ /// then that is used for comparison, otherwise is used.
+ ///
+ public int BinarySearch(T item, IComparer comparer)
+ => BinarySearch(0, Count, item, comparer);
+
+ ///
+ /// Clears the contents of the PooledList.
+ ///
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ public void Clear()
+ {
+ _version++;
+ int size = _size;
+ _size = 0;
+
+ if (size > 0 && _clearOnFree)
+ {
+ // Clear the elements so that the gc can reclaim the references.
+ Array.Clear(_items, 0, _size);
+ }
+ }
+
+ ///
+ /// Contains returns true if the specified element is in the List.
+ /// It does a linear, O(n) search. Equality is determined by calling
+ /// EqualityComparer{T}.Default.Equals.
+ ///
+ public bool Contains(T item)
+ {
+ // PERF: IndexOf calls Array.IndexOf, which internally
+ // calls EqualityComparer.Default.IndexOf, which
+ // is specialized for different types. This
+ // boosts performance since instead of making a
+ // virtual method call each iteration of the loop,
+ // via EqualityComparer.Default.Equals, we
+ // only make one virtual call to EqualityComparer.IndexOf.
+
+ return _size != 0 && IndexOf(item) != -1;
+ }
+
+ bool IList.Contains(object item)
+ {
+ if (IsCompatibleObject(item))
+ {
+ return Contains((T)item);
+ }
+ return false;
+ }
+
+ public PooledList ConvertAll(Func converter)
+ {
+ if (converter == null)
+ {
+ ThrowHelper.ThrowArgumentNullException(ExceptionArgument.converter);
+ }
+
+ var list = new PooledList(_size);
+ for (int i = 0; i < _size; i++)
+ {
+ list._items[i] = converter(_items[i]);
+ }
+ list._size = _size;
+ return list;
+ }
+
+ ///
+ /// Copies this list to the given span.
+ ///
+ public void CopyTo(Span span)
+ {
+ if (span.Length < Count)
+ throw new ArgumentException("Destination span is shorter than the list to be copied.");
+
+ Span.CopyTo(span);
+ }
+
+ void ICollection.CopyTo(T[] array, int arrayIndex)
+ {
+ Array.Copy(_items, 0, array, arrayIndex, _size);
+ }
+
+ // Copies this List into array, which must be of a
+ // compatible array type.
+ void ICollection.CopyTo(Array array, int arrayIndex)
+ {
+ if ((array != null) && (array.Rank != 1))
+ {
+ ThrowHelper.ThrowArgumentException(ExceptionResource.Arg_RankMultiDimNotSupported);
+ }
+
+ try
+ {
+ // Array.Copy will check for NULL.
+ Array.Copy(_items, 0, array, arrayIndex, _size);
+ }
+ catch (ArrayTypeMismatchException)
+ {
+ ThrowHelper.ThrowArgumentException_Argument_InvalidArrayType();
+ }
+ }
+
+ ///
+ /// Ensures that the capacity of this list is at least the given minimum
+ /// value. If the current capacity of the list is less than min, the
+ /// capacity is increased to twice the current capacity or to min,
+ /// whichever is larger.
+ ///
+ private void EnsureCapacity(int min)
+ {
+ if (_items.Length < min)
+ {
+ int newCapacity = _items.Length == 0 ? DefaultCapacity : _items.Length * 2;
+ // Allow the list to grow to maximum possible capacity (~2G elements) before encountering overflow.
+ // Note that this check works even when _items.Length overflowed thanks to the (uint) cast
+ if ((uint)newCapacity > MaxArrayLength)
+ newCapacity = MaxArrayLength;
+ if (newCapacity < min)
+ newCapacity = min;
+ Capacity = newCapacity;
+ }
+ }
+
+ public bool Exists(Func match)
+ => FindIndex(match) != -1;
+
+ public bool TryFind(Func match, out T result)
+ {
+ if (match == null)
+ ThrowHelper.ThrowArgumentNullException(ExceptionArgument.match);
+
+ for (int i = 0; i < _size; i++)
+ {
+ if (match(_items[i]))
+ {
+ result = _items[i];
+ return true;
+ }
+ }
+
+ result = default;
+ return false;
+ }
+
+ public PooledList FindAll(Func match)
+ {
+ if (match == null)
+ ThrowHelper.ThrowArgumentNullException(ExceptionArgument.match);
+
+ var list = new PooledList();
+ for (int i = 0; i < _size; i++)
+ {
+ if (match(_items[i]))
+ {
+ list.Add(_items[i]);
+ }
+ }
+ return list;
+ }
+
+ public int FindIndex(Func match)
+ => FindIndex(0, _size, match);
+
+ public int FindIndex(int startIndex, Func match)
+ => FindIndex(startIndex, _size - startIndex, match);
+
+ public int FindIndex(int startIndex, int count, Func match)
+ {
+ if ((uint)startIndex > (uint)_size)
+ ThrowHelper.ThrowStartIndexArgumentOutOfRange_ArgumentOutOfRange_Index();
+
+ if (count < 0 || startIndex > _size - count)
+ ThrowHelper.ThrowCountArgumentOutOfRange_ArgumentOutOfRange_Count();
+
+ if (match is null)
+ ThrowHelper.ThrowArgumentNullException(ExceptionArgument.match);
+
+ int endIndex = startIndex + count;
+ for (int i = startIndex; i < endIndex; i++)
+ {
+ if (match(_items[i]))
+ return i;
+ }
+ return -1;
+ }
+
+ public bool TryFindLast(Func match, out T result)
+ {
+ if (match is null)
+ {
+ ThrowHelper.ThrowArgumentNullException(ExceptionArgument.match);
+ }
+
+ for (int i = _size - 1; i >= 0; i--)
+ {
+ if (match(_items[i]))
+ {
+ result = _items[i];
+ return true;
+ }
+ }
+
+ result = default;
+ return false;
+ }
+
+ public int FindLastIndex(Func match)
+ => FindLastIndex(_size - 1, _size, match);
+
+ public int FindLastIndex(int startIndex, Func match)
+ => FindLastIndex(startIndex, startIndex + 1, match);
+
+ public int FindLastIndex(int startIndex, int count, Func match)
+ {
+ if (match == null)
+ {
+ ThrowHelper.ThrowArgumentNullException(ExceptionArgument.match);
+ }
+
+ if (_size == 0)
+ {
+ // Special case for 0 length List
+ if (startIndex != -1)
+ {
+ ThrowHelper.ThrowStartIndexArgumentOutOfRange_ArgumentOutOfRange_Index();
+ }
+ }
+ else
+ {
+ // Make sure we're not out of range
+ if ((uint)startIndex >= (uint)_size)
+ {
+ ThrowHelper.ThrowStartIndexArgumentOutOfRange_ArgumentOutOfRange_Index();
+ }
+ }
+
+ // 2nd half of this also catches when startIndex == MAXINT, so MAXINT - 0 + 1 == -1, which is < 0.
+ if (count < 0 || startIndex - count + 1 < 0)
+ {
+ ThrowHelper.ThrowCountArgumentOutOfRange_ArgumentOutOfRange_Count();
+ }
+
+ int endIndex = startIndex - count;
+ for (int i = startIndex; i > endIndex; i--)
+ {
+ if (match(_items[i]))
+ {
+ return i;
+ }
+ }
+ return -1;
+ }
+
+ public void ForEach(Action action)
+ {
+ if (action == null)
+ {
+ ThrowHelper.ThrowArgumentNullException(ExceptionArgument.action);
+ }
+
+ int version = _version;
+ for (int i = 0; i < _size; i++)
+ {
+ if (version != _version)
+ {
+ break;
+ }
+ action(_items[i]);
+ }
+
+ if (version != _version)
+ ThrowHelper.ThrowInvalidOperationException_InvalidOperation_EnumFailedVersion();
+ }
+
+ ///
+ /// Returns an enumerator for this list with the given
+ /// permission for removal of elements. If modifications made to the list
+ /// while an enumeration is in progress, the MoveNext and
+ /// GetObject methods of the enumerator will throw an exception.
+ ///
+ public Enumerator GetEnumerator()
+ => new Enumerator(this);
+
+ IEnumerator IEnumerable.GetEnumerator()
+ => new Enumerator(this);
+
+ IEnumerator IEnumerable.GetEnumerator()
+ => new Enumerator(this);
+
+ ///
+ /// Equivalent to PooledList.Span.Slice(index, count).
+ ///
+ public Span GetRange(int index, int count)
+ {
+ if (index < 0)
+ {
+ ThrowHelper.ThrowIndexArgumentOutOfRange_NeedNonNegNumException();
+ }
+
+ if (count < 0)
+ {
+ ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.count, ExceptionResource.ArgumentOutOfRange_NeedNonNegNum);
+ }
+
+ if (_size - index < count)
+ {
+ ThrowHelper.ThrowArgumentException(ExceptionResource.Argument_InvalidOffLen);
+ }
+
+ return Span.Slice(index, count);
+ }
+
+ ///
+ /// Returns the index of the first occurrence of a given value in
+ /// this list. The list is searched forwards from beginning to end.
+ ///
+ public int IndexOf(T item)
+ => Array.IndexOf(_items, item, 0, _size);
+
+ int IList.IndexOf(object item)
+ {
+ if (IsCompatibleObject(item))
+ {
+ return IndexOf((T)item);
+ }
+ return -1;
+ }
+
+ ///
+ /// Returns the index of the first occurrence of a given value in a range of
+ /// this list. The list is searched forwards, starting at index
+ /// index and ending at count number of elements.
+ ///
+ public int IndexOf(T item, int index)
+ {
+ if (index > _size)
+ ThrowHelper.ThrowArgumentOutOfRange_IndexException();
+ return Array.IndexOf(_items, item, index, _size - index);
+ }
+
+ ///
+ /// Returns the index of the first occurrence of a given value in a range of
+ /// this list. The list is searched forwards, starting at index
+ /// index and upto count number of elements.
+ ///
+ public int IndexOf(T item, int index, int count)
+ {
+ if (index > _size)
+ ThrowHelper.ThrowArgumentOutOfRange_IndexException();
+
+ if (count < 0 || index > _size - count)
+ ThrowHelper.ThrowCountArgumentOutOfRange_ArgumentOutOfRange_Count();
+
+ return Array.IndexOf(_items, item, index, count);
+ }
+
+ ///
+ /// Inserts an element into this list at a given index. The size of the list
+ /// is increased by one. If required, the capacity of the list is doubled
+ /// before inserting the new element.
+ ///
+ public void Insert(int index, T item)
+ {
+ // Note that insertions at the end are legal.
+ if ((uint)index > (uint)_size)
+ {
+ ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.index, ExceptionResource.ArgumentOutOfRange_ListInsert);
+ }
+
+ if (_size == _items.Length)
+ EnsureCapacity(_size + 1);
+ if (index < _size)
+ {
+ Array.Copy(_items, index, _items, index + 1, _size - index);
+ }
+ _items[index] = item;
+ _size++;
+ _version++;
+ }
+
+ void IList.Insert(int index, object item)
+ {
+ ThrowHelper.IfNullAndNullsAreIllegalThenThrow(item, ExceptionArgument.item);
+
+ try
+ {
+ Insert(index, (T)item);
+ }
+ catch (InvalidCastException)
+ {
+ ThrowHelper.ThrowWrongValueTypeArgumentException(item, typeof(T));
+ }
+ }
+
+ ///
+ /// Inserts the elements of the given collection at a given index. If
+ /// required, the capacity of the list is increased to twice the previous
+ /// capacity or the new size, whichever is larger. Ranges may be added
+ /// to the end of the list by setting index to the List's size.
+ ///
+ public void InsertRange(int index, IEnumerable collection)
+ {
+ if ((uint)index > (uint)_size)
+ {
+ ThrowHelper.ThrowArgumentOutOfRange_IndexException();
+ }
+
+ switch (collection)
+ {
+ case null:
+ ThrowHelper.ThrowArgumentNullException(ExceptionArgument.collection);
+ break;
+
+ case ICollection c:
+ int count = c.Count;
+ if (count > 0)
+ {
+ EnsureCapacity(_size + count);
+ if (index < _size)
+ {
+ Array.Copy(_items, index, _items, index + count, _size - index);
+ }
+
+ // If we're inserting a List into itself, we want to be able to deal with that.
+ if (this == c)
+ {
+ // Copy first part of _items to insert location
+ Array.Copy(_items, 0, _items, index, index);
+ // Copy last part of _items back to inserted location
+ Array.Copy(_items, index + count, _items, index * 2, _size - index);
+ }
+ else
+ {
+ c.CopyTo(_items, index);
+ }
+ _size += count;
+ }
+ break;
+
+ default:
+ using (var en = collection.GetEnumerator())
+ {
+ while (en.MoveNext())
+ {
+ Insert(index++, en.Current);
+ }
+ }
+ break;
+ }
+
+ _version++;
+ }
+
+ ///
+ /// Inserts the elements of the given collection at a given index. If
+ /// required, the capacity of the list is increased to twice the previous
+ /// capacity or the new size, whichever is larger. Ranges may be added
+ /// to the end of the list by setting index to the List's size.
+ ///
+ public void InsertRange(int index, ReadOnlySpan span)
+ {
+ var newSpan = InsertSpan(index, span.Length, false);
+ span.CopyTo(newSpan);
+ }
+
+ ///
+ /// Inserts the elements of the given collection at a given index. If
+ /// required, the capacity of the list is increased to twice the previous
+ /// capacity or the new size, whichever is larger. Ranges may be added
+ /// to the end of the list by setting index to the List's size.
+ ///
+ public void InsertRange(int index, T[] array)
+ {
+ if (array is null)
+ ThrowHelper.ThrowArgumentNullException(ExceptionArgument.array);
+ InsertRange(index, array.AsSpan());
+ }
+
+ ///
+ /// Advances the by the number of items specified,
+ /// increasing the capacity if required, then returns a Span representing
+ /// the set of items to be added, allowing direct writes to that section
+ /// of the collection.
+ ///
+ public Span InsertSpan(int index, int count)
+ => InsertSpan(index, count, true);
+
+ private Span InsertSpan(int index, int count, bool clearOutput)
+ {
+ EnsureCapacity(_size + count);
+
+ if (index < _size)
+ {
+ Array.Copy(_items, index, _items, index + count, _size - index);
+ }
+
+ _size += count;
+ _version++;
+
+ var output = _items.AsSpan(index, count);
+
+ if (clearOutput && _clearOnFree)
+ {
+ output.Clear();
+ }
+
+ return output;
+ }
+
+ ///
+ /// Returns the index of the last occurrence of a given value in a range of
+ /// this list. The list is searched backwards, starting at the end
+ /// and ending at the first element in the list.
+ ///
+ public int LastIndexOf(T item)
+ {
+ if (_size == 0)
+ { // Special case for empty list
+ return -1;
+ }
+ else
+ {
+ return LastIndexOf(item, _size - 1, _size);
+ }
+ }
+
+ ///
+ /// Returns the index of the last occurrence of a given value in a range of
+ /// this list. The list is searched backwards, starting at index
+ /// index and ending at the first element in the list.
+ ///
+ public int LastIndexOf(T item, int index)
+ {
+ if (index >= _size)
+ ThrowHelper.ThrowArgumentOutOfRange_IndexException();
+ return LastIndexOf(item, index, index + 1);
+ }
+
+ ///
+ /// Returns the index of the last occurrence of a given value in a range of
+ /// this list. The list is searched backwards, starting at index
+ /// index and upto count elements
+ ///
+ public int LastIndexOf(T item, int index, int count)
+ {
+ if (Count != 0 && index < 0)
+ {
+ ThrowHelper.ThrowIndexArgumentOutOfRange_NeedNonNegNumException();
+ }
+
+ if (Count != 0 && count < 0)
+ {
+ ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.count, ExceptionResource.ArgumentOutOfRange_NeedNonNegNum);
+ }
+
+ if (_size == 0)
+ {
+ // Special case for empty list
+ return -1;
+ }
+
+ if (index >= _size)
+ {
+ ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.index, ExceptionResource.ArgumentOutOfRange_BiggerThanCollection);
+ }
+
+ if (count > index + 1)
+ {
+ ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.count, ExceptionResource.ArgumentOutOfRange_BiggerThanCollection);
+ }
+
+ return Array.LastIndexOf(_items, item, index, count);
+ }
+
+ // Removes the element at the given index. The size of the list is
+ // decreased by one.
+ public bool Remove(T item)
+ {
+ int index = IndexOf(item);
+ if (index >= 0)
+ {
+ RemoveAt(index);
+ return true;
+ }
+
+ return false;
+ }
+
+ void IList.Remove(object item)
+ {
+ if (IsCompatibleObject(item))
+ {
+ Remove((T)item);
+ }
+ }
+
+ ///
+ /// This method removes all items which match the predicate.
+ /// The complexity is O(n).
+ ///
+ public int RemoveAll(Func match)
+ {
+ if (match == null)
+ ThrowHelper.ThrowArgumentNullException(ExceptionArgument.match);
+
+ int freeIndex = 0; // the first free slot in items array
+
+ // Find the first item which needs to be removed.
+ while (freeIndex < _size && !match(_items[freeIndex]))
+ freeIndex++;
+ if (freeIndex >= _size)
+ return 0;
+
+ int current = freeIndex + 1;
+ while (current < _size)
+ {
+ // Find the first item which needs to be kept.
+ while (current < _size && match(_items[current]))
+ current++;
+
+ if (current < _size)
+ {
+ // copy item to the free slot.
+ _items[freeIndex++] = _items[current++];
+ }
+ }
+
+ if (_clearOnFree)
+ {
+ // Clear the removed elements so that the gc can reclaim the references.
+ Array.Clear(_items, freeIndex, _size - freeIndex);
+ }
+
+ int result = _size - freeIndex;
+ _size = freeIndex;
+ _version++;
+ return result;
+ }
+
+ ///
+ /// Removes the element at the given index. The size of the list is
+ /// decreased by one.
+ ///
+ public void RemoveAt(int index)
+ {
+ if ((uint)index >= (uint)_size)
+ ThrowHelper.ThrowArgumentOutOfRange_IndexException();
+
+ _size--;
+ if (index < _size)
+ {
+ Array.Copy(_items, index + 1, _items, index, _size - index);
+ }
+ _version++;
+
+ if (_clearOnFree)
+ {
+ // Clear the removed element so that the gc can reclaim the reference.
+ _items[_size] = default;
+ }
+ }
+
+ ///
+ /// Removes a range of elements from this list.
+ ///
+ public void RemoveRange(int index, int count)
+ {
+ if (index < 0)
+ ThrowHelper.ThrowIndexArgumentOutOfRange_NeedNonNegNumException();
+
+ if (count < 0)
+ ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.count, ExceptionResource.ArgumentOutOfRange_NeedNonNegNum);
+
+ if (_size - index < count)
+ ThrowHelper.ThrowArgumentException(ExceptionResource.Argument_InvalidOffLen);
+
+ if (count > 0)
+ {
+ _size -= count;
+ if (index < _size)
+ {
+ Array.Copy(_items, index + count, _items, index, _size - index);
+ }
+
+ _version++;
+
+ if (_clearOnFree)
+ {
+ // Clear the removed elements so that the gc can reclaim the references.
+ Array.Clear(_items, _size, count);
+ }
+ }
+ }
+
+ ///
+ /// Reverses the elements in this list.
+ ///
+ public void Reverse()
+ => Reverse(0, _size);
+
+ ///
+ /// Reverses the elements in a range of this list. Following a call to this
+ /// method, an element in the range given by index and count
+ /// which was previously located at index i will now be located at
+ /// index index + (index + count - i - 1).
+ ///
+ public void Reverse(int index, int count)
+ {
+ if (index < 0)
+ ThrowHelper.ThrowIndexArgumentOutOfRange_NeedNonNegNumException();
+
+ if (count < 0)
+ ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.count, ExceptionResource.ArgumentOutOfRange_NeedNonNegNum);
+
+ if (_size - index < count)
+ ThrowHelper.ThrowArgumentException(ExceptionResource.Argument_InvalidOffLen);
+
+ if (count > 1)
+ {
+ Array.Reverse(_items, index, count);
+ }
+ _version++;
+ }
+
+ ///
+ /// Sorts the elements in this list. Uses the default comparer and
+ /// Array.Sort.
+ ///
+ public void Sort()
+ => Sort(0, Count, null);
+
+ ///
+ /// Sorts the elements in this list. Uses Array.Sort with the
+ /// provided comparer.
+ ///
+ ///
+ public void Sort(IComparer comparer)
+ => Sort(0, Count, comparer);
+
+ ///
+ /// Sorts the elements in a section of this list. The sort compares the
+ /// elements to each other using the given IComparer interface. If
+ /// comparer is null, the elements are compared to each other using
+ /// the IComparable interface, which in that case must be implemented by all
+ /// elements of the list.
+ ///
+ /// This method uses the Array.Sort method to sort the elements.
+ ///
+ public void Sort(int index, int count, IComparer comparer)
+ {
+ if (index < 0)
+ ThrowHelper.ThrowIndexArgumentOutOfRange_NeedNonNegNumException();
+
+ if (count < 0)
+ ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.count, ExceptionResource.ArgumentOutOfRange_NeedNonNegNum);
+
+ if (_size - index < count)
+ ThrowHelper.ThrowArgumentException(ExceptionResource.Argument_InvalidOffLen);
+
+ if (count > 1)
+ {
+ Array.Sort(_items, index, count, comparer);
+ }
+ _version++;
+ }
+
+ public void Sort(Func comparison)
+ {
+ if (comparison == null)
+ {
+ ThrowHelper.ThrowArgumentNullException(ExceptionArgument.comparison);
+ }
+
+ if (_size > 1)
+ {
+ // List uses ArraySortHelper here but since it's an internal class,
+ // we're creating an IComparer using the comparison function to avoid
+ // duplicating all that code.
+ Array.Sort(_items, 0, _size, new Comparer(comparison));
+ }
+ _version++;
+ }
+
+ ///
+ /// ToArray returns an array containing the contents of the List.
+ /// This requires copying the List, which is an O(n) operation.
+ ///
+ public T[] ToArray()
+ {
+ if (_size == 0)
+ {
+ return s_emptyArray;
+ }
+
+ return Span.ToArray();
+ }
+
+ ///
+ /// Sets the capacity of this list to the size of the list. This method can
+ /// be used to minimize a list's memory overhead once it is known that no
+ /// new elements will be added to the list. To completely clear a list and
+ /// release all memory referenced by the list, execute the following
+ /// statements:
+ ///
+ /// list.Clear();
+ /// list.TrimExcess();
+ ///
+ ///
+ public void TrimExcess()
+ {
+ int threshold = (int)(_items.Length * 0.9);
+ if (_size < threshold)
+ {
+ Capacity = _size;
+ }
+ }
+
+ public bool TrueForAll(Func match)
+ {
+ if (match == null)
+ {
+ ThrowHelper.ThrowArgumentNullException(ExceptionArgument.match);
+ }
+
+ for (int i = 0; i < _size; i++)
+ {
+ if (!match(_items[i]))
+ {
+ return false;
+ }
+ }
+ return true;
+ }
+
+ private void ReturnArray()
+ {
+ if (_items.Length == 0)
+ return;
+
+ try
+ {
+ // Clear the elements so that the gc can reclaim the references.
+ _pool.Return(_items, clearArray: _clearOnFree);
+ }
+ catch (ArgumentException)
+ {
+ // oh well, the array pool didn't like our array
+ }
+
+ _items = s_emptyArray;
+ }
+
+ private static bool ShouldClear(ClearMode mode)
+ {
+#if NETCOREAPP2_1
+ return mode == ClearMode.Always
+ || (mode == ClearMode.Auto && RuntimeHelpers.IsReferenceOrContainsReferences());
+#else
+ return mode != ClearMode.Never;
+#endif
+ }
+
+ ///
+ /// Returns the internal buffers to the ArrayPool.
+ ///
+ public void Dispose()
+ {
+ ReturnArray();
+ _size = 0;
+ _version++;
+ }
+
+ void IDeserializationCallback.OnDeserialization(object sender)
+ {
+ // We can't serialize array pools, so deserialized PooledLists will
+ // have to use the shared pool, even if they were using a custom pool
+ // before serialization.
+ _pool = ArrayPool.Shared;
+ }
+
+ public struct Enumerator : IEnumerator, IEnumerator
+ {
+ private readonly PooledList _list;
+ private int _index;
+ private readonly int _version;
+ private T _current;
+
+ internal Enumerator(PooledList list)
+ {
+ _list = list;
+ _index = 0;
+ _version = list._version;
+ _current = default;
+ }
+
+ public void Dispose()
+ {
+ }
+
+ public bool MoveNext()
+ {
+ var localList = _list;
+
+ if (_version == localList._version && ((uint)_index < (uint)localList._size))
+ {
+ _current = localList._items[_index];
+ _index++;
+ return true;
+ }
+ return MoveNextRare();
+ }
+
+ private bool MoveNextRare()
+ {
+ if (_version != _list._version)
+ {
+ ThrowHelper.ThrowInvalidOperationException_InvalidOperation_EnumFailedVersion();
+ }
+
+ _index = _list._size + 1;
+ _current = default;
+ return false;
+ }
+
+ public T Current => _current;
+
+ object IEnumerator.Current
+ {
+ get
+ {
+ if (_index == 0 || _index == _list._size + 1)
+ {
+ ThrowHelper.ThrowInvalidOperationException_InvalidOperation_EnumOpCantHappen();
+ }
+ return Current;
+ }
+ }
+
+ void IEnumerator.Reset()
+ {
+ if (_version != _list._version)
+ {
+ ThrowHelper.ThrowInvalidOperationException_InvalidOperation_EnumFailedVersion();
+ }
+
+ _index = 0;
+ _current = default;
+ }
+ }
+
+ private readonly struct Comparer : IComparer
+ {
+ private readonly Func _comparison;
+
+ public Comparer(Func comparison)
+ {
+ _comparison = comparison;
+ }
+
+ public int Compare(T x, T y) => _comparison(x, y);
+ }
+ }
+}
diff --git a/src/Avalonia.Base/Collections/Pooled/PooledStack.cs b/src/Avalonia.Base/Collections/Pooled/PooledStack.cs
new file mode 100644
index 00000000000..104a7f97e94
--- /dev/null
+++ b/src/Avalonia.Base/Collections/Pooled/PooledStack.cs
@@ -0,0 +1,699 @@
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the MIT license.
+// See the LICENSE file in the project root for more information.
+
+/*=============================================================================
+**
+**
+** Purpose: An array implementation of a generic stack.
+**
+**
+=============================================================================*/
+
+using System;
+using System.Buffers;
+using System.Collections;
+using System.Collections.Generic;
+using System.Diagnostics;
+using System.Diagnostics.CodeAnalysis;
+using System.Runtime.CompilerServices;
+using System.Runtime.Serialization;
+using System.Threading;
+
+namespace Avalonia.Collections.Pooled
+{
+ ///
+ /// A simple stack of objects. Internally it is implemented as an array,
+ /// so Push can be O(n). Pop is O(1).
+ ///
+ [DebuggerTypeProxy(typeof(StackDebugView<>))]
+ [DebuggerDisplay("Count = {Count}")]
+ [Serializable]
+ public class PooledStack : IEnumerable, ICollection, IReadOnlyCollection, IDisposable, IDeserializationCallback
+ {
+ [NonSerialized]
+ private ArrayPool _pool;
+ [NonSerialized]
+ private object _syncRoot;
+
+ private T[] _array; // Storage for stack elements. Do not rename (binary serialization)
+ private int _size; // Number of items in the stack. Do not rename (binary serialization)
+ private int _version; // Used to keep enumerator in sync w/ collection. Do not rename (binary serialization)
+ private readonly bool _clearOnFree;
+
+ private const int DefaultCapacity = 4;
+
+ #region Constructors
+
+ ///
+ /// Create a stack with the default initial capacity.
+ ///
+ public PooledStack() : this(ClearMode.Auto, ArrayPool.Shared) { }
+
+ ///
+ /// Create a stack with the default initial capacity.
+ ///
+ public PooledStack(ClearMode clearMode) : this(clearMode, ArrayPool.Shared) { }
+
+ ///
+ /// Create a stack with the default initial capacity.
+ ///
+ public PooledStack(ArrayPool customPool) : this(ClearMode.Auto, customPool) { }
+
+ ///
+ /// Create a stack with the default initial capacity and a custom ArrayPool.
+ ///
+ public PooledStack(ClearMode clearMode, ArrayPool customPool)
+ {
+ _pool = customPool ?? ArrayPool.Shared;
+ _array = Array.Empty();
+ _clearOnFree = ShouldClear(clearMode);
+ }
+
+ ///
+ /// Create a stack with a specific initial capacity. The initial capacity
+ /// must be a non-negative number.
+ ///
+ public PooledStack(int capacity) : this(capacity, ClearMode.Auto, ArrayPool.Shared) { }
+
+ ///
+ /// Create a stack with a specific initial capacity. The initial capacity
+ /// must be a non-negative number.
+ ///
+ public PooledStack(int capacity, ClearMode clearMode) : this(capacity, clearMode, ArrayPool.Shared) { }
+
+ ///
+ /// Create a stack with a specific initial capacity. The initial capacity
+ /// must be a non-negative number.
+ ///
+ public PooledStack(int capacity, ArrayPool customPool) : this(capacity, ClearMode.Auto, customPool) { }
+
+ ///
+ /// Create a stack with a specific initial capacity. The initial capacity
+ /// must be a non-negative number.
+ ///
+ public PooledStack(int capacity, ClearMode clearMode, ArrayPool customPool)
+ {
+ if (capacity < 0)
+ {
+ ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.capacity,
+ ExceptionResource.ArgumentOutOfRange_NeedNonNegNum);
+ }
+ _pool = customPool ?? ArrayPool.Shared;
+ _array = _pool.Rent(capacity);
+ _clearOnFree = ShouldClear(clearMode);
+ }
+
+ ///
+ /// Fills a Stack with the contents of a particular collection. The items are
+ /// pushed onto the stack in the same order they are read by the enumerator.
+ ///
+ public PooledStack(IEnumerable enumerable) : this(enumerable, ClearMode.Auto, ArrayPool.Shared) { }
+
+ ///
+ /// Fills a Stack with the contents of a particular collection. The items are
+ /// pushed onto the stack in the same order they are read by the enumerator.
+ ///
+ public PooledStack(IEnumerable enumerable, ClearMode clearMode) : this(enumerable, clearMode, ArrayPool.Shared) { }
+
+ ///
+ /// Fills a Stack with the contents of a particular collection. The items are
+ /// pushed onto the stack in the same order they are read by the enumerator.
+ ///
+ public PooledStack(IEnumerable enumerable, ArrayPool customPool) : this(enumerable, ClearMode.Auto, customPool) { }
+
+ ///
+ /// Fills a Stack with the contents of a particular collection. The items are
+ /// pushed onto the stack in the same order they are read by the enumerator.
+ ///
+ public PooledStack(IEnumerable enumerable, ClearMode clearMode, ArrayPool customPool)
+ {
+ _pool = customPool ?? ArrayPool.Shared;
+ _clearOnFree = ShouldClear(clearMode);
+
+ switch (enumerable)
+ {
+ case null:
+ ThrowHelper.ThrowArgumentNullException(ExceptionArgument.enumerable);
+ break;
+
+ case ICollection collection:
+ if (collection.Count == 0)
+ {
+ _array = Array.Empty();
+ }
+ else
+ {
+ _array = _pool.Rent(collection.Count);
+ collection.CopyTo(_array, 0);
+ _size = collection.Count;
+ }
+ break;
+
+ default:
+ using (var list = new PooledList(enumerable))
+ {
+ _array = _pool.Rent(list.Count);
+ list.Span.CopyTo(_array);
+ _size = list.Count;
+ }
+ break;
+ }
+ }
+
+ ///
+ /// Fills a Stack with the contents of a particular collection. The items are
+ /// pushed onto the stack in the same order they are read by the enumerator.
+ ///
+ public PooledStack(T[] array) : this(array.AsSpan(), ClearMode.Auto, ArrayPool.Shared) { }
+
+ ///
+ /// Fills a Stack with the contents of a particular collection. The items are
+ /// pushed onto the stack in the same order they are read by the enumerator.
+ ///
+ public PooledStack(T[] array, ClearMode clearMode) : this(array.AsSpan(), clearMode, ArrayPool.Shared) { }
+
+ ///
+ /// Fills a Stack with the contents of a particular collection. The items are
+ /// pushed onto the stack in the same order they are read by the enumerator.
+ ///
+ public PooledStack(T[] array, ArrayPool customPool) : this(array.AsSpan(), ClearMode.Auto, customPool) { }
+
+ ///
+ /// Fills a Stack with the contents of a particular collection. The items are
+ /// pushed onto the stack in the same order they are read by the enumerator.
+ ///
+ public PooledStack(T[] array, ClearMode clearMode, ArrayPool customPool) : this(array.AsSpan(), clearMode, customPool) { }
+
+ ///
+ /// Fills a Stack with the contents of a particular collection. The items are
+ /// pushed onto the stack in the same order they are read by the enumerator.
+ ///
+ public PooledStack(ReadOnlySpan span) : this(span, ClearMode.Auto, ArrayPool.Shared) { }
+
+ ///
+ /// Fills a Stack with the contents of a particular collection. The items are
+ /// pushed onto the stack in the same order they are read by the enumerator.
+ ///
+ public PooledStack(ReadOnlySpan span, ClearMode clearMode) : this(span, clearMode, ArrayPool.Shared) { }
+
+ ///
+ /// Fills a Stack with the contents of a particular collection. The items are
+ /// pushed onto the stack in the same order they are read by the enumerator.
+ ///
+ public PooledStack(ReadOnlySpan span, ArrayPool customPool) : this(span, ClearMode.Auto, customPool) { }
+
+ ///
+ /// Fills a Stack with the contents of a particular collection. The items are
+ /// pushed onto the stack in the same order they are read by the enumerator.
+ ///
+ public PooledStack(ReadOnlySpan span, ClearMode clearMode, ArrayPool customPool)
+ {
+ _pool = customPool ?? ArrayPool.Shared;
+ _clearOnFree = ShouldClear(clearMode);
+ _array = _pool.Rent(span.Length);
+ span.CopyTo(_array);
+ _size = span.Length;
+ }
+
+ #endregion
+
+ ///
+ /// The number of items in the stack.
+ ///
+ public int Count => _size;
+
+ ///
+ /// Returns the ClearMode behavior for the collection, denoting whether values are
+ /// cleared from internal arrays before returning them to the pool.
+ ///
+ public ClearMode ClearMode => _clearOnFree ? ClearMode.Always : ClearMode.Never;
+
+ bool ICollection.IsSynchronized => false;
+
+ object ICollection.SyncRoot
+ {
+ get
+ {
+ if (_syncRoot == null)
+ {
+ Interlocked.CompareExchange(ref _syncRoot, new object(), null);
+ }
+ return _syncRoot;
+ }
+ }
+
+ ///
+ /// Removes all Objects from the Stack.
+ ///
+ public void Clear()
+ {
+ if (_clearOnFree)
+ {
+ Array.Clear(_array, 0, _size); // clear the elements so that the gc can reclaim the references.
+ }
+ _size = 0;
+ _version++;
+ }
+
+ ///
+ /// Compares items using the default equality comparer
+ ///
+ public bool Contains(T item)
+ {
+ // PERF: Internally Array.LastIndexOf calls
+ // EqualityComparer.Default.LastIndexOf, which
+ // is specialized for different types. This
+ // boosts performance since instead of making a
+ // virtual method call each iteration of the loop,
+ // via EqualityComparer.Default.Equals, we
+ // only make one virtual call to EqualityComparer.LastIndexOf.
+
+ return _size != 0 && Array.LastIndexOf(_array, item, _size - 1) != -1;
+ }
+
+ ///
+ /// This method removes all items which match the predicate.
+ /// The complexity is O(n).
+ ///
+ public int RemoveWhere(Func match)
+ {
+ if (match == null)
+ ThrowHelper.ThrowArgumentNullException(ExceptionArgument.match);
+
+ int freeIndex = 0; // the first free slot in items array
+
+ // Find the first item which needs to be removed.
+ while (freeIndex < _size && !match(_array[freeIndex]))
+ freeIndex++;
+ if (freeIndex >= _size)
+ return 0;
+
+ int current = freeIndex + 1;
+ while (current < _size)
+ {
+ // Find the first item which needs to be kept.
+ while (current < _size && match(_array[current]))
+ current++;
+
+ if (current < _size)
+ {
+ // copy item to the free slot.
+ _array[freeIndex++] = _array[current++];
+ }
+ }
+
+ if (_clearOnFree)
+ {
+ // Clear the removed elements so that the gc can reclaim the references.
+ Array.Clear(_array, freeIndex, _size - freeIndex);
+ }
+
+ int result = _size - freeIndex;
+ _size = freeIndex;
+ _version++;
+ return result;
+ }
+
+ // Copies the stack into an array.
+ public void CopyTo(T[] array, int arrayIndex)
+ {
+ if (array == null)
+ {
+ ThrowHelper.ThrowArgumentNullException(ExceptionArgument.array);
+ }
+
+ if (arrayIndex < 0 || arrayIndex > array.Length)
+ {
+ ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.arrayIndex);
+ }
+
+ if (array.Length - arrayIndex < _size)
+ {
+ ThrowHelper.ThrowArgumentException(ExceptionResource.Arg_ArrayPlusOffTooSmall);
+ }
+
+ Debug.Assert(array != _array);
+ int srcIndex = 0;
+ int dstIndex = arrayIndex + _size;
+ while (srcIndex < _size)
+ {
+ array[--dstIndex] = _array[srcIndex++];
+ }
+ }
+
+ public void CopyTo(Span span)
+ {
+ if (span.Length < _size)
+ {
+ ThrowHelper.ThrowArgumentException_DestinationTooShort();
+ }
+
+ int srcIndex = 0;
+ int dstIndex = _size;
+ while (srcIndex < _size)
+ {
+ span[--dstIndex] = _array[srcIndex++];
+ }
+ }
+
+ void ICollection.CopyTo(Array array, int arrayIndex)
+ {
+ if (array == null)
+ {
+ ThrowHelper.ThrowArgumentNullException(ExceptionArgument.array);
+ }
+
+ if (array.Rank != 1)
+ {
+ ThrowHelper.ThrowArgumentException(ExceptionResource.Arg_RankMultiDimNotSupported);
+ }
+
+ if (array.GetLowerBound(0) != 0)
+ {
+ ThrowHelper.ThrowArgumentException(ExceptionResource.Arg_NonZeroLowerBound, ExceptionArgument.array);
+ }
+
+ if (arrayIndex < 0 || arrayIndex > array.Length)
+ {
+ ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.arrayIndex);
+ }
+
+ if (array.Length - arrayIndex < _size)
+ {
+ ThrowHelper.ThrowArgumentException(ExceptionResource.Argument_InvalidOffLen);
+ }
+
+ try
+ {
+ Array.Copy(_array, 0, array, arrayIndex, _size);
+ Array.Reverse(array, arrayIndex, _size);
+ }
+ catch (ArrayTypeMismatchException)
+ {
+ ThrowHelper.ThrowArgumentException_Argument_InvalidArrayType();
+ }
+ }
+
+ ///
+ /// Returns an IEnumerator for this PooledStack.
+ ///
+ ///
+ public Enumerator GetEnumerator()
+ => new Enumerator(this);
+
+ ///
+ IEnumerator IEnumerable.GetEnumerator()
+ => new Enumerator(this);
+
+ IEnumerator IEnumerable.GetEnumerator()
+ => new Enumerator(this);
+
+ public void TrimExcess()
+ {
+ if (_size == 0)
+ {
+ ReturnArray(replaceWith: Array.Empty());
+ _version++;
+ return;
+ }
+
+ int threshold = (int)(_array.Length * 0.9);
+ if (_size < threshold)
+ {
+ var newArray = _pool.Rent(_size);
+ if (newArray.Length < _array.Length)
+ {
+ Array.Copy(_array, newArray, _size);
+ ReturnArray(replaceWith: newArray);
+ _version++;
+ }
+ else
+ {
+ // The array from the pool wasn't any smaller than the one we already had,
+ // (we can only control minimum size) so return it and do nothing.
+ // If we create an exact-sized array not from the pool, we'll
+ // get an exception when returning it to the pool.
+ _pool.Return(newArray);
+ }
+ }
+ }
+
+ ///
+ /// Returns the top object on the stack without removing it. If the stack
+ /// is empty, Peek throws an InvalidOperationException.
+ ///
+ public T Peek()
+ {
+ int size = _size - 1;
+ T[] array = _array;
+
+ if ((uint)size >= (uint)array.Length)
+ {
+ ThrowForEmptyStack();
+ }
+
+ return array[size];
+ }
+
+ public bool TryPeek(out T result)
+ {
+ int size = _size - 1;
+ T[] array = _array;
+
+ if ((uint)size >= (uint)array.Length)
+ {
+ result = default;
+ return false;
+ }
+ result = array[size];
+ return true;
+ }
+
+ ///
+ /// Pops an item from the top of the stack. If the stack is empty, Pop
+ /// throws an InvalidOperationException.
+ ///
+ public T Pop()
+ {
+ int size = _size - 1;
+ T[] array = _array;
+
+ // if (_size == 0) is equivalent to if (size == -1), and this case
+ // is covered with (uint)size, thus allowing bounds check elimination
+ // https://github.com/dotnet/coreclr/pull/9773
+ if ((uint)size >= (uint)array.Length)
+ {
+ ThrowForEmptyStack();
+ }
+
+ _version++;
+ _size = size;
+ T item = array[size];
+ if (_clearOnFree)
+ {
+ array[size] = default; // Free memory quicker.
+ }
+ return item;
+ }
+
+ public bool TryPop(out T result)
+ {
+ int size = _size - 1;
+ T[] array = _array;
+
+ if ((uint)size >= (uint)array.Length)
+ {
+ result = default;
+ return false;
+ }
+
+ _version++;
+ _size = size;
+ result = array[size];
+ if (_clearOnFree)
+ {
+ array[size] = default; // Free memory quicker.
+ }
+ return true;
+ }
+
+ ///
+ /// Pushes an item to the top of the stack.
+ ///
+ public void Push(T item)
+ {
+ int size = _size;
+ T[] array = _array;
+
+ if ((uint)size < (uint)array.Length)
+ {
+ array[size] = item;
+ _version++;
+ _size = size + 1;
+ }
+ else
+ {
+ PushWithResize(item);
+ }
+ }
+
+ // Non-inline from Stack.Push to improve its code quality as uncommon path
+ [MethodImpl(MethodImplOptions.NoInlining)]
+ private void PushWithResize(T item)
+ {
+ var newArray = _pool.Rent((_array.Length == 0) ? DefaultCapacity : 2 * _array.Length);
+ Array.Copy(_array, newArray, _size);
+ ReturnArray(replaceWith: newArray);
+ _array[_size] = item;
+ _version++;
+ _size++;
+ }
+
+ ///
+ /// Copies the Stack to an array, in the same order Pop would return the items.
+ ///
+ public T[] ToArray()
+ {
+ if (_size == 0)
+ return Array.Empty();
+
+ T[] objArray = new T[_size];
+ int i = 0;
+ while (i < _size)
+ {
+ objArray[i] = _array[_size - i - 1];
+ i++;
+ }
+ return objArray;
+ }
+
+ private void ThrowForEmptyStack()
+ {
+ Debug.Assert(_size == 0);
+ throw new InvalidOperationException("Stack was empty.");
+ }
+
+ private void ReturnArray(T[] replaceWith = null)
+ {
+ if (_array?.Length > 0)
+ {
+ try
+ {
+ _pool.Return(_array, clearArray: _clearOnFree);
+ }
+ catch (ArgumentException)
+ {
+ // oh well, the array pool didn't like our array
+ }
+ }
+
+ if (!(replaceWith is null))
+ {
+ _array = replaceWith;
+ }
+ }
+
+ private static bool ShouldClear(ClearMode mode)
+ {
+#if NETCOREAPP2_1
+ return mode == ClearMode.Always
+ || (mode == ClearMode.Auto && RuntimeHelpers.IsReferenceOrContainsReferences());
+#else
+ return mode != ClearMode.Never;
+#endif
+ }
+
+ public void Dispose()
+ {
+ ReturnArray(replaceWith: Array.Empty());
+ _size = 0;
+ _version++;
+ }
+
+ void IDeserializationCallback.OnDeserialization(object sender)
+ {
+ // We can't serialize array pools, so deserialized PooledStacks will
+ // have to use the shared pool, even if they were using a custom pool
+ // before serialization.
+ _pool = ArrayPool.Shared;
+ }
+
+ [SuppressMessage("Microsoft.Performance", "CA1815:OverrideEqualsAndOperatorEqualsOnValueTypes", Justification = "not an expected scenario")]
+ public struct Enumerator : IEnumerator, IEnumerator
+ {
+ private readonly PooledStack _stack;
+ private readonly int _version;
+ private int _index;
+ private T _currentElement;
+
+ internal Enumerator(PooledStack stack)
+ {
+ _stack = stack;
+ _version = stack._version;
+ _index = -2;
+ _currentElement = default;
+ }
+
+ public void Dispose()
+ {
+ _index = -1;
+ }
+
+ public bool MoveNext()
+ {
+ bool retval;
+ if (_version != _stack._version)
+ throw new InvalidOperationException("Collection was modified during enumeration.");
+ if (_index == -2)
+ { // First call to enumerator.
+ _index = _stack._size - 1;
+ retval = (_index >= 0);
+ if (retval)
+ _currentElement = _stack._array[_index];
+ return retval;
+ }
+ if (_index == -1)
+ { // End of enumeration.
+ return false;
+ }
+
+ retval = (--_index >= 0);
+ if (retval)
+ _currentElement = _stack._array[_index];
+ else
+ _currentElement = default;
+ return retval;
+ }
+
+ public T Current
+ {
+ get
+ {
+ if (_index < 0)
+ ThrowEnumerationNotStartedOrEnded();
+ return _currentElement;
+ }
+ }
+
+ private void ThrowEnumerationNotStartedOrEnded()
+ {
+ Debug.Assert(_index == -1 || _index == -2);
+ throw new InvalidOperationException(_index == -2 ? "Enumeration was not started." : "Enumeration has ended.");
+ }
+
+ object IEnumerator.Current
+ {
+ get { return Current; }
+ }
+
+ void IEnumerator.Reset()
+ {
+ if (_version != _stack._version)
+ throw new InvalidOperationException("Collection was modified during enumeration.");
+ _index = -2;
+ _currentElement = default;
+ }
+ }
+ }
+}
diff --git a/src/Avalonia.Base/Collections/Pooled/StackDebugView.cs b/src/Avalonia.Base/Collections/Pooled/StackDebugView.cs
new file mode 100644
index 00000000000..b0423880798
--- /dev/null
+++ b/src/Avalonia.Base/Collections/Pooled/StackDebugView.cs
@@ -0,0 +1,28 @@
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the MIT license.
+// See the LICENSE file in the project root for more information.
+
+using System;
+using System.Diagnostics;
+
+namespace Avalonia.Collections.Pooled
+{
+ internal sealed class StackDebugView
+ {
+ private readonly PooledStack _stack;
+
+ public StackDebugView(PooledStack stack)
+ {
+ _stack = stack ?? throw new ArgumentNullException(nameof(stack));
+ }
+
+ [DebuggerBrowsable(DebuggerBrowsableState.RootHidden)]
+ public T[] Items
+ {
+ get
+ {
+ return _stack.ToArray();
+ }
+ }
+ }
+}
diff --git a/src/Avalonia.Base/Collections/Pooled/ThrowHelper.cs b/src/Avalonia.Base/Collections/Pooled/ThrowHelper.cs
new file mode 100644
index 00000000000..74558229c33
--- /dev/null
+++ b/src/Avalonia.Base/Collections/Pooled/ThrowHelper.cs
@@ -0,0 +1,691 @@
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the MIT license.
+// See the LICENSE file in the project root for more information.
+
+
+// This file defines an internal class used to throw exceptions in BCL code.
+// The main purpose is to reduce code size.
+//
+// The old way to throw an exception generates quite a lot IL code and assembly code.
+// Following is an example:
+// C# source
+// throw new ArgumentNullException(nameof(key), SR.ArgumentNull_Key);
+// IL code:
+// IL_0003: ldstr "key"
+// IL_0008: ldstr "ArgumentNull_Key"
+// IL_000d: call string System.Environment::GetResourceString(string)
+// IL_0012: newobj instance void System.ArgumentNullException::.ctor(string,string)
+// IL_0017: throw
+// which is 21bytes in IL.
+//
+// So we want to get rid of the ldstr and call to Environment.GetResource in IL.
+// In order to do that, I created two enums: ExceptionResource, ExceptionArgument to represent the
+// argument name and resource name in a small integer. The source code will be changed to
+// ThrowHelper.ThrowArgumentNullException(ExceptionArgument.key, ExceptionResource.ArgumentNull_Key);
+//
+// The IL code will be 7 bytes.
+// IL_0008: ldc.i4.4
+// IL_0009: ldc.i4.4
+// IL_000a: call void System.ThrowHelper::ThrowArgumentNullException(valuetype System.ExceptionArgument)
+// IL_000f: ldarg.0
+//
+// This will also reduce the Jitted code size a lot.
+//
+// It is very important we do this for generic classes because we can easily generate the same code
+// multiple times for different instantiation.
+//
+
+using System;
+using System.Collections.Generic;
+using System.Diagnostics;
+using System.Runtime.CompilerServices;
+using System.Runtime.Serialization;
+
+namespace Avalonia.Collections.Pooled
+{
+ internal static class ThrowHelper
+ {
+ internal static void ThrowArrayTypeMismatchException()
+ {
+ throw new ArrayTypeMismatchException();
+ }
+
+ internal static void ThrowIndexOutOfRangeException()
+ {
+ throw new IndexOutOfRangeException();
+ }
+
+ internal static void ThrowArgumentOutOfRangeException()
+ {
+ throw new ArgumentOutOfRangeException();
+ }
+
+ internal static void ThrowArgumentException_DestinationTooShort()
+ {
+ throw new ArgumentException("Destination too short.");
+ }
+
+ internal static void ThrowArgumentException_OverlapAlignmentMismatch()
+ {
+ throw new ArgumentException("Overlap alignment mismatch.");
+ }
+
+ internal static void ThrowArgumentOutOfRange_IndexException()
+ {
+ throw GetArgumentOutOfRangeException(ExceptionArgument.index,
+ ExceptionResource.ArgumentOutOfRange_Index);
+ }
+
+ internal static void ThrowIndexArgumentOutOfRange_NeedNonNegNumException()
+ {
+ throw GetArgumentOutOfRangeException(ExceptionArgument.index,
+ ExceptionResource.ArgumentOutOfRange_NeedNonNegNum);
+ }
+
+ internal static void ThrowValueArgumentOutOfRange_NeedNonNegNumException()
+ {
+ throw GetArgumentOutOfRangeException(ExceptionArgument.value,
+ ExceptionResource.ArgumentOutOfRange_NeedNonNegNum);
+ }
+
+ internal static void ThrowLengthArgumentOutOfRange_ArgumentOutOfRange_NeedNonNegNum()
+ {
+ throw GetArgumentOutOfRangeException(ExceptionArgument.length,
+ ExceptionResource.ArgumentOutOfRange_NeedNonNegNum);
+ }
+
+ internal static void ThrowStartIndexArgumentOutOfRange_ArgumentOutOfRange_Index()
+ {
+ throw GetArgumentOutOfRangeException(ExceptionArgument.startIndex,
+ ExceptionResource.ArgumentOutOfRange_Index);
+ }
+
+ internal static void ThrowCountArgumentOutOfRange_ArgumentOutOfRange_Count()
+ {
+ throw GetArgumentOutOfRangeException(ExceptionArgument.count,
+ ExceptionResource.ArgumentOutOfRange_Count);
+ }
+
+ internal static void ThrowWrongKeyTypeArgumentException(T key, Type targetType)
+ {
+ // Generic key to move the boxing to the right hand side of throw
+ throw GetWrongKeyTypeArgumentException((object)key, targetType);
+ }
+
+ internal static void ThrowWrongValueTypeArgumentException(T value, Type targetType)
+ {
+ // Generic key to move the boxing to the right hand side of throw
+ throw GetWrongValueTypeArgumentException((object)value, targetType);
+ }
+
+ private static ArgumentException GetAddingDuplicateWithKeyArgumentException(object key)
+ {
+ return new ArgumentException($"Error adding duplicate with key: {key}.");
+ }
+
+ internal static void ThrowAddingDuplicateWithKeyArgumentException(T key)
+ {
+ // Generic key to move the boxing to the right hand side of throw
+ throw GetAddingDuplicateWithKeyArgumentException((object)key);
+ }
+
+ internal static void ThrowKeyNotFoundException(T key)
+ {
+ // Generic key to move the boxing to the right hand side of throw
+ throw GetKeyNotFoundException((object)key);
+ }
+
+ internal static void ThrowArgumentException(ExceptionResource resource)
+ {
+ throw GetArgumentException(resource);
+ }
+
+ internal static void ThrowArgumentException(ExceptionResource resource, ExceptionArgument argument)
+ {
+ throw GetArgumentException(resource, argument);
+ }
+
+ private static ArgumentNullException GetArgumentNullException(ExceptionArgument argument)
+ {
+ return new ArgumentNullException(GetArgumentName(argument));
+ }
+
+ internal static void ThrowArgumentNullException(ExceptionArgument argument)
+ {
+ throw GetArgumentNullException(argument);
+ }
+
+ internal static void ThrowArgumentNullException(ExceptionResource resource)
+ {
+ throw new ArgumentNullException(GetResourceString(resource));
+ }
+
+ internal static void ThrowArgumentNullException(ExceptionArgument argument, ExceptionResource resource)
+ {
+ throw new ArgumentNullException(GetArgumentName(argument), GetResourceString(resource));
+ }
+
+ internal static void ThrowArgumentOutOfRangeException(ExceptionArgument argument)
+ {
+ throw new ArgumentOutOfRangeException(GetArgumentName(argument));
+ }
+
+ internal static void ThrowArgumentOutOfRangeException(ExceptionArgument argument, ExceptionResource resource)
+ {
+ throw GetArgumentOutOfRangeException(argument, resource);
+ }
+
+ internal static void ThrowArgumentOutOfRangeException(ExceptionArgument argument, int paramNumber, ExceptionResource resource)
+ {
+ throw GetArgumentOutOfRangeException(argument, paramNumber, resource);
+ }
+
+ internal static void ThrowInvalidOperationException(ExceptionResource resource)
+ {
+ throw GetInvalidOperationException(resource);
+ }
+
+ internal static void ThrowInvalidOperationException(ExceptionResource resource, Exception e)
+ {
+ throw new InvalidOperationException(GetResourceString(resource), e);
+ }
+
+ internal static void ThrowSerializationException(ExceptionResource resource)
+ {
+ throw new SerializationException(GetResourceString(resource));
+ }
+
+ internal static void ThrowSecurityException(ExceptionResource resource)
+ {
+ throw new System.Security.SecurityException(GetResourceString(resource));
+ }
+
+ internal static void ThrowRankException(ExceptionResource resource)
+ {
+ throw new RankException(GetResourceString(resource));
+ }
+
+ internal static void ThrowNotSupportedException(ExceptionResource resource)
+ {
+ throw new NotSupportedException(GetResourceString(resource));
+ }
+
+ internal static void ThrowUnauthorizedAccessException(ExceptionResource resource)
+ {
+ throw new UnauthorizedAccessException(GetResourceString(resource));
+ }
+
+ internal static void ThrowObjectDisposedException(string objectName, ExceptionResource resource)
+ {
+ throw new ObjectDisposedException(objectName, GetResourceString(resource));
+ }
+
+ internal static void ThrowObjectDisposedException(ExceptionResource resource)
+ {
+ throw new ObjectDisposedException(null, GetResourceString(resource));
+ }
+
+ internal static void ThrowNotSupportedException()
+ {
+ throw new NotSupportedException();
+ }
+
+ internal static void ThrowAggregateException(List exceptions)
+ {
+ throw new AggregateException(exceptions);
+ }
+
+ internal static void ThrowOutOfMemoryException()
+ {
+ throw new OutOfMemoryException();
+ }
+
+ internal static void ThrowArgumentException_Argument_InvalidArrayType()
+ {
+ throw new ArgumentException("Invalid array type.");
+ }
+
+ internal static void ThrowInvalidOperationException_InvalidOperation_EnumNotStarted()
+ {
+ throw new InvalidOperationException("Enumeration has not started.");
+ }
+
+ internal static void ThrowInvalidOperationException_InvalidOperation_EnumEnded()
+ {
+ throw new InvalidOperationException("Enumeration has ended.");
+ }
+
+ internal static void ThrowInvalidOperationException_EnumCurrent(int index)
+ {
+ throw GetInvalidOperationException_EnumCurrent(index);
+ }
+
+ internal static void ThrowInvalidOperationException_InvalidOperation_EnumFailedVersion()
+ {
+ throw new InvalidOperationException("Collection was modified during enumeration.");
+ }
+
+ internal static void ThrowInvalidOperationException_InvalidOperation_EnumOpCantHappen()
+ {
+ throw new InvalidOperationException("Invalid enumerator state: enumeration cannot proceed.");
+ }
+
+ internal static void ThrowInvalidOperationException_InvalidOperation_NoValue()
+ {
+ throw new InvalidOperationException("No value provided.");
+ }
+
+ internal static void ThrowInvalidOperationException_ConcurrentOperationsNotSupported()
+ {
+ throw new InvalidOperationException("Concurrent operations are not supported.");
+ }
+
+ internal static void ThrowInvalidOperationException_HandleIsNotInitialized()
+ {
+ throw new InvalidOperationException("Handle is not initialized.");
+ }
+
+ internal static void ThrowFormatException_BadFormatSpecifier()
+ {
+ throw new FormatException("Bad format specifier.");
+ }
+
+ private static ArgumentException GetArgumentException(ExceptionResource resource)
+ {
+ return new ArgumentException(GetResourceString(resource));
+ }
+
+ private static InvalidOperationException GetInvalidOperationException(ExceptionResource resource)
+ {
+ return new InvalidOperationException(GetResourceString(resource));
+ }
+
+ private static ArgumentException GetWrongKeyTypeArgumentException(object key, Type targetType)
+ {
+ return new ArgumentException($"Wrong key type. Expected {targetType}, got: '{key}'.", nameof(key));
+ }
+
+ private static ArgumentException GetWrongValueTypeArgumentException(object value, Type targetType)
+ {
+ return new ArgumentException($"Wrong value type. Expected {targetType}, got: '{value}'.", nameof(value));
+ }
+
+ private static KeyNotFoundException GetKeyNotFoundException(object key)
+ {
+ return new KeyNotFoundException($"Key not found: {key}");
+ }
+
+ private static ArgumentOutOfRangeException GetArgumentOutOfRangeException(ExceptionArgument argument, ExceptionResource resource)
+ {
+ return new ArgumentOutOfRangeException(GetArgumentName(argument), GetResourceString(resource));
+ }
+
+ private static ArgumentException GetArgumentException(ExceptionResource resource, ExceptionArgument argument)
+ {
+ return new ArgumentException(GetResourceString(resource), GetArgumentName(argument));
+ }
+
+ private static ArgumentOutOfRangeException GetArgumentOutOfRangeException(ExceptionArgument argument, int paramNumber, ExceptionResource resource)
+ {
+ return new ArgumentOutOfRangeException(GetArgumentName(argument) + "[" + paramNumber.ToString() + "]", GetResourceString(resource));
+ }
+
+ private static InvalidOperationException GetInvalidOperationException_EnumCurrent(int index)
+ {
+ return new InvalidOperationException(
+ index < 0 ?
+ "Enumeration has not started" :
+ "Enumeration has ended");
+ }
+
+ // Allow nulls for reference types and Nullable, but not for value types.
+ // Aggressively inline so the jit evaluates the if in place and either drops the call altogether
+ // Or just leaves null test and call to the Non-returning ThrowHelper.ThrowArgumentNullException
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ internal static void IfNullAndNullsAreIllegalThenThrow(object value, ExceptionArgument argName)
+ {
+ // Note that default(T) is not equal to null for value types except when T is Nullable.
+ if (!(default(T) == null) && value == null)
+ ThrowHelper.ThrowArgumentNullException(argName);
+ }
+
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ internal static void ThrowForUnsupportedVectorBaseType() where T : struct
+ {
+ if (typeof(T) != typeof(byte) && typeof(T) != typeof(sbyte) &&
+ typeof(T) != typeof(short) && typeof(T) != typeof(ushort) &&
+ typeof(T) != typeof(int) && typeof(T) != typeof(uint) &&
+ typeof(T) != typeof(long) && typeof(T) != typeof(ulong) &&
+ typeof(T) != typeof(float) && typeof(T) != typeof(double))
+ {
+ ThrowNotSupportedException(ExceptionResource.Arg_TypeNotSupported);
+ }
+ }
+
+#if false // Reflection-based implementation does not work for CoreRT/ProjectN
+ // This function will convert an ExceptionArgument enum value to the argument name string.
+ [MethodImpl(MethodImplOptions.NoInlining)]
+ private static string GetArgumentName(ExceptionArgument argument)
+ {
+ Debug.Assert(Enum.IsDefined(typeof(ExceptionArgument), argument),
+ "The enum value is not defined, please check the ExceptionArgument Enum.");
+
+ return argument.ToString();
+ }
+#endif
+
+ private static string GetArgumentName(ExceptionArgument argument)
+ {
+ switch (argument)
+ {
+ case ExceptionArgument.obj:
+ return "obj";
+ case ExceptionArgument.dictionary:
+ return "dictionary";
+ case ExceptionArgument.array:
+ return "array";
+ case ExceptionArgument.info:
+ return "info";
+ case ExceptionArgument.key:
+ return "key";
+ case ExceptionArgument.text:
+ return "text";
+ case ExceptionArgument.values:
+ return "values";
+ case ExceptionArgument.value:
+ return "value";
+ case ExceptionArgument.startIndex:
+ return "startIndex";
+ case ExceptionArgument.task:
+ return "task";
+ case ExceptionArgument.ch:
+ return "ch";
+ case ExceptionArgument.s:
+ return "s";
+ case ExceptionArgument.input:
+ return "input";
+ case ExceptionArgument.list:
+ return "list";
+ case ExceptionArgument.index:
+ return "index";
+ case ExceptionArgument.capacity:
+ return "capacity";
+ case ExceptionArgument.collection:
+ return "collection";
+ case ExceptionArgument.item:
+ return "item";
+ case ExceptionArgument.converter:
+ return "converter";
+ case ExceptionArgument.match:
+ return "match";
+ case ExceptionArgument.count:
+ return "count";
+ case ExceptionArgument.action:
+ return "action";
+ case ExceptionArgument.comparison:
+ return "comparison";
+ case ExceptionArgument.exceptions:
+ return "exceptions";
+ case ExceptionArgument.exception:
+ return "exception";
+ case ExceptionArgument.enumerable:
+ return "enumerable";
+ case ExceptionArgument.start:
+ return "start";
+ case ExceptionArgument.format:
+ return "format";
+ case ExceptionArgument.culture:
+ return "culture";
+ case ExceptionArgument.comparer:
+ return "comparer";
+ case ExceptionArgument.comparable:
+ return "comparable";
+ case ExceptionArgument.source:
+ return "source";
+ case ExceptionArgument.state:
+ return "state";
+ case ExceptionArgument.length:
+ return "length";
+ case ExceptionArgument.comparisonType:
+ return "comparisonType";
+ case ExceptionArgument.manager:
+ return "manager";
+ case ExceptionArgument.sourceBytesToCopy:
+ return "sourceBytesToCopy";
+ case ExceptionArgument.callBack:
+ return "callBack";
+ case ExceptionArgument.creationOptions:
+ return "creationOptions";
+ case ExceptionArgument.function:
+ return "function";
+ case ExceptionArgument.delay:
+ return "delay";
+ case ExceptionArgument.millisecondsDelay:
+ return "millisecondsDelay";
+ case ExceptionArgument.millisecondsTimeout:
+ return "millisecondsTimeout";
+ case ExceptionArgument.timeout:
+ return "timeout";
+ case ExceptionArgument.type:
+ return "type";
+ case ExceptionArgument.sourceIndex:
+ return "sourceIndex";
+ case ExceptionArgument.sourceArray:
+ return "sourceArray";
+ case ExceptionArgument.destinationIndex:
+ return "destinationIndex";
+ case ExceptionArgument.destinationArray:
+ return "destinationArray";
+ case ExceptionArgument.other:
+ return "other";
+ case ExceptionArgument.newSize:
+ return "newSize";
+ case ExceptionArgument.lowerBounds:
+ return "lowerBounds";
+ case ExceptionArgument.lengths:
+ return "lengths";
+ case ExceptionArgument.len:
+ return "len";
+ case ExceptionArgument.keys:
+ return "keys";
+ case ExceptionArgument.indices:
+ return "indices";
+ case ExceptionArgument.endIndex:
+ return "endIndex";
+ case ExceptionArgument.elementType:
+ return "elementType";
+ case ExceptionArgument.arrayIndex:
+ return "arrayIndex";
+ default:
+ Debug.Fail("The enum value is not defined, please check the ExceptionArgument Enum.");
+ return argument.ToString();
+ }
+ }
+
+#if false // Reflection-based implementation does not work for CoreRT/ProjectN
+ // This function will convert an ExceptionResource enum value to the resource string.
+ [MethodImpl(MethodImplOptions.NoInlining)]
+ private static string GetResourceString(ExceptionResource resource)
+ {
+ Debug.Assert(Enum.IsDefined(typeof(ExceptionResource), resource),
+ "The enum value is not defined, please check the ExceptionResource Enum.");
+
+ return SR.GetResourceString(resource.ToString());
+ }
+#endif
+
+ private static string GetResourceString(ExceptionResource resource)
+ {
+ switch (resource)
+ {
+ case ExceptionResource.ArgumentOutOfRange_Index:
+ return "Argument 'index' was out of the range of valid values.";
+ case ExceptionResource.ArgumentOutOfRange_Count:
+ return "Argument 'count' was out of the range of valid values.";
+ case ExceptionResource.Arg_ArrayPlusOffTooSmall:
+ return "Array plus offset too small.";
+ case ExceptionResource.NotSupported_ReadOnlyCollection:
+ return "This operation is not supported on a read-only collection.";
+ case ExceptionResource.Arg_RankMultiDimNotSupported:
+ return "Multi-dimensional arrays are not supported.";
+ case ExceptionResource.Arg_NonZeroLowerBound:
+ return "Arrays with a non-zero lower bound are not supported.";
+ case ExceptionResource.ArgumentOutOfRange_ListInsert:
+ return "Insertion index was out of the range of valid values.";
+ case ExceptionResource.ArgumentOutOfRange_NeedNonNegNum:
+ return "The number must be non-negative.";
+ case ExceptionResource.ArgumentOutOfRange_SmallCapacity:
+ return "The capacity cannot be set below the current Count.";
+ case ExceptionResource.Argument_InvalidOffLen:
+ return "Invalid offset length.";
+ case ExceptionResource.ArgumentOutOfRange_BiggerThanCollection:
+ return "The given value was larger than the size of the collection.";
+ case ExceptionResource.Serialization_MissingKeys:
+ return "Serialization error: missing keys.";
+ case ExceptionResource.Serialization_NullKey:
+ return "Serialization error: null key.";
+ case ExceptionResource.NotSupported_KeyCollectionSet:
+ return "The KeyCollection does not support modification.";
+ case ExceptionResource.NotSupported_ValueCollectionSet:
+ return "The ValueCollection does not support modification.";
+ case ExceptionResource.InvalidOperation_NullArray:
+ return "Null arrays are not supported.";
+ case ExceptionResource.InvalidOperation_HSCapacityOverflow:
+ return "Set hash capacity overflow. Cannot increase size.";
+ case ExceptionResource.NotSupported_StringComparison:
+ return "String comparison not supported.";
+ case ExceptionResource.ConcurrentCollection_SyncRoot_NotSupported:
+ return "SyncRoot not supported.";
+ case ExceptionResource.ArgumentException_OtherNotArrayOfCorrectLength:
+ return "The other array is not of the correct length.";
+ case ExceptionResource.ArgumentOutOfRange_EndIndexStartIndex:
+ return "The end index does not come after the start index.";
+ case ExceptionResource.ArgumentOutOfRange_HugeArrayNotSupported:
+ return "Huge arrays are not supported.";
+ case ExceptionResource.Argument_AddingDuplicate:
+ return "Duplicate item added.";
+ case ExceptionResource.Argument_InvalidArgumentForComparison:
+ return "Invalid argument for comparison.";
+ case ExceptionResource.Arg_LowerBoundsMustMatch:
+ return "Array lower bounds must match.";
+ case ExceptionResource.Arg_MustBeType:
+ return "Argument must be of type: ";
+ case ExceptionResource.InvalidOperation_IComparerFailed:
+ return "IComparer failed.";
+ case ExceptionResource.NotSupported_FixedSizeCollection:
+ return "This operation is not suppored on a fixed-size collection.";
+ case ExceptionResource.Rank_MultiDimNotSupported:
+ return "Multi-dimensional arrays are not supported.";
+ case ExceptionResource.Arg_TypeNotSupported:
+ return "Type not supported.";
+ default:
+ Debug.Assert(false,
+ "The enum value is not defined, please check the ExceptionResource Enum.");
+ return resource.ToString();
+ }
+ }
+ }
+
+ //
+ // The convention for this enum is using the argument name as the enum name
+ //
+ internal enum ExceptionArgument
+ {
+ obj,
+ dictionary,
+ array,
+ info,
+ key,
+ text,
+ values,
+ value,
+ startIndex,
+ task,
+ ch,
+ s,
+ input,
+ list,
+ index,
+ capacity,
+ collection,
+ item,
+ converter,
+ match,
+ count,
+ action,
+ comparison,
+ exceptions,
+ exception,
+ enumerable,
+ start,
+ format,
+ culture,
+ comparer,
+ comparable,
+ source,
+ state,
+ length,
+ comparisonType,
+ manager,
+ sourceBytesToCopy,
+ callBack,
+ creationOptions,
+ function,
+ delay,
+ millisecondsDelay,
+ millisecondsTimeout,
+ timeout,
+ type,
+ sourceIndex,
+ sourceArray,
+ destinationIndex,
+ destinationArray,
+ other,
+ newSize,
+ lowerBounds,
+ lengths,
+ len,
+ keys,
+ indices,
+ endIndex,
+ elementType,
+ arrayIndex
+ }
+
+ //
+ // The convention for this enum is using the resource name as the enum name
+ //
+ internal enum ExceptionResource
+ {
+ ArgumentOutOfRange_Index,
+ ArgumentOutOfRange_Count,
+ Arg_ArrayPlusOffTooSmall,
+ NotSupported_ReadOnlyCollection,
+ Arg_RankMultiDimNotSupported,
+ Arg_NonZeroLowerBound,
+ ArgumentOutOfRange_ListInsert,
+ ArgumentOutOfRange_NeedNonNegNum,
+ ArgumentOutOfRange_SmallCapacity,
+ Argument_InvalidOffLen,
+ ArgumentOutOfRange_BiggerThanCollection,
+ Serialization_MissingKeys,
+ Serialization_NullKey,
+ NotSupported_KeyCollectionSet,
+ NotSupported_ValueCollectionSet,
+ InvalidOperation_NullArray,
+ InvalidOperation_HSCapacityOverflow,
+ NotSupported_StringComparison,
+ ConcurrentCollection_SyncRoot_NotSupported,
+ ArgumentException_OtherNotArrayOfCorrectLength,
+ ArgumentOutOfRange_EndIndexStartIndex,
+ ArgumentOutOfRange_HugeArrayNotSupported,
+ Argument_AddingDuplicate,
+ Argument_InvalidArgumentForComparison,
+ Arg_LowerBoundsMustMatch,
+ Arg_MustBeType,
+ InvalidOperation_IComparerFailed,
+ NotSupported_FixedSizeCollection,
+ Rank_MultiDimNotSupported,
+ Arg_TypeNotSupported,
+ }
+}
diff --git a/src/Avalonia.Visuals/Avalonia.Visuals.csproj b/src/Avalonia.Visuals/Avalonia.Visuals.csproj
index 750fd4448d7..2cc7741bbb8 100644
--- a/src/Avalonia.Visuals/Avalonia.Visuals.csproj
+++ b/src/Avalonia.Visuals/Avalonia.Visuals.csproj
@@ -3,9 +3,6 @@
netstandard2.0
Avalonia
-
-
-
diff --git a/src/Avalonia.Visuals/Rendering/SceneGraph/Scene.cs b/src/Avalonia.Visuals/Rendering/SceneGraph/Scene.cs
index 30f25e97ad8..819b23e5ee1 100644
--- a/src/Avalonia.Visuals/Rendering/SceneGraph/Scene.cs
+++ b/src/Avalonia.Visuals/Rendering/SceneGraph/Scene.cs
@@ -4,8 +4,8 @@
using System;
using System.Collections.Generic;
using System.Linq;
+using Avalonia.Collections.Pooled;
using Avalonia.VisualTree;
-using Collections.Pooled;
namespace Avalonia.Rendering.SceneGraph
{