using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
namespace InspectorToolkit {
public static class MyCollections {
///
/// Returns new array with inserted empty element at index
///
public static T[] InsertAt(this T[] array, int index) {
if(index < 0) {
Debug.LogError("Index is less than zero. Array is not modified");
return array;
}
if(index > array.Length) {
Debug.LogError("Index exceeds array length. Array is not modified");
return array;
}
T[] newArray = new T[array.Length + 1];
int index1 = 0;
for(int index2 = 0; index2 < newArray.Length; ++index2) {
if(index2 == index) continue;
newArray[index2] = array[index1];
++index1;
}
return newArray;
}
#region IsNullOrEmpty and NotNullOrEmpty
///
/// Is array null or empty
///
public static bool IsNullOrEmpty(this T[] collection) => collection == null || collection.Length == 0;
///
/// Is list null or empty
///
public static bool IsNullOrEmpty(this IList collection) => collection == null || collection.Count == 0;
///
/// Is collection null or empty. IEnumerable is relatively slow. Use Array or List implementation if possible
///
public static bool IsNullOrEmpty(this IEnumerable collection) => collection == null || !collection.Any();
public static bool NotNullOrEmpty(this IEnumerable collection) => !collection.IsNullOrEmpty();
#endregion
///
/// Performs a function on each element of a collection.
///
public static IEnumerable ForEach(this IEnumerable source, Func func) {
foreach(T element in source) func(element);
return source;
}
}
}