fix masking algorithm

Previously the `getMasks` did not always produce the correct number of
masks, only returning 3 for the call `getMasks(4, 9)`. Fix this, and
refactor the `LabelMask` constructor to take the start index and
cardinality, which is much clearer than taking the start and end
indices. Refactor elsewhere where appropriate.