-
Notifications
You must be signed in to change notification settings - Fork 10
/
MatrixCol.java
314 lines (278 loc) · 7.84 KB
/
MatrixCol.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
/*-
* APT - Analysis of Petri Nets and labeled Transition systems
* Copyright (C) 2012-2013 Members of the project group APT
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
package fr.lip6.move.gal.util;
import java.util.ArrayList;
import java.util.List;
import android.util.SparseArray;
/**
* A sparse Matrix of arbitrary object, stored by COLUMN so that deleteColumn has good complexity.
* Each column is using SparseArray storage.
*
* This code has diverged quite a lot from its origins in APT codebase.
* @author Manuel Gieseking, Dennis-Michael Borde, Yann Thierry-Mieg
*/
public class MatrixCol<T> {
private int iRows;
private int iCols;
private final List<SparseArray<T>> lCols;
/**
* Constructor for a new Matrix with the given col and row count.
* @param rows - the row count of the resulting matrix.
* @param cols - the col count of the resulting matrix.
*/
public MatrixCol(int rows, int cols) {
this.iRows = rows;
this.iCols = cols;
this.lCols = new ArrayList<>(this.iCols);
for (int col = 0 ; col < iCols ; col++) {
lCols.add(new SparseArray<>());
}
}
/**
* build a copy of a MatrixCol
* @param ori
*/
public MatrixCol (MatrixCol<T> ori) {
iRows = ori.iRows;
iCols = ori.iCols;
lCols = new ArrayList<>(iCols);
for (SparseArray<T> a : ori.lCols) {
lCols.add(a.clone());
}
}
/**
* Constructor for a new Matrix with the values from the given array.
* @param src - the template to create the matrix from.
*/
public MatrixCol(T[][] src) {
this.iRows = src.length;
this.iCols = src[0].length;
this.lCols = new ArrayList<>(this.iCols);
for (int col = 0; col < this.iCols; ++col) {
final SparseArray<T> toadd = new SparseArray<>();
for (int row = 0; row < this.iRows; ++row) {
T val = src[row][col];
if (val != null) {
toadd.put(row,val);
}
}
lCols.add(toadd);
}
}
/** Exchange back to explicit form if required.
* @return an explicit verion of this matrix, mat[i] giving a column at index i.
*/
public T [][] explicit () {
@SuppressWarnings("unchecked")
T [][] mat = (T[][]) new Object[iRows][iCols];
for (int col = 0; col < this.iCols; ++col) {
SparseArray<T> arr = lCols.get(col);
for (int i = 0 ; i < arr.size() ; i++) {
int row = arr.keyAt(i);
T val = arr.valueAt(i);
mat [row][col] = val;
}
}
return mat;
}
/**
* Returns the count of rows of this matrix.
* @return the count of rows of this matrix.
*/
public int getRowCount() {
return this.iRows;
}
/**
* Add a new empty row.
*/
public void addRow() {
iRows ++;
}
/**
* Returns the count of cols of this matrix.
* @return the count of cols of this matrix.
*/
public int getColumnCount() {
return this.iCols;
}
// /**
// * Returns the row with the given index in this matrix. Changes to this row will have affect to this matrix.
// * @param i - the index of the wished row of this matrix.
// * @return the row with the given index in this matrix.
// */
// public List<Integer> getRow(int i) {
// List<Integer> result = new ArrayList<>(iRows);
// for (List<Integer> row : lCols) {
// result.add(row.get(i));
// }
// return result;
//// return this.lCols.get(i);
// }
/**
* Returns the column with the given index of this matrix.
* @param i - the index of the wished column of this matrix.
* @return a copy of the column with the given index of this matrix.
*/
public SparseArray<T> getColumn(int i) {
return lCols.get(i);
}
public SparseArray<T> setColumn(int i,SparseArray<T> v) {
return lCols.set(i,v);
}
public T get (int row, int col) {
return lCols.get(col).get(row);
}
public void set (int row, int col, T val) {
if (row < 0 || col < 0 || row >= iRows || col >= iCols)
throw new IllegalArgumentException();
if (val != null) {
SparseArray<T> column = lCols.get(col);
if (column.size()== 0 || column.keyAt(column.size()-1) < row) {
column.append(row, val);
} else {
column.put(row,val);
}
} else {
lCols.get(col).delete(row);
}
}
/**
* Returns a row which has at least one component different from zero. It also returns the index of the column
* where a component not equal to zero was found. If such a row does not exists, then null.
* @return the index of the column with a none zero component and the addicted row or null if not existent.
*/
public int[] getNoneZeroRow() {
// optimize to prefer to return 1
for (int tcol = 0; tcol < getColumnCount(); tcol++) {
if (lCols.get(tcol).size()==0) {
continue;
} else {
return new int [] {lCols.get(tcol).keyAt(0) , tcol};
}
}
return null;
}
/**
* Deletes the column with the given index from this matrix.
* @param j - the index of the column which should be deleted.
*/
public void deleteColumn(int j) {
lCols.remove(j);
this.iCols -= 1;
}
/**
* Appends a given column to this matrix. That means adding the given column from the right side to this matrix.
* @param column - the column to append.
*/
public void appendColumn(SparseArray<T> column) {
assert column.size()==0 || iRows > column.keyAt(column.size()-1);
lCols.add(column);
this.iCols++;
}
/**
* Checks if this matrix only contains of components equal to zero.
* @return true if this matrix has just components equal to zero.
*/
public boolean isZero() {
for (SparseArray<T> row : this.lCols) {
if (row.size() != 0) {
return false;
}
}
return true;
}
/**
* Transpose the Matrix in a new copy.
*/
public MatrixCol<T> transpose() {
MatrixCol<T> tr = new MatrixCol<>(iCols, iRows);
transposeTo(tr,false);
return tr;
}
public void transposeTo(MatrixCol<T> tr) {
transposeTo(tr, true);
}
public void transposeTo(MatrixCol<T> tr, boolean clear) {
if (clear)
tr.clear(getColumnCount(),getRowCount());
for (int tcol = 0; tcol < iCols; tcol++) {
SparseArray<T> col = lCols.get(tcol);
for (int k =0 ; k < col.size() ; k++) {
int trow = col.keyAt(k);
T val = col.valueAt(k);
tr.set(tcol, trow, val);
}
}
}
@Override
public String toString() {
return "Matrix{" + "lCols=" + lCols + '}';
}
/**
* Use with care !! Basically, just consider this is readonly.
* @return our very own storage !
*/
public List<SparseArray<T>> getColumns () {
return lCols;
}
public void clear(int rowCount, int colCount) {
int missing = colCount - lCols.size();
if (missing > 0) {
for (int i = 0; i < missing; i++) {
lCols.add(new SparseArray<T>());
}
} else {
for (int i = 0; i < missing; i++) {
lCols.remove(lCols.size()-1);
}
}
iCols = colCount;
iRows = rowCount;
for (SparseArray<T> s : lCols) {
s.clear();
}
}
public void deleteRow(int row) {
for (SparseArray<T> col : getColumns()) {
col.deleteAndShift(row);
}
iRows -= 1;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
@SuppressWarnings("unchecked")
MatrixCol<T> other = (MatrixCol<T>) obj;
if (iCols != other.iCols)
return false;
if (iRows != other.iRows)
return false;
if (lCols == null) {
if (other.lCols != null)
return false;
} else if (!lCols.equals(other.lCols))
return false;
return true;
}
}