Skip to content

Commit 7021ce3

Browse files
committed
Use array instead of ArrayList for VariableMatrix storage
1 parent baf133b commit 7021ce3

File tree

1 file changed

+31
-21
lines changed

1 file changed

+31
-21
lines changed

wpimath/src/main/java/org/wpilib/math/autodiff/VariableMatrix.java

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44

55
package org.wpilib.math.autodiff;
66

7-
import java.util.ArrayList;
8-
import java.util.Arrays;
97
import java.util.Iterator;
108
import java.util.NoSuchElementException;
119
import java.util.function.BinaryOperator;
@@ -16,7 +14,7 @@
1614

1715
/** A matrix of autodiff variables. */
1816
public class VariableMatrix implements AutoCloseable, Iterable<Variable> {
19-
private final ArrayList<Variable> m_storage = new ArrayList<>();
17+
private final Variable[] m_storage;
2018
private int m_rows;
2119
private int m_cols;
2220

@@ -34,8 +32,9 @@ public VariableMatrix(int rows, int cols, long[] handles) {
3432

3533
m_rows = rows;
3634
m_cols = cols;
37-
for (int index = 0; index < rows * cols; ++index) {
38-
m_storage.add(new Variable(Variable.HANDLE, handles[index]));
35+
m_storage = new Variable[rows * cols];
36+
for (int index = 0; index < m_storage.length; ++index) {
37+
m_storage[index] = new Variable(Variable.HANDLE, handles[index]);
3938
}
4039
}
4140

@@ -57,8 +56,9 @@ public VariableMatrix(int rows) {
5756
public VariableMatrix(int rows, int cols) {
5857
m_rows = rows;
5958
m_cols = cols;
60-
for (int index = 0; index < rows * cols; ++index) {
61-
m_storage.add(new Variable());
59+
m_storage = new Variable[rows * cols];
60+
for (int index = 0; index < m_storage.length; ++index) {
61+
m_storage[index] = new Variable();
6262
}
6363
}
6464

@@ -80,9 +80,12 @@ public VariableMatrix(double[][] list) {
8080
assert row.length == m_cols;
8181
}
8282

83+
m_storage = new Variable[m_rows * m_cols];
84+
int index = 0;
8385
for (var row : list) {
8486
for (var elem : row) {
85-
m_storage.add(new Variable(elem));
87+
m_storage[index] = new Variable(elem);
88+
++index;
8689
}
8790
}
8891
}
@@ -105,8 +108,13 @@ public VariableMatrix(Variable[][] list) {
105108
assert row.length == m_cols;
106109
}
107110

111+
m_storage = new Variable[m_rows * m_cols];
112+
int index = 0;
108113
for (var row : list) {
109-
m_storage.addAll(Arrays.asList(row));
114+
for (var elem : row) {
115+
m_storage[index] = elem;
116+
++index;
117+
}
110118
}
111119
}
112120

@@ -118,9 +126,10 @@ public VariableMatrix(Variable[][] list) {
118126
public VariableMatrix(SimpleMatrix values) {
119127
m_rows = values.getNumRows();
120128
m_cols = values.getNumCols();
129+
m_storage = new Variable[m_rows * m_cols];
121130
for (int row = 0; row < values.getNumRows(); ++row) {
122131
for (int col = 0; col < values.getNumCols(); ++col) {
123-
m_storage.add(new Variable(values.get(row, col)));
132+
m_storage[row * m_cols + col] = new Variable(values.get(row, col));
124133
}
125134
}
126135
}
@@ -133,7 +142,7 @@ public VariableMatrix(SimpleMatrix values) {
133142
public VariableMatrix(Variable variable) {
134143
m_rows = 1;
135144
m_cols = 1;
136-
m_storage.add(variable);
145+
m_storage = new Variable[] {variable};
137146
}
138147

139148
/**
@@ -144,17 +153,18 @@ public VariableMatrix(Variable variable) {
144153
public VariableMatrix(VariableBlock values) {
145154
m_rows = values.rows();
146155
m_cols = values.cols();
156+
m_storage = new Variable[m_rows * m_cols];
147157
for (int row = 0; row < m_rows; ++row) {
148158
for (int col = 0; col < m_cols; ++col) {
149-
m_storage.add(values.get(row, col));
159+
m_storage[row * m_cols + col] = values.get(row, col);
150160
}
151161
}
152162
}
153163

154164
@Override
155165
public void close() {
156166
for (int index = 0; index < rows() * cols(); ++index) {
157-
m_storage.get(index).close();
167+
m_storage[index].close();
158168
}
159169
}
160170

@@ -258,7 +268,7 @@ public VariableMatrix set(double value) {
258268
public VariableMatrix set(Variable value) {
259269
assert rows() == 1 && cols() == 1;
260270

261-
m_storage.set(0, value);
271+
m_storage[0] = value;
262272

263273
return this;
264274
}
@@ -273,7 +283,7 @@ public VariableMatrix set(Variable value) {
273283
public void set(int row, int col, Variable value) {
274284
assert row >= 0 && row < rows();
275285
assert col >= 0 && col < cols();
276-
m_storage.set(row * cols() + col, value);
286+
m_storage[row * cols() + col] = value;
277287
}
278288

279289
/**
@@ -286,7 +296,7 @@ public void set(int row, int col, Variable value) {
286296
public void set(int row, int col, double value) {
287297
assert row >= 0 && row < rows();
288298
assert col >= 0 && col < cols();
289-
m_storage.set(row * cols() + col, new Variable(value));
299+
m_storage[row * cols() + col] = new Variable(value);
290300
}
291301

292302
/**
@@ -307,7 +317,7 @@ public void set(int index, double value) {
307317
*/
308318
public void set(int index, Variable value) {
309319
assert index >= 0 && index < rows() * cols();
310-
m_storage.set(index, value);
320+
m_storage[index] = value;
311321
}
312322

313323
/**
@@ -355,7 +365,7 @@ public void setValue(SimpleMatrix values) {
355365
public Variable get(int row, int col) {
356366
assert row >= 0 && row < rows();
357367
assert col >= 0 && col < cols();
358-
return m_storage.get(row * cols() + col);
368+
return m_storage[row * cols() + col];
359369
}
360370

361371
/**
@@ -366,7 +376,7 @@ public Variable get(int row, int col) {
366376
*/
367377
public Variable get(int index) {
368378
assert index >= 0 && index < rows() * cols();
369-
return m_storage.get(index);
379+
return m_storage[index];
370380
}
371381

372382
/**
@@ -850,7 +860,7 @@ public VariableMatrix cwiseMap(UnaryOperator<Variable> unaryOp) {
850860
* @return Number of elements in matrix.
851861
*/
852862
public int size() {
853-
return m_storage.size();
863+
return m_storage.length;
854864
}
855865

856866
@Override
@@ -1024,7 +1034,7 @@ public static VariableMatrix solve(VariableMatrix A, VariableMatrix B) {
10241034
long[] getHandles() {
10251035
var handles = new long[size()];
10261036
for (int index = 0; index < size(); ++index) {
1027-
handles[index] = m_storage.get(index).getHandle();
1037+
handles[index] = m_storage[index].getHandle();
10281038
}
10291039
return handles;
10301040
}

0 commit comments

Comments
 (0)