generateDeepHashCode static method
Recursively hashes object, including Maps and Iterables.
With sorting, map entries/elements are hashed in a canonical
(hash-code) order first, so the result doesn't depend on iteration order.
Implementation
static int generateDeepHashCode(Object? object, {bool sorting = false}) {
int hash = 12;
if (object == null) {
return hash ^ 0;
} else if (object is Map) {
var entries = object.entries;
if (sorting) {
// Sort by key hash to ensure consistent order
entries =
entries.toList()
..sort((a, b) => a.key.hashCode.compareTo(b.key.hashCode));
}
for (final entry in entries) {
hash =
(hash ^ generateDeepHashCode(entry.key, sorting: sorting)) &
BinaryOps.mask32;
hash =
(hash ^ generateDeepHashCode(entry.value, sorting: sorting)) &
BinaryOps.mask32;
}
} else if (object is Iterable) {
var elements = object;
if (sorting) {
// Sort elements by hash to ensure consistent order
elements =
elements.toList()..sort((a, b) => a.hashCode.compareTo(b.hashCode));
}
for (final element in elements) {
hash =
(hash ^ generateDeepHashCode(element, sorting: sorting)) &
BinaryOps.mask32;
}
} else {
hash = (hash ^ object.hashCode) & BinaryOps.mask32;
}
// Final mix step to reduce collisions
hash = ((hash << 3) | (hash >> 29)) & BinaryOps.mask32;
return hash;
}