Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8366238

Improve RBTree API with stricter comparator semantics and pluggable validation/printing hooks.

XMLWordPrintable

    • Icon: Enhancement Enhancement
    • Resolution: Unresolved
    • Icon: P4 P4
    • None
    • None
    • hotspot

      The current red-black tree can be made safer and more flexible:

      1. Comparator semantics.
          - COMPARATOR::cmp used for ordering returns a raw int, encouraging brittle ”return b - a” code that can underflow/overflow.
          - The validation comparator also uses the same name, but returns bool, which is misleading.
      2. Limited tree inspection capabilities.
          - Validation only checks the core structure and cannot verify additional invariants of intrusive, user-defined nodes.
          - Printing is hard-wired to the core node, which is not very useful for intrusive nodes.

      To improve the comparator, we should replace the int in favour of a strongly-typed three-way result, similar to C++20’s operator<=>, which would make incorrect arithmetic impossible. The validation comparator should also be renamed to better reflect its purpose.

      For inspection, we should expand the verify and printing functions so callers can supply custom validation/printing routines. This would enable full inspection of intrusive nodes without touching core tree verification.

            cnorrbin Casper Norrbin
            cnorrbin Casper Norrbin
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated: