> "... move to something
> where you use an array of integers to "point" to elements stored in
> another array. "
>
Sorry I didn't go into more detail, but I wasn't sure this would help.
The technique I mentioned has to do with building parallel arrays to
hold different elements of a struct, or building a parallel array that
gives you quicker searches of a complex datatype. For example, if you
have an array of personel info with names, ages, social security
numbers, etc. You sometimes want to find someone by name, sometimes by
social, and perhaps the array is originally given to you in age sorted
order.
This would mean that everytime you try to find someone by name, you
can't use a binary search or even stop when you pass the name and know
they aren't in the array. You have to look at every element. The
technique would be to build two parallel lookup arrays. The string
sorted array would simply be an array of the integers 0 through N-1,
ordered so that the first element contains the index of the first
element by string. The second element contains the index of the second,
and so on. So now you have essentially pointers from the index array to
the storage array. You build another to keep order by social, and as
many others as you like. These of course have to be updated when a new
record is added, so there is a cost, but for database types of access to
a largly static data set, this can help a lot on efficiency.
The other point where this helps is when there is lots of redundancy in
the info stored in the main array. As an example, lets say you are
storing cartesian points to describe 3D shapes. One way would simply be
to have arrays of faces, each face having an array of 3D vertices. This
works, but you aren't taking advantage of the fact that each vertex is
typically shared by three or more faces. In our structure it is
duplicated again and again, and actually makes it difficult to reshape a
3D shape.
A better structure, using the index technique is to build an array of
faces, each face has an array of vertex indices. The shape also has an
array of vertices, in no particular order, but the face vertices can now
"point" to any vertex and the faces now share it.
To test it, put a cube in the structures. The cube has six faces and
only eight points. In the first structure, there are six face arrays,
each with four 3D points for a total of 24 3D coords. The second has
six faces, each with four integer indices and the shape has a second
array of eight coordinates. It doesn't duplicate information making it
easier to update, it uses less storage, and it is quicker to access for
many operations.
> As I see it, that would not solve my problems with the memory
> alocation in case of change the array size. The system will still need
> to enlarge every and single element of the array I am changing for
> every element in the top level array, I guess.
>
When an array grows, that array may need to be moved in memory, since
arrays are stored in contiguous memory. If it is an array of one
million I8s, and you add one more byte, the 1M elements may need to move
so that the additional element can be stored with them. By contrast, an
array of 1,000 arrays of I8s, stores the same info. When adding a new
element, to one of the 1000 element arrays, the other 999,000 elements
do not need to move. Adding a new array to the 1000 already there means
that the array of 1000 array references may move, but none of the I8
elements will be moved.
If you want more help, please describe more about what is in the array,
to see if there is redundant storage that the first technique may help
with, and describe how you will access it. Will you do more inserts,
more deletes, or more accesses? These are the things that affect the
efficiency of various data structures.
Greg McKaskle