using System; using System.Collections.Generic; using System.Linq; using ProtoCSStruct; namespace Sog { public static class GenericUtils { public static void Shuffle(Random rnd, T[] array) { var n = array.Length; while (n > 1) { var k = rnd.Next(n--); (array[n], array[k]) = (array[k], array[n]); } } public static void Shuffle(Random rnd, List list) { for (int i = 0; i < list.Count - 1; i++) { int j = rnd.Next(i, list.Count); (list[i], list[j]) = (list[j], list[i]); } } public static bool RemoveAll(LinkedList list, Predicate predicate) { bool ret = false; var node = list.First; while (node != null) { if (predicate(node.Value)) { list.Remove(node); ret = true; } node = node.Next; } return ret; } public static bool RemoveFirst(LinkedList list, Predicate predicate) { var node = list.First; while (node != null) { if (predicate(node.Value)) { list.Remove(node); return true; } node = node.Next; } return false; } public static LinkedListNode FindFirst(LinkedList list, Predicate predicate) { var node = list.First; while (node != null) { if (predicate(node.Value)) { return node; } node = node.Next; } return null; } public static void Merge(List pool, int type, int id, int chgNum) { if (pool == null) { TypeIDValue32 tiv = new TypeIDValue32() { Type = type, Id = id, Value = (int)chgNum }; pool = new List(); pool.Add(tiv); return; } bool append = false; for (int i = 0; i < pool.Count; i++) { if (pool[i].Type == type && id == pool[i].Id) { var b = pool[i].Value + (int)chgNum; TypeIDValue32 tiv = new TypeIDValue32() { Type = type, Id = id, Value = b }; pool[i] = tiv; append = true; break; } } if (!append) { TypeIDValue32 tiv = new TypeIDValue32() { Type = type, Id = id, Value = (int)chgNum }; pool.Add(tiv); } } public static void Merge(List pool, TypeIDValue32 value) { if (value.Id == 0 || value.Value == 0 || value.Type == 0) { return; } Merge(pool, value.Type, value.Id, value.Value); } public static void Merge(List pool, int[] types, int[] ids, int[] values) { if (types.Length != ids.Length || types.Length != values.Length || ids.Length != values.Length) { return; } for (int i = 0; i < types.Length; i++) { if (types[i] == 0 || ids[i] == 0 || values[i] == 0) { continue; } Merge(pool, types[i], ids[i], values[i]); } } public static List Merge(List pool1, List pool2) { if (pool1 == null) { return pool2; } if (pool2 == null) { return pool1; } var result = new List(); foreach (var t in pool1) { Merge(result, t); } foreach (var t in pool2) { Merge(result, t); } return result; } public static void Merge(List pool, int id, int chgNum) { if (pool == null) { var tiv = new IDValue32() { Id = id, Value = (int)chgNum }; pool = new List(); pool.Add(tiv); return; } bool append = false; for (var i = 0; i < pool.Count; i++) { if (id != pool[i].Id) continue; var b = pool[i].Value + (int)chgNum; var tiv = new IDValue32() { Id = id, Value = b }; pool[i] = tiv; append = true; break; } if (!append) { IDValue32 tiv = new IDValue32() { Id = id, Value = (int)chgNum }; pool.Add(tiv); } } public static void Merge(List pool, IDValue64 value) { if (value.Id == 0 || value.Value == 0) { return; } Merge(pool, value.Id, value.Value); } public static void Merge(List pool, long id, long chgNum) { if (pool == null) { var tiv = new IDValue64() { Id = id, Value = chgNum }; pool = new List(); pool.Add(tiv); return; } bool append = false; for (int i = 0; i < pool.Count; i++) { if (id == pool[i].Id) { var b = pool[i].Value + chgNum; IDValue64 tiv = new IDValue64() { Id = id, Value = b }; pool[i] = tiv; append = true; break; } } if (!append) { IDValue64 tiv = new IDValue64() { Id = id, Value = chgNum }; pool.Add(tiv); } } public static void Merge(List pool,TypeIDValueString value) { Merge(pool, value.Type, value.Id.GetString(), value.Value); } public static void Merge(List pool, int type, string id, int chgNum) { if (pool == null) { var tiv = new TypeIDValueString() { Id = new FixedStructString64(id), Type = type, Value = chgNum }; pool = new List(); pool.Add(tiv); return; } bool append = false; for (int i = 0; i < pool.Count; i++) { if (id == pool[i].Id.GetString()) { var b = pool[i].Value + chgNum; TypeIDValueString tiv = new TypeIDValueString() { Id = new FixedStructString64(id), Value = b, Type = type }; pool[i] = tiv; append = true; break; } } if (!append) { TypeIDValueString tiv = new TypeIDValueString() { Id = new FixedStructString64(id), Value = chgNum, Type = type }; pool.Add(tiv); } } public static void Merge(List pool,List pool2) { if (pool == null || pool2 == null) { return; } foreach (var value in pool2) { Merge(pool, value.Type, value.Id.GetString(), value.Value); } } public static List MergedDuplicates(IEnumerable list) { return list.GroupBy(item => item.Id).Select(group => new IDValue32() { Id = group.Key, Value = group.Select(ob => ob.Value).Sum() } ).ToList(); } public static List MergedDuplicates(IEnumerable list) { return list.GroupBy(item => item.Id).Select(group => new IDValue64() { Id = group.Key, Value = group.Select(ob => ob.Value).Sum() } ).ToList(); } public static List MergedDuplicates(IEnumerable list) { var tempList = list.GroupBy(item => item.Type); var result = new List(); foreach (var temp in tempList) { var type = temp.Key; var items = temp.GroupBy(item => item.Id); result.AddRange(from item in items let id = item.Key let value = item.Sum(i => i.Value) select new TypeIDValue32 { Type = type, Id = id, Value = value }); } return result; } public static List MergedDuplicates(List list) { if (list == null || !list.Any()) { return list; } var tempList = list.GroupBy(item => item.type); var result = new List(); foreach (var temp in tempList) { var type = temp.Key; var items = temp.GroupBy(item => item.id); result.AddRange(from item in items let id = item.Key let value = item.Sum(i => i.value) select new TypeIDValue32Class { type = type, id = id, value = value }); } return result; } public static List Merge(List pool1, TypeIDValue32[] freeAward) { if (pool1 == null) { return freeAward.ToList(); } if (freeAward == null) { return pool1; } var result = new List(); foreach (var t in pool1) { Merge(result, t); } foreach (var t in freeAward) { Merge(result, t); } return result; } } }