using System; using System.Collections.Generic; using Unity.VisualScripting; namespace NBF { public static class DataArrayExtends { public static T Find(this IList obj, Predicate match) { if (obj == null) throw new ArgumentNullException(); if (match == null) throw new ArgumentNullException(); for (int i = 0; i < obj.Count; i++) { if (match(obj[i])) { return obj[i]; } } return default(T); } public static int FindIndex(this IList obj, Predicate match) { return FindIndex(obj, 0, obj.Count, match); } public static int FindIndex(this IList obj, int startIndex, int count, Predicate match) { if (obj == null) throw new ArgumentNullException(); if (startIndex < 0 || startIndex > count) throw new ArgumentOutOfRangeException(); if (count < 0 || startIndex > obj.Count - count) throw new ArgumentOutOfRangeException(); int num = startIndex + count; for (int i = startIndex; i < num; i++) { if (match(obj[i])) { return i; } } return -1; } public static void ForEach(this IList obj, Action action) { if (obj == null) throw new ArgumentNullException(); if (action == null) throw new ArgumentNullException(); for (int i = 0; i < obj.Count; i++) { action(obj[i]); } } public static List FindAll(this IList obj, Predicate match) { if (obj == null) throw new ArgumentNullException(); if (match == null) throw new ArgumentNullException(); List list = new List(); for (int i = 0; i < obj.Count; i++) { if (match(obj[i])) list.Add(obj[i]); } return list; } public static int RemoveAll(this IList obj, Predicate match) { if (obj == null) throw new ArgumentNullException(); if (match == null) throw new ArgumentNullException(); int originalSize = obj.Count; int index = 0; while (index < obj.Count) { if (match(obj[index])) { obj.RemoveAt(index); } else { index++; } } return originalSize - obj.Count; } public static void Sort(this IList obj, Comparison comparison) { Sort(obj, 0, obj.Count, comparison); } public static void Sort(this IList obj, int index, int count, Comparison comparison) { if (obj == null) throw new ArgumentNullException(); if (index < 0 || count < 0) throw new ArgumentOutOfRangeException(); if (obj.Count - index < count) throw new ArgumentException(); AlgorithmUtils.QuickSortRecursive(obj, index, count - 1, comparison); } public static void RemoveRange(this IList obj, int index, int count) { var list = new List(obj); list.RemoveRange(index, count); obj.Clear(); obj.AddRange(list); } public static void Overlay(this IList obj, IEnumerable collection) { if (obj != collection) { obj.Clear(); obj.AddRange(collection); } } } }