remove method

Future<void> remove(
  1. int index
)

Implementation

Future<void> remove(int index) async {

  return await _lock.synchronized(() async {
    if (index < 0 || index >= _expiringHeap.length) {
      // Invalid index or empty heap if length is 0
      return;
    }

    final ExpiringAddr itemToRemove = _expiringHeap[index];

    final int lastIndex = _expiringHeap.length - 1;
    if (index == lastIndex) {
      // Removing the last element, just pop it.
      _expiringHeap.removeLast();
    } else {
      // Not the last element. Swap with the last, then sift.
      final ExpiringAddr lastItem = _expiringHeap
          .removeLast(); // Get last and shrink heap.
      _expiringHeap[index] = lastItem; // Place lastItem at 'index'.
      lastItem.heapIndex = index; // Update lastItem's heapIndex.
      _siftUpOrDown(index); // Re-heapify the element now at 'index'.
    }

    itemToRemove.heapIndex =
    -1; // Crucial: mark the targeted item as out of heap.
  });
}