coderplay
3/29/2013 - 4:20 AM

group variant integer encoding

group variant integer encoding

import java.lang.reflect.Field;
import java.util.Random;
 
import sun.misc.Unsafe;
 
public class GroupVarInt {
 private final static int UINT24_MAX = ((2<<23) - 1);
	private final static int UINT16_MAX = ((2<<15) - 1);
	private final static int UINT8_MAX = ((2<<7) -1);
	private static final long BYTE_ARRAY_OFFSET;
	public static final Unsafe unsafe;
	static {
		try {
			Field field = Unsafe.class.getDeclaredField("theUnsafe");
			field.setAccessible(true);
			unsafe = (Unsafe) field.get(null);
			BYTE_ARRAY_OFFSET = unsafe.arrayBaseOffset(byte[].class);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
	
	
	private final static byte GROUP_VARINT_IDX_ARR[][] = {
		
		/* 00 00 00 00 */{2, 3, 4, 1, 1, 1, 1, 5},
		/* 00 00 00 01 */{2, 3, 4, 1, 1, 1, 2, 6},
		/* 00 00 00 10 */{2, 3, 4, 1, 1, 1, 3, 7},
		/* 00 00 00 11 */{2, 3, 4, 1, 1, 1, 4, 8},
	
		/* 00 00 01 00 */{2, 3, 5, 1, 1, 2, 1, 6},
		/* 00 00 01 01 */{2, 3, 5, 1, 1, 2, 2, 7},
		/* 00 00 01 10 */{2, 3, 5, 1, 1, 2, 3, 8},
		/* 00 00 01 11 */{2, 3, 5, 1, 1, 2, 4, 9},
	
		/* 00 00 10 00 */{2, 3, 6, 1, 1, 3, 1, 7},
		/* 00 00 10 01 */{2, 3, 6, 1, 1, 3, 2, 8},
		/* 00 00 10 10 */{2, 3, 6, 1, 1, 3, 3, 9},
		/* 00 00 10 11 */{2, 3, 6, 1, 1, 3, 4, 10},
	
		/* 00 00 11 00 */{2, 3, 7, 1, 1, 4, 1, 8},
		/* 00 00 11 01 */{2, 3, 7, 1, 1, 4, 2, 9},
		/* 00 00 11 10 */{2, 3, 7, 1, 1, 4, 3, 10},
		/* 00 00 11 11 */{2, 3, 7, 1, 1, 4, 4, 11},
	
		/* 00 01 00 00 */{2, 4, 5, 1, 2, 1, 1, 6},
		/* 00 01 00 01 */{2, 4, 5, 1, 2, 1, 2, 7},
		/* 00 01 00 10 */{2, 4, 5, 1, 2, 1, 3, 8},
		/* 00 01 00 11 */{2, 4, 5, 1, 2, 1, 4, 9},
	
		/* 00 01 01 00 */{2, 4, 6, 1, 2, 2, 1, 7},
		/* 00 01 01 01 */{2, 4, 6, 1, 2, 2, 2, 8},
		/* 00 01 01 10 */{2, 4, 6, 1, 2, 2, 3, 9},
		/* 00 01 01 11 */{2, 4, 6, 1, 2, 2, 4, 10},
	
		/* 00 01 10 00 */{2, 4, 7, 1, 2, 3, 1, 8},
		/* 00 01 10 01 */{2, 4, 7, 1, 2, 3, 2, 9},
		/* 00 01 10 10 */{2, 4, 7, 1, 2, 3, 3, 10},
		/* 00 01 10 11 */{2, 4, 7, 1, 2, 3, 4, 11},
	
		/* 00 01 11 00 */{2, 4, 8, 1, 2, 4, 1, 9},
		/* 00 01 11 01 */{2, 4, 8, 1, 2, 4, 2, 10},
		/* 00 01 11 10 */{2, 4, 8, 1, 2, 4, 3, 11},
		/* 00 01 11 11 */{2, 4, 8, 1, 2, 4, 4, 12},
	
		/* 00 10 00 00 */{2, 5, 6, 1, 3, 1, 1, 7},
		/* 00 10 00 01 */{2, 5, 6, 1, 3, 1, 2, 8},
		/* 00 10 00 10 */{2, 5, 6, 1, 3, 1, 3, 9},
		/* 00 10 00 11 */{2, 5, 6, 1, 3, 1, 4, 10},
	
		/* 00 10 01 00 */{2, 5, 7, 1, 3, 2, 1, 8},
		/* 00 10 01 01 */{2, 5, 7, 1, 3, 2, 2, 9},
		/* 00 10 01 10 */{2, 5, 7, 1, 3, 2, 3, 10},
		/* 00 10 01 11 */{2, 5, 7, 1, 3, 2, 4, 11},
	
		/* 00 10 10 00 */{2, 5, 8, 1, 3, 3, 1, 9},
		/* 00 10 10 01 */{2, 5, 8, 1, 3, 3, 2, 10},
		/* 00 10 10 10 */{2, 5, 8, 1, 3, 3, 3, 11},
		/* 00 10 10 11 */{2, 5, 8, 1, 3, 3, 4, 12},
	
		/* 00 10 11 00 */{2, 5, 9, 1, 3, 4, 1, 10},
		/* 00 10 11 01 */{2, 5, 9, 1, 3, 4, 2, 11},
		/* 00 10 11 10 */{2, 5, 9, 1, 3, 4, 3, 12},
		/* 00 10 11 11 */{2, 5, 9, 1, 3, 4, 4, 13},
	
		/* 00 11 00 00 */{2, 6, 7, 1, 4, 1, 1, 8},
		/* 00 11 00 01 */{2, 6, 7, 1, 4, 1, 2, 9},
		/* 00 11 00 10 */{2, 6, 7, 1, 4, 1, 3, 10},
		/* 00 11 00 11 */{2, 6, 7, 1, 4, 1, 4, 11},
	
		/* 00 11 01 00 */{2, 6, 8, 1, 4, 2, 1, 9},
		/* 00 11 01 01 */{2, 6, 8, 1, 4, 2, 2, 10},
		/* 00 11 01 10 */{2, 6, 8, 1, 4, 2, 3, 11},
		/* 00 11 01 11 */{2, 6, 8, 1, 4, 2, 4, 12},
	
		/* 00 11 10 00 */{2, 6, 9, 1, 4, 3, 1, 10},
		/* 00 11 10 01 */{2, 6, 9, 1, 4, 3, 2, 11},
		/* 00 11 10 10 */{2, 6, 9, 1, 4, 3, 3, 12},
		/* 00 11 10 11 */{2, 6, 9, 1, 4, 3, 4, 13},
	
		/* 00 11 11 00 */{2, 6, 10, 1, 4, 4, 1, 11},
		/* 00 11 11 01 */{2, 6, 10, 1, 4, 4, 2, 12},
		/* 00 11 11 10 */{2, 6, 10, 1, 4, 4, 3, 13},
		/* 00 11 11 11 */{2, 6, 10, 1, 4, 4, 4, 14},
	
		/* 01 00 00 00 */{3, 4, 5, 2, 1, 1, 1, 6},
		/* 01 00 00 01 */{3, 4, 5, 2, 1, 1, 2, 7},
		/* 01 00 00 10 */{3, 4, 5, 2, 1, 1, 3, 8},
		/* 01 00 00 11 */{3, 4, 5, 2, 1, 1, 4, 9},
	
		/* 01 00 01 00 */{3, 4, 6, 2, 1, 2, 1, 7},
		/* 01 00 01 01 */{3, 4, 6, 2, 1, 2, 2, 8},
		/* 01 00 01 10 */{3, 4, 6, 2, 1, 2, 3, 9},
		/* 01 00 01 11 */{3, 4, 6, 2, 1, 2, 4, 10},
	
		/* 01 00 10 00 */{3, 4, 7, 2, 1, 3, 1, 8},
		/* 01 00 10 01 */{3, 4, 7, 2, 1, 3, 2, 9},
		/* 01 00 10 10 */{3, 4, 7, 2, 1, 3, 3, 10},
		/* 01 00 10 11 */{3, 4, 7, 2, 1, 3, 4, 11},
	
		/* 01 00 11 00 */{3, 4, 8, 2, 1, 4, 1, 9},
		/* 01 00 11 01 */{3, 4, 8, 2, 1, 4, 2, 10},
		/* 01 00 11 10 */{3, 4, 8, 2, 1, 4, 3, 11},
		/* 01 00 11 11 */{3, 4, 8, 2, 1, 4, 4, 12},
	
		/* 01 01 00 00 */{3, 5, 6, 2, 2, 1, 1, 7},
		/* 01 01 00 01 */{3, 5, 6, 2, 2, 1, 2, 8},
		/* 01 01 00 10 */{3, 5, 6, 2, 2, 1, 3, 9},
		/* 01 01 00 11 */{3, 5, 6, 2, 2, 1, 4, 10},
	
		/* 01 01 01 00 */{3, 5, 7, 2, 2, 2, 1, 8},
		/* 01 01 01 01 */{3, 5, 7, 2, 2, 2, 2, 9},
		/* 01 01 01 10 */{3, 5, 7, 2, 2, 2, 3, 10},
		/* 01 01 01 11 */{3, 5, 7, 2, 2, 2, 4, 11},
	
		/* 01 01 10 00 */{3, 5, 8, 2, 2, 3, 1, 9},
		/* 01 01 10 01 */{3, 5, 8, 2, 2, 3, 2, 10},
		/* 01 01 10 10 */{3, 5, 8, 2, 2, 3, 3, 11},
		/* 01 01 10 11 */{3, 5, 8, 2, 2, 3, 4, 12},
	
		/* 01 01 11 00 */{3, 5, 9, 2, 2, 4, 1, 10},
		/* 01 01 11 01 */{3, 5, 9, 2, 2, 4, 2, 11},
		/* 01 01 11 10 */{3, 5, 9, 2, 2, 4, 3, 12},
		/* 01 01 11 11 */{3, 5, 9, 2, 2, 4, 4, 13},
	
		/* 01 10 00 00 */{3, 6, 7, 2, 3, 1, 1, 8},
		/* 01 10 00 01 */{3, 6, 7, 2, 3, 1, 2, 9},
		/* 01 10 00 10 */{3, 6, 7, 2, 3, 1, 3, 10},
		/* 01 10 00 11 */{3, 6, 7, 2, 3, 1, 4, 11},
	
		/* 01 10 01 00 */{3, 6, 8, 2, 3, 2, 1, 9},
		/* 01 10 01 01 */{3, 6, 8, 2, 3, 2, 2, 10},
		/* 01 10 01 10 */{3, 6, 8, 2, 3, 2, 3, 11},
		/* 01 10 01 11 */{3, 6, 8, 2, 3, 2, 4, 12},
	
		/* 01 10 10 00 */{3, 6, 9, 2, 3, 3, 1, 10},
		/* 01 10 10 01 */{3, 6, 9, 2, 3, 3, 2, 11},
		/* 01 10 10 10 */{3, 6, 9, 2, 3, 3, 3, 12},
		/* 01 10 10 11 */{3, 6, 9, 2, 3, 3, 4, 13},
	
		/* 01 10 11 00 */{3, 6, 10, 2, 3, 4, 1, 11},
		/* 01 10 11 01 */{3, 6, 10, 2, 3, 4, 2, 12},
		/* 01 10 11 10 */{3, 6, 10, 2, 3, 4, 3, 13},
		/* 01 10 11 11 */{3, 6, 10, 2, 3, 4, 4, 14},
	
		/* 01 11 00 00 */{3, 7, 8, 2, 4, 1, 1, 9},
		/* 01 11 00 01 */{3, 7, 8, 2, 4, 1, 2, 10},
		/* 01 11 00 10 */{3, 7, 8, 2, 4, 1, 3, 11},
		/* 01 11 00 11 */{3, 7, 8, 2, 4, 1, 4, 12},
	
		/* 01 11 01 00 */{3, 7, 9, 2, 4, 2, 1, 10},
		/* 01 11 01 01 */{3, 7, 9, 2, 4, 2, 2, 11},
		/* 01 11 01 10 */{3, 7, 9, 2, 4, 2, 3, 12},
		/* 01 11 01 11 */{3, 7, 9, 2, 4, 2, 4, 13},
	
		/* 01 11 10 00 */{3, 7, 10, 2, 4, 3, 1, 11},
		/* 01 11 10 01 */{3, 7, 10, 2, 4, 3, 2, 12},
		/* 01 11 10 10 */{3, 7, 10, 2, 4, 3, 3, 13},
		/* 01 11 10 11 */{3, 7, 10, 2, 4, 3, 4, 14},
	
		/* 01 11 11 00 */{3, 7, 11, 2, 4, 4, 1, 12},
		/* 01 11 11 01 */{3, 7, 11, 2, 4, 4, 2, 13},
		/* 01 11 11 10 */{3, 7, 11, 2, 4, 4, 3, 14},
		/* 01 11 11 11 */{3, 7, 11, 2, 4, 4, 4, 15},
	
		/* 10 00 00 00 */{4, 5, 6, 3, 1, 1, 1, 7},
		/* 10 00 00 01 */{4, 5, 6, 3, 1, 1, 2, 8},
		/* 10 00 00 10 */{4, 5, 6, 3, 1, 1, 3, 9},
		/* 10 00 00 11 */{4, 5, 6, 3, 1, 1, 4, 10},
	
		/* 10 00 01 00 */{4, 5, 7, 3, 1, 2, 1, 8},
		/* 10 00 01 01 */{4, 5, 7, 3, 1, 2, 2, 9},
		/* 10 00 01 10 */{4, 5, 7, 3, 1, 2, 3, 10},
		/* 10 00 01 11 */{4, 5, 7, 3, 1, 2, 4, 11},
	
		/* 10 00 10 00 */{4, 5, 8, 3, 1, 3, 1, 9},
		/* 10 00 10 01 */{4, 5, 8, 3, 1, 3, 2, 10},
		/* 10 00 10 10 */{4, 5, 8, 3, 1, 3, 3, 11},
		/* 10 00 10 11 */{4, 5, 8, 3, 1, 3, 4, 12},
	
		/* 10 00 11 00 */{4, 5, 9, 3, 1, 4, 1, 10},
		/* 10 00 11 01 */{4, 5, 9, 3, 1, 4, 2, 11},
		/* 10 00 11 10 */{4, 5, 9, 3, 1, 4, 3, 12},
		/* 10 00 11 11 */{4, 5, 9, 3, 1, 4, 4, 13},
	
		/* 10 01 00 00 */{4, 6, 7, 3, 2, 1, 1, 8},
		/* 10 01 00 01 */{4, 6, 7, 3, 2, 1, 2, 9},
		/* 10 01 00 10 */{4, 6, 7, 3, 2, 1, 3, 10},
		/* 10 01 00 11 */{4, 6, 7, 3, 2, 1, 4, 11},
	
		/* 10 01 01 00 */{4, 6, 8, 3, 2, 2, 1, 9},
		/* 10 01 01 01 */{4, 6, 8, 3, 2, 2, 2, 10},
		/* 10 01 01 10 */{4, 6, 8, 3, 2, 2, 3, 11},
		/* 10 01 01 11 */{4, 6, 8, 3, 2, 2, 4, 12},
	
		/* 10 01 10 00 */{4, 6, 9, 3, 2, 3, 1, 10},
		/* 10 01 10 01 */{4, 6, 9, 3, 2, 3, 2, 11},
		/* 10 01 10 10 */{4, 6, 9, 3, 2, 3, 3, 12},
		/* 10 01 10 11 */{4, 6, 9, 3, 2, 3, 4, 13},
	
		/* 10 01 11 00 */{4, 6, 10, 3, 2, 4, 1, 11},
		/* 10 01 11 01 */{4, 6, 10, 3, 2, 4, 2, 12},
		/* 10 01 11 10 */{4, 6, 10, 3, 2, 4, 3, 13},
		/* 10 01 11 11 */{4, 6, 10, 3, 2, 4, 4, 14},
	
		/* 10 10 00 00 */{4, 7, 8, 3, 3, 1, 1, 9},
		/* 10 10 00 01 */{4, 7, 8, 3, 3, 1, 2, 10},
		/* 10 10 00 10 */{4, 7, 8, 3, 3, 1, 3, 11},
		/* 10 10 00 11 */{4, 7, 8, 3, 3, 1, 4, 12},
	
		/* 10 10 01 00 */{4, 7, 9, 3, 3, 2, 1, 10},
		/* 10 10 01 01 */{4, 7, 9, 3, 3, 2, 2, 11},
		/* 10 10 01 10 */{4, 7, 9, 3, 3, 2, 3, 12},
		/* 10 10 01 11 */{4, 7, 9, 3, 3, 2, 4, 13},
	
		/* 10 10 10 00 */{4, 7, 10, 3, 3, 3, 1, 11},
		/* 10 10 10 01 */{4, 7, 10, 3, 3, 3, 2, 12},
		/* 10 10 10 10 */{4, 7, 10, 3, 3, 3, 3, 13},
		/* 10 10 10 11 */{4, 7, 10, 3, 3, 3, 4, 14},
	
		/* 10 10 11 00 */{4, 7, 11, 3, 3, 4, 1, 12},
		/* 10 10 11 01 */{4, 7, 11, 3, 3, 4, 2, 13},
		/* 10 10 11 10 */{4, 7, 11, 3, 3, 4, 3, 14},
		/* 10 10 11 11 */{4, 7, 11, 3, 3, 4, 4, 15},
	
		/* 10 11 00 00 */{4, 8, 9, 3, 4, 1, 1, 10},
		/* 10 11 00 01 */{4, 8, 9, 3, 4, 1, 2, 11},
		/* 10 11 00 10 */{4, 8, 9, 3, 4, 1, 3, 12},
		/* 10 11 00 11 */{4, 8, 9, 3, 4, 1, 4, 13},
	
		/* 10 11 01 00 */{4, 8, 10, 3, 4, 2, 1, 11},
		/* 10 11 01 01 */{4, 8, 10, 3, 4, 2, 2, 12},
		/* 10 11 01 10 */{4, 8, 10, 3, 4, 2, 3, 13},
		/* 10 11 01 11 */{4, 8, 10, 3, 4, 2, 4, 14},
	
		/* 10 11 10 00 */{4, 8, 11, 3, 4, 3, 1, 12},
		/* 10 11 10 01 */{4, 8, 11, 3, 4, 3, 2, 13},
		/* 10 11 10 10 */{4, 8, 11, 3, 4, 3, 3, 14},
		/* 10 11 10 11 */{4, 8, 11, 3, 4, 3, 4, 15},
	
		/* 10 11 11 00 */{4, 8, 12, 3, 4, 4, 1, 13},
		/* 10 11 11 01 */{4, 8, 12, 3, 4, 4, 2, 14},
		/* 10 11 11 10 */{4, 8, 12, 3, 4, 4, 3, 15},
		/* 10 11 11 11 */{4, 8, 12, 3, 4, 4, 4, 16},
	
		/* 11 00 00 00 */{5, 6, 7, 4, 1, 1, 1, 8},
		/* 11 00 00 01 */{5, 6, 7, 4, 1, 1, 2, 9},
		/* 11 00 00 10 */{5, 6, 7, 4, 1, 1, 3, 10},
		/* 11 00 00 11 */{5, 6, 7, 4, 1, 1, 4, 11},
	
		/* 11 00 01 00 */{5, 6, 8, 4, 1, 2, 1, 9},
		/* 11 00 01 01 */{5, 6, 8, 4, 1, 2, 2, 10},
		/* 11 00 01 10 */{5, 6, 8, 4, 1, 2, 3, 11},
		/* 11 00 01 11 */{5, 6, 8, 4, 1, 2, 4, 12},
	
		/* 11 00 10 00 */{5, 6, 9, 4, 1, 3, 1, 10},
		/* 11 00 10 01 */{5, 6, 9, 4, 1, 3, 2, 11},
		/* 11 00 10 10 */{5, 6, 9, 4, 1, 3, 3, 12},
		/* 11 00 10 11 */{5, 6, 9, 4, 1, 3, 4, 13},
	
		/* 11 00 11 00 */{5, 6, 10, 4, 1, 4, 1, 11},
		/* 11 00 11 01 */{5, 6, 10, 4, 1, 4, 2, 12},
		/* 11 00 11 10 */{5, 6, 10, 4, 1, 4, 3, 13},
		/* 11 00 11 11 */{5, 6, 10, 4, 1, 4, 4, 14},
	
		/* 11 01 00 00 */{5, 7, 8, 4, 2, 1, 1, 9},
		/* 11 01 00 01 */{5, 7, 8, 4, 2, 1, 2, 10},
		/* 11 01 00 10 */{5, 7, 8, 4, 2, 1, 3, 11},
		/* 11 01 00 11 */{5, 7, 8, 4, 2, 1, 4, 12},
	
		/* 11 01 01 00 */{5, 7, 9, 4, 2, 2, 1, 10},
		/* 11 01 01 01 */{5, 7, 9, 4, 2, 2, 2, 11},
		/* 11 01 01 10 */{5, 7, 9, 4, 2, 2, 3, 12},
		/* 11 01 01 11 */{5, 7, 9, 4, 2, 2, 4, 13},
	
		/* 11 01 10 00 */{5, 7, 10, 4, 2, 3, 1, 11},
		/* 11 01 10 01 */{5, 7, 10, 4, 2, 3, 2, 12},
		/* 11 01 10 10 */{5, 7, 10, 4, 2, 3, 3, 13},
		/* 11 01 10 11 */{5, 7, 10, 4, 2, 3, 4, 14},
	
		/* 11 01 11 00 */{5, 7, 11, 4, 2, 4, 1, 12},
		/* 11 01 11 01 */{5, 7, 11, 4, 2, 4, 2, 13},
		/* 11 01 11 10 */{5, 7, 11, 4, 2, 4, 3, 14},
		/* 11 01 11 11 */{5, 7, 11, 4, 2, 4, 4, 15},
	
		/* 11 10 00 00 */{5, 8, 9, 4, 3, 1, 1, 10},
		/* 11 10 00 01 */{5, 8, 9, 4, 3, 1, 2, 11},
		/* 11 10 00 10 */{5, 8, 9, 4, 3, 1, 3, 12},
		/* 11 10 00 11 */{5, 8, 9, 4, 3, 1, 4, 13},
	
		/* 11 10 01 00 */{5, 8, 10, 4, 3, 2, 1, 11},
		/* 11 10 01 01 */{5, 8, 10, 4, 3, 2, 2, 12},
		/* 11 10 01 10 */{5, 8, 10, 4, 3, 2, 3, 13},
		/* 11 10 01 11 */{5, 8, 10, 4, 3, 2, 4, 14},
	
		/* 11 10 10 00 */{5, 8, 11, 4, 3, 3, 1, 12},
		/* 11 10 10 01 */{5, 8, 11, 4, 3, 3, 2, 13},
		/* 11 10 10 10 */{5, 8, 11, 4, 3, 3, 3, 14},
		/* 11 10 10 11 */{5, 8, 11, 4, 3, 3, 4, 15},
	
		/* 11 10 11 00 */{5, 8, 12, 4, 3, 4, 1, 13},
		/* 11 10 11 01 */{5, 8, 12, 4, 3, 4, 2, 14},
		/* 11 10 11 10 */{5, 8, 12, 4, 3, 4, 3, 15},
		/* 11 10 11 11 */{5, 8, 12, 4, 3, 4, 4, 16},
	
		/* 11 11 00 00 */{5, 9, 10, 4, 4, 1, 1, 11},
		/* 11 11 00 01 */{5, 9, 10, 4, 4, 1, 2, 12},
		/* 11 11 00 10 */{5, 9, 10, 4, 4, 1, 3, 13},
		/* 11 11 00 11 */{5, 9, 10, 4, 4, 1, 4, 14},
	
		/* 11 11 01 00 */{5, 9, 11, 4, 4, 2, 1, 12},
		/* 11 11 01 01 */{5, 9, 11, 4, 4, 2, 2, 13},
		/* 11 11 01 10 */{5, 9, 11, 4, 4, 2, 3, 14},
		/* 11 11 01 11 */{5, 9, 11, 4, 4, 2, 4, 15},
	
		/* 11 11 10 00 */{5, 9, 12, 4, 4, 3, 1, 13},
		/* 11 11 10 01 */{5, 9, 12, 4, 4, 3, 2, 14},
		/* 11 11 10 10 */{5, 9, 12, 4, 4, 3, 3, 15},
		/* 11 11 10 11 */{5, 9, 12, 4, 4, 3, 4, 16},
	
		/* 11 11 11 00 */{5, 9, 13, 4, 4, 4, 1, 14},
		/* 11 11 11 01 */{5, 9, 13, 4, 4, 4, 2, 15},
		/* 11 11 11 10 */{5, 9, 13, 4, 4, 4, 3, 16},
		/* 11 11 11 11 */{5, 9, 13, 4, 4, 4, 4, 17}	
	};
 
	/**
	 * 一次编码4个int
	 * @param values
	 * @param target
	 * @param start
	 * @return bytes数组的下一个下标地址
	 */
	static int groupVarintEncodeInt(final int[] values, int vstat, byte[] target, int tstart)
	{

 
	    int val1 = values[vstat+0];
	    int val2 = values[vstat+1];
	    int val3 = values[vstat+2];
	    int val4 = values[vstat+3];
 
	    int idx = tstart + 1;
	    
	    byte len1 = intToBytes(val1, target, idx);
	    byte len = len1;
	    
	    byte len2 = intToBytes(val2, target, idx + len);
	    len += len2;
	    
	    byte len3 = intToBytes(val3, target, idx + len);
	    len += len3;
	      
	    byte len4 = intToBytes(val4, target, idx + len);
	    len += len4;


	    /* 处理第一个索引字节 */
	    target[tstart] = (byte) (((len1 - 1) << 6) | ((len2 - 1) << 4) | ((len3 - 1) << 2) | (len4 - 1));
	    
	    return idx + len;
	}
	
 
	/**
	 * 
	 * @param buf
	 * @param start
	 * @param values
	 * @return
	 */
	public static int groupVarintDecodeInt32 ( byte[] target, int tstart, int[] values, int vstart)
	{
		int idx = (target[tstart] > 0 ? target[tstart] : target[tstart] + 256);
	    //int idx  = Math.abs(target[0]);
	    int start1 = tstart + 1;
	    int start2 = tstart + GROUP_VARINT_IDX_ARR[idx][0];
	    int start3 = tstart + GROUP_VARINT_IDX_ARR[idx][1];
	    int start4 = tstart + GROUP_VARINT_IDX_ARR[idx][2];
 
	    values[vstart] = intFromBytes(target, start1, GROUP_VARINT_IDX_ARR[idx][3]); 
	    values[vstart+1] = intFromBytes(target, start2, GROUP_VARINT_IDX_ARR[idx][4]); 
	    values[vstart+2] = intFromBytes(target, start3, GROUP_VARINT_IDX_ARR[idx][5]);
	    values[vstart+3] = intFromBytes(target, start4, GROUP_VARINT_IDX_ARR[idx][6]);
	    return tstart + GROUP_VARINT_IDX_ARR[ idx ][ 7 ];
	}
 
 
	private static final byte intToBytes(int value, byte[] target, int start) {
	  byte len = 0;
    if (value > UINT24_MAX ){
      len = 4;
  }else if ( value > UINT16_MAX ){
    len = 3;
  }else if ( value > UINT8_MAX ){
    len = 2;
  }else{
    len = 1;
  }
		unsafe.putInt(target, BYTE_ARRAY_OFFSET + start, value);
		return len;
	}
	
	private final static int[] x = {
		0, 0xFF, 0xFFFF, 0xFFFFFF, 0xFFFFFFFF
	};
	
	private static final int intFromBytes(byte[] source, int start, int count) {
		int value = unsafe.getInt(source, BYTE_ARRAY_OFFSET + start);
		value &= x[count];
		/*
		int value = 0;
		for (int i = 0; i < count; i++) {
			if (source[start+i] < 0) {
				value += ((int)source[start+i] + 256);
			}else {
				value += (source[start+i] << i*8);
			}
		}
		*/
		/*
		int value = source[start];
		if (count > 1) {
			for (int i = 1; i < count; i++) {
				if (source[start+i] < 0) {
					value |= (source[start+i]+256) << (i*8);
				}else {
					value |= (source[start+i] << i*8);
				}
			}
		}
		*/
		return value;
	}
 
	/**
	 * 有符号数编码为无符号数
	 * @param n
	 * @return
	 */
	public static int zigZagEncodeInt(int n) {
		return ((n << 1) ^ (n >> 31));
	}
	public static long zigZagEncodeLong(long n) {
		return ((n << 1) ^ (n >> 63));
	}
	
	/**
	 * 无符号数解码为有符号数
	 * @param n
	 * @return
	 */
	public static int zigZagDecodeInt(int n) {
		return (n >>> 1) ^ -(n & 1);
	}
	public static long zigZagDecodeLong(long n) {
		return (n >>> 1) ^ -(n & 1);
	}
	
	public static void main(String[] args) {
		int count = 1000000;
		int[] raw = new int[count];
		byte[] target = new byte[count*5];
		int[] newInt = new int[count];
 		Random random = new Random();
		for (int i = 0; i < count; i++) {
			raw[i] = random.nextInt(count);
			//raw[i] = 255000000+i;
		}

		long startTime = System.nanoTime();
		int tstart = 0;
		for (int i = 0; i < count / 4; i++) {
			tstart = groupVarintEncodeInt(raw, i*4, target, tstart);
		}
		System.out.println("encode " + count + " ints, using=>" + (System.nanoTime() - startTime) + "ns");
		startTime = System.nanoTime();
		tstart = 0;
		for (int i = 0; i < count / 4; i++) {
			tstart = groupVarintDecodeInt32 (target, tstart, newInt, i * 4);
		}
		System.out.println("decode " + count + " ints, using=>" + (System.nanoTime() - startTime) + "ns");
		for (int i = 0; i < count; i++) {
			if (raw[i] != newInt[i]) {
				System.out.println("error=> " + i + " raw[i]=>" + raw[i] + " newInt[i]=>" + newInt[i]);
				break;
			}
		}
		System.out.println("ok");
	}
}