jmalkin commented on code in PR #513: URL: https://github.com/apache/datasketches-java/pull/513#discussion_r1508159306
########## src/main/java/org/apache/datasketches/filters/bloomfilter/BitArray.java: ########## @@ -0,0 +1,219 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.datasketches.filters.bloomfilter; + +import org.apache.datasketches.common.SketchesArgumentException; +import org.apache.datasketches.common.SketchesStateException; +import org.apache.datasketches.memory.Memory; +import org.apache.datasketches.memory.WritableMemory; + +/** + * This class holds an array of bits suitable for use in a Bloom Filter + * + * <p>Rounds the number of bits up to the smallest multiple of 64 (one long) + * that is not smaller than the specified number. + */ +final class BitArray { + // MAX_BITS using longs, based on array indices being capped at Integer.MAX_VALUE + private static final long MAX_BITS = Integer.MAX_VALUE * (long) Long.SIZE; + private static final long DATA_OFFSET = 8; // offset into memory for start of data array + + private long numBitsSet_; // if -1, need to recompute value + private long[] data_; + + // creates an array of a given size + BitArray(final long numBits) { + if (numBits <= 0) { + throw new SketchesArgumentException("Number of bits must be strictly positive. Found: " + numBits); + } + if (numBits > MAX_BITS) { + throw new SketchesArgumentException("Number of bits may not exceed " + MAX_BITS + ". Found: " + numBits); + } + + final int numLongs = (int) Math.ceil(numBits / 64.0); + numBitsSet_ = 0; + data_ = new long[numLongs]; + } + + // uses the provided array + BitArray(final long[] data) { + data_ = data; + numBitsSet_ = 0; + for (long val : data) { + numBitsSet_ += Long.bitCount(val); + } + } + + // reads a serialized image, but the BitArray is not fully self-describing so requires + // a flag to indicate whether the array is empty + static BitArray heapify(final Memory mem, final boolean isEmpty) { + final int numLongs = mem.getInt(0); + if (numLongs < 0) { + throw new SketchesArgumentException("Possible corruption: Must have strictly positive array size. Found: " + numLongs); + } + + if (isEmpty) { + return new BitArray((long) numLongs * Long.SIZE); + } + + final long[] data = new long[numLongs]; + mem.getLongArray(DATA_OFFSET, data, 0, numLongs); + return new BitArray(data); + } + + boolean isEmpty() { + return numBitsSet_ == 0; Review Comment: i fixed that in the branch -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
