X-Git-Url: https://git.mdrn.pl/pylucene.git/blobdiff_plain/a2e61f0c04805cfcb8706176758d1283c7e3a55c..aaeed5504b982cf3545252ab528713250aa33eed:/lucene-java-3.5.0/lucene/contrib/facet/src/java/org/apache/lucene/util/encoding/VInt8IntEncoder.java diff --git a/lucene-java-3.5.0/lucene/contrib/facet/src/java/org/apache/lucene/util/encoding/VInt8IntEncoder.java b/lucene-java-3.5.0/lucene/contrib/facet/src/java/org/apache/lucene/util/encoding/VInt8IntEncoder.java new file mode 100644 index 0000000..228951d --- /dev/null +++ b/lucene-java-3.5.0/lucene/contrib/facet/src/java/org/apache/lucene/util/encoding/VInt8IntEncoder.java @@ -0,0 +1,86 @@ +package org.apache.lucene.util.encoding; + +import java.io.IOException; + +/** + * 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. + */ + +/** + * An {@link IntEncoder} which implements variable length encoding. A number is + * encoded as follows: + * + * Example: + *
    + *
  1. n = 117 = 01110101: This has less than 8 significant bits, therefore is + * encoded as 01110101 = 0x75. + *
  2. n = 100000 = (binary) 11000011010100000. This has 17 significant bits, + * thus needs three Vint8 bytes. Pad it to a multiple of 7 bits, then split it + * into chunks of 7 and add an MSB, 0 for the last byte, 1 for the others: + * 1|0000110 1|0001101 0|0100000 = 0x86 0x8D 0x20. + *
+ * NOTE: although this encoder is not limited to values ≥ 0, it is not + * recommended for use with negative values, as their encoding will result in 5 + * bytes written to the output stream, rather than 4. For such values, either + * use {@link SimpleIntEncoder} or write your own version of variable length + * encoding, which can better handle negative values. + * + * @lucene.experimental + */ +public class VInt8IntEncoder extends IntEncoder { + + @Override + public void encode(int value) throws IOException { + if ((value & ~0x7F) == 0) { + out.write(value); + } else if ((value & ~0x3FFF) == 0) { + out.write(0x80 | (value >> 7)); + out.write(0x7F & value); + } else if ((value & ~0x1FFFFF) == 0) { + out.write(0x80 | (value >> 14)); + out.write(0x80 | (value >> 7)); + out.write(0x7F & value); + } else if ((value & ~0xFFFFFFF) == 0) { + out.write(0x80 | (value >> 21)); + out.write(0x80 | (value >> 14)); + out.write(0x80 | (value >> 7)); + out.write(0x7F & value); + } else { + out.write(0x80 | (value >> 28)); + out.write(0x80 | (value >> 21)); + out.write(0x80 | (value >> 14)); + out.write(0x80 | (value >> 7)); + out.write(0x7F & value); + } + } + + @Override + public IntDecoder createMatchingDecoder() { + return new VInt8IntDecoder(); + } + + @Override + public String toString() { + return "VInt8"; + } + +}