Integrated Dynamics

Integrated Dynamics

63M Downloads

uniq and uniq_p don't work as intended

josephcsible opened this issue ยท 1 comments

commented

Set<IValue> values = new TreeSet<>((o1, o2) -> {
try {
ValueTypeBoolean.ValueBoolean value = (ValueTypeBoolean.ValueBoolean) operator
.evaluate(new Variable(o1), new Variable(o2));
return value.getRawValue() ? 0 : o1.hashCode() - o2.hashCode();
} catch (EvaluationException e) {
throw Lombok.sneakyThrow(e);
}
});

Set<IValue> values = new TreeSet<>(
(o1, o2) -> o1.equals(o2) ? 0 : o1.hashCode() - o2.hashCode()
);

TwoThree problems are apparent with the above code:

  1. If two objects have the same hash code, they'll be treated as equal whether or not they actually are.
  2. It's possible that two objects that the predicate considers equal will have different hash codes. This means that even discounting problem 1, this isn't a total order, so the set may misbehave.
  3. The order of elements isn't preserved.
commented

Not sure where my mind was when I wrote that...