buildListStructure<T> function decoder

List buildListStructure<T>(
  1. List<List<int>> positions, {
  2. List<T>? valueList,
})

Build a nested List which has all required children to set all values.

Doing this beforehand makes it possible to make every (sub) List strongly typed. Values can then be set without checking if a List is present/large enought etc.

Implementation

List buildListStructure<T>(List<List<int>> positions, {List<T>? valueList}) {
  positions.sort((a, b) => a.length.compareTo(b.length));
  if (positions.isEmpty) {
    return [];
  }

  final maxDepth = positions.last.length - 1;
  final maxEntriesForEachDepth = <int>[];

  for (var i = 0; i <= maxDepth; i++) {
    final indexes = <int>[];
    for (final position in positions) {
      if (position.length > i) {
        indexes.add(position[i]);
      }
    }
    indexes.sort((int a, int b) => a.compareTo(b));
    maxEntriesForEachDepth.add(indexes.last);
  }
  maxEntriesForEachDepth.sort((a, b) => a.compareTo(b));
  return getNestedList<T>(
    depth: maxDepth,
    valueList: valueList ?? <T>[],
  );
}