remove method

  1. @override
bool remove(
  1. E e
)
override

Remove a single element that is equal to e.

If there are multiple elements identical to e, only the first will be removed. To remove all, use something like:

set.removeWhere((a) => a == e);

Note: when using the MappingOrderedSet implementation, this will only work if the element's priority hasn't changed since last rebalance.

Implementation

@override
bool remove(E e) {
  K? key = _mappingFunction(e);
  var bucket = _backingSet[key];
  if (bucket == null || !bucket.contains(e)) {
    // We need a fallback in case [e] has changed and it's no longer found by
    // lookup. Note: changing priorities will leave the splay set on an
    // unknown state; other methods might not work. You must call rebalance to
    // make sure the state is consistent. This is just for convenient usage by
    // the rebalancing method itself.
    final possibleBuckets = _backingSet.entries.where((bucket) {
      return bucket.value.any((element) => identical(element, e));
    });
    final possibleBucket = possibleBuckets.firstOrNull;
    bucket = possibleBucket?.value;
    key = possibleBucket?.key;
  }
  if (bucket == null || key == null) {
    return false;
  }
  final result = bucket.remove(e);
  if (result) {
    _length--;
    // If the removal resulted in an empty bucket, remove the bucket as well.
    if (bucket.isEmpty) {
      _backingSet.remove(key);
    }
    _validReverseCache = false;
  }
  return result;
}