Skip to content

Instantly share code, notes, and snippets.

@bergwerf
Created September 30, 2017 17:01
Show Gist options
  • Save bergwerf/8b55e89d96dd7ed386285d36d2ef736a to your computer and use it in GitHub Desktop.
Save bergwerf/8b55e89d96dd7ed386285d36d2ef736a to your computer and use it in GitHub Desktop.
Packing and unpacking integer lists into a single integer list
/// Unpack an integer list into a list of integer lists.
List<List<int>> _unpackIntLists(List<int> input) {
final output = new List<List<int>>();
for (var i = 0; i < input.length;) {
final length = input[i++];
output.add(input.sublist(i, i + length));
i += length;
}
return output;
}
/// Pack list of integer lists into a single integer list.
List<int> _packIntLists(List<List<int>> input) {
final output = new List<int>();
for (final list in input) {
output.add(list.length);
output.addAll(list);
}
return output;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment