combinePathSets function

Set<String> combinePathSets(
  1. List<Set<String>> pathSets
)

Combines multiple pathSets into a single set, returning all possible combinations.

Note: This function is recursive and works by joining the first two sets and then combining the resulting set with the next set, until all sets are processed.

Example:

final pathSets = [
  {'path1', 'path2'},
  {'segmentA', 'segmentB'},
  {'end1', 'end2'},
];
final combinedPaths = combineSets(pathSets);
print(combinedPaths);

Output:

{path1/segmentA/end1, path1/segmentA/end2, path1/segmentB/end1, path1/segmentB/end2, path2/segmentA/end1, path2/segmentA/end2, path2/segmentB/end1, path2/segmentB/end2}

Implementation

Set<String> combinePathSets(List<Set<String>> pathSets) {
  late Set<String> output;
  final input = List.of(pathSets).where((e) => e.isNotEmpty).toList();
  if (input.isEmpty) {
    output = {};
  } else if (input.length == 1) {
    output = input[0];
  } else {
    final joined = <String>{};
    for (final a in input[0]) {
      for (final b in input[1]) {
        if (b.isEmpty) {
          joined.add(a);
        } else if (a.isEmpty) {
          joined.add(b);
        } else {
          joined.add(p.join(a, b));
        }
      }
    }
    output = combinePathSets([
      joined,
      ...input.skip(2),
    ]);
  }
  output = output.map((e) => p.normalize(toLocalSystemPathFormat(e))).toSet();
  return output;
}