208 lines
7.0 KiB
C#
208 lines
7.0 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using UnityEngine;
|
|
|
|
namespace SLSUtilities.General
|
|
{
|
|
public static class DictionaryExtension
|
|
{
|
|
/// <summary>
|
|
/// 删除所有满足 predicate 的键值对。
|
|
/// </summary>
|
|
public static void RemoveWhere<TKey, TValue>(this IDictionary<TKey, TValue> dict, Func<TKey, TValue, bool> predicate)
|
|
{
|
|
dict.Where(pair => predicate(pair.Key, pair.Value)).Select(pair => pair.Key).ToList().ForEach(key => dict.Remove(key));
|
|
}
|
|
|
|
/// <summary>
|
|
/// 修改字典中指定键的值,如果键不存在则添加该键值对。
|
|
/// </summary>
|
|
public static void ModifyOrAdd(this IDictionary<string, float> dictionary, string key, float delta, float defaultValue = 0)
|
|
{
|
|
if (!dictionary.TryAdd(key, defaultValue + delta))
|
|
{
|
|
dictionary[key] += delta;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 从字典中获取指定键的原始值。
|
|
/// </summary>
|
|
public static float GetRawValue(this Dictionary<string, float> dictionary, string key, float defaultValue = 0)
|
|
{
|
|
return dictionary.GetValueOrDefault(key, defaultValue);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 从字典中获取指定键的值,并将其四舍五入为整数返回。
|
|
/// </summary>
|
|
public static int GetRoundValue(this Dictionary<string, float> dictionary, string key, int defaultValue = 0)
|
|
{
|
|
return dictionary.TryGetValue(key, out float value) ? Mathf.RoundToInt(value) : defaultValue;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 从字典中获取指定键的值,并将其向下取整为整数返回。
|
|
/// </summary>
|
|
public static int GetFloorValue(this Dictionary<string, float> dictionary, string key, int defaultValue = 0)
|
|
{
|
|
return dictionary.TryGetValue(key, out float value) ? Mathf.FloorToInt(value) : defaultValue;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 将源字典中的所有键值对粘贴到目标字典中,避免重复键。
|
|
/// </summary>
|
|
public static void Paste<T1, T2>(this IDictionary<T1, T2> source, IDictionary<T1, T2> 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<string, PrioritizedAction> dictionary)
|
|
{
|
|
foreach (string key in dictionary.Keys.ToList())
|
|
{
|
|
var pAction = dictionary[key];
|
|
pAction.Invoke();
|
|
|
|
if (pAction.ShouldRemove)
|
|
{
|
|
dictionary.Remove(key);
|
|
}
|
|
}
|
|
}
|
|
|
|
public static void Invoke<T>(this IDictionary<string, PrioritizedAction<T>> 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<T0, T1>(this IDictionary<string, PrioritizedAction<T0, T1>> 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<T0, T1, T2>(this IDictionary<string, PrioritizedAction<T0, T1, T2>> 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<string, TR> Invoke<TR>(this IDictionary<string, PrioritizedFunc<TR>> dictionary)
|
|
{
|
|
Dictionary<string, TR> results = new Dictionary<string, TR>();
|
|
|
|
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<string, TR> Invoke<T, TR>(this IDictionary<string, PrioritizedFunc<T, TR>> dictionary, T arg)
|
|
{
|
|
Dictionary<string, TR> results = new Dictionary<string, TR>();
|
|
|
|
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<string, TR> Invoke<T0, T1, TR>(this IDictionary<string, PrioritizedFunc<T0, T1, TR>> dictionary, T0 arg0, T1 arg1)
|
|
{
|
|
Dictionary<string, TR> results = new Dictionary<string, TR>();
|
|
|
|
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<string, TR> Invoke<T0, T1, T2, TR>(this IDictionary<string, PrioritizedFunc<T0, T1, T2, TR>> dictionary, T0 arg0, T1 arg1, T2 arg2)
|
|
{
|
|
Dictionary<string, TR> results = new Dictionary<string, TR>();
|
|
|
|
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
|
|
}
|
|
} |