/** * Gets one of the original multi-dimensional values from a scalar value. * * @param dimensions the number of dimensions * @param scalar the scalar value * @param dim the dimension of the returned value (starting from 0) * @return the value */ public int deinterleave(int dimensions, long scalar, int dim) { int bitsPerValue = getBitsPerValue(dimensions); int value = 0; for (int i = 0; i < bitsPerValue; i++) { value |= (scalar >> (dim + (dimensions - 1) * i)) & (1L << i); } return value; }
/** * Get the maximum value for the given dimension count. For two dimensions, * each value must contain at most 32 bit, for 3: 21 bit, 4: 16 bit, 5: 12 * bit, 6: 10 bit, 7: 9 bit, 8: 8 bit. * * @param dimensions the number of dimensions * @return the maximum value */ public int getMaxValue(int dimensions) { if (dimensions < 2 || dimensions > 32) { throw new IllegalArgumentException("" + dimensions); } int bitsPerValue = getBitsPerValue(dimensions); return (int) ((1L << bitsPerValue) - 1); }
/** * Convert the multi-dimensional value into a one-dimensional (scalar) * value. This is done by interleaving the bits of the values. Each values * must be between 0 (including) and the maximum value for the given number * of dimensions (getMaxValue, excluding). To normalize values to this * range, use the normalize function. * * @param values the multi-dimensional value * @return the scalar value */ public long interleave(int... values) { int dimensions = values.length; long max = getMaxValue(dimensions); int bitsPerValue = getBitsPerValue(dimensions); long x = 0; for (int i = 0; i < dimensions; i++) { long k = values[i]; if (k < 0 || k > max) { throw new IllegalArgumentException(0 + "<" + k + "<" + max); } for (int b = 0; b < bitsPerValue; b++) { x |= (k & (1L << b)) << (i + (dimensions - 1) * b); } } return x; }
/** * Gets one of the original multi-dimensional values from a scalar value. * * @param dimensions the number of dimensions * @param scalar the scalar value * @param dim the dimension of the returned value (starting from 0) * @return the value */ public int deinterleave(int dimensions, long scalar, int dim) { int bitsPerValue = getBitsPerValue(dimensions); int value = 0; for (int i = 0; i < bitsPerValue; i++) { value |= (scalar >> (dim + (dimensions - 1) * i)) & (1L << i); } return value; }
/** * Gets one of the original multi-dimensional values from a scalar value. * * @param dimensions the number of dimensions * @param scalar the scalar value * @param dim the dimension of the returned value (starting from 0) * @return the value */ public int deinterleave(int dimensions, long scalar, int dim) { int bitsPerValue = getBitsPerValue(dimensions); int value = 0; for (int i = 0; i < bitsPerValue; i++) { value |= (scalar >> (dim + (dimensions - 1) * i)) & (1L << i); } return value; }
/** * Get the maximum value for the given dimension count. For two dimensions, * each value must contain at most 32 bit, for 3: 21 bit, 4: 16 bit, 5: 12 * bit, 6: 10 bit, 7: 9 bit, 8: 8 bit. * * @param dimensions the number of dimensions * @return the maximum value */ public int getMaxValue(int dimensions) { if (dimensions < 2 || dimensions > 32) { throw new IllegalArgumentException("" + dimensions); } int bitsPerValue = getBitsPerValue(dimensions); return (int) ((1L << bitsPerValue) - 1); }
/** * Get the maximum value for the given dimension count. For two dimensions, * each value must contain at most 32 bit, for 3: 21 bit, 4: 16 bit, 5: 12 * bit, 6: 10 bit, 7: 9 bit, 8: 8 bit. * * @param dimensions the number of dimensions * @return the maximum value */ public int getMaxValue(int dimensions) { if (dimensions < 2 || dimensions > 32) { throw new IllegalArgumentException("" + dimensions); } int bitsPerValue = getBitsPerValue(dimensions); return (int) ((1L << bitsPerValue) - 1); }
/** * Convert the multi-dimensional value into a one-dimensional (scalar) * value. This is done by interleaving the bits of the values. Each values * must be between 0 (including) and the maximum value for the given number * of dimensions (getMaxValue, excluding). To normalize values to this * range, use the normalize function. * * @param values the multi-dimensional value * @return the scalar value */ public long interleave(int... values) { int dimensions = values.length; long max = getMaxValue(dimensions); int bitsPerValue = getBitsPerValue(dimensions); long x = 0; for (int i = 0; i < dimensions; i++) { long k = values[i]; if (k < 0 || k > max) { throw new IllegalArgumentException(0 + "<" + k + "<" + max); } for (int b = 0; b < bitsPerValue; b++) { x |= (k & (1L << b)) << (i + (dimensions - 1) * b); } } return x; }
/** * Convert the multi-dimensional value into a one-dimensional (scalar) * value. This is done by interleaving the bits of the values. Each values * must be between 0 (including) and the maximum value for the given number * of dimensions (getMaxValue, excluding). To normalize values to this * range, use the normalize function. * * @param values the multi-dimensional value * @return the scalar value */ public long interleave(int... values) { int dimensions = values.length; long max = getMaxValue(dimensions); int bitsPerValue = getBitsPerValue(dimensions); long x = 0; for (int i = 0; i < dimensions; i++) { long k = values[i]; if (k < 0 || k > max) { throw new IllegalArgumentException(0 + "<" + k + "<" + max); } for (int b = 0; b < bitsPerValue; b++) { x |= (k & (1L << b)) << (i + (dimensions - 1) * b); } } return x; }