|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.spark.util.sketch; |
| 19 | + |
| 20 | +import java.io.DataInputStream; |
| 21 | +import java.io.DataOutputStream; |
| 22 | +import java.io.IOException; |
| 23 | +import java.util.Arrays; |
| 24 | + |
| 25 | +public final class BitArray { |
| 26 | + private final long[] data; |
| 27 | + private long bitCount; |
| 28 | + |
| 29 | + static int numWords(long numBits) { |
| 30 | + if (numBits <= 0) { |
| 31 | + throw new IllegalArgumentException("numBits must be positive, but got " + numBits); |
| 32 | + } |
| 33 | + long numWords = (long) Math.ceil(numBits / 64.0); |
| 34 | + if (numWords > Integer.MAX_VALUE) { |
| 35 | + throw new IllegalArgumentException("Can't allocate enough space for " + numBits + " bits"); |
| 36 | + } |
| 37 | + return (int) numWords; |
| 38 | + } |
| 39 | + |
| 40 | + BitArray(long numBits) { |
| 41 | + this(new long[numWords(numBits)]); |
| 42 | + } |
| 43 | + |
| 44 | + private BitArray(long[] data) { |
| 45 | + this.data = data; |
| 46 | + long bitCount = 0; |
| 47 | + for (long word : data) { |
| 48 | + bitCount += Long.bitCount(word); |
| 49 | + } |
| 50 | + this.bitCount = bitCount; |
| 51 | + } |
| 52 | + |
| 53 | + /** Returns true if the bit changed value. */ |
| 54 | + boolean set(long index) { |
| 55 | + if (!get(index)) { |
| 56 | + data[(int) (index >>> 6)] |= (1L << index); |
| 57 | + bitCount++; |
| 58 | + return true; |
| 59 | + } |
| 60 | + return false; |
| 61 | + } |
| 62 | + |
| 63 | + boolean get(long index) { |
| 64 | + return (data[(int) (index >>> 6)] & (1L << index)) != 0; |
| 65 | + } |
| 66 | + |
| 67 | + /** Number of bits */ |
| 68 | + long bitSize() { |
| 69 | + return (long) data.length * Long.SIZE; |
| 70 | + } |
| 71 | + |
| 72 | + /** Number of set bits (1s) */ |
| 73 | + long cardinality() { |
| 74 | + return bitCount; |
| 75 | + } |
| 76 | + |
| 77 | + /** Combines the two BitArrays using bitwise OR. */ |
| 78 | + void putAll(BitArray array) { |
| 79 | + assert data.length == array.data.length : "BitArrays must be of equal length when merging"; |
| 80 | + long bitCount = 0; |
| 81 | + for (int i = 0; i < data.length; i++) { |
| 82 | + data[i] |= array.data[i]; |
| 83 | + bitCount += Long.bitCount(data[i]); |
| 84 | + } |
| 85 | + this.bitCount = bitCount; |
| 86 | + } |
| 87 | + |
| 88 | + void writeTo(DataOutputStream out) throws IOException { |
| 89 | + out.writeInt(data.length); |
| 90 | + for (long datum : data) { |
| 91 | + out.writeLong(datum); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + static BitArray readFrom(DataInputStream in) throws IOException { |
| 96 | + int numWords = in.readInt(); |
| 97 | + long[] data = new long[numWords]; |
| 98 | + for (int i = 0; i < numWords; i++) { |
| 99 | + data[i] = in.readLong(); |
| 100 | + } |
| 101 | + return new BitArray(data); |
| 102 | + } |
| 103 | + |
| 104 | + @Override |
| 105 | + public boolean equals(Object other) { |
| 106 | + if (this == other) return true; |
| 107 | + if (other == null || !(other instanceof BitArray)) return false; |
| 108 | + BitArray that = (BitArray) other; |
| 109 | + return Arrays.equals(data, that.data); |
| 110 | + } |
| 111 | + |
| 112 | + @Override |
| 113 | + public int hashCode() { |
| 114 | + return Arrays.hashCode(data); |
| 115 | + } |
| 116 | +} |
0 commit comments