IndexSet<E> constructor
Creates an insertion-ordered indexing based Set.
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 E
, like the arguments of []
, remove, and
contains, 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 set.
The isValidKey
function defaults is E
if omitted.
Implementation
@pragma('vm:prefer-inline')
factory IndexSet({
bool Function(E e1, E e2)? equals,
int Function(E e)? hashCode,
bool Function(Object? potentialKey)? isValidKey,
}) {
if (isValidKey == null) {
if (hashCode == null) {
if (equals == null) {
return _IndexSet();
}
hashCode = _defaultHashCode;
} else {
if (identical(identityHashCode, hashCode) &&
identical(identical, equals)) {
return IndexSet.identity();
}
equals ??= _defaultEquals;
}
} else {
equals ??= _defaultEquals;
hashCode ??= _defaultHashCode;
}
return _CustomIndexSet(
equals: equals,
hashCode: hashCode,
isValidKey: isValidKey,
);
}