# How to use DataStructure in FunctionCompile?

The official document said

compile a function that can work with data structures

but how to use it?

There is no doubt that it is possible to use DataStructure as a function parameter:

FunctionCompile[Function[Typed[ds, "BitVector"], ds]]


but I find no way to manipulate it, the following code will give an error:

FunctionCompile[Function[Typed[ds, "BitVector"], ds["BitCount"]]]


So how do I manipulate the DataStructure object?

• Good question. So far, I found the functionality of FunctionCompile quite underwhelming: With quite an expansive syntax it manages to produce only suboptimally performing compiled functions. And the docs of FunctionCompile and DataStructure  in version 12.1 are so sparse that I do not have any hope that it will work any better. I doubt that FunctionCompile can serve any reasonable purpose at the moment. The symbols are still marked as [EXPERIMENTAL] in the docs, so we probalby have to wait for a later release. (I have to say that I don't have access to 12.1, yet). – Henrik Schumacher Mar 20 '20 at 12:11
• So in short: I believe that the current answer for your question is: "You cannnot." But maybe some WRI internal can give us some hints. (I would indeed really appreciate that. I find these data structures super fascinating.) – Henrik Schumacher Mar 20 '20 at 12:12

Note that this code works in 12.2 (did not work in 12.1):

cf = FunctionCompile[Function[{Typed[ds, "BitVector"]}, ds["BitCount"]]];
ds = CreateDataStructure["BitVector", 32];
ds["BitSet", 1];
ds["BitSet", 2];
ds["BitSet", 3];
cf[ds]


(Gives 3)

The quoted sentence from the guide does seem a bit forward-looking.

As evidenced by all the documentation examples given for any of the new data structures, the current emphasis is on working with them in top level (interpreted) Wolfram Language code.

The goal is to eventually support also in FunctionCompile the same nice syntax shown in the question. This kind of integration is made easier by the fact that all of the data structures are implemented using the Compiler (and shipped in a compiled library form).

While there is an internal API for operating on data structures within compiled functions, at present it remains undocumented and subject to change.

For example, the code from the question would currently look like

cf = FunctionCompile[Function[Typed[ds, "BitVector"], Count[ds]]];


which performs an equivalent operation to ds["BitCount"]

len = 128;

ds = CreateDataStructure["BitVector", len];

Scan[ds["BitSet", #] &,
RandomInteger[len - 1, RandomInteger[len - 1]]];

{ds["BitCount"], cf[ds]}

(* {43, 43} *)