Skip to content

Cannot use Vectors or Matrics in sets because hashCodes are not equal. #3

@nikonthethird

Description

@nikonthethird

The same vectors return different hashCodes, for example:

final vec1 = Vector.column([1, 2, 3]);
final vec2 = Vector.column([1, 2, 3]);
print('Hash codes are equal: ${vec1.hashCode == vec2.hashCode}');
// This prints: Hash codes are equal: false

To work around this, I have added a function that calculates the hashCode using quiver's hashObjects function:

HashSet<Vector> createVectorSet(Iterable<Vector> vectors) =>
  HashSet<Vector>(
    equals: (vec1, vec2) => vec1 == vec2,
    hashCode: (vec) => hashObjects(vec.toList()),
  )
  ..addAll(vectors);

This fixes the problem and I now can use Vectors in sets. I hope this can be fixed in a future version, computing the union and intersection of Vector sets should work without any fuss.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions