LongToPrefixCoded() public static method

Returns prefix coded bits after reducing the precision by shift bits. this is method is used by NumericTokenStream. After encoding, {@code bytes.offset} will always be 0.
public static LongToPrefixCoded ( long val, int shift, BytesRef bytes ) : void
val long the numeric value
shift int how many bits to strip from the right
bytes BytesRef will contain the encoded value
return void
Beispiel #1
0
        public virtual void  TestLongSpecialValues()
        {
            long[]          vals       = new long[] { System.Int64.MinValue, System.Int64.MinValue + 1, System.Int64.MinValue + 2, -5003400000000L, -4000L, -3000L, -2000L, -1000L, -1L, 0L, 1L, 10L, 300L, 50006789999999999L, System.Int64.MaxValue - 2, System.Int64.MaxValue - 1, System.Int64.MaxValue };
            System.String[] prefixVals = new System.String[vals.Length];

            for (int i = 0; i < vals.Length; i++)
            {
                prefixVals[i] = NumericUtils.LongToPrefixCoded(vals[i]);

                // check forward and back conversion
                Assert.AreEqual(vals[i], NumericUtils.PrefixCodedToLong(prefixVals[i]), "forward and back conversion should generate same long");

                // test if decoding values as int fails correctly
                Assert.Throws <FormatException>(() => NumericUtils.PrefixCodedToInt(prefixVals[i]),
                                                "decoding a prefix coded long value as int should fail");
            }

            // check sort order (prefixVals should be ascending)
            for (int i = 1; i < prefixVals.Length; i++)
            {
                Assert.IsTrue(String.CompareOrdinal(prefixVals[i - 1], prefixVals[i]) < 0, "check sort order");
            }

            // check the prefix encoding, lower precision should have the difference to original value equal to the lower removed bits
            for (int i = 0; i < vals.Length; i++)
            {
                for (int j = 0; j < 64; j++)
                {
                    long prefixVal = NumericUtils.PrefixCodedToLong(NumericUtils.LongToPrefixCoded(vals[i], j));
                    long mask      = (1L << j) - 1L;
                    Assert.AreEqual(vals[i] & mask, vals[i] - prefixVal, "difference between prefix val and original value for " + vals[i] + " with shift=" + j);
                }
            }
        }
Beispiel #2
0
 public virtual void  TestLongConversionAndOrdering()
 {
     // generate a series of encoded longs, each numerical one bigger than the one before
     System.String last = null;
     for (long l = -100000L; l < 100000L; l++)
     {
         System.String act = NumericUtils.LongToPrefixCoded(l);
         if (last != null)
         {
             // test if smaller
             Assert.IsTrue(String.CompareOrdinal(last, act) < 0, "actual bigger than last");
         }
         // test is back and forward conversion works
         Assert.AreEqual(l, NumericUtils.PrefixCodedToLong(act), "forward and back conversion should generate same long");
         // next step
         last = act;
     }
 }