encode static method
Encodes the days of a week into a bitfield. The length of week
should be less
than or equal to 7
with elements ranging from 1 to 7.
Behaviour is undefined if the given week is invalid.
Implementation
static int encode(Iterable<int> week) {
assert(week.length <= 7, 'Number of days is "${week.length}", should be less than 7');
assert(week.every((day) => day >= 1 && day <= 7), 'Invalid day in week, should be between 1 and 7');
var encoded = 0;
for (var day = 7; day >= 1; day--) {
encoded <<= 1;
if (week.contains(day)) {
encoded += 1;
}
}
return encoded;
}