using System; using System.Collections.Generic; using UnityEngine; public static class KData { public static Dictionary BuildMap(Dictionary map, List list, Func func) { map ??= new Dictionary(); map.Clear(); if (list == null || list.Count == 0) return map; for (var i = 0; i < list.Count; i++) { TList item = list[i]; if (item == null) continue; (K key, V value) = func(item, i); if (key == null) { Debug.LogWarning($"Key should not be null!"); continue; } if (map.ContainsKey(key)) { Debug.LogWarning($"Duplicated key found <{key}!"); continue; } map.Add(key, value); } return map; } public static Dictionary BuildMap(this List list, Func func, Dictionary result = null) { return BuildMap(result, list, (item, _) => func(item)); } public static Dictionary BuildMap(this List list, Func func, Dictionary result = null) { return BuildMap(result, list, (item, _)=> (func(item), item)); } public static void Compact(List list) { // Remove all null items var idx = -1; var count = list.Count; for (var i = 0; i < count; i++) //shift items up in O(N) fashion { var isNull = list[i] == null; if (isNull) continue; // skip null items idx++; if (idx == i) continue; // did not found any null since start list[idx] = list[i]; // there were some null found, and now we need to migrate data items to the left } list.RemoveRange(idx+1, count-1-idx); } public static void Shuffle(List list) { throw new NotImplementedException(); } public static void Resize(List list) { throw new NotImplementedException(); } }