< prev index next >

src/jdk.incubator.foreign/share/classes/jdk/incubator/foreign/SequenceLayout.java

Print this page
*** 23,35 ***
   *  questions.
   *
   */
  package jdk.incubator.foreign;
  
- import java.lang.constant.Constable;
  import java.lang.constant.ConstantDescs;
  import java.lang.constant.DynamicConstantDesc;
- import java.util.Map;
  import java.util.Objects;
  import java.util.Optional;
  import java.util.OptionalLong;
  
  /**
   * A sequence layout. A sequence layout is used to denote a repetition of a given layout, also called the sequence layout's <em>element layout</em>.
!  * The repetition count, where it exists (e.g. for <em>finite</em> sequence layouts) is said to be the the sequence layout's <em>element count</em>.
   * 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:
   *
   * <pre>{@code
! MemoryLayout.sequenceLayout(3, MemoryLayout.valueLayout(32, ByteOrder.BIG_ENDIAN));
   * }</pre>
   *
   * is equivalent to the following layout:
   *
   * <pre>{@code
  MemoryLayout.structLayout(
!     MemoryLayout.valueLayout(32, ByteOrder.BIG_ENDIAN),
!     MemoryLayout.valueLayout(32, ByteOrder.BIG_ENDIAN),
!     MemoryLayout.valueLayout(32, ByteOrder.BIG_ENDIAN));
   * }</pre>
   *
   * <p>
   * This is a <a href="{@docRoot}/java.base/java/lang/doc-files/ValueBased.html">value-based</a>
   * class; programmers should treat instances that are
--- 23,33 ---
   *  questions.
   *
   */
  package jdk.incubator.foreign;
  
  import java.lang.constant.ConstantDescs;
  import java.lang.constant.DynamicConstantDesc;
  import java.util.Objects;
  import java.util.Optional;
  import java.util.OptionalLong;
  
  /**
   * A sequence layout. A sequence layout is used to denote a repetition of a given layout, also called the sequence layout's <em>element layout</em>.
!  * The repetition count, where it exists (e.g. for <em>finite</em> sequence layouts) is said to be the sequence layout's <em>element count</em>.
   * 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:
   *
   * <pre>{@code
! MemoryLayout.sequenceLayout(3, ValueLayout.JAVA_INT.withOrder(ByteOrder.BIG_ENDIAN));
   * }</pre>
   *
   * is equivalent to the following layout:
   *
   * <pre>{@code
  MemoryLayout.structLayout(
!     ValueLayout.JAVA_INT.withOrder(ByteOrder.BIG_ENDIAN),
!     ValueLayout.JAVA_INT.withOrder(ByteOrder.BIG_ENDIAN),
!     ValueLayout.JAVA_INT.withOrder(ByteOrder.BIG_ENDIAN));
   * }</pre>
   *
   * <p>
   * This is a <a href="{@docRoot}/java.base/java/lang/doc-files/ValueBased.html">value-based</a>
   * class; programmers should treat instances that are

*** 70,17 ***
  
      private final OptionalLong elemCount;
      private final MemoryLayout elementLayout;
  
      SequenceLayout(OptionalLong elemCount, MemoryLayout elementLayout) {
!         this(elemCount, elementLayout, elementLayout.bitAlignment(), Map.of());
      }
  
!     SequenceLayout(OptionalLong elemCount, MemoryLayout elementLayout, long alignment, Map<String, Constable> attributes) {
          super(elemCount.isPresent() && AbstractLayout.optSize(elementLayout).isPresent() ?
                  OptionalLong.of(elemCount.getAsLong() * elementLayout.bitSize()) :
!                 OptionalLong.empty(), alignment, attributes);
          this.elemCount = elemCount;
          this.elementLayout = elementLayout;
      }
  
      /**
--- 68,17 ---
  
      private final OptionalLong elemCount;
      private final MemoryLayout elementLayout;
  
      SequenceLayout(OptionalLong elemCount, MemoryLayout elementLayout) {
!         this(elemCount, elementLayout, elementLayout.bitAlignment(), Optional.empty());
      }
  
!     SequenceLayout(OptionalLong elemCount, MemoryLayout elementLayout, long alignment, Optional<String> name) {
          super(elemCount.isPresent() && AbstractLayout.optSize(elementLayout).isPresent() ?
                  OptionalLong.of(elemCount.getAsLong() * elementLayout.bitSize()) :
!                 OptionalLong.empty(), alignment, name);
          this.elemCount = elemCount;
          this.elementLayout = elementLayout;
      }
  
      /**

*** 108,11 ***
       * @return a new sequence with given element count.
       * @throws IllegalArgumentException if {@code elementCount < 0}.
       */
      public SequenceLayout withElementCount(long elementCount) {
          AbstractLayout.checkSize(elementCount, true);
!         return new SequenceLayout(OptionalLong.of(elementCount), elementLayout, alignment, attributes);
      }
  
      /**
       * Returns a new sequence layout where element layouts in the flattened projection of this
       * sequence layout (see {@link #flatten()}) are re-arranged into one or more nested sequence layouts
--- 106,11 ---
       * @return a new sequence with given element count.
       * @throws IllegalArgumentException if {@code elementCount < 0}.
       */
      public SequenceLayout withElementCount(long elementCount) {
          AbstractLayout.checkSize(elementCount, true);
!         return new SequenceLayout(OptionalLong.of(elementCount), elementLayout, alignment, name());
      }
  
      /**
       * Returns a new sequence layout where element layouts in the flattened projection of this
       * sequence layout (see {@link #flatten()}) are re-arranged into one or more nested sequence layouts

*** 120,15 ***
       * that is, multiplying the provided element counts must yield the same element count
       * as the flattened projection of this sequence layout.
       * <p>
       * For instance, given a sequence layout of the kind:
       * <pre>{@code
!     var seq = MemoryLayout.sequenceLayout(4, MemoryLayout.sequenceLayout(3, MemoryLayouts.JAVA_INT));
       * }</pre>
       * calling {@code seq.reshape(2, 6)} will yield the following sequence layout:
       * <pre>{@code
!     var reshapeSeq = MemoryLayout.sequenceLayout(2, MemoryLayout.sequenceLayout(6, MemoryLayouts.JAVA_INT));
       * }</pre>
       * <p>
       * If one of the provided element count is the special value {@code -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
--- 118,15 ---
       * that is, multiplying the provided element counts must yield the same element count
       * as the flattened projection of this sequence layout.
       * <p>
       * For instance, given a sequence layout of the kind:
       * <pre>{@code
!     var seq = MemoryLayout.sequenceLayout(4, MemoryLayout.sequenceLayout(3, ValueLayout.JAVA_INT));
       * }</pre>
       * calling {@code seq.reshape(2, 6)} will yield the following sequence layout:
       * <pre>{@code
!     var reshapeSeq = MemoryLayout.sequenceLayout(2, MemoryLayout.sequenceLayout(6, ValueLayout.JAVA_INT));
       * }</pre>
       * <p>
       * If one of the provided element count is the special value {@code -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

*** 149,11 ***
      public SequenceLayout reshape(long... elementCounts) {
          Objects.requireNonNull(elementCounts);
          if (elementCounts.length == 0) {
              throw new IllegalArgumentException();
          }
!         if (!elementCount().isPresent()) {
              throw new UnsupportedOperationException("Cannot reshape a sequence layout whose element count is unspecified");
          }
          SequenceLayout flat = flatten();
          long expectedCount = flat.elementCount().getAsLong();
  
--- 147,11 ---
      public SequenceLayout reshape(long... elementCounts) {
          Objects.requireNonNull(elementCounts);
          if (elementCounts.length == 0) {
              throw new IllegalArgumentException();
          }
!         if (elementCount().isEmpty()) {
              throw new UnsupportedOperationException("Cannot reshape a sequence layout whose element count is unspecified");
          }
          SequenceLayout flat = flatten();
          long expectedCount = flat.elementCount().getAsLong();
  

*** 196,29 ***
       * 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:
       * <pre>{@code
!     var seq = MemoryLayout.sequenceLayout(4, MemoryLayout.sequenceLayout(3, MemoryLayouts.JAVA_INT));
       * }</pre>
       * calling {@code seq.flatten()} will yield the following sequence layout:
       * <pre>{@code
!     var flattenedSeq = MemoryLayout.sequenceLayout(12, MemoryLayouts.JAVA_INT);
       * }</pre>
       * @return 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.
       */
      public SequenceLayout flatten() {
!         if (!elementCount().isPresent()) {
              throw badUnboundSequenceLayout();
          }
          long count = elementCount().getAsLong();
          MemoryLayout elemLayout = elementLayout();
!         while (elemLayout instanceof SequenceLayout) {
-             SequenceLayout elemSeq = (SequenceLayout)elemLayout;
              count = count * elemSeq.elementCount().orElseThrow(this::badUnboundSequenceLayout);
              elemLayout = elemSeq.elementLayout();
          }
          return MemoryLayout.sequenceLayout(count, elemLayout);
      }
--- 194,28 ---
       * 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:
       * <pre>{@code
!     var seq = MemoryLayout.sequenceLayout(4, MemoryLayout.sequenceLayout(3, ValueLayout.JAVA_INT));
       * }</pre>
       * calling {@code seq.flatten()} will yield the following sequence layout:
       * <pre>{@code
!     var flattenedSeq = MemoryLayout.sequenceLayout(12, ValueLayout.JAVA_INT);
       * }</pre>
       * @return 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.
       */
      public SequenceLayout flatten() {
!         if (elementCount().isEmpty()) {
              throw badUnboundSequenceLayout();
          }
          long count = elementCount().getAsLong();
          MemoryLayout elemLayout = elementLayout();
!         while (elemLayout instanceof SequenceLayout elemSeq) {
              count = count * elemSeq.elementCount().orElseThrow(this::badUnboundSequenceLayout);
              elemLayout = elemSeq.elementLayout();
          }
          return MemoryLayout.sequenceLayout(count, elemLayout);
      }

*** 239,25 ***
              return true;
          }
          if (!super.equals(other)) {
              return false;
          }
!         if (!(other instanceof SequenceLayout)) {
              return false;
          }
-         SequenceLayout s = (SequenceLayout)other;
          return elemCount.equals(s.elemCount) && elementLayout.equals(s.elementLayout);
      }
  
      @Override
      public int hashCode() {
          return Objects.hash(super.hashCode(), elemCount, elementLayout);
      }
  
      @Override
!     SequenceLayout dup(long alignment, Map<String, Constable> attributes) {
!         return new SequenceLayout(elementCount(), elementLayout, alignment, attributes);
      }
  
      @Override
      boolean hasNaturalAlignment() {
          return alignment == elementLayout.bitAlignment();
--- 236,24 ---
              return true;
          }
          if (!super.equals(other)) {
              return false;
          }
!         if (!(other instanceof SequenceLayout s)) {
              return false;
          }
          return elemCount.equals(s.elemCount) && elementLayout.equals(s.elementLayout);
      }
  
      @Override
      public int hashCode() {
          return Objects.hash(super.hashCode(), elemCount, elementLayout);
      }
  
      @Override
!     SequenceLayout dup(long alignment, Optional<String> name) {
!         return new SequenceLayout(elementCount(), elementLayout, alignment, name);
      }
  
      @Override
      boolean hasNaturalAlignment() {
          return alignment == elementLayout.bitAlignment();

*** 288,14 ***
       */
      @Override
      public SequenceLayout withBitAlignment(long alignmentBits) {
          return (SequenceLayout)super.withBitAlignment(alignmentBits);
      }
- 
-     /**
-      * {@inheritDoc}
-      */
-     @Override
-     public SequenceLayout withAttribute(String name, Constable value) {
-         return (SequenceLayout)super.withAttribute(name, value);
-     }
  }
--- 284,6 ---
< prev index next >