You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

93 lines
3.7 KiB

  1. #region License
  2. // Copyright (c) 2007 James Newton-King
  3. //
  4. // Permission is hereby granted, free of charge, to any person
  5. // obtaining a copy of this software and associated documentation
  6. // files (the "Software"), to deal in the Software without
  7. // restriction, including without limitation the rights to use,
  8. // copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the
  10. // Software is furnished to do so, subject to the following
  11. // conditions:
  12. //
  13. // The above copyright notice and this permission notice shall be
  14. // included in all copies or substantial portions of the Software.
  15. //
  16. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  17. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  18. // OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  19. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  20. // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  21. // WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  22. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  23. // OTHER DEALINGS IN THE SOFTWARE.
  24. #endregion
  25. using System;
  26. using System.Collections.Generic;
  27. using System.Globalization;
  28. namespace Newtonsoft.Json.Utilities
  29. {
  30. internal class BidirectionalDictionary<TFirst, TSecond>
  31. {
  32. private readonly IDictionary<TFirst, TSecond> _firstToSecond;
  33. private readonly IDictionary<TSecond, TFirst> _secondToFirst;
  34. private readonly string _duplicateFirstErrorMessage;
  35. private readonly string _duplicateSecondErrorMessage;
  36. public BidirectionalDictionary()
  37. : this(EqualityComparer<TFirst>.Default, EqualityComparer<TSecond>.Default)
  38. {
  39. }
  40. public BidirectionalDictionary(IEqualityComparer<TFirst> firstEqualityComparer, IEqualityComparer<TSecond> secondEqualityComparer)
  41. : this(
  42. firstEqualityComparer,
  43. secondEqualityComparer,
  44. "Duplicate item already exists for '{0}'.",
  45. "Duplicate item already exists for '{0}'.")
  46. {
  47. }
  48. public BidirectionalDictionary(IEqualityComparer<TFirst> firstEqualityComparer, IEqualityComparer<TSecond> secondEqualityComparer,
  49. string duplicateFirstErrorMessage, string duplicateSecondErrorMessage)
  50. {
  51. _firstToSecond = new Dictionary<TFirst, TSecond>(firstEqualityComparer);
  52. _secondToFirst = new Dictionary<TSecond, TFirst>(secondEqualityComparer);
  53. _duplicateFirstErrorMessage = duplicateFirstErrorMessage;
  54. _duplicateSecondErrorMessage = duplicateSecondErrorMessage;
  55. }
  56. public void Set(TFirst first, TSecond second)
  57. {
  58. if (_firstToSecond.TryGetValue(first, out TSecond existingSecond))
  59. {
  60. if (!existingSecond.Equals(second))
  61. {
  62. throw new ArgumentException(_duplicateFirstErrorMessage.FormatWith(CultureInfo.InvariantCulture, first));
  63. }
  64. }
  65. if (_secondToFirst.TryGetValue(second, out TFirst existingFirst))
  66. {
  67. if (!existingFirst.Equals(first))
  68. {
  69. throw new ArgumentException(_duplicateSecondErrorMessage.FormatWith(CultureInfo.InvariantCulture, second));
  70. }
  71. }
  72. _firstToSecond.Add(first, second);
  73. _secondToFirst.Add(second, first);
  74. }
  75. public bool TryGetByFirst(TFirst first, out TSecond second)
  76. {
  77. return _firstToSecond.TryGetValue(first, out second);
  78. }
  79. public bool TryGetBySecond(TSecond second, out TFirst first)
  80. {
  81. return _secondToFirst.TryGetValue(second, out first);
  82. }
  83. }
  84. }