You will find the full LibraryLink user guide here:
LibraryLink only has direct support for a few types, mostly full arrays. Ragged arrays (i.e. a general list of lists) is not directly supported. You can use one of these two indirect ways:
-
Use MathLink from within a LibraryLink function. See the last two examples here , as well as the user guide. “MathLink” has recently been renamed WSTP for silly marketing reasons, which causes all sorts of inconveniences. You will notice that the user guide uses WS-prefixed functions, and the example I linked to uses ML-prefixed ones. The fact is that the WS-prefixed functions do not work in a straightforward way with LibraryLink (despite what the documentation says). Just use the ML-prefixed ones, and forget about “WSTP”. MathLink works just fine, even if it is “deprecated” according to the documentation.
If you go this route, you will need to learn a bit of MathLink programming. I recommend this guide.
-
Flatten your array into one long vector. Prepends the length of each sublist. Then prepend the number of sublists. Thus you would transform
{{1,2}, {3,4,5}}into
{2 (* two lists *), 2 (* of length 2 ... *), 3 (* ... and length 3 *), (* { *)1, 2 (* } *), (* { *) 3, 4, 5 (* } *)Then write a decoder function for it in Mathematica.
This approach will be much faster for lists with lots of sublists than the MathLink-based method.
Here’s a function to unpack such a format into a ragged array (quickly adapted from something I use, but not tested)
(* almost the same as Internal`PartitionRagged *)
partitionRagged[v_List, l_?VectorQ] :=
MapThread[Take[v, {#1, #2}] &,
Module[{a = Accumulate[l]}, {a - l + 1, a}]]
unpack[packed_] :=
With[{len = Round@First[packed]},
partitionRagged[
1 + packed[[len + 2 ;; All]],
Round[packed[[2 ;; len + 1]]]
]
]