IndexMap<K, V> constructor
Creates an insertion-ordered indexing based Map.
Equality and Hashing
If equals is provided, it is usd to compare the keys in the table with
new keys. If it is omitted, the key's own Object.== is used instead.
Similarly, if hashCode is provided, it is used to produce a hash value
for keys in order to place them in the hash table, and if omitted, the
key's own Object.hashCode is used instead.
The used equals and hashCode functions must form an equivalence
relation, and must be consistent with each other, so that if
equals(a, b) then hashCode(a) == hashCode(b). The hash of an object,
or what it compares equal to, should not change while the object is in
the table. If it does change, the result is unpredictable.
If you supply one of equals or hashCode, you should supply both.
Some equals and hashCode functions might not work for all objects. If
isValidKey is provided, it's used to check a potential key which is not
necessarily an instance of K, like the arguments of [], remove, and
containsKey, which are typed as Object?. If isValidKey returns
false for an object, the equals and hashCode functions are not
called, and no key equal to that object is assumed to be in the map.
The isValidKey function defaults is E if omitted.
Implementation
@pragma('vm:prefer-inline')
factory IndexMap({
bool Function(K e1, K e2)? equals,
int Function(K e)? hashCode,
bool Function(Object? potentialKey)? isValidKey,
}) {
if (isValidKey == null) {
if (hashCode == null) {
if (equals == null) {
return _IndexMap<K, V>();
}
hashCode = _defaultHashCode;
} else {
if (identical(identityHashCode, hashCode) &&
identical(identical, equals)) {
return IndexMap.identity();
}
equals ??= _defaultEquals;
}
} else {
equals ??= _defaultEquals;
hashCode ??= _defaultHashCode;
}
return _IndexMap<K, V>(
HashMap<K, int>(
equals: equals,
hashCode: hashCode,
isValidKey: isValidKey,
),
);
}