Various solutions using arrays will "work" for some situations, and I
have in fact used arrays, sparse matrices, etc to represent structured
information. Usually the transform is from struct.field to field
[indexofstruct]. This means I end up using integers (or short, long,
whatever) as the "type" of every (logical) structure. I think even a
set of macros to do this kind of transform would be better than
nothing, though it would be far from trivial to get right because you
have to re-invent memory management in some form, even if it's just
passing the burden explicitly to the user. Doing it "manually"
involves all sorts of bug-prone boilerplate littering my code.
For example I might have a logical struct implemented as a bunch of
arrays: int struct_field1[SIZE], double struct_field2[SIZE], etc. I
can use integers as "pointers", so that struct_field2[structp] can be
used in place of (C-style) p->struct_field2. But suppose I want to
create an array of structs sorted using some special predicate? If I
don't want every instance of struct to be in this new array, I have to
either create a separate set of arrays sorted_struct_field1[OTHERSIZE]
etc, or use an index,length pair to define this logical array in terms
of the "master" array, so that logically sorted_structs[index] is
represented as struct_field1[sorted_structs_offset+index]. Needless
to say, without having these logical entities reified more explicitly
the code becomes crazy-making fast. I have made a few attempts at
frameworks to make this less painful, but ultimately threw up my hands
because Java's treatment of basic types makes it impossible to encode
the relevant operations in a generic (unboxed) way.
I'm also convinced that my performance suffers in such a scheme even
when I maintain arrays manually, because I usually want to access
multiple fields of one struct, not the same field of multiple structs,
consecutively.
Hope the above rant makes sense, perhaps others have discovered a
disciplined way of simulating structs which does not drive them crazy.
-Joe