Here you can find the source of intToPrefixCoded(final int val, final int shift, final char[] buffer)
shift
bits.
Parameter | Description |
---|---|
val | the numeric value |
shift | how many bits to strip from the right |
buffer | that will contain the encoded chars, must be at least of #BUF_SIZE_INT length |
public static int intToPrefixCoded(final int val, final int shift, final char[] buffer)
//package com.java2s; /**//from w ww . ja va 2 s. c o m * 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 class Main { /** * Expert: Integers are stored at lower precision by shifting off lower bits. The shift count is * stored as <code>SHIFT_START_INT+shift</code> in the first character */ public static final char SHIFT_START_INT = (char) 0x60; /** * Expert: The maximum term length (used for <code>char[]</code> buffer size) * for encoding <code>int</code> values. * @see #intToPrefixCoded(int,int,char[]) */ public static final int BUF_SIZE_INT = 31 / 7 + 2; /** * Expert: Returns prefix coded bits after reducing the precision by <code>shift</code> bits. * This is method is used by {@link NumericTokenStream}. * @param val the numeric value * @param shift how many bits to strip from the right * @param buffer that will contain the encoded chars, must be at least of {@link #BUF_SIZE_INT} * length * @return number of chars written to buffer */ public static int intToPrefixCoded(final int val, final int shift, final char[] buffer) { if (shift > 31 || shift < 0) throw new IllegalArgumentException("Illegal shift value, must be 0..31"); int nChars = (31 - shift) / 7 + 1, len = nChars + 1; buffer[0] = (char) (SHIFT_START_INT + shift); int sortableBits = val ^ 0x80000000; sortableBits >>>= shift; while (nChars >= 1) { // Store 7 bits per character for good efficiency when UTF-8 encoding. // The whole number is right-justified so that lucene can prefix-encode // the terms more efficiently. buffer[nChars--] = (char) (sortableBits & 0x7f); sortableBits >>>= 7; } return len; } /** * Expert: Returns prefix coded bits after reducing the precision by <code>shift</code> bits. * This is method is used by {@link IntRangeBuilder}. * @param val the numeric value * @param shift how many bits to strip from the right */ public static String intToPrefixCoded(final int val, final int shift) { final char[] buffer = new char[BUF_SIZE_INT]; final int len = intToPrefixCoded(val, shift, buffer); return new String(buffer, 0, len); } /** * This is a convenience method, that returns prefix coded bits of an int without * reducing the precision. It can be used to store the full precision value as a * stored field in index. * <p>To decode, use {@link #prefixCodedToInt}. */ public static String intToPrefixCoded(final int val) { return intToPrefixCoded(val, 0); } }