Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/sorteddict.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ SortedDict
.. automethod:: __getitem__
.. automethod:: __setitem__
.. automethod:: __delitem__
.. automethod:: range_del
.. automethod:: __iter__
.. automethod:: __len__
.. automethod:: setdefault
Expand Down
45 changes: 45 additions & 0 deletions src/sortedcontainers/sorteddict.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ class SortedDict(dict):
* :func:`SortedDict.clear`
* :func:`SortedDict.pop`
* :func:`SortedDict.popitem`
* :func:`SortedDict.range_del`

Methods for looking up items:

Expand Down Expand Up @@ -230,6 +231,50 @@ def __delitem__(self, key):
dict.__delitem__(self, key)
self._list_remove(key)

def range_del(self, minimum=None, maximum=None, inclusive=(True, True)):
"""Remove keys between `minimum` and `maximum`.

``inclusive`` is a pair of booleans indicating whether the lower and
upper bounds are included. ``None`` for either bound means the
corresponding end of the sorted dictionary. The operation removes the
range in one block-level update and returns ``None``.

Bounds use the same ordering as :meth:`irange`. With a key function,
bounds are dictionary keys, and all keys with an equal comparison key
are included or excluded together. Reversed or empty ranges do nothing.

Finding the bounds takes approximately ``O(log(n))`` comparisons.
Deletion takes ``O(k + m + n/m)`` time in the worst case, where ``k``
is the number of removed keys and ``m`` is the sorted-list load factor.
This includes copying boundary blocks and moving references in the
flat list of blocks. It uses ``O(k + m)`` temporary space, avoids
building the positional index, and does not search for each key again.

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

:param minimum: minimum key to remove
:param maximum: maximum key to remove
:param inclusive: pair of booleans
:return: ``None``

"""
keys = self._list._delete_range_by_bounds(minimum, maximum, inclusive)
if not keys:
return

# Delete from the mapping directly after all range comparisons and
# coordinate calculations have succeeded. Keys are required to remain
# hashable and order-stable by SortedDict's existing contract.
dict_delitem = dict.__delitem__
for key in keys:
dict_delitem(self, key)

def __iter__(self):
"""Return an iterator over the keys of the sorted dict.

Expand Down
130 changes: 130 additions & 0 deletions src/sortedcontainers/sortedlist.py
Original file line number Diff line number Diff line change
Expand Up @@ -1072,6 +1072,136 @@ def irange(self, minimum=None, maximum=None, inclusive=(True, True), reverse=Fal

return self._islice(min_pos, min_idx, max_pos, max_idx, reverse)

def _range_bounds(self, minimum=None, maximum=None, inclusive=(True, True)):
"""Return block coordinates for a value range.

This is the coordinate part of :meth:`irange`. It deliberately works
with block-local bisect operations and does not build the positional
index, which is useful for operations that already have the values in
sorted order.
"""
if self.key is not None:
minimum = self._key(minimum) if minimum is not None else None
maximum = self._key(maximum) if maximum is not None else None
_lists = self._keys
else:
_lists = self._lists

_maxes = self._maxes
if not _maxes:
return None

if minimum is None:
min_pos = 0
min_idx = 0
elif inclusive[0]:
min_pos = bisect_left(_maxes, minimum)
if min_pos == len(_maxes):
return None
min_idx = bisect_left(_lists[min_pos], minimum)
else:
min_pos = bisect_right(_maxes, minimum)
if min_pos == len(_maxes):
return None
min_idx = bisect_right(_lists[min_pos], minimum)

if maximum is None:
max_pos = len(_maxes) - 1
max_idx = len(_lists[max_pos])
elif inclusive[1]:
max_pos = bisect_right(_maxes, maximum)
if max_pos == len(_maxes):
max_pos -= 1
max_idx = len(_lists[max_pos])
else:
max_idx = bisect_right(_lists[max_pos], maximum)
else:
max_pos = bisect_left(_maxes, maximum)
if max_pos == len(_maxes):
max_pos -= 1
max_idx = len(_lists[max_pos])
else:
max_idx = bisect_left(_lists[max_pos], maximum)

if min_pos > max_pos or (min_pos == max_pos and min_idx >= max_idx):
return None

return min_pos, min_idx, max_pos, max_idx

def _delete_range(self, min_pos, min_idx, max_pos, max_idx):
"""Delete a contiguous range using block coordinates.

The operation avoids positional-index construction and per-item
deletion. Only the boundary blocks are copied; complete interior
blocks are discarded in one slice. The small amount of local
rebalancing keeps short fragments out of the middle of the structure.
"""
arrays = [self._lists]
if hasattr(self, '_keys'):
arrays.append(self._keys)

lists = self._lists
removed = sum(len(block) for block in lists[min_pos : max_pos + 1])
removed -= min_idx + len(lists[max_pos]) - max_idx
fragments = [
blocks[min_pos][:min_idx] + blocks[max_pos][max_idx:] for blocks in arrays
]
stop = max_pos + 1

# A short surviving fragment needs one neighbor to restore the load
# invariant. Keep cached keys alongside values without calling _key.
size = len(fragments[0])
if 0 < size < (self._load >> 1):
if min_pos:
min_pos -= 1
fragments = [
blocks[min_pos] + fragments[index]
for index, blocks in enumerate(arrays)
]
elif stop < len(lists):
fragments = [
fragments[index] + blocks[stop]
for index, blocks in enumerate(arrays)
]
stop += 1

size = len(fragments[0])
replacements = [[] for _ in arrays]
if size:
# Even splitting avoids a short last fragment in the middle of
# the list. At most two old boundary blocks and one neighbor are
# involved; whole interior blocks need no element-wise rebuilding.
count = (size + (self._load << 1) - 1) // (self._load << 1)
width, extra = divmod(size, count)
offset = 0
for pos in range(count):
end = offset + width + (pos < extra)
for index, fragment in enumerate(fragments):
replacements[index].append(fragment[offset:end])
offset = end

maxes = [block[-1] for block in replacements[-1]]
for index, blocks in enumerate(arrays):
blocks[min_pos:stop] = replacements[index]
self._maxes[min_pos:stop] = maxes
self._len -= removed
# Any successful structural deletion invalidates the positional index;
# lazy rebuilding preserves the normal cache contract.
self._index[:] = []
self._offset = 0

def _delete_range_by_bounds(
self, minimum=None, maximum=None, inclusive=(True, True)
):
"""Delete and return values in a sorted value range."""
bounds = self._range_bounds(minimum, maximum, inclusive)
if bounds is None:
return []
min_pos, min_idx, max_pos, max_idx = bounds
values = list(self._islice(min_pos, min_idx, max_pos, max_idx, False))
self._delete_range(min_pos, min_idx, max_pos, max_idx)
return values

def __len__(self):
"""Return the size of the sorted list.

Expand Down
Loading