disambiguateBackendNames function

List<String> disambiguateBackendNames(
  1. List<String> names
)

Returns unique display names for the backends in names.

Repeated names receive their native list index, such as Metal: GPU [0]. Unique names and the original backend order are preserved.

Implementation

List<String> disambiguateBackendNames(List<String> names) {
  final counts = <String, int>{};
  for (final name in names) {
    counts[name] = (counts[name] ?? 0) + 1;
  }
  return [
    for (int i = 0; i < names.length; ++i)
      counts[names[i]]! > 1 ? '${names[i]} [$i]' : names[i],
  ];
}