1 package org.apache.lucene.util.encoding;
3 import java.io.IOException;
6 * Licensed to the Apache Software Foundation (ASF) under one or more
7 * contributor license agreements. See the NOTICE file distributed with
8 * this work for additional information regarding copyright ownership.
9 * The ASF licenses this file to You under the Apache License, Version 2.0
10 * (the "License"); you may not use this file except in compliance with
11 * the License. You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
23 * A {@link ChunksIntEncoder} which encodes values in chunks of 4. Every group
24 * starts with a single byte (called indicator) which represents 4 - 2 bit
25 * flags, where the values:
27 * <li>1, 2 or 3 mean the encoded value is '1', '2' or '3' respectively.
28 * <li>0 means the value is encoded using {@link VInt8IntEncoder}, and the
29 * encoded bytes follow the indicator.<br>
30 * Since value 0 is illegal, and 1-3 are encoded in the indicator, the actual
31 * value that is encoded is <code>value-4</code>, which saves some more bits.
35 * <li>Original values: 6, 16, 5, 9, 7, 1, 11
36 * <li>After sorting: 1, 5, 6, 7, 9, 11, 16
37 * <li>D-Gap computing: 1, 4, 1, 1, 2, 5 (so far - done by
38 * {@link DGapIntEncoder})
39 * <li>Encoding: 1,0,1,1 as the first indicator, followed by 0 (4-4), than
40 * 2,0,0,0 as the second indicator, followed by 1 (5-4) encoded with.
41 * <li>Binary encode: <u>01 | 01 | 00 | 01</u> 00000000 <u>00 | 00 | 00 | 10</u>
42 * 00000001 (indicators are <u>underlined</u>).<br>
43 * <b>NOTE:</b> the order of the values in the indicator is lsb ⇒ msb,
44 * which allows for more efficient decoding.
47 * @lucene.experimental
49 public class FourFlagsIntEncoder extends ChunksIntEncoder {
52 * Holds all combinations of <i>indicator</i> flags for fast encoding (saves
53 * time on bit manipulation @ encode time)
55 private static byte[][] encodeTable = new byte[][] {
56 new byte[] { 0x00, 0x00, 0x00, 0x00 },
57 new byte[] { 0x01, 0x04, 0x10, 0x40 },
58 new byte[] { 0x02, 0x08, 0x20, (byte) 0x80 },
59 new byte[] { 0x03, 0x0C, 0x30, (byte) 0xC0 },
62 public FourFlagsIntEncoder() {
67 * Small values (<=3) are stored in the <code>indicator</code> while larger
68 * values are saved for later encoding in the {@link #encodeQueue}. Since
69 * Vint8 will only encode values larger or equal to 4, the values saves for
70 * encoded are transformed to (value - 4).<br>
71 * When a chunk is ready (got 4 values), the {@link #encodeChunk()}
75 public void encode(int data) throws IOException {
77 indicator |= encodeTable[data][ordinal];
79 encodeQueue[encodeQueueSize++] = data - 4;
83 // If 4 values were encoded thus far, 'flush' them including the indicator.
84 if ((ordinal & 0x3) == 0) {
90 public IntDecoder createMatchingDecoder() {
91 return new FourFlagsIntDecoder();
95 public String toString() {
96 return "FourFlags (" + encoder.toString() + ")";