Skip to content

Feature Proposal: Introduce Higher Level APIs like ceil / floor #207

@Asayu123

Description

@Asayu123

Motivation

  • Other programming languages support high-level APIs like

    • Get next smaller value (or item)
    • Get floor value (or item)
    • Get ceil value (or item)
    • Get next larger value (or item)
      In Tree data structure.
  • We can achieve this by using bisect method + index range check, but providing higher-level API would help users to avoid writing wrapper code every time.

API Definition

SortedList

  • def next_smaller(self, value: object) -> Optional[object]
  • def floor (self, value: object) -> Optional[object]
  • def ceil (self, value: object) -> Optional[object]
  • def next_greater(self, value: object) -> Optional[object]

SortedSet

  • def next_smaller(self, value: Hashable) -> Optional[object]
  • def floor (self, value: Hashable) -> Optional[object]
  • def ceil (self, value: Hashable) -> Optional[object]
  • def next_greater(self, value: Hashable) -> Optional[object]

SortedDict

  • def next_smaller_item(self, key: Hashable) -> tuple[Optional[Hashable], Optional[object]]
  • def floor_item (self, key: Hashable) -> tuple[Optional[Hashable], Optional[object]]
  • def ceil_item (self, key: Hashable) -> tuple[Optional[Hashable], Optional[object]]
  • def next_greater_item(self, key: Hashable) -> tuple[Optional[Hashable], Optional[object]]

Prototype Code (Implementation / Tests)

P.S

  • If this idea sounds good to you, I'd love to discuss API design (including naming), testing strategy, etc.
  • Thank you so much for your attention and participation.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions