提交 487b0e21 authored 作者: Thomas Mueller's avatar Thomas Mueller

Slightly improved API for the multi-dimension tool.

上级 60033163
...@@ -41,7 +41,7 @@ public class MultiDimension implements Comparator<long[]> { ...@@ -41,7 +41,7 @@ public class MultiDimension implements Comparator<long[]> {
* Convert the multi-dimensional value into a one-dimensional (scalar) value. * Convert the multi-dimensional value into a one-dimensional (scalar) value.
* This is done by interleaving the bits of the values. * This is done by interleaving the bits of the values.
* Each values must be bigger or equal to 0. The maximum value * Each values must be bigger or equal to 0. The maximum value
* is dependent on the number of dimensions. For two keys, it is 32 bit, * depends on the number of dimensions. For two keys, it is 32 bit,
* for 3: 21 bit, 4: 16 bit, 5: 12 bit, 6: 10 bit, 7: 9 bit, 8: 8 bit. * for 3: 21 bit, 4: 16 bit, 5: 12 bit, 6: 10 bit, 7: 9 bit, 8: 8 bit.
* *
* @param values the multi-dimensional value * @param values the multi-dimensional value
...@@ -51,11 +51,11 @@ public class MultiDimension implements Comparator<long[]> { ...@@ -51,11 +51,11 @@ public class MultiDimension implements Comparator<long[]> {
int dimensions = values.length; int dimensions = values.length;
int bitsPerValue = 64 / dimensions; int bitsPerValue = 64 / dimensions;
// for 2 keys: 0x800000; 3: 0x // for 2 keys: 0x800000; 3: 0x
long max = 1L << bitsPerValue; long max = getMaxValue(dimensions);
long x = 0; long x = 0;
for (int i = 0; i < dimensions; i++) { for (int i = 0; i < dimensions; i++) {
long k = values[i]; long k = values[i];
if (k < 0 || k > max) { if (k < 0 || k >= max) {
throw new IllegalArgumentException("value out of range; value=" + values[i] + " min=0 max=" + max); throw new IllegalArgumentException("value out of range; value=" + values[i] + " min=0 max=" + max);
} }
for (int b = 0; b < bitsPerValue; b++) { for (int b = 0; b < bitsPerValue; b++) {
...@@ -71,6 +71,21 @@ public class MultiDimension implements Comparator<long[]> { ...@@ -71,6 +71,21 @@ public class MultiDimension implements Comparator<long[]> {
return x; return x;
} }
/**
* Get the maximum value for the given dimension count
* @param dimensions the number of dimensions
*
* @return the maximum value
*/
public static long getMaxValue(int dimensions) {
if (dimensions < 2 || dimensions > 64) {
throw new IllegalArgumentException("dimensions: " + dimensions);
}
int bitsPerValue = 64 / dimensions;
// for 2 keys: 0x800000; 3: 0x
return 1L << bitsPerValue;
}
/** /**
* Gets one of the original multi-dimensional values from a scalar value. * Gets one of the original multi-dimensional values from a scalar value.
* *
...@@ -88,7 +103,6 @@ public class MultiDimension implements Comparator<long[]> { ...@@ -88,7 +103,6 @@ public class MultiDimension implements Comparator<long[]> {
return value; return value;
} }
// public static int get(long z, int d) { // public static int get(long z, int d) {
// int n = 0; // int n = 0;
// for (int i = 0; i < 31; i++) { // for (int i = 0; i < 31; i++) {
...@@ -149,7 +163,7 @@ public class MultiDimension implements Comparator<long[]> { ...@@ -149,7 +163,7 @@ public class MultiDimension implements Comparator<long[]> {
* Gets a list of ranges to be searched for a multi-dimensional range query * Gets a list of ranges to be searched for a multi-dimensional range query
* where min &lt;= value &lt;= max. In most cases, the ranges will be larger * where min &lt;= value &lt;= max. In most cases, the ranges will be larger
* than required in order to combine smaller ranges into one. Usually, about * than required in order to combine smaller ranges into one. Usually, about
* double as much points will be included in the resulting range. * double as many points will be included in the resulting range.
* *
* @param min the minimum value * @param min the minimum value
* @param max the maximum value * @param max the maximum value
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论