0

For a Java project, I have 5 ENUM types that I need to use to index an 3-dimensional array of doubles, which define a particular property for each of several XYZ points in 3D rectilinear space. Would it be better to organize it like so:

double[][][][][][][][] arr =
        new double[enum1Size][enum2Size][enum3Size][enum4Size][enum5Size]
                [maxX+1][maxY+1][maxZ+1];

arr[enum1][enum2][enum3][enum4][enum5][x][y][z] = theDouble;

Or by using a simple array and indexing it using a hashCode of an object containing each of the enums:

class EnumIndex {
    Enum1Type enum1;
    Enum2Type enum2;
    Enum3Type enum3;
    Enum4Type enum4;
    Enum5Type enum5;

    public EnumIndex(Enum1Type enum1, Enum2Type enum2,
            Enum3Type enum3, Enum4Type enum4, Enum5Type enum5)  {
        this.enum1 = enum1;
        this.enum2 = enum2;
        this.enum3 = enum3;
        this.enum4 = enum4;
        this.enum5 = enum5;
    }

    public int hashCode() {
         // Eclipse-generated hashcode function
    }

    public static int maxHashCode() {
         // generate maximum hashcode based on maximum ordinal of each enum
    }
}

double[][][][] arr = new double[EnumIndex.maxHashcode+1][maxX+1][maxY+1][maxZ+1];

EnumIndex ei1 = new EnumIndex(enum1, enum2, enum3, enum4, enum5);

double[ei1][x][y][z] = theDouble;
  • Enum1Type has 15 values.
  • Enum2Type has 4 values.
  • Enum3Type has 4 values.
  • Enum4Type has 2 values.
  • Enum5Type has 2 values.

  • X ranges from 0-9

  • Y ranges from 0-5
  • Z ranges from 0-22

Therefore there are on the order of 1,324,800 doubles to be indexed.

I was going to use EnumMaps of EnumMaps, but that seemed to be overkill. Processing speed is a big concern on this project, so I'm trying to avoid iteration; forcing the runtime to use pointer arithmetic to get at the right memory locations.

livefree75
  • 720
  • 8
  • 18
  • wait, what are you trying to accomplish? What needs to be fast here? Are you using all elements of this insane array or is it sparse? Will you iterate it later or will you use look up specific elements? – vidstige Jan 29 '13 at 20:23

1 Answers1

1

If speed is a serious concern, I suggest to use an spatial index structure such as an R-tree. In particular, if you want to index so many elements. I suspect that a self-made index will not solve your problem.

However, I do not know whether there is a free Java R-tree implementation.

Edit: There is one - http://sourceforge.net/projects/jsi/

Dan
  • 1,539
  • 12
  • 23
  • Not really familiar with R-trees. Not sure I can take the time to learn it right now. :) – livefree75 Jan 29 '13 at 20:04
  • MySQL provides an R-tree index on a database. You can define such an index on several columns and get the R-tree for free. It might not help the OP since he wants it to be all in-memory, but in general this will probably be faster than trying to debug what you have above. – Andrew Mao Jan 29 '13 at 20:07
  • @livefree75: if the library is well designed, you don't need the know the interna. It should work like a Java collection. – Dan Jan 29 '13 at 20:10
  • What do you know, a Java R-tree data structure: http://sourceforge.net/projects/jsi/. Plus check out other suggestions from this post: http://stackoverflow.com/questions/7435645/java-commercial-friendly-r-tree-implementation – Andrew Mao Jan 29 '13 at 20:11