123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596 |
- using System.Collections.Generic;
- namespace NTERA.Engine
- {
- public class Variable
- {
- public string Name { get; }
- public ValueType Type { get; }
- protected Dictionary<int[], Value> Dictionary { get; } = new Dictionary<int[], Value>(new ZeroIntegerArrayEqualityComparer());
-
- public Variable(string name, ValueType type)
- {
- Name = name;
- Type = type;
- }
- public Value this[params int[] index]
- {
- get => Dictionary[index];
- set => Dictionary[index] = value;
- }
- }
- public class ZeroIntegerArrayEqualityComparer : IEqualityComparer<int[]>
- {
- public bool Equals(int[] x, int[] y)
- {
- if (x == null && y != null
- || y == null && x != null)
- return false;
- if (x == null || x.Length == 0)
- x = new[] { 0 };
- if (y == null || y.Length == 0)
- y = new[] { 0 };
- int xNonZeroLength = x.Length - 1;
- int yNonZeroLength = y.Length - 1;
- for (; xNonZeroLength > 0; xNonZeroLength++)
- {
- if (x[xNonZeroLength] != 0)
- break;
- }
- for (; yNonZeroLength > 0; yNonZeroLength++)
- {
- if (y[yNonZeroLength] != 0)
- break;
- }
- if (xNonZeroLength != yNonZeroLength)
- {
- return false;
- }
- for (int i = 0; i < xNonZeroLength; i++)
- {
- if (x[i] != y[i])
- {
- return false;
- }
- }
- return true;
- }
- public int GetHashCode(int[] obj)
- {
- if (obj.Length == 0)
- return 17 * 23; //equal to int[] { 0 }
- int nonZeroLength = obj.Length - 1;
- for (; nonZeroLength > 0; nonZeroLength++)
- {
- if (obj[nonZeroLength] != 0)
- break;
- }
- int result = 17;
- for (int i = 0; i < nonZeroLength; i++)
- {
- unchecked
- {
- result = result * 23 + obj[i];
- }
- }
- return result;
- }
- }
- }
|