pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / contrib / facet / src / java / org / apache / lucene / util / encoding / EightFlagsIntEncoder.java
1 package org.apache.lucene.util.encoding;
2
3 import java.io.IOException;
4
5 /**
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
12  *
13  *     http://www.apache.org/licenses/LICENSE-2.0
14  *
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.
20  */
21
22 /**
23  * A {@link ChunksIntEncoder} which encodes data in chunks of 8. Every group starts with a single
24  * byte (called indicator) which represents 8 - 1 bit flags, where the value:
25  * <ul>
26  * <li>1 means the encoded value is '1'
27  * <li>0 means the value is encoded using {@link VInt8IntEncoder}, and the
28  * encoded bytes follow the indicator.<br>
29  * Since value 0 is illegal, and 1 is encoded in the indicator, the actual
30  * value that is encoded is <code>value-2</code>, which saves some more bits.
31  * </ul>
32  * Encoding example:
33  * <ul>
34  * <li>Original values: 6, 16, 5, 9, 7, 1
35  * <li>After sorting: 1, 5, 6, 7, 9, 16
36  * <li>D-Gap computing: 1, 4, 1, 1, 2, 5 (so far - done by
37  * {@link DGapIntEncoder})
38  * <li>Encoding: 1,0,1,1,0,0,0,0 as the indicator, by 2 (4-2), 0 (2-2), 3 (5-2).
39  * <li>Binary encode: <u>0 | 0 | 0 | 0 | 1 | 1 | 0 | 1</u> 00000010 00000000
40  * 00000011 (indicator is <u>underlined</u>).<br>
41  * <b>NOTE:</b> the order of the values in the indicator is lsb &rArr; msb,
42  * which allows for more efficient decoding.
43  * </ul>
44  * 
45  * @lucene.experimental
46  */
47 public class EightFlagsIntEncoder extends ChunksIntEncoder {
48
49   /**
50    * Holds all combinations of <i>indicator</i> flags for fast encoding (saves
51    * time on bit manipulation at encode time)
52    */
53   private static byte[] encodeTable = new byte[] { 0x1, 0x2, 0x4, 0x8, 0x10, 0x20, 0x40, (byte) 0x80 };
54
55   public EightFlagsIntEncoder() {
56     super(8);
57   }
58
59   @Override
60   public void encode(int data) throws IOException {
61     if (data == 1) {
62       indicator |= encodeTable[ordinal];
63     } else {
64       encodeQueue[encodeQueueSize++] = data - 2;
65     }
66     ++ordinal;
67
68     // If 8 values were encoded thus far, 'flush' them including the indicator.
69     if ((ordinal & 0x7) == 0) {
70       encodeChunk();
71     }
72   }
73
74   @Override
75   public IntDecoder createMatchingDecoder() {
76     return new EightFlagsIntDecoder();
77   }
78
79   @Override
80   public String toString() {
81     return "EightFlags (" + encoder.toString() + ")";
82   }
83
84 }