首次提交
This commit is contained in:
144
Assets/Scripts/NBC/Core/Runtime/Structures/DoubleMap.cs
Normal file
144
Assets/Scripts/NBC/Core/Runtime/Structures/DoubleMap.cs
Normal file
@@ -0,0 +1,144 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace NBC
|
||||
{
|
||||
public class DoubleMap<K, V>
|
||||
{
|
||||
private readonly Dictionary<K, V> kv = new();
|
||||
private readonly Dictionary<V, K> vk = new();
|
||||
|
||||
public DoubleMap()
|
||||
{
|
||||
}
|
||||
|
||||
public DoubleMap(int capacity)
|
||||
{
|
||||
kv = new Dictionary<K, V>(capacity);
|
||||
vk = new Dictionary<V, K>(capacity);
|
||||
}
|
||||
|
||||
public void ForEach(Action<K, V> action)
|
||||
{
|
||||
if (action == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
Dictionary<K, V>.KeyCollection keys = kv.Keys;
|
||||
foreach (K key in keys)
|
||||
{
|
||||
action(key, kv[key]);
|
||||
}
|
||||
}
|
||||
|
||||
public List<K> Keys
|
||||
{
|
||||
get
|
||||
{
|
||||
return new List<K>(kv.Keys);
|
||||
}
|
||||
}
|
||||
|
||||
public List<V> Values
|
||||
{
|
||||
get
|
||||
{
|
||||
return new List<V>(vk.Keys);
|
||||
}
|
||||
}
|
||||
|
||||
public void Add(K key, V value)
|
||||
{
|
||||
if (key == null || value == null || kv.ContainsKey(key) || vk.ContainsKey(value))
|
||||
{
|
||||
return;
|
||||
}
|
||||
kv.Add(key, value);
|
||||
vk.Add(value, key);
|
||||
}
|
||||
|
||||
public V GetValueByKey(K key)
|
||||
{
|
||||
if (key != null && kv.ContainsKey(key))
|
||||
{
|
||||
return kv[key];
|
||||
}
|
||||
return default(V);
|
||||
}
|
||||
|
||||
public K GetKeyByValue(V value)
|
||||
{
|
||||
if (value != null && vk.ContainsKey(value))
|
||||
{
|
||||
return vk[value];
|
||||
}
|
||||
return default(K);
|
||||
}
|
||||
|
||||
public void RemoveByKey(K key)
|
||||
{
|
||||
if (key == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
V value;
|
||||
if (!kv.TryGetValue(key, out value))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
kv.Remove(key);
|
||||
vk.Remove(value);
|
||||
}
|
||||
|
||||
public void RemoveByValue(V value)
|
||||
{
|
||||
if (value == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
K key;
|
||||
if (!vk.TryGetValue(value, out key))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
kv.Remove(key);
|
||||
vk.Remove(value);
|
||||
}
|
||||
|
||||
public void Clear()
|
||||
{
|
||||
kv.Clear();
|
||||
vk.Clear();
|
||||
}
|
||||
|
||||
public bool ContainsKey(K key)
|
||||
{
|
||||
if (key == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return kv.ContainsKey(key);
|
||||
}
|
||||
|
||||
public bool ContainsValue(V value)
|
||||
{
|
||||
if (value == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return vk.ContainsKey(value);
|
||||
}
|
||||
|
||||
public bool Contains(K key, V value)
|
||||
{
|
||||
if (key == null || value == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return kv.ContainsKey(key) && vk.ContainsKey(value);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 512992ac5b0b472fb720cfbd3a0d768a
|
||||
timeCreated: 1733994332
|
||||
@@ -0,0 +1,88 @@
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace NBC
|
||||
{
|
||||
public class UnOrderMultiMapSet<T, K> : Dictionary<T, HashSet<K>>
|
||||
{
|
||||
// 重用HashSet
|
||||
public new HashSet<K> this[T t]
|
||||
{
|
||||
get
|
||||
{
|
||||
HashSet<K> set;
|
||||
if (!this.TryGetValue(t, out set))
|
||||
{
|
||||
set = new HashSet<K>();
|
||||
}
|
||||
|
||||
return set;
|
||||
}
|
||||
}
|
||||
|
||||
public Dictionary<T, HashSet<K>> GetDictionary()
|
||||
{
|
||||
return this;
|
||||
}
|
||||
|
||||
public void Add(T t, K k)
|
||||
{
|
||||
HashSet<K> set;
|
||||
this.TryGetValue(t, out set);
|
||||
if (set == null)
|
||||
{
|
||||
set = new HashSet<K>();
|
||||
base[t] = set;
|
||||
}
|
||||
|
||||
set.Add(k);
|
||||
}
|
||||
|
||||
public bool Remove(T t, K k)
|
||||
{
|
||||
HashSet<K> set;
|
||||
this.TryGetValue(t, out set);
|
||||
if (set == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!set.Remove(k))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (set.Count == 0)
|
||||
{
|
||||
this.Remove(t);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool Contains(T t, K k)
|
||||
{
|
||||
HashSet<K> set;
|
||||
this.TryGetValue(t, out set);
|
||||
if (set == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return set.Contains(k);
|
||||
}
|
||||
|
||||
public new int Count
|
||||
{
|
||||
get
|
||||
{
|
||||
int count = 0;
|
||||
foreach (KeyValuePair<T, HashSet<K>> kv in this)
|
||||
{
|
||||
count += kv.Value.Count;
|
||||
}
|
||||
|
||||
return count;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: f7f20db4c53f4bd1959c0bfcb4e1c8db
|
||||
timeCreated: 1733995067
|
||||
Reference in New Issue
Block a user