Yeah sorry. It was late last night an I lost my ability to articulate myself. The bodies were fixed length and everyhting was easy. Now they are not.
The code that worked for fixed sized vectors was:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| public class CompositeVector implements Vector { int vectorSize = -1; ArrayList<Vector> vectors = new ArrayList<Vector>(); public void addVector(Vector v) { if (vectorSize == -1) vectorSize = v.size(); assert vectorSize == v.size() : "vectors must be same size"; vectors.add(v); }
public void set(int index, float val) { vectors.get(index / vectorSize).set(index % vectorSize, val); }
public float get(int index) { return vectors.get(index / vectorSize).get(index % vectorSize); } } |
Note that the get and set methods work out how to get to the correct sub vector with modulus and divide. No extra memory overhead and extremely quick.
I am not trying to work out a new method for doing this which will work for variable sized subvectors. I do not expect to get quite such an elagant way, but I am trying to avoid having to copy everything to a buffer. The problem with using a buffer or something is that you then need to add an update method to write the buffer back into the originals. Basically I really want the underlying storage for the values in the composite vector to be the underlying vectors. I also want the access time to be near constant.
One definite solution would be to create a HashMap that contained an entry for every large vector index. This entry would contain two values indicating which subvector and which local index to use. This hashmap can be generated as the sub-vectors are appended to the composite vector. I feel like this is a bit crude though (and not very gc freindly).