1 /*
   2  * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have
  23  * questions.
  24  */
  25 package jdk.incubator.vector;
  26 
  27 import java.util.Arrays;
  28 
  29 abstract class AbstractMask<E> extends Vector.Mask<E> {
  30 
  31     /*package-private*/
  32     abstract boolean[] getBits();
  33 
  34     // Unary operator
  35 
  36     interface MUnOp {
  37         boolean apply(int i, boolean a);
  38     }
  39 
  40     abstract AbstractMask<E> uOp(MUnOp f);
  41 
  42     // Binary operator
  43 
  44     interface MBinOp {
  45         boolean apply(int i, boolean a, boolean b);
  46     }
  47 
  48     abstract AbstractMask<E> bOp(Vector.Mask<E> o, MBinOp f);
  49 
  50     @Override
  51     public String toString() {
  52         return Arrays.toString(getBits());
  53     }
  54 
  55     @Override
  56     public boolean getElement(int i) {
  57         return getBits()[i];
  58     }
  59 
  60     @Override
  61     public long toLong() {
  62         long res = 0;
  63         long set = 1;
  64         boolean[] bits = getBits();
  65         for (int i = 0; i < species().length(); i++) {
  66             res = bits[i] ? res | set : res;
  67             set = set << 1;
  68         }
  69         return res;
  70     }
  71 
  72     @Override
  73     public void intoArray(boolean[] bits, int i) {
  74         System.arraycopy(getBits(), 0, bits, i, species().length());
  75     }
  76 
  77     @Override
  78     public boolean[] toArray() {
  79         return getBits().clone();
  80     }
  81 
  82     @Override
  83     public int trueCount() {
  84         int c = 0;
  85         for (boolean i : getBits()) {
  86             if (i) c++;
  87         }
  88         return c;
  89     }
  90 
  91     @Override
  92     public AbstractMask<E> and(Vector.Mask<E> o) {
  93         return bOp(o, (i, a, b) -> a && b);
  94     }
  95 
  96     @Override
  97     public AbstractMask<E> or(Vector.Mask<E> o) {
  98         return bOp(o, (i, a, b) -> a || b);
  99     }
 100 
 101     @Override
 102     public AbstractMask<E> not() {
 103         return uOp((i, a) -> !a);
 104     }
 105 
 106     /*package-private*/
 107     static boolean anyTrueHelper(boolean[] bits) {
 108         for (boolean i : bits) {
 109             if (i) return true;
 110         }
 111         return false;
 112     }
 113 
 114     /*package-private*/
 115     static boolean allTrueHelper(boolean[] bits) {
 116         for (boolean i : bits) {
 117             if (!i) return false;
 118         }
 119         return true;
 120     }
 121 }