Class Mask<E>

  • Type Parameters:
    E - the boxed element type of this mask

    public abstract class Mask<E>
    extends Object
    A Mask represents an ordered immutable sequence of boolean values. A Mask can be used with a mask accepting vector operation to control the selection and operation of lane elements of input vectors.

    The number of values in the sequence is referred to as the Mask length. The length also corresponds to the number of Mask lanes. The lane element at lane index N (from 0, inclusive, to length, exclusive) corresponds to the N + 1'th value in the sequence. A Mask and Vector of the same element type and shape have the same number of lanes.

    A lane is said to be set if the lane element is true, otherwise a lane is said to be unset if the lane element is false.

    Mask declares a limited set of unary, binary and reductive mask operations.

    • A mask unary operation (1-ary) operates on one input mask to produce a result mask. For each lane of the input mask the lane element is operated on using the specified scalar unary operation and the boolean result is placed into the mask result at the same lane. The following pseudocode expresses the behaviour of this operation category:
      
       Mask<E> a = ...;
       boolean[] ar = new boolean[a.length()];
       for (int i = 0; i < a.length(); i++) {
           ar[i] = boolean_unary_op(a.isSet(i));
       }
       Mask<E> r = a.species().maskFromArray(ar, 0);
       
    • A mask binary operation (2-ary) operates on two input masks to produce a result mask. For each lane of the two input masks, a and b say, the corresponding lane elements from a and b are operated on using the specified scalar binary operation and the boolean result is placed into the mask result at the same lane. The following pseudocode expresses the behaviour of this operation category:
      
       Mask<E> a = ...;
       Mask<E> b = ...;
       boolean[] ar = new boolean[a.length()];
       for (int i = 0; i < a.length(); i++) {
           ar[i] = scalar_binary_op(a.isSet(i), b.isSet(i));
       }
       Mask<E> r = a.species().maskFromArray(ar, 0);
       
    • Method Summary

      Modifier and Type Method Description
      abstract boolean allTrue()
      Returns true if all of the mask lanes are set.
      abstract Mask<E> and​(Mask<E> o)
      Logically ands this mask with an input mask.
      abstract boolean anyTrue()
      Returns true if any of the mask lanes are set.
      abstract <F> Mask<F> cast​(Species<F> s)
      Converts this mask to a mask of the given species shape of element type F.
      static <E> Mask<E> fromArray​(Species<E> species, boolean[] bits, int ix)
      Loads a mask from a boolean array starting at an offset.
      static <E> Mask<E> fromValues​(Species<E> species, boolean... bits)
      Returns a mask where each lane is set or unset according to given boolean values
      abstract boolean getElement​(int i)
      Tests if the lane at index i is set
      abstract void intoArray​(boolean[] a, int i)
      Stores this mask into a boolean array starting at offset.
      boolean isSet​(int i)
      Tests if the lane at index i is set
      int length()
      Returns the number of mask lanes (the length).
      static <E> Mask<E> maskAllFalse​(Species<E> species)
      Returns a mask where all lanes are unset.
      static <E> Mask<E> maskAllTrue​(Species<E> species)
      Returns a mask where all lanes are set.
      abstract Mask<E> not()
      Logically negates this mask.
      abstract Mask<E> or​(Mask<E> o)
      Logically ors this mask with an input mask.
      abstract Species<E> species()
      Returns the species of this mask.
      abstract boolean[] toArray()
      Returns an boolean array containing the lane elements of this mask.
      abstract long toLong()
      Returns the lane elements of this mask packed into a long value for at most the first 64 lane elements.
      abstract Vector<E> toVector()
      Returns a vector representation of this mask.
      abstract int trueCount()
      Returns the number of mask lanes that are set.
    • Method Detail

      • species

        public abstract Species<E> species()
        Returns the species of this mask.
        Returns:
        the species of this mask
      • length

        public int length()
        Returns the number of mask lanes (the length).
        Returns:
        the number of mask lanes
      • fromValues

        public static <E> Mask<E> fromValues​(Species<E> species,
                                             boolean... bits)
        Returns a mask where each lane is set or unset according to given boolean values

        For each mask lane, where N is the mask lane index, if the given boolean value at index N is true then the mask lane at index N is set, otherwise it is unset.

        Parameters:
        species - mask species
        bits - the given boolean values
        Returns:
        a mask where each lane is set or unset according to the given boolean value
        Throws:
        IndexOutOfBoundsException - if bits.length < species.length()
      • fromArray

        public static <E> Mask<E> fromArray​(Species<E> species,
                                            boolean[] bits,
                                            int ix)
        Loads a mask from a boolean array starting at an offset.

        For each mask lane, where N is the mask lane index, if the array element at index ix + N is true then the mask lane at index N is set, otherwise it is unset.

        Parameters:
        species - mask species
        bits - the boolean array
        ix - the offset into the array
        Returns:
        the mask loaded from a boolean array
        Throws:
        IndexOutOfBoundsException - if ix < 0, or ix > bits.length - species.length()
      • maskAllTrue

        public static <E> Mask<E> maskAllTrue​(Species<E> species)
        Returns a mask where all lanes are set.
        Parameters:
        species - mask species
        Returns:
        a mask where all lanes are set
      • maskAllFalse

        public static <E> Mask<E> maskAllFalse​(Species<E> species)
        Returns a mask where all lanes are unset.
        Parameters:
        species - mask species
        Returns:
        a mask where all lanes are unset
      • cast

        public abstract <F> Mask<F> cast​(Species<F> s)
        Converts this mask to a mask of the given species shape of element type F.

        For each mask lane, where N is the lane index, if the mask lane at index N is set, then the mask lane at index N of the resulting mask is set, otherwise that mask lane is not set.

        Type Parameters:
        F - the boxed element type of the species
        Parameters:
        s - the species of the desired mask
        Returns:
        a mask converted by shape and element type
        Throws:
        IllegalArgumentException - if this mask length and the species length differ
      • toLong

        public abstract long toLong()
        Returns the lane elements of this mask packed into a long value for at most the first 64 lane elements.

        The lane elements are packed in the order of least significant bit to most significant bit. For each mask lane where N is the mask lane index, if the mask lane is set then the N'th bit is set to one in the resulting long value, otherwise the N'th bit is set to zero.

        Returns:
        the lane elements of this mask packed into a long value.
      • toArray

        public abstract boolean[] toArray()
        Returns an boolean array containing the lane elements of this mask.

        This method behaves as if it intoArray(boolean[], int) stores} this mask into an allocated array and returns that array as follows:

        
         boolean[] a = new boolean[this.length()];
         this.intoArray(a, 0);
         return a;
         
        Returns:
        an array containing the the lane elements of this vector
      • intoArray

        public abstract void intoArray​(boolean[] a,
                                       int i)
        Stores this mask into a boolean array starting at offset.

        For each mask lane, where N is the mask lane index, the lane element at index N is stored into the array at index i + N.

        Parameters:
        a - the array
        i - the offset into the array
        Throws:
        IndexOutOfBoundsException - if i < 0, or i > a.length - this.length()
      • anyTrue

        public abstract boolean anyTrue()
        Returns true if any of the mask lanes are set.
        Returns:
        true if any of the mask lanes are set, otherwise false.
      • allTrue

        public abstract boolean allTrue()
        Returns true if all of the mask lanes are set.
        Returns:
        true if all of the mask lanes are set, otherwise false.
      • trueCount

        public abstract int trueCount()
        Returns the number of mask lanes that are set.
        Returns:
        the number of mask lanes that are set.
      • and

        public abstract Mask<E> and​(Mask<E> o)
        Logically ands this mask with an input mask.

        This is a mask binary operation where the logical and operation (&& is applied to lane elements.

        Parameters:
        o - the input mask
        Returns:
        the result of logically and'ing this mask with an input mask
      • or

        public abstract Mask<E> or​(Mask<E> o)
        Logically ors this mask with an input mask.

        This is a mask binary operation where the logical or operation (|| is applied to lane elements.

        Parameters:
        o - the input mask
        Returns:
        the result of logically or'ing this mask with an input mask
      • not

        public abstract Mask<E> not()
        Logically negates this mask.

        This is a mask unary operation where the logical not operation (! is applied to lane elements.

        Returns:
        the result of logically negating this mask.
      • toVector

        public abstract Vector<E> toVector()
        Returns a vector representation of this mask.

        For each mask lane, where N is the mask lane index, if the mask lane is set then an element value whose most significant bit is set is placed into the resulting vector at lane index N, otherwise the default element value is placed into the resulting vector at lane index N.

        Returns:
        a vector representation of this mask.
      • getElement

        public abstract boolean getElement​(int i)
        Tests if the lane at index i is set
        Parameters:
        i - the lane index
        Returns:
        true if the lane at index i is set, otherwise false
      • isSet

        public boolean isSet​(int i)
        Tests if the lane at index i is set
        Parameters:
        i - the lane index
        Returns:
        true if the lane at index i is set, otherwise false
        See Also:
        getElement(int)