159. Introducing layout flattening
Let’s suppose that we have the following nested model (the exact same model as in Problem 158):
SequenceLayout innerSeq
= MemoryLayout.sequenceLayout(5, ValueLayout.JAVA_DOUBLE);
SequenceLayout outerSeq
= MemoryLayout.sequenceLayout(10, innerSeq);
Next, we define a VarHandle
via PathElement
and we populate this model accordingly with some random data in a memory segment named segment
(you can see the code listed in Problem 158).
Our goal is to take this nested model and obtain a flat model. So, instead of having 10 sequences of 5 double
values each, we want one sequence of 50 double
values. This can be achieved via the flatten()
method, as follows:
SequenceLayout flatten = outerSeq.flatten();
VarHandle fhandle = flatten.varHandle(
PathElement.sequenceElement());
for (int i = 0; i < flatten.elementCount(); i++) {
System.out.printf("\nx = %.2f", fhandle.get(segment, i));
}
Notice the PathElement
, which...