apply<T> method

void apply<T>(
  1. List<T> list
)

Applies the permutations to a provided list.

This method rearranges the elements in the list based on the recorded permutations. The original order is modified to reflect the new order specified by the permutations.

Parameters:

  • list: The list to which the permutations should be applied.

Implementation

void apply<T>(List<T> list) {
  final unordered = <int, T>{};
  final emptySlots = <int>{};
  for (final p in _permutations) {
    if (!emptySlots.contains(p.to)) {
      unordered[p.to] = list[p.to];
    }
    if (unordered.containsKey(p.from)) {
      list[p.to] = unordered.remove(p.from) as T;
    } else {
      list[p.to] = list[p.from];
      emptySlots.add(p.from);
    }
    emptySlots.remove(p.to);
  }
}