source: src/main/java/weka/classifiers/misc/monotone/BooleanBitMatrix.java @ 10

Last change on this file since 10 was 4, checked in by gnappo, 14 years ago

Import di weka.

File size: 6.8 KB
Line 
1/*
2 *    This program is free software; you can redistribute it and/or modify
3 *    it under the terms of the GNU General Public License as published by
4 *    the Free Software Foundation; either version 2 of the License, or
5 *    (at your option) any later version.
6 *
7 *    This program is distributed in the hope that it will be useful,
8 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
9 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10 *    GNU General Public License for more details.
11 *
12 *    You should have received a copy of the GNU General Public License
13 *    along with this program; if not, write to the Free Software
14 *    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
15 */
16
17/*
18 *    BooleanBitMatrix.java
19 *    Copyright (C) 2004 Stijn Lievens
20 *
21 */
22
23package weka.classifiers.misc.monotone;
24
25import weka.core.RevisionHandler;
26import weka.core.RevisionUtils;
27
28/**
29 * This class is a very simple implementation of a BitMatrix.
30 * In fact, it uses a two-dimensional array of booleans, so it is
31 * not very space efficient.
32 *
33 * <p>
34 * This implementation is part of the master's thesis: "Studie
35 * en implementatie van instantie-gebaseerde algoritmen voor gesuperviseerd
36 * rangschikken", Stijn Lievens, Ghent University, 2004.
37 * </p>
38 *
39 * @author Stijn Lievens (stijn.lievens@ugent.be)
40 * @version $Revision: 5922 $
41 */
42public class BooleanBitMatrix
43  implements BitMatrix, RevisionHandler {
44
45  /** The two-dimensional array of booleans. */
46  private boolean[][] m_bits;
47
48  /** The number of rows. */
49  private int m_rows;
50
51  /** The number of columns */
52  private int m_columns;
53
54  /**
55   * Construct a <code> BitMatrix </code> with the indicated
56   * number of rows and columns.  All bits are initially
57   * set to <code> false </code>.
58   *
59   * @param rows the number of rows
60   * @param columns the number of column
61   */
62  public BooleanBitMatrix(int rows, int columns) {
63    m_bits = new boolean[rows][columns];
64    m_rows = rows;
65    m_columns = columns;
66  }
67
68  /**
69   * A copy constructor.  Constructs a copy of the given
70   * <code> BitMatrix </code>.
71   *
72   * @param bm the <code> BitMatrix </code> to be copied.
73   */
74  public BooleanBitMatrix(BooleanBitMatrix bm) {
75    this(bm.m_rows, bm.m_columns);
76    for (int i = 0; i < m_rows; i++) {
77      System.arraycopy(bm.m_bits[i], 0, m_bits[i], 0, m_columns);
78    }
79  }
80
81  /**
82   * Returns the element a the specified position.
83   *
84   * @param row the row of the position
85   * @param column the column of the position
86   * @return <code> true </code> if the bit at the
87   * specified position is set, <code> false </code>
88   * otherwise
89   */
90  public boolean get(int row, int column) {
91    return m_bits[row][column];
92  }
93
94  /**
95   * Sets the bit at the specified position to the specified
96   * value.
97   *
98   * @param row the row of the position
99   * @param column the column of the position
100   * @param bool the value to fill in
101   * @return the value of <code> bool </code>
102   */
103  public boolean set(int row, int column, boolean bool) {
104    m_bits[row][column] = bool;
105    return bool;
106  }
107
108  /**
109   * Sets the bit at the specified position to <code> true. </code>
110   *
111   * @param row the row of the position
112   * @param column the column of the position
113   * @return <code> true </code> if the bit was actually
114   * set, <code> false </code> otherwise
115   */
116  public boolean set(int row, int column) {
117    return !get(row, column) && set(row, column, true);
118  }
119
120  /**
121   * Clears the bit at the specified position.
122   *
123   * @param row the row of the position
124   * @param column the column of the position
125   * @return <code> true </code> if the bit was actually
126   * cleared, <code> false </code> otherwise
127   */
128  public boolean clear(int row, int column) {
129    return get(row, column) && !set(row, column, false);
130  }
131
132  /**
133   * Gets the number of rows.
134   *
135   * @return the number of rows of the matrix
136   */
137  public int rows() {
138    return m_rows;
139  }
140
141  /**
142   * Gets the number of columns.
143   *
144   * @return the number of columns of the matrix
145   */
146  public int columns() {
147    return m_columns;
148  }
149
150  /**
151   * Counts the number of bits that are set in the specified column.
152   *
153   * @param column index of the column of which the bits are to be counted
154   * @return the number of bits that are set in the requested column
155   */
156  public int columnCount(int column) {
157    int count = 0;
158    for (int i = 0; i < m_rows; i++) {
159      count += (m_bits[i][column] ? 1 : 0);
160    }
161    return count;
162  }
163
164  /**
165   * Counts the number of bits that are set in the specified row.
166   *
167   * @param row index of the row of which the bits are to be counted
168   * @return the number of bits that are set in the requested row
169   */
170  public int rowCount(int row) {
171    int count = 0;
172    for (int i = 0; i < m_columns; i++) {
173      count += (m_bits[row][i] ? 1 : 0);
174    }
175    return count;
176  }
177
178  /**
179   * Swap the rows and the columns of the <code> BooleanBitMatrix. </code>
180   *
181   * @return the transposed matrix
182   */
183  public BooleanBitMatrix transpose() {
184    BooleanBitMatrix transposed = new BooleanBitMatrix(m_columns, m_rows);
185
186    for (int i = 0; i < m_rows; i++) {
187      for (int j = 0; j < m_columns; j++) {
188        transposed.set(j, i, get(i, j));
189      }
190    }
191
192    return transposed;
193  }
194
195  /**
196   * Swaps the rows and the columns of the <code> BooleanBitMatrix, </code>
197   * without creating a new object.
198   * The <code> BooleanBitMatrix </code> must be a square matrix.
199   *
200   * @throws IllegalArgumentException if the <code> BooleanBitMatrix </code>
201   * is not square.
202   */
203  public void transposeInPlace() throws IllegalArgumentException {
204    if (m_rows != m_columns) {
205      throw new IllegalArgumentException
206      ("The BooleanBitMatrix is not square"); 
207    }
208    for (int i = 0; i < m_rows; i++) {
209      for (int j = i + 1; j < m_columns; j++) {
210        swap(i, j, j, i);
211      }
212    }
213  }
214
215  /**
216   * Swap the elements with coordinates <code> (r1,c1) </code>  and
217   * <code> (r2,c2). </code>
218   *
219   * @param r1 index of first row
220   * @param c1 index of first column
221   * @param r2 index of second row
222   * @param c2 index of second column
223   */
224  private void swap(int r1, int c1, int r2, int c2) {
225    boolean tmp = get(r1, c1);
226    set(r1, c1, get(r2, c2));
227    set(r2, c2, tmp);
228  }
229
230  /**
231   * Create a compact string representation of the matrix.
232   *
233   * @return a <code> String </code> representing the matrix,
234   * row by row.
235   */
236  public String toString() {
237    StringBuffer sb = new StringBuffer(m_rows * (m_columns + 1) );
238    for (int i = 0; i < m_rows; i++) {
239      for (int j = 0; j < m_columns; j++) { 
240        sb.append(get(i, j) ? 1 : 0);
241      }
242      sb.append("\n");
243    }
244    return sb.toString();
245  }
246 
247  /**
248   * Returns the revision string.
249   *
250   * @return            the revision
251   */
252  public String getRevision() {
253    return RevisionUtils.extract("$Revision: 5922 $");
254  }
255}
Note: See TracBrowser for help on using the repository browser.