Add item with weight, maintaining sorted order
(self, item: Any, weight: float)
| 16 | self._lock = threading.RLock() |
| 17 | |
| 18 | def add(self, item: Any, weight: float) -> None: |
| 19 | """Add item with weight, maintaining sorted order""" |
| 20 | with self._lock: |
| 21 | # Find insertion point using binary search |
| 22 | left, right = 0, len(self._items) |
| 23 | while left < right: |
| 24 | mid = (left + right) // 2 |
| 25 | if self._items[mid][1] < weight: |
| 26 | right = mid |
| 27 | else: |
| 28 | left = mid + 1 |
| 29 | |
| 30 | self._items.insert(left, (item, weight)) |
| 31 | |
| 32 | def remove(self, item): |
| 33 | """Remove first occurrence of item""" |