Skip to content

Add optimized key-range deletion to SortedDict - #263

Open
Aschenbath wants to merge 1 commit into
grantjenks:masterfrom
Aschenbath:feat/sorteddict-range-del-180
Open

Aschenbath wants to merge 1 commit into
grantjenks:masterfrom
Aschenbath:feat/sorteddict-range-del-180

Conversation

@Aschenbath

@Aschenbath Aschenbath commented Nov 7, 2026 •

Copy link
Copy Markdown

Deleting a key range from SortedDict currently requires repeated per-key removal or conversion to positional indices. Add range_del(minimum=None, maximum=None, inclusive=(True, True)), following the API proposed in #180, to delete the range directly by block coordinates.

The implementation removes complete interior blocks, rebalances boundary fragments, and preserves cached sort keys. It avoids constructing the positional index and searching again for every removed key. Bounds follow irange, including key functions and equal comparison keys; empty ranges leave the block layout and positional cache intact. The method returns None.

sd = SortedDict({1: 'a', 2: 'b', 3: 'c', 4: 'd'})
sd.range_del(2, 3)
# SortedDict({1: 'a', 4: 'd'})

Deletion is documented as worst-case O(k + m + n/m), including movement of the flat block array, with O(k + m) temporary space. In a local million-key benchmark, deleting the middle 100,000 plain keys took 13.37 ms, versus 113.80 ms for the loop and 366.76 ms for view slicing. Deleting 900,000 keys took 115.30 ms, versus 911.71 ms and 127.37 ms. Single-key deletion has extra fixed overhead: 135 us versus 47 us for the loop and 258 us for slicing. Measurements are medians of five fresh-map runs on Windows / CPython 3.12.12, excluding construction and verifying every result.

Validation:

  • Full configured tests, doctests and stress tests: 409 passed, including 42 new cases. Final in-memory coverage: 1,449/1,449 statements and 496/498 branches; both uncovered branches predate this change, and all new implementation branches are covered.
  • The initial 39 regression cases fail against the unchanged source. Separate model-based review passed 37,800 mixed mutation steps and 109,856 range deletions across 200 uneven block layouts, checking mapping contents, views, cached methods, positional caches and internal invariants.
  • Installed-wheel tests: 97 passed. Sphinx documentation, doc8, README rstcheck, wheel/sdist builds and strict Twine checks passed.
  • Ruff with the tox matrix's Python 3.8 target and whitespace checks pass. The default latest Ruff has five existing B905 findings; repository-wide formatting has two existing differences. These baseline issues are also covered by open Keep lint checks compatible with supported Python 3.8 #259. The full suite also passes on Linux (musl) / CPython 3.12.12: 409 passed. The Linux run used the exact PR source, verified by SHA256, in a RAM-backed WSL workspace with pure-Python test dependencies.

Fixes #180.

AI assistance: OpenAI Codex assisted with implementation, tests and documentation; a separate Codex reviewer checked the change and ran an independent oracle. No human code review is claimed.

Aschenbath added a commit to Aschenbath/python-sortedcontainers that referenced this pull request Nov 7, 2026
Combine PR grantjenks#263 at 76e9cae with only the three reviewed lint/format changes from PR grantjenks#259 at a7d452d.

Preserve every existing workflow file byte-for-byte. This fork-only validation commit retains the full original checks and Ubuntu Python matrix.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Feature request: SortedDict Range Deletion

1 participant