1 package org.apache.lucene.util.encoding;
3 import java.io.IOException;
4 import java.io.OutputStream;
7 * Licensed to the Apache Software Foundation (ASF) under one or more
8 * contributor license agreements. See the NOTICE file distributed with
9 * this work for additional information regarding copyright ownership.
10 * The ASF licenses this file to You under the Apache License, Version 2.0
11 * (the "License"); you may not use this file except in compliance with
12 * the License. You may obtain a copy of the License at
14 * http://www.apache.org/licenses/LICENSE-2.0
16 * Unless required by applicable law or agreed to in writing, software
17 * distributed under the License is distributed on an "AS IS" BASIS,
18 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 * See the License for the specific language governing permissions and
20 * limitations under the License.
24 * A variation of {@link FourFlagsIntEncoder} which translates the data as
27 * <li>Values ≥ 2 are trnalsated to <code>value+1</code> (2 ⇒ 3, 3
28 * ⇒ 4 and so forth).
29 * <li>Any <code>N</code> occurrences of 1 are encoded as a single 2.
30 * <li>Otherwise, each 1 is encoded as 1.
35 * <li>N = 4: the data 1,1,1,1,1 is translated to: 2, 1
36 * <li>N = 3: the data 1,2,3,4,1,1,1,1,5 is translated to 1,3,4,5,2,1,6
38 * <b>NOTE:</b> this encoder does not support values ≤ 0 and
39 * {@link Integer#MAX_VALUE}. 0 is not supported because it's not supported by
40 * {@link FourFlagsIntEncoder} and {@link Integer#MAX_VALUE} because this
41 * encoder translates N to N+1, which will cause an overflow and
42 * {@link Integer#MAX_VALUE} will become a negative number, which is not
43 * supported as well.<br>
44 * This does not mean you cannot encode {@link Integer#MAX_VALUE}. If it is not
45 * the first value to encode, and you wrap this encoder with
46 * {@link DGapIntEncoder}, then the value that will be sent to this encoder will
47 * be <code>MAX_VAL - prev</code>.
49 * @lucene.experimental
51 public class NOnesIntEncoder extends FourFlagsIntEncoder {
53 /** Number of consecutive '1's to be translated into single target value '2'. */
56 /** Counts the number of consecutive ones seen. */
57 private int onesCounter = 0;
60 * Constructs an encoder with a given value of N (N: Number of consecutive
61 * '1's to be translated into single target value '2').
63 public NOnesIntEncoder(int n) {
68 public void close() throws IOException {
69 // We might have ones in our buffer, encode them as neccesary.
70 while (onesCounter-- > 0) {
78 public void encode(int value) throws IOException {
80 // Increment the number of consecutive ones seen so far
81 if (++onesCounter == n) {
88 // If it's not one - there might have been ones we had to encode prior to
90 while (onesCounter > 0) {
95 // encode value + 1 --> the translation.
96 super.encode(value + 1);
100 public IntDecoder createMatchingDecoder() {
101 return new NOnesIntDecoder(n);
105 public void reInit(OutputStream out) {
111 public String toString() {
112 return "NOnes (" + n + ") (" + super.toString() + ")";