Skip to content

Comparator (NumericRangeComparator<T>::operator()) should never fail #1

@ianbtr

Description

@ianbtr

This is a feature request.

I recognize that NumericRangeComparator<T>::operator() throws a runtime error because it only produces valid results for disjoint ranges. However, the result of this behavior is that this comparator can't produce a total ordering of an arbitrary set of NumericRange objects. This comparator doesn't live up to the guarantees that most other comparators provide, and this could upset users who wanted to sort (or store in a std::map) ranges that happen to overlap. I would argue that this comparator is not as useful as some possible alternatives, and I think you should implement all three of these:

  1. NumericRangeStartComparator: Compare two ranges by their minima, or starting values. Ending values serve as a tiebreaker.
  2. NumericRangeEndComparator: Compare two ranges by their maxima, or ending values. Starting values serve as a tiebreaker.
  3. NumericRangeCenterComparator: Compare two ranges by their center values. The length of the range (end minus start) serves as a tiebreaker.

I suspect that each of these will be a bit cleaner to implement than the original NumericRangeComparator.

All of these give total orderings of arbitrary sets of NumericRange objects, and produce deterministic results. (Don't forget the tiebreakers, because they provide this determinism).

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions