BiDictionary.cs 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. // RichTextKit
  2. // Copyright © 2019-2020 Topten Software. All Rights Reserved.
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License"); you may
  5. // not use this product except in compliance with the License. You may obtain
  6. // a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. // License for the specific language governing permissions and limitations
  14. // under the License.
  15. using System;
  16. using System.Collections.Generic;
  17. using System.Text;
  18. namespace Topten.RichTextKit.Utils
  19. {
  20. /// <summary>
  21. /// A simple bi-directional dictionary
  22. /// </summary>
  23. /// <typeparam name="T1">Key type</typeparam>
  24. /// <typeparam name="T2">Value type</typeparam>
  25. class BiDictionary<T1, T2>
  26. {
  27. public Dictionary<T1, T2> Forward = new Dictionary<T1, T2>();
  28. public Dictionary<T2, T1> Reverse = new Dictionary<T2, T1>();
  29. public void Clear()
  30. {
  31. Forward.Clear();
  32. Reverse.Clear();
  33. }
  34. public void Add(T1 key, T2 value)
  35. {
  36. Forward.Add(key, value);
  37. Reverse.Add(value, key);
  38. }
  39. public bool TryGetValue(T1 key, out T2 value)
  40. {
  41. return Forward.TryGetValue(key, out value);
  42. }
  43. public bool TryGetKey(T2 value, out T1 key)
  44. {
  45. return Reverse.TryGetValue(value, out key);
  46. }
  47. public bool ContainsKey(T1 key)
  48. {
  49. return Forward.ContainsKey(key);
  50. }
  51. public bool ContainsValue(T2 value)
  52. {
  53. return Reverse.ContainsKey(value);
  54. }
  55. }
  56. }