There is new DataStructure in V 12.1. I thought at first it is similar to classical struct or record found in other languages and discussed in
Struct equivalent in Mathematica?
How to use classical data structure in Mathematica
Wikipedia entry for struct is https://en.wikipedia.org/wiki/Struct_(C_programming_language)
To make this question concrete, suppose one wants to make this struct, which in C is
struct personal_info
{
char name[50];
int age;
char address[50];
};
Then make a variable of the above type and access the struct fields using the dot notation in C. This is similar to record
in Pascal and Ada also.
How to use the new DataStructure
to do this? In C one will do something like the following
struct personal_info my_info;
my_info.age = 90;
fprintf("my age is %d",my_info.age);
I could not find one basic example in the help pages on how to actually use this new DataStructure
. I also looked at Wolfram blog on this, where he says
Think about all those data structures that get mentioned in textbooks, papers, libraries, etc. Our goal is to have all of them seamlessly usable directly in the Wolfram Language, and accessible in compiled code, etc. Of course it’s huge that we already have a universal “data structure”
But following Wolfram's examples on his blog, I still do not know how to make such a simple struct using Datastruct
(I am not looking at queues or linked list, or other advanced structs, just the basic and most common struct).
So I tried few random things (when all else fails, I switch to the trial and error method)
ds = CreateDataStructure["DynamicArray"]
ds["Append", {"name" -> "doe joe", "age" -> 90,
"Address" -> "POBox 100, main street, California"}];
I used DynamicArray
above, as I did not know what else to pick for classical struct.
But I do not think this is right at all, as I do not know now how to obtain say the name
back.
Ofcourse one can use Association
for this now in Mathematica, but I thought the new DataStructure
will do this a little easier.
Question is: How to use new DataStructure
similar to how it is used in C?
DataStructure
stuff is really an algorithms-oriented development. They're intend to make your code run faster andstruct
is already fast enough usingAssociation
and some object-oriented programming on top of that. Unless you're working with compiled code, there's no real reason to have astruct
that I can see (in that case it would just make it a bit easier to implement things, I figure). $\endgroup$FunctionCompile
and friends there's gonna be no benefit to having it be a bespokeDataStructure
$\endgroup$NumericArray
is a mostly a transfer format at this point that has special use in compiled code and friends.PackedArray
is more relevant for Mathematica programming. $\endgroup$Association
keep the data in sequencial chunks of memory at the time of initialization at the very least? Also the moment you try to replace an in-between item with larger sizeAssociation
doesn't complain. Probably it stores it somewhere else and keeps track of pointers like a linked list. SoAssociation
might not really be a true struct down at memory level. $\endgroup$Association
is not implemented as astruct
. It's not memory optimizing because that's not what top-level code does... That's why we haveFunctionCompile
. Low-level features should enter where low-level features are relevant. The only place where we have an awkward intermediate isPackedArray
. $\endgroup$