Search code examples
eiffel

Why is LINKED_SET unable to compare objects?


As I'd like to know if some object is into a LINKED_SET to prune it in my context, I'm unable to compare it as an object instead of its reference.

changeable_comparison_criterion: BOOLEAN
        -- May `object_comparison' be changed?
        -- (Answer: only if set empty; otherwise insertions might
        -- introduce duplicates, destroying the set property.)
    do
        Result := is_empty
    ensure then
        only_on_empty: Result = is_empty
    end

Into the SET class (as above) it seems that its not possible to change a set to compare_objects. So my questions are:

  1. What is the semantic of not being able to compare objects into a SET
  2. If my choice of LINKED_SET is wrong by misunderstanding of its semantic, how should I do for having a unique items collection based on object comparison and then being able to prune an item again based on object comparison again

Solution

  • The comparison criterion should be set right after container creation, then it works without a problem. If there are some objects in the set already, it becomes unclear what should be done to them if the comparison criterion changes.

    For example, if there is a set {A, B} of two distinct objects A and B that have the same value, i.e. are equal, what should be done if the comparison criterion changes from compare_references to compare_objects? Clearly, the set now should have only one object, because according to the new setting, it cannot hold two or more equal objects. Does it mean, object A should be removed and B should be kept? Or should it be done in reverse order? The precondition you are referring to removes this ambiguity.

    The solution is to modify the setting before there are any objects in the container:

    create my_set.make
    my_set.compare_objects