Creating a "Composite" Vector Array Field

In a numerically intensive code, I have a Cartesian vector class Vector3d which has the normal operator overloading and basic functions such as dot product, magnitude, etc. For simplicity, assume that it is not a templated class and its components are of type double.

I frequently need large 1-d arrays (e.g. stl vectors) of Vector3d. Two use-cases must be satisfied:
1) The trivial case in which the data are stored as stl vectors of Vector3d; and
2) The more difficult case where the individual components are stored as stl vectors of double, and are not guaranteed to be contiguous in memory (so one cannot rely on "stride").

Assuming the array lengths are all identical, I'd like to be able to access both types in a single loop. The straightforward way for case 2) is to construct a temporary Vector3d from the three components (indexed with the loop index). However, I would prefer not to incur the overhead of the constructor.

I have an idea that this is possible using template metaprogramming. Ideally I'd like a CompositeVector3d that inherits from Vector3d and is constructed with the component vectors, but can be dereferenced using the loop index in the same way as one would do with case 1.

I am not looking for the typical template metaprogramming utility of being able to operate on the entire array without explicit loops. I just want the syntactic "sugar" whereby CompositeVector3d and Vector3d act the same, plus the avoidance of the cost of the constructor. I am not averse to using an additional templated class (perhaps a Field or a View class) to access the basic storage in both case.

Any suggestions as to the simplest approach? Is it possible to do this without using a full template metaprogramming utility?

Last edited on
If you're array is big enough to eat a lot of RAM on it's own I would suggest moving it to a binary file if you know the outer limits of your arrays.
Here's an example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
bool get3DArray( 3DArray& data, uint16_t index )
  char* name = printf( "temp/3darray/%04X.bin", index );
  FILE bin = fopen( name, "br" );
  int i = 0, iEnd = 255; // 255 here is the number of elements in your array
  size_t objSize = sizeOf( float ) * 3;
  3DInfo obj;
  float* d = new float[3];
  // ensure bin ptr is at start of file
  for ( ; i < iEnd; ++i )
  {
     // read from where the pointer was left at using objSize to tell how many bytes
      obj.x = d[0];
      obj.y = d[1];
      obj.z = d[2];
      data[ i ] = obj;
  }
  fclose( bin );
  delete [] d;
}

I haven't looked in depth at FILE so I don't know the functions (I'm using wxWidgets) but this helps a great deal if you're dealing with vast quantities of bytes at a time, it reduces the chance of a crash from over using the RAM.

Just have one 3DArray object and just feed information to and from it using functions like this.
Last edited on
Topic archived. No new replies allowed.