-
Notifications
You must be signed in to change notification settings - Fork 0
Home
Welcome to the MDAAJ wiki!
Multidimensional Dynamic Array Api for Java. You can create dynamic multidimensional generic arrays at runtime.
The There exist three implementations (extends from MDDABasic)
- MDDANested The array elements have an inner array and the inner array have an array inside it..., its nested for each dimension.
- MDDAGenerated For this type, there will be generated a .class (byte code) at runtime with the specific dimensions.
- MDDA This class provides a multidimensional access for the user, but the array is internally only one dimensional. This type gives best performance!
The MDDAInterface function overview
// set the generic element at the indices (return type indicated if it was successfull)
boolean set(T value, int... indices)
// get the generic element at the indices
public T get(int... indices)
// fill the whole array with the generic value
public boolean fill(T value)
// convert the array into one dimensional array
public T[] flatten()
// print the array
public void print(OutputStream out)
// the raw array object
public Object getArray();
// array with elements, which conatain for each dimension the length of the dimension
public int[] getSize();
The standard way to create an array looks like this:
int[] a = new int[10];
double[][] b = new double[10][20];
String[][][][][] c = new String[10][20][5][8][15];
c[0][0][2][1][0] = "xyz";
String s = c[0][0][0][0][0];
for (int i=0; i<b.length; i++) {
for (int j=0; j<b[i].length; i++) {
b[i][j] = random.nextDouble();
}
}
Creating the same array with MDDAJ:
MDDA<Integer> a = new MDDA<Integer>(10);
MDDA<Double> b = new MDDA<Double>(10,20);
MDDA<String> c = new MDDA<String>(10,20,5,8,15);
c.set("xyz", 0,0,2,1,0);
String s = c.get(0,0,0,0,0);
for (int i=0; i<b.getSize()[0]; i++) {
for (int j=0; j<getSize()[1]; i++) {
b.set(random.nextDouble(), i,j);
}
}
MDDA holds every array as one dimensional array. You can also use the index for the internal array. Append for the getter and setter 1D
for one dimensional index access.
// loop over the whole array with the one dim index
for (int i=0; i<a.getArray().length; i++) {
a.set1D(r.nextDouble(), i);
lastValue = a.get1D(i);
}
Double sum = 0.0;
// MDDA implements the Iterable, you can use the foreach construct
for (Double elem : a) {
sum += elem;
}
The MDDA implements this interface and provides the neighbors indices of an element for a grid topology. For example in a 2d space your array have the dimension 3x3, MDDA<Integer> a = new MDDA<Integer>(3,3);
The numbers are the internal one dimensional indicies:
0 - 1 - 2
| | |
3 - 4 - 5
| | |
6 - 7 - 8
// internal one dim index <==> multi dim index
a.get1D(0) <= equivalent => a.get(0,0);
a.get1D(1) <= equivalent => a.get(0,1);
a.get1D(2) <= equivalent => a.get(0,2);
a.get1D(3) <= equivalent => a.get(1,0);
a.get1D(4) <= equivalent => a.get(1,1);
The neighbors indices will always used with the internal one dim index
int distance = 1; // reachable within one steps from the origin
Set<Integer> getNeighbors = a.getNeighborForAllDims(distance, 1,1)
// getNeighbors = {1,3,5,7}
Set<Integer> getNeighbors2 = a.getNeighborForAllDims(distance+1, 1,1)
// getNeighbors2 = {1,3,5,7,0,2,6,8}
You can also call a.getNeighborForDim(distance, 1, 1,1)
for only the 2nd dimension, the result will be {3,5}