belliottsmith commented on code in PR #50: URL: https://github.com/apache/cassandra-accord/pull/50#discussion_r1239477528
########## accord-core/src/main/java/accord/utils/SimpleBitSet.java: ########## @@ -0,0 +1,348 @@ +/* + * 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 accord.utils; + +import java.util.Arrays; + +import static java.lang.Long.highestOneBit; +import static java.lang.Long.lowestOneBit; +import static java.lang.Long.numberOfTrailingZeros; + +public class SimpleBitSet +{ + public static class SerializationSupport + { + public static long[] getArray(SimpleBitSet bs) + { + return bs.bits; + } + + public static SimpleBitSet construct(long[] bits) + { + return new SimpleBitSet(bits); + } + } + + final long[] bits; + int count; + + public SimpleBitSet(int size) + { + bits = new long[(size + 63)/64]; + } + + public SimpleBitSet(int size, boolean set) + { + this(size); + if (set) + { + Arrays.fill(bits, 0, size / 64, -1L); + if ((size & 63) != 0) + bits[indexOf(size - 1)] = -1L >>> (64 - (size & 63)); + count = size; + } + } + + public SimpleBitSet(SimpleBitSet copy) + { + bits = copy.bits.clone(); + count = copy.count; + } + + SimpleBitSet(long[] bits) + { + this.bits = bits; + for (long v : bits) + count += Long.bitCount(v); + } + + public boolean set(int i) + { + int index = indexOf(i); + long bit = bit(i); + if (0 != (bits[index] & bit)) + return false; + bits[index] |= bit; + ++count; + return true; + } + + public void setRange(int from, int to) + { + if (to <= from) + { + Invariants.checkArgument(to >= from, "to < from (%s < %s)", to, from); + return; + } + + int fromIndex = from >>> 6; + int toIndex = (to + 63) >>> 6; + if (fromIndex + 1 == toIndex) + { + long addBits = (-1L >>> (64 - (to & 63))) & (-1L << (from & 63)); + orBitsAtIndex(fromIndex, addBits); + } + else if (count == 0) + { + bits[toIndex - 1] = -1L >>> (64 - (to & 63)); + for (int i = fromIndex + 1, maxi = toIndex - 1; i < maxi ; ++i) + bits[i] = -1L; + bits[fromIndex] = -1L << (from & 63); + count = to - from; + } + else + { + orBitsAtIndex(fromIndex, -1L << (from & 63)); + for (int i = fromIndex + 1, maxi = toIndex - 1; i < maxi ; ++i) + { + count += 64 - Long.bitCount(bits[i]); + bits[i] = -1L; + } + orBitsAtIndex(toIndex - 1, -1L >>> (64 - (to & 63))); + } + } + + private void orBitsAtIndex(int index, long setBits) + { + long prevBits = bits[index]; + bits[index] = setBits | prevBits; + count += Long.bitCount(setBits) - Long.bitCount(prevBits); + } + + public boolean unset(int i) + { + int index = indexOf(i); + long bit = bit(i); + if (0 == (bits[index] & bit)) + return false; + bits[index] &= ~bit; + --count; + return true; + } + + public boolean get(int i) + { + int index = indexOf(i); + long bit = bit(i); + return 0 != (bits[index] & bit); + } + + public int size() + { + return bits.length * 64; Review Comment: The number of elements and capacity are the same in this case - we just round up the number of requested elements. It didn't seem particularly valuable to store a separate size limit that we impose that is fractionally lower, implying additional bounds checks and larger memory footprint. -- 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]

