At the risk of repeating myselfrepeating myself, I would like to stress that one has to be critical towards the superficial flexibility offered by Mathematica, when (particularly mutable) data structures are concerned. Using mutable data structures assumes a programming style for which Mathematica is not optimized. It can emulate it, yes, and we have seen a number of such emulations in the mentioned and newly posted answers (and I am myself responsible for a number of such emulations), but I personally find most of them unsatisfactory in several important aspects. For mutable data structures, some of them are:
- Peformance (top-level overhead)
- Instantiation and reference mechanism (usually use hash-tables or arrays as "memory heap", but this is just an emulation)
- Lack of pass-by-reference semantics (related to the previous. Again, have to emulate, another recent attempt herehere)
- Garbage collection (hard to make both reliable and efficient)
For immutable data structures (such as lists of rules), which some folks suggest to use to emulate structs, the main issues I see are
- Element access time (often much worse than
O(1)
) - Element update time (more of the same)
- No real pass-by-reference, and even no control over it (often large structures are copied when you pass / modify such a struct)
- Harder to estimate the complexity coming from such structures.
I suspect that the above are some of the reasons why most or all of the multiple suggested approaches based on the top-level code did not massively take off. The main reason must be, of course, performance. HereHere and herehere are two recent examples of how much the choice of the right data structures in Mathematica may affect the performance (how about 3-4 orders of magnitude difference?). The problem is, you will not get a good performance from mutable data structures implemented in the top-level, and you have to understand Mathematica really well to pick the right (for a given problem) fast immutable structures.
So, my suggestion is that we stop offering countless new emulations and stop praising Mathematica flexibility where it does not serve us well, since data structures are not the place where we have to be flexible - it is a place where we have to be clear and fast and compositional. Let's face it, currently Mathematica does not have a native support for mutable performant general data structures in the way that C or Java do. Perhaps, at some point it will, and then it will be different.
Now, why I am so much against emulations? Because at the end, you waste your time. I found that when I really need mutable data structures (which is usually for performance - demanding problems), I do things much faster in Java and link to Mathematica, than write in Mathematica and spend hours to optimize the top-level code. So, a pragmatic advice: either reformulate your problem in such a way that you can handle it with constructs natural to Mathematica (immutable lists, trees, rules, symbolic heads as containers for types, etc - if performance requirements of your problem allow that), or, if you really need mutable data structures, implement that part in a language which natively supports them, and link that to Mathematica. You will save your time.