bruno-roustant commented on a change in pull request #1416: LUCENE-9286: 
FST.Arc.BitTable is read directly from the FST bytes.
URL: https://github.com/apache/lucene-solr/pull/1416#discussion_r405516244
 
 

 ##########
 File path: lucene/core/src/java/org/apache/lucene/util/fst/BitTableUtil.java
 ##########
 @@ -0,0 +1,200 @@
+/*
+ * 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.lucene.util.fst;
+
+import java.io.IOException;
+
+/**
+ * Static helper methods for {@link FST.Arc.BitTable}.
+ *
+ * @lucene.experimental
+ */
+class BitTableUtil {
+
+  /**
+   * Returns whether the bit at given zero-based index is set.
+   * <br>Example: bitIndex 10 means the third bit on the right of the second 
byte.
+   *
+   * @param bitIndex The bit zero-based index. It must be greater than or 
equal to 0, and strictly less than
+   *                 {@code number of bit-table bytes * Byte.SIZE}.
+   * @param reader   The {@link FST.BytesReader} to read. It must be 
positioned at the beginning of the bit-table.
+   */
+  static boolean isBitSet(int bitIndex, FST.BytesReader reader) throws 
IOException {
+    assert bitIndex >= 0 : "bitIndex=" + bitIndex;
+    reader.skipBytes(bitIndex >> 3);
+    return (readByte(reader) & (1L << (bitIndex & (Byte.SIZE - 1)))) != 0;
+  }
+
+
+  /**
+   * Counts all bits set in the bit-table.
+   *
+   * @param bitTableBytes The number of bytes in the bit-table.
+   * @param reader        The {@link FST.BytesReader} to read. It must be 
positioned at the beginning of the bit-table.
+   */
+  static int countBits(int bitTableBytes, FST.BytesReader reader) throws 
IOException {
+    assert bitTableBytes >= 0 : "bitTableBytes=" + bitTableBytes;
 
 Review comment:
   > wouldn't it be more efficient to entire longs + the remainder?
   
   Yes good point. I don't know why I didn't do the same way as countBitsUpTo() 
below. This effectively avoids many conditions. I'll change that.
   
   > read8bytes - this is effectively the same as reader.readLong for bitcounts?
   
   Not the same, there is a difference in the byte order. reader.readLong() 
reads 2 ints by reading the high bytes first. read8Bytes() reads 8 bytes with 
low byte first. This matters when shifting the bit index. I agree that this 
does not matter here for bit count, but this way read8Bytes() is compatible for 
future use with bit index. And in addition it uses less operation since it 
requires 1 less bit shift and bit mask.

----------------------------------------------------------------
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.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscr...@lucene.apache.org
For additional commands, e-mail: issues-h...@lucene.apache.org

Reply via email to