I was looking at this section of the vm spec:
http://java.sun.com/docs/books/vmspec/html/Compiling.doc.html#4095
and realized there are two methods to resolve a switch. That is tableswitch and lookupswitch. Tableswitch is good for when the switch cases are sequencial numbers like this (0,1,2,3), which happen to be the indices of a 4D vector.
Since I have recently restarted some work on a vecmath api i was planning i was thinking about doing something like this, using switches to fake array indexing. The float4D class is like Vector4d except that atributes are private and the indexing methods.
class float4D {
private float _0;
private float _1;
private float _2;
private float _3;
... constructors ...
public float get0() { return _0; }
public float get1() { return _1; }
public float get2() { return _2; }
public float get3() { return _3; }
public void set0(float f) { _0=f; }
public void set1(float f) { _1=f; }
public void set2(float f) { _2=f; }
public void set3(float f) { _3=f; }
public float get(int i) { switch (i) {
case 0: return _0;
case 1: return _1;
case 2: return _2;
case 3: return _3;
case 4: return _4;
default: return 0 } }
public void set(int i,float f) { switch (i) {
case 0: _0=f;
case 1: _1=f;
case 2: _2=f;
case 3: _3=f;
case 4: _4=f; } }
... more stuff ...
}
What do you guys think of this class? Would it be useful to have these indexing methods? Any idea if the speed match to array indexing?