namespace adas_core.Domain.Utils;
///
/// Represents a sealed dictionary that inherits from and provides equality comparison with instances through .
///
/// The type of the keys stored in the dictionary, constrained to be non-null.
/// The type of the values stored in the dictionary, constrained to be non-null.
///
/// The implementation targets rather than the declaring type, enabling cross-type equality semantics between the two dictionary variants.
///
///
public sealed class EquatableDictionary
: Dictionary, IEquatable>
where TKey : notnull where TValue : notnull
{
///
/// Determines whether the current dictionary is equal to another by comparing their counts and key-value pairs.
/// Returns false if the other dictionary is null, has a different count, is missing any key present in this dictionary, or contains a different value for any shared key.
///
/// The dictionary to compare against this instance.
/// true if both dictionaries contain the same keys with equal values; otherwise, false.
///
public bool Equals(ComparableDictionary? other)
{
if (other is null) return false;
if (Count != other.Count) return false;
foreach (var pair in this)
{
if (!other.TryGetValue(pair.Key, out var otherValue)) return false;
if (!EqualityComparer.Default.Equals(pair.Value, otherValue)) return false;
}
return true;
}
//private readonly Dictionary dictionary = new();
///
/// Determines whether the current instance is equal to the specified object by attempting to cast it to a and delegating to the typed equality comparison. Returns false when the supplied object is not a .
///
/// The object to compare with the current instance.
/// true if is a and is equal to the current instance; otherwise, false.
///
public override bool Equals(object? other)
{
return Equals(other as ComparableDictionary);
}
///
/// Computes a hash code for the collection by combining the hash codes of each key-value pair.
/// Each pair contributes a hash derived from its key and value, and the per-pair hashes are combined using XOR.
///
/// An integer hash code that represents the contents of the collection.
///
public override int GetHashCode()
{
var hash = 0;
foreach (var pair in this)
{
var miniHash = 17;
miniHash = miniHash * 31 +
EqualityComparer.Default.GetHashCode(pair.Key);
miniHash = miniHash * 31 +
EqualityComparer.Default.GetHashCode(pair.Value);
hash ^= miniHash;
}
return hash;
}
// Implementation of IDictionary<,> which just delegates to the dictionary
}