Index: lucene/facet/src/java/org/apache/lucene/util/encoding/PackedIntEncoder.java =================================================================== --- lucene/facet/src/java/org/apache/lucene/util/encoding/PackedIntEncoder.java (revision 0) +++ lucene/facet/src/java/org/apache/lucene/util/encoding/PackedIntEncoder.java (working copy) @@ -0,0 +1,181 @@ +package org.apache.lucene.util.encoding; + +import java.util.Arrays; + +import org.apache.lucene.util.ArrayUtil; +import org.apache.lucene.util.BytesRef; +import org.apache.lucene.util.IntsRef; +import org.apache.lucene.util.packed.PackedInts; + +/* + * 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. + */ + +public final class PackedIntEncoder extends IntEncoder { + + static final int[] MASK = new int[32]; + static { + int upto = 2; + for(int i=1;i<32;i++) { + MASK[i] = upto-1; + upto *= 2; + } + } + + + @Override + public void encode(IntsRef values, BytesRef buf) { + int maxValue = 0; + for (int i = 0; i < values.length; ++i) { + final int v = values.ints[values.offset + i]; + assert v >= 0; + maxValue = Math.max(maxValue, v); + } + final int bitsPerValue = PackedInts.bitsRequired(maxValue); + buf.offset = 0; + buf.length = 1 + (values.length * bitsPerValue + 7) / 8; + if (buf.bytes.length < buf.length) { + buf.bytes = new byte[ArrayUtil.oversize(buf.length, 1)]; + } + int leftoverBits = 8*(buf.length - 1) - (values.length * bitsPerValue); + assert leftoverBits < 8; + Arrays.fill(buf.bytes, (byte) 0); + + buf.bytes[0] = (byte) ((bitsPerValue << 3) | leftoverBits); + /* + System.out.println("encode bpv=" + bitsPerValue + " leftoverBits=" + leftoverBits + " inputCount=" + values.length + " outputBytes=" + buf.length); + for(int i=0;i