We've got a list of instances of a class. We effectively want a Set
i.e. a group with no repeated elements. Elements of the list which are the same equate, but their hashes are different as they have been instantiated separately. So a==b
is True
, a is b
is False
.
Is there a way to vectorise this problem or otherwise make it efficient. The only solutions we can think of involved for loops, and it seems like there might be a more efficient solution.
EDIT: I think its different from the "Elegant ways to support equivalence" as the equivalence works well, its just that Set
relies on comparing hashes.
EDIT: The for loop solution would go something like, sort the list, and then iterate over, removing the current value if its the same as the last value
EDIT: To be clear, we don't own this class, we just have instances of it. So we could wrap the instances and implement a more useful hash function, however, this seems like it might be almost as expensive as the for loop approach - could be wrong though
EDIT: sorry if it feels like I'm moving the goalposts a bit here - there isn't a simple val
of the object that can be subbed in for a hash, that approach would need to somehow generate UIDs for each different instance.