From 4df61fa1b9d73ef8f19812588daa6a915c9ad80e Mon Sep 17 00:00:00 2001 From: Nash Aschenbath Date: Mon, 23 Nov 2026 22:28:08 +0800 Subject: [PATCH] Refresh a changed SortedKeyList item by its index --- docs/sortedlist.rst | 1 + src/sortedcontainers/sortedlist.py | 89 +++++++++++++++ tests/test_update_key.py | 177 +++++++++++++++++++++++++++++ 3 files changed, 267 insertions(+) create mode 100644 tests/test_update_key.py diff --git a/docs/sortedlist.rst b/docs/sortedlist.rst index 06466444..6b492b45 100644 --- a/docs/sortedlist.rst +++ b/docs/sortedlist.rst @@ -59,5 +59,6 @@ SortedKeyList .. automethod:: bisect_key_left .. automethod:: bisect_key_right .. automethod:: irange_key + .. automethod:: update_key .. autoclass:: sortedcontainers.SortedListWithKey diff --git a/src/sortedcontainers/sortedlist.py b/src/sortedcontainers/sortedlist.py index aecf7871..548d6302 100644 --- a/src/sortedcontainers/sortedlist.py +++ b/src/sortedcontainers/sortedlist.py @@ -23,6 +23,7 @@ from itertools import chain, repeat, starmap from math import log from operator import add, eq, ge, gt, iadd, le, lt, ne +from operator import index as as_index from reprlib import recursive_repr from textwrap import dedent @@ -1606,6 +1607,7 @@ class SortedKeyList(SortedList): * :func:`SortedKeyList.bisect_key_left` * :func:`SortedKeyList.bisect_key_right` * :func:`SortedKeyList.irange_key` + * :func:`SortedKeyList.update_key` Some examples below use: @@ -1718,6 +1720,93 @@ def add(self, value): self._len += 1 + def update_key(self, index): + """Recompute the key of the value at `index` and return its new index. + + Use the value's position from before changing it, then call this method + before any other operation that depends on sorted order. Only this + occurrence's key is refreshed; keys of other occurrences must still be + valid. The key function must return a separate, stable comparison key. + + If the value still belongs between its neighbors, keep its position, + including within equal keys. Otherwise place it after existing equal + keys. Moving within a sublist preserves that sublist's length and the + positional index. Key extraction and comparisons finish before changes + are made to stored values or cached keys. + + Runtime complexity: `O(log(n))` -- approximate. + + >>> items = [[1, 'first'], [2, 'second'], [3, 'third']] + >>> skl = SortedKeyList(items, key=lambda item: item[0]) + >>> items[0][0] = 4 + >>> skl.update_key(0) + 2 + >>> list(skl) + [[2, 'second'], [3, 'third'], [4, 'first']] + + :param int index: position of the changed value (negative indices allowed) + :return: new nonnegative index of the value + :raises IndexError: if the index is out of range + + """ + index = as_index(index) + pos, idx = self._pos(index) + index = index if index >= 0 else self._len + index + _lists, _keys, _maxes = self._lists, self._keys, self._maxes + value = _lists[pos][idx] + key = self._key(value) + + move_left = (idx and key < _keys[pos][idx - 1]) or ( + not idx and pos and key < _maxes[pos - 1] + ) + move_right = (idx + 1 < len(_keys[pos]) and _keys[pos][idx + 1] < key) or ( + idx + 1 == len(_keys[pos]) + and pos + 1 < len(_keys) + and _keys[pos + 1][0] < key + ) + if not move_left and not move_right: + _keys[pos][idx] = key + if idx + 1 == len(_keys[pos]): + _maxes[pos] = key + return index + + if move_left: + target_pos = bisect_right(_maxes, key, 0, pos + 1) + else: + target_pos = bisect_right(_maxes, key, pos) + if target_pos == len(_keys): + target_pos -= 1 + target_idx = len(_keys[target_pos]) + else: + target_idx = bisect_right(_keys[target_pos], key) + + if target_pos == pos: + target_idx -= target_idx > idx + del _keys[pos][idx] + del _lists[pos][idx] + _keys[pos].insert(target_idx, key) + _lists[pos].insert(target_idx, value) + _maxes[pos] = _keys[pos][-1] + return index + target_idx - idx + + # Deletion may merge or split sublists, so retain the destination as a + # logical index rather than carrying block coordinates across it. + target_index = self._loc(target_pos, target_idx) - (target_pos > pos) + needs_rebalance = len(_keys[pos]) - 1 <= (self._load >> 1) + self._delete(pos, idx) + if needs_rebalance: + if target_index == self._len: + target_pos = len(_lists) - 1 + target_idx = len(_lists[target_pos]) + else: + target_pos, target_idx = self._pos(target_index) + _lists[target_pos].insert(target_idx, value) + _keys[target_pos].insert(target_idx, key) + _maxes[target_pos] = _keys[target_pos][-1] + self._expand(target_pos) + self._len += 1 + return target_index + def _expand(self, pos): """Split sublists with length greater than double the load-factor. diff --git a/tests/test_update_key.py b/tests/test_update_key.py new file mode 100644 index 00000000..34285adb --- /dev/null +++ b/tests/test_update_key.py @@ -0,0 +1,177 @@ +from bisect import bisect_right +from random import Random + +import pytest + +from sortedcontainers import SortedKeyList, SortedList + + +@pytest.mark.parametrize('load', [4, 7, 1000]) +@pytest.mark.parametrize('old, new', [(0, 99), (29, -1), (2, 9.5), (10, 1.5), (7, 7.5)]) +def test_repositions_by_index_and_preserves_identity(load, old, new): + values = [[i, i] for i in range(30)] + skl = SortedKeyList(values, key=lambda value: value[0]) + skl._reset(load) + skl._build_index() + changed = skl[old] + changed[0] = new + position = skl.update_key(old) + assert skl[position] is changed + assert list(skl) == sorted(values, key=lambda value: value[0]) + assert len(skl) == 30 + skl._check() + + +def test_unchanged_position_does_not_remove_insert_or_rebalance(): + values = [[i] for i in range(30)] + calls = [] + skl = SortedKeyList(values, key=lambda value: calls.append(value) or value[0]) + skl._reset(4) + skl._build_index() + blocks = [id(block) for block in skl._lists] + tree = list(skl._index) + calls.clear() + values[7][0] = 7.5 + assert skl.update_key(7) == 7 + assert [id(block) for block in skl._lists] == blocks + assert skl._index == tree + assert calls == [values[7]] + skl._check() + assert skl.bisect_key_right(7.5) == 8 + + +@pytest.mark.parametrize('old, new', [(1, 5.5), (5, 0.5)]) +def test_move_inside_block_keeps_block_sizes_and_index(old, new): + values = [[i] for i in range(24)] + skl = SortedKeyList(values, key=lambda value: value[0]) + skl._reset(8) + skl._build_index() + blocks = [id(block) for block in skl._lists] + lengths = list(map(len, skl._lists)) + tree = list(skl._index) + values[old][0] = new + new_index = skl.update_key(old) + assert skl[new_index] is values[old] + assert list(skl) == sorted(values, key=lambda value: value[0]) + assert [id(block) for block in skl._lists] == blocks + assert list(map(len, skl._lists)) == lengths + assert skl._index == tree + skl._check() + + +def test_negative_index_and_keyed_sorted_list_factory(): + values = [[1], [2], [3]] + skl = SortedList(values, key=lambda value: value[0]) + values[-1][0] = 0 + assert skl.update_key(-1) == 0 + assert skl[0] is values[-1] + skl._check() + + +def test_singleton(): + value = [1] + skl = SortedKeyList([value], key=lambda item: item[0]) + value[0] = -5 + assert skl.update_key(-1) == 0 + assert skl.bisect_key_left(-5) == 0 + skl._check() + + +def test_equal_keys_keep_position_when_possible_and_append_after_move(): + values = [[0, 'moving'], [1, 'a'], [1, 'b'], [2, 'c']] + skl = SortedKeyList(values, key=lambda value: value[0]) + assert skl.update_key(2) == 2 + values[0][0] = 2 + assert skl.update_key(0) == 3 + assert [value[1] for value in skl] == ['a', 'b', 'c', 'moving'] + skl._check() + + +@pytest.mark.parametrize('index', [0, -1, 20, -20]) +def test_empty_index_error_does_not_call_key(index): + skl = SortedKeyList(key=lambda value: pytest.fail('unexpected key call')) + with pytest.raises(IndexError): + skl.update_key(index) + + +@pytest.mark.parametrize('index', [3, -4, 1.0, slice(1), '1']) +def test_invalid_index_leaves_data_unchanged(index): + skl = SortedKeyList([1, 2, 3]) + with pytest.raises((IndexError, TypeError)): + skl.update_key(index) + assert list(skl) == [1, 2, 3] + skl._check() + + +def test_index_protocol(): + class Position: + def __index__(self): + return 1 + + skl = SortedKeyList([1, 2, 3]) + assert skl.update_key(Position()) == 1 + + +def test_key_error_does_not_change_cached_keys_or_items(): + value = [1] + skl = SortedKeyList([value, [2]], key=lambda item: item[0]) + value.clear() + with pytest.raises(IndexError): + skl.update_key(0) + assert skl[0] is value + assert skl._keys == [[1, 2]] + assert len(skl) == 2 + + +def test_comparison_error_does_not_change_cached_keys_or_items(): + values = [[1], [2]] + skl = SortedKeyList(values, key=lambda value: value[0]) + values[0][0] = object() + with pytest.raises(TypeError): + skl.update_key(0) + assert skl[0] is values[0] + assert skl._keys == [[1, 2]] + + +def test_equal_values_are_identified_by_occurrence_not_equality(): + class Value: + def __init__(self, key): + self.key = key + + def __eq__(self, other): + return isinstance(other, Value) + + values = [Value(i) for i in range(12)] + skl = SortedKeyList(values, key=lambda value: value.key) + skl._reset(4) + values[3].key = 99 + assert skl.update_key(3) == 11 + assert list(map(id, skl)) == list(map(id, values[:3] + values[4:] + values[3:4])) + skl._check() + + +@pytest.mark.parametrize('load', [4, 5, 8, 32]) +def test_random_updates_match_reference_with_merges_splits_and_duplicate_keys(load): + random = Random(181) + expected = [[i % 13, i] for i in range(160)] + expected.sort(key=lambda value: value[0]) + skl = SortedKeyList(expected, key=lambda value: value[0]) + skl._reset(load) + for _ in range(1500): + old = random.randrange(len(expected)) + value = expected[old] + value[0] = random.randrange(-100, 100) + ordered = (old == 0 or expected[old - 1][0] <= value[0]) and ( + old == len(expected) - 1 or value[0] <= expected[old + 1][0] + ) + new = old + if not ordered: + expected.pop(old) + new = bisect_right([item[0] for item in expected], value[0]) + expected.insert(new, value) + actual = skl.update_key(old if random.randrange(2) else old - len(skl)) + assert actual == new + assert [id(item) for item in skl] == [id(item) for item in expected] + assert skl[actual] is value + assert skl.bisect_key_left(value[0]) <= actual < skl.bisect_key_right(value[0]) + skl._check()