HKMBFZ/Assets/SKFramework/Core/Extension/DictionaryExtension.cs

112 lines
3.8 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.Collections.Generic;
namespace SK.Framework
{
public static class DictionaryExtension
{
public static Dictionary<K, V> Copy<K, V>(this Dictionary<K, V> self)
{
Dictionary<K, V> retDic = new Dictionary<K, V>(self.Count);
using (var dicE = self.GetEnumerator())
{
while (dicE.MoveNext())
{
retDic.Add(dicE.Current.Key, dicE.Current.Value);
}
}
return retDic;
}
public static Dictionary<K, V> ForEach<K, V>(this Dictionary<K, V> self, Action<K, V> action)
{
using(var dicE = self.GetEnumerator())
{
while (dicE.MoveNext())
{
action(dicE.Current.Key, dicE.Current.Value);
}
}
return self;
}
/// <summary>
/// 合并字典
/// </summary>
/// <typeparam name="K">键类型</typeparam>
/// <typeparam name="V">值类型</typeparam>
/// <param name="self">字典</param>
/// <param name="target">被合并的字典</param>
/// <param name="isOverride">若存在相同键,是否覆盖对应值</param>
/// <returns>合并后的字典</returns>
public static Dictionary<K, V> AddRange<K, V>(this Dictionary<K, V> self, Dictionary<K, V> target, bool isOverride = false)
{
using(var dicE = target.GetEnumerator())
{
while (dicE.MoveNext())
{
var current = dicE.Current;
if (self.ContainsKey(current.Key))
{
if (isOverride)
{
self[current.Key] = current.Value;
continue;
}
}
self.Add(current.Key, current.Value);
}
}
return self;
}
/// <summary>
/// 将字典的所有值放入一个列表
/// </summary>
/// <typeparam name="K">键类型</typeparam>
/// <typeparam name="V">值类型</typeparam>
/// <param name="self">字典</param>
/// <returns>列表</returns>
public static List<V> Value2List<K, V>(this Dictionary<K, V> self)
{
List<V> retList = new List<V>(self.Count);
foreach (var kv in self)
{
retList.Add(kv.Value);
}
return retList;
}
/// <summary>
/// 将字典的所有值放入一个数组
/// </summary>
/// <typeparam name="K">键类型</typeparam>
/// <typeparam name="V">值类型</typeparam>
/// <param name="self">字典</param>
/// <returns>数组</returns>
public static V[] Value2Array<K, V>(this Dictionary<K, V> self)
{
V[] retArray = new V[self.Count];
int index = -1;
foreach (var kv in self)
{
retArray[++index] = kv.Value;
}
return retArray;
}
/// <summary>
/// 尝试添加
/// </summary>
/// <typeparam name="K">键类型</typeparam>
/// <typeparam name="V">值类型</typeparam>
/// <param name="self">字典</param>
/// <param name="k">键</param>
/// <param name="v">值</param>
/// <returns>若不存在相同键添加成功并返回true否则返回false</returns>
public static bool TryAdd<K, V>(this Dictionary<K, V> self, K k, V v)
{
if (!self.ContainsKey(k))
{
self.Add(k, v);
return true;
}
return false;
}
}
}