Created
September 30, 2017 17:01
-
-
Save bergwerf/8b55e89d96dd7ed386285d36d2ef736a to your computer and use it in GitHub Desktop.
Packing and unpacking integer lists into a single integer list
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// 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