using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
namespace SLSUtilities.General
{
public static class DictionaryExtension
{
///
/// 删除所有满足 predicate 的键值对。
///
public static void RemoveWhere(this IDictionary dict, Func predicate)
{
dict.Where(pair => predicate(pair.Key, pair.Value)).Select(pair => pair.Key).ToList().ForEach(key => dict.Remove(key));
}
///
/// 修改字典中指定键的值,如果键不存在则添加该键值对。
///
public static void ModifyOrAdd(this IDictionary dictionary, string key, float delta, float defaultValue = 0)
{
if (!dictionary.TryAdd(key, defaultValue + delta))
{
dictionary[key] += delta;
}
}
///
/// 从字典中获取指定键的原始值。
///
public static float GetRawValue(this Dictionary dictionary, string key, float defaultValue = 0)
{
return dictionary.GetValueOrDefault(key, defaultValue);
}
///
/// 从字典中获取指定键的值,并将其四舍五入为整数返回。
///
public static int GetRoundValue(this Dictionary dictionary, string key, int defaultValue = 0)
{
return dictionary.TryGetValue(key, out float value) ? Mathf.RoundToInt(value) : defaultValue;
}
///
/// 从字典中获取指定键的值,并将其向下取整为整数返回。
///
public static int GetFloorValue(this Dictionary dictionary, string key, int defaultValue = 0)
{
return dictionary.TryGetValue(key, out float value) ? Mathf.FloorToInt(value) : defaultValue;
}
///
/// 将源字典中的所有键值对粘贴到目标字典中,避免重复键。
///
public static void Paste(this IDictionary source, IDictionary target)
{
foreach (var pair in source)
{
if (!target.ContainsKey(pair.Key))
{
target[pair.Key] = pair.Value;
}
else
{
Debug.LogWarning($"Attribute \"{pair.Key}\" already exists. Skipping duplicate.");
}
}
}
}
public static class DictionaryExtensionForEvents
{
#region 不含返回值的事件
public static void Invoke(this IDictionary dictionary)
{
foreach (string key in dictionary.Keys.ToList())
{
var pAction = dictionary[key];
pAction.Invoke();
if (pAction.ShouldRemove)
{
dictionary.Remove(key);
}
}
}
public static void Invoke(this IDictionary> dictionary, T arg)
{
foreach (string key in dictionary.Keys.ToList())
{
var pAction = dictionary[key];
pAction.Invoke(arg);
if (pAction.ShouldRemove)
{
dictionary.Remove(key);
}
}
}
public static void Invoke(this IDictionary> dictionary, T0 arg0, T1 arg1)
{
foreach (string key in dictionary.Keys.ToList())
{
var pAction = dictionary[key];
pAction.Invoke(arg0, arg1);
if (pAction.ShouldRemove)
{
dictionary.Remove(key);
}
}
}
public static void Invoke(this IDictionary> dictionary, T0 arg0, T1 arg1, T2 arg2)
{
foreach (string key in dictionary.Keys.ToList())
{
var pAction = dictionary[key];
pAction.Invoke(arg0, arg1, arg2);
if (pAction.ShouldRemove)
{
dictionary.Remove(key);
}
}
}
#endregion
#region 含有返回值的事件
public static Dictionary Invoke(this IDictionary> dictionary)
{
Dictionary results = new Dictionary();
foreach (string key in dictionary.Keys.ToList())
{
var pFunc = dictionary[key];
results[key] = pFunc.Invoke();
if (pFunc.ShouldRemove)
{
dictionary.Remove(key);
}
}
return results;
}
public static Dictionary Invoke(this IDictionary> dictionary, T arg)
{
Dictionary results = new Dictionary();
foreach (string key in dictionary.Keys.ToList())
{
var pFunc = dictionary[key];
results[key] = pFunc.Invoke(arg);
if (pFunc.ShouldRemove)
{
dictionary.Remove(key);
}
}
return results;
}
public static Dictionary Invoke(this IDictionary> dictionary, T0 arg0, T1 arg1)
{
Dictionary results = new Dictionary();
foreach (string key in dictionary.Keys.ToList())
{
var pFunc = dictionary[key];
results[key] = pFunc.Invoke(arg0, arg1);
if (pFunc.ShouldRemove)
{
dictionary.Remove(key);
}
}
return results;
}
public static Dictionary Invoke(this IDictionary> dictionary, T0 arg0, T1 arg1, T2 arg2)
{
Dictionary results = new Dictionary();
foreach (string key in dictionary.Keys.ToList())
{
var pFunc = dictionary[key];
results[key] = pFunc.Invoke(arg0, arg1, arg2);
if (pFunc.ShouldRemove)
{
dictionary.Remove(key);
}
}
return results;
}
#endregion
}
}