dcapwell commented on code in PR #50: URL: https://github.com/apache/cassandra-accord/pull/50#discussion_r1242700866
########## 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); Review Comment: > Why is from <= to simpler than to >= from? This is identical to the original check! Sorry if I am not clear, I was asking we change ``` if (to <= from) { Invariants.checkArgument(to >= from, "to < from (%s < %s)", to, from); } ``` to ``` Invariants.checkArgument(from <= to, "to < from (%s < %s)", to, from); ``` the reason its simpler is that we have 1 single check rather than 2, which requires the reads to undo to figure out the actual single logical check being done. With the previous logic we want to fail in the following boolean condition: `(to <= from) && !(to >= from)`. Which simplifies `(to <= from) && (to < from)` which simplifies again to `to < from`... so with this `checkArgument` api that means we want `to >= from` -- 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]

