+ * Filter the sequence through the mask leaving only characters at positions
+ * where the mask value was true. The length of the resulting array is
+ * the cardinality of the mask from 0 to sequence length.
+ *
+ * @param sequence
+ * input sequence
+ * @param mask
+ * mask used to filter the sequence characters
+ * @return input array filtered through the mask
+ */
+ public static char[] filterSequence(char[] sequence, BitSet mask)
+ {
+ mask = mask.get(0, sequence.length);
+ char[] result = new char[mask.cardinality()];
+ for (int i = mask.nextSetBit(0), j = 0; i >= 0;)
+ {
+ result[j++] = sequence[i];
+ if (i == Integer.MAX_VALUE)
+ // prevents integer overflow of (i + 1)
+ break;
+ i = mask.nextSetBit(i + 1);
+ }
+ return result;
+ }
+
+ /**