* Instantiates an empty {@code BIT_STRING}.
*/
public BIT_STRING() {
- set(new boolean[0]);
}
/**
* specified by the indexes.
*
* @param indexes
- * The value to be assigned.
+ * Indexes of bits to be set true.
*/
public BIT_STRING(int... indexes) {
- for (int e : indexes) {
- set(e);
- }
+ set(indexes);
}
/**
}
/**
- * Sets {@code true} to a bit specified by the index number. If the
+ * Sets {@code true} to bits specified by index numbers. If the
* {@code boolean} array of the value of this data does not have enough size
- * to hold the bit specified by the index, it will be expanded
+ * to hold the bit specified by the biggest index, it will be expanded
* automatically.
*
* @param index
- * The index of a bit.
+ * Indexes of bits to be set true.
*/
- public void set(int index) {
- expand(index);
- value()[index] = true;
+ public void set(int ...indexes) {
+ Arrays.sort(indexes);
+ expand(indexes[indexes.length - 1]);
+ for (int e: indexes) {
+ value()[e] = true;
+ }
}
/**
- * Sets {@code false} to a bit specified by the index number. If the
+ * Sets {@code false} to bits specified by index numbers. If the
* {@code boolean} array of the value of this data does not have enough size
- * to hold the bit specified by the index, it will be expanded
+ * to hold the bit specified by the biggest index, it will be expanded
* automatically.
*
* @param index
- * The index of a bit.
+ * Indexes of bits to be set false.
*/
- public void unset(int index) {
- expand(index);
- value()[index] = false;
+ public void unset(int ...indexes) {
+ Arrays.sort(indexes);
+ expand(indexes[indexes.length - 1]);
+ for (int e: indexes) {
+ value()[e] = false;
+ }
}
/**
* size to hold the last {@code true} bit.
*/
public void contract() {
+ if (value() == null) {
+ set(new boolean[0]);
+ return;
+ }
int index = size();
for (index--; index >= 0; index--) {
if (value()[index]) {
private void expand(int index) {
if (size() <= index) {
boolean[] newValue = new boolean[index + 1];
- System.arraycopy(value(), 0, newValue, 0, size());
+ if (size() > 0) {
+ System.arraycopy(value(), 0, newValue, 0, size());
+ }
set(newValue);
}
}
*/
@Override
public int size() {
+ if (value() == null) {
+ return 0;
+ }
return value().length;
}
*/
@Override
protected boolean[] cloneValue() {
+ if (value() == null) {
+ return null;
+ }
return value().clone();
}