removeValues method

  1. @useCopy
IMapOfSets<K, V> removeValues(
  1. List<V> values, {
  2. Output<int>? numberOfRemovedValues,
})

Remove all given values from all sets. If a set becomes empty and removeEmptySets is true, its key will be removed entirely. Otherwise, its key will be kept and the set will be empty (not null). If you want, you can pass numberOfRemovedValues to get the number of removed values.

Implementation

@useCopy
IMapOfSets<K, V> removeValues(
  List<V> values, {
  Output<int>? numberOfRemovedValues,
}) {
  int countRemoved = 0;

  Map<K, ISet<V>> map = {};
  for (MapEntry<K, ISet<V>> entry in _mapOfSets.entries) {
    K key = entry.key;
    ISet<V> set = entry.value;
    int setLength = set.length;

    ISet<V> newSet;
    int newSetLength;
    if (setLength == 0) {
      newSet = set;
      newSetLength = 0;
    } else {
      newSet = set.removeAll(values);
      newSetLength = newSet.length;
    }

    countRemoved += setLength - newSetLength;

    if (newSetLength == 0 && config.removeEmptySets) {
      // Set is empty. Discard the key:set.
    } else if (setLength == newSetLength) {
      // Set was NOT modified.
      map[key] = set;
    } else {
      // Set was modified.
      map[key] = newSet;
    }
  }

  if (numberOfRemovedValues != null) numberOfRemovedValues.save(countRemoved);

  return (countRemoved == 0) ? this : IMapOfSets<K, V>._unsafe(map.lock, config);
}