Skip to content

Commit

Permalink
fix: Support Add() in ArrayList when Capacity is not set before
Browse files Browse the repository at this point in the history
  • Loading branch information
robloo committed Sep 14, 2020
1 parent f97af39 commit daad101
Showing 1 changed file with 21 additions and 14 deletions.
35 changes: 21 additions & 14 deletions src/Uno.UI/Microsoft/UI/Xaml/Controls/ColorPicker/ArrayList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ namespace Microsoft.UI.Xaml.Controls
/// <typeparam name="T">The type of elements in the list.</typeparam>
internal class ArrayList<T>
{
private T[] _list;
private T[] _Array;

public ArrayList()
{
_list = System.Array.Empty<T>();
_Array = System.Array.Empty<T>();
}

public ArrayList(int capacity)
Expand All @@ -26,19 +26,19 @@ public ArrayList(int capacity)
{
throw new ArgumentOutOfRangeException("Capacity cannot be less than zero.");
}
else if (capacity == 0)
else if (capacity == 0)
{
_list = System.Array.Empty<T>();
_Array = System.Array.Empty<T>();
}
else
{
_list = new T[capacity];
_Array = new T[capacity];
}
}
}

internal T[] Array
{
get => _list;
get => _Array;
}

public int Capacity
Expand All @@ -54,18 +54,18 @@ public int Capacity
{
if (value > 0)
{
T[] newList = new T[value];
var newList = new T[value];

if (Count > 0)
{
System.Array.Copy(_list, 0, newList, 0, Count);
System.Array.Copy(_Array, 0, newList, 0, Count);
}

_list = newList;
_Array = newList;
}
else
{
_list = System.Array.Empty<T>();
_Array = System.Array.Empty<T>();
}
}
}
Expand All @@ -75,12 +75,19 @@ public int Capacity

public void Add(T item)
{
if (Count == _list.Length)
if (Count == _Array.Length)
{
System.Array.Resize(ref _list, _list.Length * 2);;
if (_Array.Length == 0)
{
System.Array.Resize(ref _Array, 4);
}
else
{
System.Array.Resize(ref _Array, _Array.Length * 2);
}
}

_list[Count] = item;
_Array[Count] = item;
Count++;
}
}
Expand Down

0 comments on commit daad101

Please sign in to comment.