using System; using System.Collections.Generic; using System.Linq; using Unity.UIWidgets.InternalBridge; namespace Unity.UIWidgets.foundation { public static class CollectionUtils { public static V putIfAbsent(this IDictionary it, K key, Func ifAbsent) { V value; if (it.TryGetValue(key, out value)) { return value; } value = ifAbsent(); it[key] = value; return value; } public static bool isEmpty(this ICollection it) { return it.Count == 0; } public static bool isNotEmpty(this ICollection it) { return it.Count != 0; } public static bool isEmpty(this Queue it) { return it.Count == 0; } public static bool isNotEmpty(this Queue it) { return it.Count != 0; } public static bool isEmpty(this IDictionary it) { return it.Count == 0; } public static bool isNotEmpty(this IDictionary it) { return it.Count != 0; } public static bool isEmpty(this string it) { return string.IsNullOrEmpty(it); } public static bool isNotEmpty(this string it) { return !string.IsNullOrEmpty(it); } public static TValue getOrDefault(this IDictionary it, TKey key) { TValue v; it.TryGetValue(key, out v); return v; } public static TValue getOrDefault( this IDictionary it, TKey key, TValue defaultVal) { TValue v = defaultVal; if (key == null) { return v; } it.TryGetValue(key, out v); return v; } public static T first(this IList it) { return it[0]; } public static T last(this IList it) { return it[it.Count - 1]; } public static T removeLast(this IList it) { var lastIndex = it.Count - 1; var result = it[lastIndex]; it.RemoveAt(lastIndex); return result; } public static int hashList(this IList it) { unchecked { var hashCode = 0; if (it != null) { foreach (var item in it) { hashCode = (hashCode * 397) ^ item.GetHashCode(); } } return hashCode; } } public static bool equalsList(this IList it, IList list) { if (it == null && list == null) { return true; } if (ReferenceEquals(it, list)) { return true; } if (it == null || list == null) { return false; } if (it.Count != list.Count) { return false; } for (int i = it.Count - 1; i >= 0; --i) { if (!Equals(it[i], list[i])) { return false; } } return true; } public static string toStringList(this IList it) { if (it == null) { return null; } return "{ " + string.Join(", ", it.Select(item => item.ToString())) + " }"; } public static void reset(this List list, int size) { NoAllocHelpersBridge.EnsureListElemCount(list, size); } public static ref T refAt(this List list, int index) { var array = NoAllocHelpersBridge.ExtractArrayFromListT(list); return ref array[index]; } public static T[] array(this List list) { return NoAllocHelpersBridge.ExtractArrayFromListT(list); } public static List CreateRepeatedList(T value, int length) { List newList = new List(length); for (int i = 0; i < length; i++) { newList.Add(value); } return newList; } } }