You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

376 lines
11 KiB

using System;
using System.Collections.Generic;
using System.Linq;
using ProtoCSStruct;
namespace Sog
{
public static class GenericUtils
{
public static void Shuffle<T>(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<T>(Random rnd, List<T> 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<T>(LinkedList<T> list, Predicate<T> 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<T>(LinkedList<T> list, Predicate<T> 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<T> FindFirst<T>(LinkedList<T> list, Predicate<T> predicate)
{
var node = list.First;
while (node != null)
{
if (predicate(node.Value))
{
return node;
}
node = node.Next;
}
return null;
}
public static void Merge(List<TypeIDValue32> pool, int type, int id, int chgNum)
{
if (pool == null)
{
TypeIDValue32 tiv = new TypeIDValue32() { Type = type, Id = id, Value = (int)chgNum };
pool = new List<TypeIDValue32>();
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<TypeIDValue32> 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<TypeIDValue32> 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<TypeIDValue32> Merge(List<TypeIDValue32> pool1, List<TypeIDValue32> pool2)
{
if (pool1 == null)
{
return pool2;
}
if (pool2 == null)
{
return pool1;
}
var result = new List<TypeIDValue32>();
foreach (var t in pool1)
{
Merge(result, t);
}
foreach (var t in pool2)
{
Merge(result, t);
}
return result;
}
public static void Merge(List<IDValue32> pool, int id, int chgNum)
{
if (pool == null)
{
var tiv = new IDValue32() { Id = id, Value = (int)chgNum };
pool = new List<IDValue32>();
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<IDValue64> pool, IDValue64 value)
{
if (value.Id == 0 || value.Value == 0)
{
return;
}
Merge(pool, value.Id, value.Value);
}
public static void Merge(List<IDValue64> pool, long id, long chgNum)
{
if (pool == null)
{
var tiv = new IDValue64() { Id = id, Value = chgNum };
pool = new List<IDValue64>();
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<TypeIDValueString> pool,TypeIDValueString value)
{
Merge(pool, value.Type, value.Id.GetString(), value.Value);
}
public static void Merge(List<TypeIDValueString> 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<TypeIDValueString>();
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<TypeIDValueString> pool,List<TypeIDValueString> pool2)
{
if (pool == null || pool2 == null)
{
return;
}
foreach (var value in pool2)
{
Merge(pool, value.Type, value.Id.GetString(), value.Value);
}
}
public static List<IDValue32> MergedDuplicates(IEnumerable<IDValue32> 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<IDValue64> MergedDuplicates(IEnumerable<IDValue64> 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<TypeIDValue32> MergedDuplicates(IEnumerable<TypeIDValue32> list)
{
var tempList = list.GroupBy(item => item.Type);
var result = new List<TypeIDValue32>();
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<TypeIDValue32Class> MergedDuplicates(List<TypeIDValue32Class> list)
{
if (list == null || !list.Any())
{
return list;
}
var tempList = list.GroupBy(item => item.type);
var result = new List<TypeIDValue32Class>();
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<TypeIDValue32> Merge(List<TypeIDValue32> pool1, TypeIDValue32[] freeAward)
{
if (pool1 == null)
{
return freeAward.ToList();
}
if (freeAward == null)
{
return pool1;
}
var result = new List<TypeIDValue32>();
foreach (var t in pool1)
{
Merge(result, t);
}
foreach (var t in freeAward)
{
Merge(result, t);
}
return result;
}
}
}