Files
aibis-dream/Assets/Scripts/FixSystemNew/StateTransition/StateDictionary.cs
T
2024-12-16 22:09:56 +08:00

71 lines
2.0 KiB
C#

using System;
using System.Collections.Generic;
namespace AibisDream.FixSystem
{
/// <summary>
/// 以两个Key为索引的Dic
/// </summary>
/// <typeparam name="T">Value类型</typeparam>
public class StateDictionary<T>
{
private readonly Dictionary<StateTransKey, T> _stateDic = new();
public T Get(FixState source, FixState target)
{
var key = new StateTransKey(source, target);
return _stateDic[key];
}
public void Add(FixState source, FixState target, T value)
{
var key = new StateTransKey(source, target);
_stateDic[key] = value;
}
public void Clear()
{
_stateDic.Clear();
}
public bool ContainsKey(FixState source, FixState target)
{
var key = new StateTransKey(source, target);
return _stateDic.ContainsKey(key);
}
public bool TryGetValue(FixState source, FixState target, out T value)
{
var key = new StateTransKey(source, target);
return _stateDic.TryGetValue(key, out value);
}
/// <summary>
/// 用于实现两个Key的Dictionary
/// </summary>
private class StateTransKey
{
private readonly FixState _sourceState;
private readonly FixState _targetState;
public StateTransKey(FixState sourceState, FixState targetState)
{
_sourceState = sourceState;
_targetState = targetState;
}
public override bool Equals(object obj)
{
// 类型不同肯定不相等
if (obj is not StateTransKey other) return false;
// 判断两个key相等
return _sourceState == other._sourceState && _targetState == other._targetState;
}
public override int GetHashCode()
{
return HashCode.Combine(_sourceState, _targetState);
}
}
}
}