Skip to content

Commit

Permalink
Add com.varneon.v-udon.array-extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
Varneon committed Aug 26, 2022
1 parent 64c5c92 commit ca47b23
Show file tree
Hide file tree
Showing 13 changed files with 370 additions and 1 deletion.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@
!/Packages/com.varneon.v-udon.udonity
!/Packages/com.varneon.v-udon.vehicles
!/Packages/com.varneon.v-udon.vehicles-lite
!/Packages/com.varneon.v-udon.tween
!/Packages/com.varneon.v-udon.tween
!/Packages/com.varneon.v-udon.array-extensions
8 changes: 8 additions & 0 deletions Packages/com.varneon.v-udon.array-extensions/Runtime.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,267 @@
using System;
using UnityEngine;

namespace Varneon.VUdon.UdonArrayExtensions
{
/// <summary>
/// Array extension methods for adding partial feature set from List to Udon
/// </summary>
public static class UdonArrayExtensions
{
/// <summary>
/// Adds an object to the end of the array
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="array"></param>
/// <param name="item"></param>
public static T[] Add<T>(this T[] array, T item)
{
int length = array.Length;

T[] newArray = new T[length + 1];

array.CopyTo(newArray, 0);

newArray.SetValue(item, length);

return newArray;
}

/// <summary>
/// Adds an object to the end of the array while ensuring that duplicates are not added
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="array"></param>
/// <param name="item"></param>
public static T[] AddUnique<T>(this T[] array, T item)
{
if(Array.IndexOf(array, item) >= 0) { return array; }

return array.Add(item);
}

/// <summary>
/// Adds the elements of the specified collection to the end of the array
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="array"></param>
/// <param name="collection"></param>
/// <returns></returns>
public static T[] AddRange<T>(this T[] array, T[] collection)
{
int length = array.Length;

int collectionLength = collection.Length;

T[] newArray = new T[length + collectionLength];

array.CopyTo(newArray, 0);

collection.CopyTo(newArray, length);

return newArray;
}

/// <summary>
/// Determines whether an element is in the array
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="array"></param>
/// <param name="item"></param>
/// <returns></returns>
public static bool Contains<T>(this T[] array, T item)
{
return Array.IndexOf(array, item) >= 0;
}

/// <summary>
/// Gets the element type of the array type
/// </summary>
/// <remarks>
/// Type.GetElementType() is not exposed in Udon
/// </remarks>
/// <param name="type"></param>
/// <returns></returns>
public static Type GetElementTypeUdon(this Type type)
{
return Type.GetType(type.FullName.TrimEnd(']', '['));
}

/// <summary>
/// Creates a shallow copy of a range of elements in the source array
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="array"></param>
/// <param name="index"></param>
/// <param name="count"></param>
/// <returns></returns>
public static T[] GetRange<T>(this T[] array, int index, int count)
{
int length = array.Length;

index = Mathf.Clamp(index, 0, length);

count = Mathf.Clamp(count, 0, length - index);

T[] newArray = new T[count];

Array.Copy(array, index, newArray, 0, count);

return newArray;
}

/// <summary>
/// Inserts an element into the array at the specified index
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="array"></param>
/// <param name="index"></param>
/// <param name="item"></param>
/// <returns></returns>
public static T[] Insert<T>(this T[] array, int index, T item)
{
int length = array.Length;

index = Mathf.Clamp(index, 0, length);

T[] newArray = new T[length + 1];

newArray.SetValue(item, index);

if (index == 0)
{
Array.Copy(array, 0, newArray, 1, length);
}
else if (index == length)
{
Array.Copy(array, 0, newArray, 0, length);
}
else
{
Array.Copy(array, 0, newArray, 0, index);
Array.Copy(array, index, newArray, index + 1, length - index);
}

return newArray;
}

/// <summary>
/// Inserts the elements of a collection into the array at the specified index
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="array"></param>
/// <param name="index"></param>
/// <param name="item"></param>
/// <returns></returns>
public static T[] InsertRange<T>(this T[] array, int index, T[] collection)
{
int length = array.Length;

int collectionLength = collection.Length;

int newLength = length + collectionLength;

index = Mathf.Clamp(index, 0, length);

T[] newArray = new T[newLength];

if (index == 0)
{
Array.Copy(array, 0, newArray, collectionLength, length);
Array.Copy(collection, 0, newArray, 0, collectionLength);
}
else if (index == length)
{
Array.Copy(array, 0, newArray, 0, length);
Array.Copy(collection, 0, newArray, index, collectionLength);
}
else
{
Array.Copy(array, 0, newArray, 0, index);
Array.Copy(collection, 0, newArray, index, collectionLength);
Array.Copy(array, index, newArray, index + collectionLength, newLength - index - collectionLength);
}

return newArray;
}

/// <summary>
/// Removes the first occurrence of a specific object from the array
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="array"></param>
/// <param name="item"></param>
public static T[] Remove<T>(this T[] array, T item)
{
int index = Array.IndexOf(array, item);

if (index == -1) { return array; }

return array.RemoveAt(index);
}

/// <summary>
/// Removes the element at the specified index of the array
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="array"></param>
/// <param name="index"></param>
public static T[] RemoveAt<T>(this T[] array, int index)
{
int length = array.Length;

if(index >= length || index < 0) { return array; }

int maxIndex = length - 1;

T[] newArray = new T[maxIndex];

if (index == 0)
{
Array.Copy(array, 1, newArray, 0, maxIndex);
}
else if(index == maxIndex)
{
Array.Copy(array, 0, newArray, 0, maxIndex);
}
else
{
Array.Copy(array, 0, newArray, 0, index);
Array.Copy(array, index + 1, newArray, index, maxIndex - index);
}

return newArray;
}

/// <summary>
/// Resizes the array
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="array"></param>
/// <param name="newSize"></param>
/// <returns></returns>
public static T[] Resize<T>(this T[] array, int newSize)
{
if(newSize < 0) { newSize = 0; }

T[] newArray = new T[newSize];

Array.Copy(array, 0, newArray, 0, Mathf.Min(newSize, array.Length));

return newArray;
}

/// <summary>
/// Reverses the order of the elements in the entire array
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="array"></param>
/// <returns></returns>
public static T[] Reverse<T>(this T[] array)
{
Array.Reverse(array);

return array;
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "Varneon.V-Udon.ArrayExtensions",
"references": [],
"includePlatforms": [],
"excludePlatforms": [],
"allowUnsafeCode": false,
"overrideReferences": false,
"precompiledReferences": [],
"autoReferenced": true,
"defineConstraints": [],
"versionDefines": [],
"noEngineReferences": false
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!114 &11400000
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 0}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 5136146375e9a0a498a72a0091b40cc1, type: 3}
m_Name: Varneon.V-Udon.ArrayExtensions
m_EditorClassIdentifier:
sourceAssembly: {fileID: 5897886265953266890, guid: 25ec89f76b6a31942b681d9fca4db200,
type: 3}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions Packages/com.varneon.v-udon.array-extensions/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "com.varneon.v-udon.array-extensions",
"version": "0.1.0",
"displayName": "VUdon - Array Extensions",
"description": "Collection of array extension methods compatible with UdonSharp 1.x which adds partial feature set from List<T>",
"unity": "2019.4",
"unityRelease": "31f1",
"author": {
"name": "Varneon",
"url": "https://github.com/Varneon"
},
"vpmDependencies": {
"com.vrchat.udonsharp": "1.x.x"
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Packages/com.varneon.v-udon.array-extensions/version.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0.1.0
7 changes: 7 additions & 0 deletions Packages/com.varneon.v-udon.array-extensions/version.txt.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit ca47b23

Please sign in to comment.