using System; using System.Collections; using System.Collections.Generic; using UnityEngine; public static class CustomExtension { /// /// insert value to list and sort. /// static public void OrderInsert(this IList arr, T value, Func comparison) { arr.Add(value); for (int i = arr.Count - 1; i > 0; --i) { if (comparison(arr[i - 1], arr[i]) > 0) { T tmp = arr[i]; arr[i - 1] = arr[i]; arr[i] = tmp; } else { break; } } } /// /// sort list. /// static public void InsertionSort(this IList arr, Func comparison) { for (int i = 1; i < arr.Count; ++i) { for (int j = i; j > 0; --j) { if (comparison(arr[j - 1], arr[j]) > 0) { T tmp = arr[j]; arr[j] = arr[j - 1]; arr[j - 1] = tmp; } else { break; } } } } /// /// sort list. /// static public void InsertionSort(this IList arr, Func comparison) { InsertionSort(arr, (a, b) => comparison(a, b) ? 1 : -1); } /// /// insert value to dictionary, if key is exist, replace value. /// static public void SafeInsert(this IDictionary dic, TKey key, TValue value) { if (dic.ContainsKey(key)) dic[key] = value; else dic.Add(key, value); } /// /// insert value to dictionary, if key is exist, replace value. /// static public void SafeInsert(this IDictionary dic, KeyValuePair kvPair) { SafeInsert(dic, kvPair.Key, kvPair.Value); } /// /// get value from dictionary, if key is not exist, return default value. /// static public TValue SafeGet(this IDictionary dic, TKey key) { if (dic.ContainsKey(key)) return dic[key]; else return default; } static public bool TryGetRandomValue(this IDictionary dic, out TValue result) { result = default; int rnd = UnityEngine.Random.Range(0, dic.Count); foreach (var kv in dic) { if (rnd-- == 0) { result = kv.Value; return true; } } return false; } /// /// get value from list, if index less than 0, return first value. if index greater than list count, return last value. /// static public TValue ClampGet(this IList list, int index) { if(list.Count == 0) throw new Exception("list is empty"); if(index < 0) return list[0]; else if (index >= list.Count) return list[list.Count - 1]; else return list[index]; } /// /// try get value from list, if index is not valid, return false. /// static public bool TryGetValueAt(this IList list, int index, out TValue result) { if(index >= 0 && index < list.Count) { result = list[index]; return true; } else { result = default; return false; } } /// /// set elements with given range to value. /// static public void SetElements(this IList list, TValue value, int offset, int count) { for(int i = offset; i < offset + count; ++i) { if(i < 0 || i >= list.Count) continue; list[i] = value; } } /// /// set all elements to value. /// static public void SetElements(this IList list, TValue value) { SetElements(list, value, 0, list.Count); } /// /// swapping elements with predict /// static public int SwapElements(this IList list, Func predict) { const int needSetFlag = 0; const int trueFlag = 1; const int falseFlag = 2; int ret = -1; int[] flags = new int[list.Count]; for(int i = 0; i < list.Count; ++i) { if (flags[i] == needSetFlag) flags[i] = predict(list[i]) ? trueFlag : falseFlag; if (flags[i] == trueFlag) continue; int j; for(j = i + 1; j < list.Count; ++j) { if (flags[j] == needSetFlag) flags[j] = predict(list[j]) ? trueFlag : falseFlag; if (flags[j] == falseFlag) continue; TValue tmp = list[i]; list[i] = list[j]; list[j] = tmp; flags[i] = trueFlag; flags[j] = falseFlag; break; } ret = i; if(j == list.Count) break; } return ret; } }