diff --git a/MiniScript-cpp/src/MiniScript/MiniscriptTypes.cpp b/MiniScript-cpp/src/MiniScript/MiniscriptTypes.cpp index fd3a0e8..8d751c5 100644 --- a/MiniScript-cpp/src/MiniScript/MiniscriptTypes.cpp +++ b/MiniScript-cpp/src/MiniScript/MiniscriptTypes.cpp @@ -700,7 +700,7 @@ namespace MiniScript { ValuePair(const Value& inA, const Value& inB) : a(inA), b(inB) {} ValuePair() {} bool operator==(const ValuePair& rhs) { - // Careful: we muste use RefEqual here to detect reference loops + // Careful: we must use RefEqual here to detect reference loops // below; if we used ==, which does a deep comparison, it could // just send us into an infinite recursion right here. return Value::RefEqual(a, rhs.a) && Value::RefEqual(b, rhs.b); @@ -718,6 +718,7 @@ namespace MiniScript { long aCount = listA.Count(); ValueList listB((ListStorage*)pair.b.data.ref); if (listB.Count() != aCount) return false; + if (Value::RefEqual(pair.a, pair.b)) continue; for (int i=0; i < aCount; i++) { ValuePair newPair(listA[i], listB[i]); if (!visited.Contains(newPair)) toDo.push_back(newPair); @@ -728,6 +729,7 @@ namespace MiniScript { long countA = dictA.Count(); ValueDict dictB((DictionaryStorage*)pair.b.data.ref); if (dictB.Count() != countA) return false; + if (Value::RefEqual(pair.a, pair.b)) continue; ValueList keys = dictA.Keys(); for (int i=0; i 0) { var pair = toDo.Pop(); + visited.Add(pair); if (pair.a is ValList listA) { var listB = pair.b as ValList; if (listB == null) return false; + if (Object.ReferenceEquals(listA, listB)) continue; int aCount = listA.values.Count; if (aCount != listB.values.Count) return false; for (int i = 0; i < aCount; i++) { @@ -203,6 +205,7 @@ protected bool RecursiveEqual(Value rhs) { } else if (pair.a is ValMap mapA) { var mapB = pair.b as ValMap; if (mapB == null) return false; + if (Object.ReferenceEquals(mapA, mapB)) continue; if (mapA.map.Count != mapB.map.Count) return false; foreach (KeyValuePair kv in mapA.map) { Value valFromB; @@ -265,6 +268,18 @@ protected int RecursiveHash() struct ValuePair { public Value a; public Value b; + public override bool Equals(object obj) { + if (obj is ValuePair other) { + return ReferenceEquals(a, other.a) && ReferenceEquals(b, other.b); + } + return false; + } + + public override int GetHashCode() { + unchecked { + return ((a != null ? a.GetHashCode() : 0) * 397) ^ (b != null ? b.GetHashCode() : 0); + } + } } public class ValueSorter : IComparer