M
Modular5mo ago
samufi

Is there a (N-D) contiguous data structure for non-trivial structs?

I need a data strucuture that is a 2-dimensional array of (inline) lists of pairs. That is, if subdata = data[i][j], then subdata is a dynamic data structure that contains tuples of size 2. My take on solving this was to use a Tensor for the 2-D data structure, an InlinedFixedVector for the flexible part, and a StaticTuple for the pair.
from tensor import Tensor
from collections import InlinedFixedVector
from utils import StaticTuple


struct MyStruct:
var data: Tensor[InlinedFixedVector[StaticTuple[Float64, 2], 10]]
from tensor import Tensor
from collections import InlinedFixedVector
from utils import StaticTuple


struct MyStruct:
var data: Tensor[InlinedFixedVector[StaticTuple[Float64, 2], 10]]
However, I get the following error:
'Tensor' parameter #0 has 'DType' type, but value has type 'AnyStruct[InlinedFixedVector[StaticTuple[SIMD[float64, 1], 2], 10]]'
'Tensor' parameter #0 has 'DType' type, but value has type 'AnyStruct[InlinedFixedVector[StaticTuple[SIMD[float64, 1], 2], 10]]'
I figure that Tensors can only hold trivial data types. If so, is there a good general data structure for multi-dimensional data? I was thinking of putting InlineList into a self-made 2D represntation of List, but InlineList does not implement CollectionElement (as of yet?).
4 Replies
Darkmatter
Darkmatter5mo ago
https://docs.modular.com/mojo/stdlib/utils/static_tuple/InlineArray InlineArray[InlineArray[T, N], M] for an M by N 2d array.
InlineArray | Modular Docs
A fixed-size sequence of size homogeneous elements where size is a constant expression.
samufi
samufiOP5mo ago
Thanks! Hmm... I do not know the size of the outer array at compile time. Is there an alternative with dynamic memory?
Darkmatter
Darkmatter4mo ago
List will put it all behind one pointer, but you probably want that anyways.
samufi
samufiOP4mo ago
Yes, I think in principle, List is not too bad for the "outer" array (except that I need to wrap it into a 2D structure around for simple access). I guess this is the easiest approach if no one else has done the work so far... Then the only remaining question would be which container provides inline-vector-like functionality but can hold tuples and implements CollectionElement
Want results from more Discord servers?
Add your server