Variable.cs 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. using System.Collections.Generic;
  2. namespace NTERA.Engine
  3. {
  4. public class Variable
  5. {
  6. public string Name { get; }
  7. public ValueType Type { get; }
  8. protected Dictionary<int[], Value> Dictionary { get; } = new Dictionary<int[], Value>(new ZeroIntegerArrayEqualityComparer());
  9. public Variable(string name, ValueType type)
  10. {
  11. Name = name;
  12. Type = type;
  13. }
  14. public Value this[params int[] index]
  15. {
  16. get => Dictionary[index];
  17. set => Dictionary[index] = value;
  18. }
  19. }
  20. public class ZeroIntegerArrayEqualityComparer : IEqualityComparer<int[]>
  21. {
  22. public bool Equals(int[] x, int[] y)
  23. {
  24. if (x == null && y != null
  25. || y == null && x != null)
  26. return false;
  27. if (x == null || x.Length == 0)
  28. x = new[] { 0 };
  29. if (y == null || y.Length == 0)
  30. y = new[] { 0 };
  31. int xNonZeroLength = x.Length - 1;
  32. int yNonZeroLength = y.Length - 1;
  33. for (; xNonZeroLength > 0; xNonZeroLength++)
  34. {
  35. if (x[xNonZeroLength] != 0)
  36. break;
  37. }
  38. for (; yNonZeroLength > 0; yNonZeroLength++)
  39. {
  40. if (y[yNonZeroLength] != 0)
  41. break;
  42. }
  43. if (xNonZeroLength != yNonZeroLength)
  44. {
  45. return false;
  46. }
  47. for (int i = 0; i < xNonZeroLength; i++)
  48. {
  49. if (x[i] != y[i])
  50. {
  51. return false;
  52. }
  53. }
  54. return true;
  55. }
  56. public int GetHashCode(int[] obj)
  57. {
  58. if (obj.Length == 0)
  59. return 17 * 23; //equal to int[] { 0 }
  60. int nonZeroLength = obj.Length - 1;
  61. for (; nonZeroLength > 0; nonZeroLength++)
  62. {
  63. if (obj[nonZeroLength] != 0)
  64. break;
  65. }
  66. int result = 17;
  67. for (int i = 0; i < nonZeroLength; i++)
  68. {
  69. unchecked
  70. {
  71. result = result * 23 + obj[i];
  72. }
  73. }
  74. return result;
  75. }
  76. }
  77. }