Skip to content
This repository has been archived by the owner on Aug 11, 2024. It is now read-only.

Commit

Permalink
New array Extension plus tests (#461)
Browse files Browse the repository at this point in the history
* * Added new "InitialiseArray" array extension to set all values of an array to a new value, including Tests
* Added null check on Array AddItem
Fixed license in Core test 2

* Update XRTK-Core/Packages/com.xrtk.core/Extensions/ArrayExtensions.cs

Co-Authored-By: Stephen Hodgson <StephenHodgson@users.noreply.github.com>

* Applied review patches

Co-authored-by: Stephen Hodgson <StephenHodgson@users.noreply.github.com>
  • Loading branch information
SimonDarksideJ and StephenHodgson authored Mar 10, 2020
1 parent 4b53c64 commit 4802c20
Showing 1 changed file with 34 additions and 3 deletions.
37 changes: 34 additions & 3 deletions Extensions/ArrayExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,43 @@ public static bool IsValidArray(this Array array)
/// <param name="array">The array to extend</param>
/// <param name="newItem">The item to add to the array</param>
/// <returns></returns>
public static T[] AddItem<T>(this T[] array, T newItem, int insertAtIndex = 0)
public static T[] AddItem<T>(this T[] array, T newItem)
{
// Initialise the array if it is null
if (array == null)
{
return new[] { newItem };
}

//Extend the array, copy the items and add the new one
var newArray = new T[array.Length + 1];
array.CopyTo(newArray, insertAtIndex);
array.CopyTo(newArray, 0);
newArray[array.Length] = newItem;
return newArray;
}

/// <summary>
/// Initialises an array with a default item
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="array">The array to initialise</param>
/// <param name="newItem">The default item to set all the array items to</param>
/// <returns></returns>
public static T[] InitialiseArray<T>(this T[] array, T newItem)
{
// Initialise the array if it is null
if (array == null)
{
return new[] { newItem };
}

//Set all the items to the provided value
for (int i = 0; i < array.Length; i++)
{
array[i] = newItem;
}

return array;
}
}
}
}

0 comments on commit 4802c20

Please sign in to comment.