quantum-space-buddies/EpicOnlineTransport/BidirectionalDictionary.cs

130 lines
3.9 KiB
C#
Raw Normal View History

2022-02-06 02:06:25 +00:00
using System.Collections;
using System.Collections.Generic;
/// <summary>
/// Copyright
/// MIT License
///
/// Copyright Fizz Cube Ltd(c) 2018
///
/// Permission is hereby granted, free of charge, to any person obtaining a copy
/// of this software and associated documentation files (the "Software"), to deal
/// in the Software without restriction, including without limitation the rights
/// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
/// copies of the Software, and to permit persons to whom the Software is
/// furnished to do so, subject to the following conditions:
///
/// The above copyright notice and this permission notice shall be included in all
/// copies or substantial portions of the Software.
///
/// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
/// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
/// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
/// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
/// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
/// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
/// SOFTWARE.
///
/// ===
///
/// Copyright Marco Hoffmann(c) 2020
///
/// Permission is hereby granted, free of charge, to any person obtaining a copy
/// of this software and associated documentation files (the "Software"), to deal
/// in the Software without restriction, including without limitation the rights
/// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
/// copies of the Software, and to permit persons to whom the Software is
///furnished to do so, subject to the following conditions:
///
/// The above copyright notice and this permission notice shall be included in all
/// copies or substantial portions of the Software.
///
/// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
/// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
/// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
/// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
/// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
/// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
/// SOFTWARE.
///
/// MIT License
/// </summary>
2022-03-03 03:46:33 +00:00
namespace EpicTransport;
public class BidirectionalDictionary<T1, T2> : IEnumerable
2022-02-06 04:17:08 +00:00
{
2022-03-03 03:46:33 +00:00
private Dictionary<T1, T2> t1ToT2Dict = new();
private Dictionary<T2, T1> t2ToT1Dict = new();
2022-02-06 04:17:08 +00:00
2022-03-03 03:46:33 +00:00
public IEnumerable<T1> FirstTypes => t1ToT2Dict.Keys;
public IEnumerable<T2> SecondTypes => t2ToT1Dict.Keys;
2022-02-06 04:17:08 +00:00
2022-03-03 03:46:33 +00:00
public IEnumerator GetEnumerator() => t1ToT2Dict.GetEnumerator();
2022-02-06 04:17:08 +00:00
2022-03-03 03:46:33 +00:00
public int Count => t1ToT2Dict.Count;
2022-02-06 04:17:08 +00:00
2022-03-03 03:46:33 +00:00
public void Add(T1 key, T2 value)
{
t1ToT2Dict[key] = value;
t2ToT1Dict[value] = key;
}
2022-02-06 04:17:08 +00:00
2022-03-03 03:46:33 +00:00
public void Add(T2 key, T1 value)
{
t2ToT1Dict[key] = value;
t1ToT2Dict[value] = key;
}
2022-02-06 04:17:08 +00:00
2022-03-03 03:46:33 +00:00
public T2 Get(T1 key) => t1ToT2Dict[key];
2022-02-06 04:17:08 +00:00
2022-03-03 03:46:33 +00:00
public T1 Get(T2 key) => t2ToT1Dict[key];
2022-02-06 04:17:08 +00:00
2022-03-03 03:46:33 +00:00
public bool TryGetValue(T1 key, out T2 value) => t1ToT2Dict.TryGetValue(key, out value);
2022-02-06 04:17:08 +00:00
2022-03-03 03:46:33 +00:00
public bool TryGetValue(T2 key, out T1 value) => t2ToT1Dict.TryGetValue(key, out value);
2022-02-06 04:17:08 +00:00
2022-03-03 03:46:33 +00:00
public bool Contains(T1 key) => t1ToT2Dict.ContainsKey(key);
2022-02-06 04:17:08 +00:00
2022-03-03 03:46:33 +00:00
public bool Contains(T2 key) => t2ToT1Dict.ContainsKey(key);
2022-02-06 04:17:08 +00:00
2022-03-03 03:46:33 +00:00
public void Remove(T1 key)
{
if (Contains(key))
2022-02-06 04:17:08 +00:00
{
2022-03-03 03:46:33 +00:00
var val = t1ToT2Dict[key];
t1ToT2Dict.Remove(key);
t2ToT1Dict.Remove(val);
2022-02-06 04:17:08 +00:00
}
2022-03-03 03:46:33 +00:00
}
2022-02-06 04:17:08 +00:00
2022-03-03 03:46:33 +00:00
public void Remove(T2 key)
{
if (Contains(key))
2022-02-06 04:17:08 +00:00
{
2022-03-03 03:46:33 +00:00
var val = t2ToT1Dict[key];
t1ToT2Dict.Remove(val);
t2ToT1Dict.Remove(key);
2022-02-06 04:17:08 +00:00
}
2022-03-03 03:46:33 +00:00
}
2022-02-06 04:17:08 +00:00
2022-03-03 03:46:33 +00:00
public T1 this[T2 key]
{
get => t2ToT1Dict[key];
set
2022-02-06 04:17:08 +00:00
{
2022-03-03 03:46:33 +00:00
t2ToT1Dict[key] = value;
t1ToT2Dict[value] = key;
2022-02-06 04:17:08 +00:00
}
2022-03-03 03:46:33 +00:00
}
2022-02-06 04:17:08 +00:00
2022-03-03 03:46:33 +00:00
public T2 this[T1 key]
{
get => t1ToT2Dict[key];
set
2022-02-06 04:17:08 +00:00
{
2022-03-03 03:46:33 +00:00
t1ToT2Dict[key] = value;
t2ToT1Dict[value] = key;
2022-02-06 04:17:08 +00:00
}
}
}