Class SequenceLayout

java.lang.Object
jdk.incubator.foreign.SequenceLayout
All Implemented Interfaces:
Constable, MemoryLayout

public final class SequenceLayout extends Object implements MemoryLayout
A sequence layout. A sequence layout is used to denote a repetition of a given layout, also called the sequence layout's element layout. The repetition count, where it exists (e.g. for finite sequence layouts) is said to be the the sequence layout's element count. A finite sequence layout can be thought of as a group layout where the sequence layout's element layout is repeated a number of times that is equal to the sequence layout's element count. In other words this layout:

MemoryLayout.sequenceLayout(3, MemoryLayout.valueLayout(32, ByteOrder.BIG_ENDIAN));
 
is equivalent to the following layout:

MemoryLayout.structLayout(
    MemoryLayout.valueLayout(32, ByteOrder.BIG_ENDIAN),
    MemoryLayout.valueLayout(32, ByteOrder.BIG_ENDIAN),
    MemoryLayout.valueLayout(32, ByteOrder.BIG_ENDIAN));
 

This is a value-based class; programmers should treat instances that are equal as interchangeable and should not use instances for synchronization, or unpredictable behavior may occur. For example, in a future release, synchronization may fail. The equals method should be used for comparisons.

Unless otherwise specified, passing a null argument, or an array argument containing one or more null elements to a method in this class causes a NullPointerException to be thrown.

Implementation Requirements:
This class is immutable and thread-safe.
  • Method Details

    • elementLayout

      public MemoryLayout elementLayout()
      Returns the element layout associated with this sequence layout.
      Returns:
      The element layout associated with this sequence layout.
    • elementCount

      public OptionalLong elementCount()
      Returns the element count of this sequence layout (if any).
      Returns:
      the element count of this sequence layout (if any).
    • withElementCount

      public SequenceLayout withElementCount(long elementCount)
      Obtains a new sequence layout with same element layout, alignment constraints and name as this sequence layout but with the new specified element count.
      Parameters:
      elementCount - the new element count.
      Returns:
      a new sequence with given element count.
      Throws:
      IllegalArgumentException - if elementCount < 0.
    • reshape

      public SequenceLayout reshape(long... elementCounts)
      Returns a new sequence layout where element layouts in the flattened projection of this sequence layout (see flatten()) are re-arranged into one or more nested sequence layouts according to the provided element counts. This transformation preserves the layout size; that is, multiplying the provided element counts must yield the same element count as the flattened projection of this sequence layout.

      For instance, given a sequence layout of the kind:

      
          var seq = MemoryLayout.sequenceLayout(4, MemoryLayout.sequenceLayout(3, MemoryLayouts.JAVA_INT));
       
      calling seq.reshape(2, 6) will yield the following sequence layout:
      
          var reshapeSeq = MemoryLayout.sequenceLayout(2, MemoryLayout.sequenceLayout(6, MemoryLayouts.JAVA_INT));
       

      If one of the provided element count is the special value -1, then the element count in that position will be inferred from the remaining element counts and the element count of the flattened projection of this layout. For instance, a layout equivalent to the above reshapeSeq can also be computed in the following ways:

      
          var reshapeSeqImplicit1 = seq.reshape(-1, 6);
          var reshapeSeqImplicit2 = seq.reshape(2, -1);
       
      Parameters:
      elementCounts - an array of element counts, of which at most one can be -1.
      Returns:
      a new sequence layout where element layouts in the flattened projection of this sequence layout (see flatten()) are re-arranged into one or more nested sequence layouts.
      Throws:
      UnsupportedOperationException - if this sequence layout does not have an element count.
      IllegalArgumentException - if two or more element counts are set to -1, or if one or more element count is <= 0 (but other than -1) or, if, after any required inference, multiplying the element counts does not yield the same element count as the flattened projection of this sequence layout.
    • flatten

      public SequenceLayout flatten()
      Returns a new, flattened sequence layout whose element layout is the first non-sequence element layout found by recursively traversing the element layouts of this sequence layout. This transformation preserves the layout size; nested sequence layout in this sequence layout will be dropped and their element counts will be incorporated into that of the returned sequence layout. For instance, given a sequence layout of the kind:
      
          var seq = MemoryLayout.sequenceLayout(4, MemoryLayout.sequenceLayout(3, MemoryLayouts.JAVA_INT));
       
      calling seq.flatten() will yield the following sequence layout:
      
          var flattenedSeq = MemoryLayout.sequenceLayout(12, MemoryLayouts.JAVA_INT);
       
      Returns:
      a new sequence layout with the same size as this layout (but, possibly, with different element count), whose element layout is not a sequence layout.
      Throws:
      UnsupportedOperationException - if this sequence layout, or one of the nested sequence layouts being flattened, does not have an element count.
    • toString

      public String toString()
      Description copied from class: Object
      Answers a string containing a concise, human-readable description of the receiver.
      Specified by:
      toString in interface MemoryLayout
      Overrides:
      toString in class Object
      Returns:
      String a printable representation for the receiver.
    • equals

      public boolean equals(Object other)
      Description copied from class: Object
      Compares the argument to the receiver, and answers true if they represent the same object using a class specific comparison. The implementation in Object answers true only if the argument is the exact same object as the receiver (==).
      Specified by:
      equals in interface MemoryLayout
      Parameters:
      other - Object the object to compare with this object.
      Returns:
      boolean true if the object is the same as this object false if it is different from this object.
      See Also:
    • hashCode

      public int hashCode()
      Description copied from class: Object
      Answers an integer hash code for the receiver. Any two objects which answer true when passed to .equals must answer the same value for this method.
      Specified by:
      hashCode in interface MemoryLayout
      Returns:
      the receiver's hash.
      See Also:
    • describeConstable

      public Optional<DynamicConstantDesc<SequenceLayout>> describeConstable()
      Description copied from interface: MemoryLayout
      Returns an Optional containing the nominal descriptor for this layout, if one can be constructed, or an empty Optional if one cannot be constructed.
      Specified by:
      describeConstable in interface Constable
      Specified by:
      describeConstable in interface MemoryLayout
      Returns:
      An Optional containing the resulting nominal descriptor, or an empty Optional if one cannot be constructed.
    • withName

      public SequenceLayout withName(String name)
      Creates a new layout which features the desired layout name.

      This is equivalent to the following code:

      
          withAttribute(LAYOUT_NAME, name);
       
      Specified by:
      withName in interface MemoryLayout
      Parameters:
      name - the layout name.
      Returns:
      a new layout which is the same as this layout, except for the name associated with it.
      See Also:
    • withBitAlignment

      public SequenceLayout withBitAlignment(long alignmentBits)
      Creates a new layout which features the desired alignment constraint.
      Specified by:
      withBitAlignment in interface MemoryLayout
      Parameters:
      alignmentBits - the layout alignment constraint, expressed in bits.
      Returns:
      a new layout which is the same as this layout, except for the alignment constraint associated with it.
    • withAttribute

      public SequenceLayout withAttribute(String name, Constable value)
      Returns a new memory layout which features the same attributes as this layout, plus the newly specified attribute. If this layout already contains an attribute with the same name, the existing attribute value is overwritten in the returned layout.
      Specified by:
      withAttribute in interface MemoryLayout
      Parameters:
      name - the attribute name.
      value - the attribute value.
      Returns:
      a new memory layout which features the same attributes as this layout, plus the newly specified attribute.
    • name

      public final Optional<String> name()
      Description copied from interface: MemoryLayout
      Return the name (if any) associated with this layout.

      This is equivalent to the following code:

      
          attribute(LAYOUT_NAME).map(String.class::cast);
       
      Specified by:
      name in interface MemoryLayout
      Returns:
      the layout name (if any).
      See Also:
    • attribute

      public Optional<Constable> attribute(String name)
      Description copied from interface: MemoryLayout
      Returns the attribute with the given name (if it exists).
      Specified by:
      attribute in interface MemoryLayout
      Parameters:
      name - the attribute name
      Returns:
      the attribute with the given name (if it exists).
    • attributes

      public Stream<String> attributes()
      Description copied from interface: MemoryLayout
      Returns a stream of the attribute names associated with this layout.
      Specified by:
      attributes in interface MemoryLayout
      Returns:
      a stream of the attribute names associated with this layout.
    • bitAlignment

      public final long bitAlignment()
      Description copied from interface: MemoryLayout
      Returns the alignment constraint associated with this layout, expressed in bits. Layout alignment defines a power of two A which is the bit-wise alignment of the layout. If A <= 8 then A/8 is the number of bytes that must be aligned for any pointer that correctly points to this layout. Thus:
      • A=8 means unaligned (in the usual sense), which is common in packets.
      • A=64 means word aligned (on LP64), A=32 int aligned, A=16 short aligned, etc.
      • A=512 is the most strict alignment required by the x86/SV ABI (for AVX-512 data).
      If no explicit alignment constraint was set on this layout (see MemoryLayout.withBitAlignment(long)), then this method returns the natural alignment constraint (in bits) associated with this layout.
      Specified by:
      bitAlignment in interface MemoryLayout
      Returns:
      the layout alignment constraint, in bits.
    • hasSize

      public boolean hasSize()
      Description copied from interface: MemoryLayout
      Does this layout have a specified size? A layout does not have a specified size if it is (or contains) a sequence layout whose size is unspecified (see elementCount()). Value layouts (see ValueLayout) and padding layouts (see MemoryLayout.paddingLayout(long)) always have a specified size, therefore this method always returns true in these cases.
      Specified by:
      hasSize in interface MemoryLayout
      Returns:
      true, if this layout has a specified size.
    • bitSize

      public long bitSize()
      Description copied from interface: MemoryLayout
      Computes the layout size, in bits.
      Specified by:
      bitSize in interface MemoryLayout
      Returns:
      the layout size, in bits.
    • isPadding

      public boolean isPadding()
      Description copied from interface: MemoryLayout
      Is this a padding layout (e.g. a layout created from MemoryLayout.paddingLayout(long)) ?
      Specified by:
      isPadding in interface MemoryLayout
      Returns:
      true, if this layout is a padding layout.