|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectorg.apache.commons.math3.geometry.partitioning.utilities.OrderedTuple
public class OrderedTuple
This class implements an ordering operation for T-uples.
Ordering is done by encoding all components of the T-uple into a
single scalar value and using this value as the sorting
key. Encoding is performed using the method invented by Georg
Cantor in 1877 when he proved it was possible to establish a
bijection between a line and a plane. The binary representations of
the components of the T-uple are mixed together to form a single
scalar. This means that the 2k bit of component 0 is
followed by the 2k bit of component 1, then by the
2k bit of component 2 up to the 2k bit of
component t
, which is followed by the 2k-1
bit of component 0, followed by the 2k-1 bit of
component 1 ... The binary representations are extended as needed
to handle numbers with different scales and a suitable
2p offset is added to the components in order to avoid
negative numbers (this offset is adjusted as needed during the
comparison operations).
The more interesting property of the encoding method for our purpose is that it allows to select all the points that are in a given range. This is depicted in dimension 2 by the following picture:
This picture shows a set of 100000 random 2-D pairs having their first component between -50 and +150 and their second component between -350 and +50. We wanted to extract all pairs having their first component between +30 and +70 and their second component between -120 and -30. We built the lower left point at coordinates (30, -120) and the upper right point at coordinates (70, -30). All points smaller than the lower left point are drawn in red and all points larger than the upper right point are drawn in blue. The green points are between the two limits. This picture shows that all the desired points are selected, along with spurious points. In this case, we get 15790 points, 4420 of which really belonging to the desired rectangle. It is possible to extract very small subsets. As an example extracting from the same 100000 points set the points having their first component between +30 and +31 and their second component between -91 and -90, we get a subset of 11 points, 2 of which really belonging to the desired rectangle.
the previous selection technique can be applied in all dimensions, still using two points to define the interval. The first point will have all its components set to their lower bounds while the second point will have all its components set to their upper bounds.
T-uples with negative infinite or positive infinite components are sorted logically.
Since the specification of the Comparator
interface
allows only ClassCastException
errors, some arbitrary
choices have been made to handle specific cases. The rationale for
these choices is to keep regular and consistent T-uples
together.
Double.NaN
components are sorted
after all other ones (even after instances with positive infinite
componentsDouble.NaN
components
Field Summary | |
---|---|
private double[] |
components
Double components of the T-uple. |
private long[] |
encoding
Ordering encoding of the double components. |
private static long |
EXPONENT_MASK
Exponent bits mask. |
private static long |
IMPLICIT_ONE
Implicit MSB for normalized numbers. |
private int |
lsb
Least Significant Bit scale. |
private static long |
MANTISSA_MASK
Mantissa bits mask. |
private boolean |
nan
Not A Number marker. |
private boolean |
negInf
Negative infinity marker. |
private int |
offset
Offset scale. |
private boolean |
posInf
Positive infinity marker. |
private static long |
SIGN_MASK
Sign bit mask. |
Constructor Summary | |
---|---|
OrderedTuple(double... components)
Build an ordered T-uple from its components. |
Method Summary | |
---|---|
int |
compareTo(OrderedTuple ot)
Compares this ordered T-uple with the specified object. |
private static int |
computeLSB(long l)
Compute the least significant bit of a long. |
private static int |
computeMSB(long l)
Compute the most significant bit of a long. |
private void |
encode(int minOffset)
Encode the T-uple with a given offset. |
boolean |
equals(Object other)
|
private static int |
exponent(long bits)
Extract the exponent from the bits of a double. |
private int |
getBit(int i,
int k)
Get a bit from the mantissa of a double. |
double[] |
getComponents()
Get the components array. |
int |
hashCode()
|
private static long |
mantissa(long bits)
Extract the mantissa from the bits of a double. |
private static long |
sign(long bits)
Extract the sign from the bits of a double. |
Methods inherited from class java.lang.Object |
---|
clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
private static final long SIGN_MASK
private static final long EXPONENT_MASK
private static final long MANTISSA_MASK
private static final long IMPLICIT_ONE
private double[] components
private int offset
private int lsb
private long[] encoding
private boolean posInf
private boolean negInf
private boolean nan
Constructor Detail |
---|
public OrderedTuple(double... components)
components
- double components of the T-upleMethod Detail |
---|
private void encode(int minOffset)
minOffset
- minimal scale of the offset to add to all
components (must be greater than the MSBs of all components)public int compareTo(OrderedTuple ot)
The ordering method is detailed in the general description of the class. Its main property is to be consistent with distance: geometrically close T-uples stay close to each other when stored in a sorted collection using this comparison method.
T-uples with negative infinite, positive infinite are sorted logically.
Some arbitrary choices have been made to handle specific cases. The rationale for these choices is to keep normal and consistent T-uples together.
Double.NaN
components are sorted
after all other ones (evan after instances with positive infinite
componentsDouble.NaN
components
compareTo
in interface Comparable<OrderedTuple>
ot
- T-uple to compare instance with
public boolean equals(Object other)
equals
in class Object
public int hashCode()
hashCode
in class Object
public double[] getComponents()
private static long sign(long bits)
bits
- binary representation of the double
private static int exponent(long bits)
bits
- binary representation of the double
private static long mantissa(long bits)
bits
- binary representation of the double
private static int computeMSB(long l)
l
- long from which the most significant bit is requested
l
,
or 0 if l
is zerocomputeLSB(long)
private static int computeLSB(long l)
l
- long from which the least significant bit is requested
l
,
or 63 if l
is zerocomputeMSB(long)
private int getBit(int i, int k)
i
- index of the componentk
- scale of the requested bit
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |