Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 import java.io.Serializable;
00034 import java.util.Arrays;
00035 import java.util.BitSet;
00036
00043 final class BitMatrix implements Cloneable, Serializable {
00044
00048 private static final long serialVersionUID = 2831903376066135583L;
00049
00053 private int rows;
00054
00058 private int columns;
00059
00063 private BitSet[] bitsets;
00064
00071 public BitMatrix(final int rows, final int columns) {
00072 this.rows = rows;
00073 this.columns = columns;
00074 this.bitsets = new BitSet[this.rows];
00075 for (int i = 0; i < this.rows; i++) {
00076 this.bitsets[i] = new BitSet(this.columns);
00077 }
00078 }
00079
00085 public BitMatrix(int size) {
00086 this(size, size);
00087 }
00088
00095 public void set(int i, int j) {
00096 this.bitsets[i].set(j);
00097 }
00098
00105 public void clear(int i, int j) {
00106 this.bitsets[i].clear(j);
00107 }
00108
00115 public BitSet getRow(int i) {
00116 return this.bitsets[i];
00117 }
00118
00125 public BitSet getColumn(int j) {
00126 BitSet column = new BitSet(this.rows);
00127 for (int i = 0; i < this.rows; i++) {
00128 column.set(i, this.bitsets[i].get(j));
00129 }
00130 return column;
00131 }
00132
00140 public boolean get(int i, int j) {
00141 return this.bitsets[i].get(j);
00142 }
00143
00150 public int cardinality() {
00151 int cardinality = 0;
00152 for (int i = 0; i < this.rows; i++) {
00153 cardinality += this.bitsets[i].cardinality();
00154 }
00155 return cardinality;
00156 }
00157
00163 public int columns() {
00164 return this.columns;
00165 }
00166
00172 public int rows() {
00173 return this.rows;
00174 }
00175
00182 public Object clone() {
00183 try {
00184 final BitMatrix clone = (BitMatrix) super.clone();
00185 System.arraycopy(this.bitsets, 0, clone.bitsets, 0, this.bitsets.length);
00186 return clone;
00187 } catch (CloneNotSupportedException e) {
00188 throw new InternalError();
00189 }
00190 }
00191
00200 public boolean equals(Object obj) {
00201 if (obj != null && obj instanceof BitMatrix) {
00202 BitMatrix other = (BitMatrix) obj;
00203 return Arrays.equals(this.bitsets, other.bitsets);
00204 }
00205 return false;
00206 }
00207
00214 public int hashCode() {
00215 return Arrays.hashCode(this.bitsets);
00216 }
00217
00223 public String toString() {
00224 return Arrays.toString(this.bitsets);
00225 }
00226 }