OrderedTuple.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.apache.commons.math3.geometry.partitioning.utilities;

  18. import java.util.Arrays;

  19. import org.apache.commons.math3.util.FastMath;

  20. /** This class implements an ordering operation for T-uples.
  21.  *
  22.  * <p>Ordering is done by encoding all components of the T-uple into a
  23.  * single scalar value and using this value as the sorting
  24.  * key. Encoding is performed using the method invented by Georg
  25.  * Cantor in 1877 when he proved it was possible to establish a
  26.  * bijection between a line and a plane. The binary representations of
  27.  * the components of the T-uple are mixed together to form a single
  28.  * scalar. This means that the 2<sup>k</sup> bit of component 0 is
  29.  * followed by the 2<sup>k</sup> bit of component 1, then by the
  30.  * 2<sup>k</sup> bit of component 2 up to the 2<sup>k</sup> bit of
  31.  * component {@code t}, which is followed by the 2<sup>k-1</sup>
  32.  * bit of component 0, followed by the 2<sup>k-1</sup> bit of
  33.  * component 1 ... The binary representations are extended as needed
  34.  * to handle numbers with different scales and a suitable
  35.  * 2<sup>p</sup> offset is added to the components in order to avoid
  36.  * negative numbers (this offset is adjusted as needed during the
  37.  * comparison operations).</p>
  38.  *
  39.  * <p>The more interesting property of the encoding method for our
  40.  * purpose is that it allows to select all the points that are in a
  41.  * given range. This is depicted in dimension 2 by the following
  42.  * picture:</p>
  43.  *
  44.  * <img src="doc-files/OrderedTuple.png" />
  45.  *
  46.  * <p>This picture shows a set of 100000 random 2-D pairs having their
  47.  * first component between -50 and +150 and their second component
  48.  * between -350 and +50. We wanted to extract all pairs having their
  49.  * first component between +30 and +70 and their second component
  50.  * between -120 and -30. We built the lower left point at coordinates
  51.  * (30, -120) and the upper right point at coordinates (70, -30). All
  52.  * points smaller than the lower left point are drawn in red and all
  53.  * points larger than the upper right point are drawn in blue. The
  54.  * green points are between the two limits. This picture shows that
  55.  * all the desired points are selected, along with spurious points. In
  56.  * this case, we get 15790 points, 4420 of which really belonging to
  57.  * the desired rectangle. It is possible to extract very small
  58.  * subsets. As an example extracting from the same 100000 points set
  59.  * the points having their first component between +30 and +31 and
  60.  * their second component between -91 and -90, we get a subset of 11
  61.  * points, 2 of which really belonging to the desired rectangle.</p>
  62.  *
  63.  * <p>the previous selection technique can be applied in all
  64.  * dimensions, still using two points to define the interval. The
  65.  * first point will have all its components set to their lower bounds
  66.  * while the second point will have all its components set to their
  67.  * upper bounds.</p>
  68.  *
  69.  * <p>T-uples with negative infinite or positive infinite components
  70.  * are sorted logically.</p>
  71.  *
  72.  * <p>Since the specification of the {@code Comparator} interface
  73.  * allows only {@code ClassCastException} errors, some arbitrary
  74.  * choices have been made to handle specific cases. The rationale for
  75.  * these choices is to keep <em>regular</em> and consistent T-uples
  76.  * together.</p>
  77.  * <ul>
  78.  * <li>instances with different dimensions are sorted according to
  79.  * their dimension regardless of their components values</li>
  80.  * <li>instances with {@code Double.NaN} components are sorted
  81.  * after all other ones (even after instances with positive infinite
  82.  * components</li>
  83.  * <li>instances with both positive and negative infinite components
  84.  * are considered as if they had {@code Double.NaN}
  85.  * components</li>
  86.  * </ul>
  87.  *
  88.  * @since 3.0
  89.  * @deprecated as of 3.4, this class is not used anymore and considered
  90.  * to be out of scope of Apache Commons Math
  91.  */
  92. @Deprecated
  93. public class OrderedTuple implements Comparable<OrderedTuple> {

  94.     /** Sign bit mask. */
  95.     private static final long SIGN_MASK     = 0x8000000000000000L;

  96.     /** Exponent bits mask. */
  97.     private static final long EXPONENT_MASK = 0x7ff0000000000000L;

  98.     /** Mantissa bits mask. */
  99.     private static final long MANTISSA_MASK = 0x000fffffffffffffL;

  100.     /** Implicit MSB for normalized numbers. */
  101.     private static final long IMPLICIT_ONE  = 0x0010000000000000L;

  102.     /** Double components of the T-uple. */
  103.     private double[] components;

  104.     /** Offset scale. */
  105.     private int offset;

  106.     /** Least Significant Bit scale. */
  107.     private int lsb;

  108.     /** Ordering encoding of the double components. */
  109.     private long[] encoding;

  110.     /** Positive infinity marker. */
  111.     private boolean posInf;

  112.     /** Negative infinity marker. */
  113.     private boolean negInf;

  114.     /** Not A Number marker. */
  115.     private boolean nan;

  116.     /** Build an ordered T-uple from its components.
  117.      * @param components double components of the T-uple
  118.      */
  119.     public OrderedTuple(final double ... components) {
  120.         this.components = components.clone();
  121.         int msb = Integer.MIN_VALUE;
  122.         lsb     = Integer.MAX_VALUE;
  123.         posInf  = false;
  124.         negInf  = false;
  125.         nan     = false;
  126.         for (int i = 0; i < components.length; ++i) {
  127.             if (Double.isInfinite(components[i])) {
  128.                 if (components[i] < 0) {
  129.                     negInf = true;
  130.                 } else {
  131.                     posInf = true;
  132.                 }
  133.             } else if (Double.isNaN(components[i])) {
  134.                 nan = true;
  135.             } else {
  136.                 final long b = Double.doubleToLongBits(components[i]);
  137.                 final long m = mantissa(b);
  138.                 if (m != 0) {
  139.                     final int e = exponent(b);
  140.                     msb = FastMath.max(msb, e + computeMSB(m));
  141.                     lsb = FastMath.min(lsb, e + computeLSB(m));
  142.                 }
  143.             }
  144.         }

  145.         if (posInf && negInf) {
  146.             // instance cannot be sorted logically
  147.             posInf = false;
  148.             negInf = false;
  149.             nan    = true;
  150.         }

  151.         if (lsb <= msb) {
  152.             // encode the T-upple with the specified offset
  153.             encode(msb + 16);
  154.         } else {
  155.             encoding = new long[] {
  156.                 0x0L
  157.             };
  158.         }

  159.     }

  160.     /** Encode the T-uple with a given offset.
  161.      * @param minOffset minimal scale of the offset to add to all
  162.      * components (must be greater than the MSBs of all components)
  163.      */
  164.     private void encode(final int minOffset) {

  165.         // choose an offset with some margins
  166.         offset  = minOffset + 31;
  167.         offset -= offset % 32;

  168.         if ((encoding != null) && (encoding.length == 1) && (encoding[0] == 0x0L)) {
  169.             // the components are all zeroes
  170.             return;
  171.         }

  172.         // allocate an integer array to encode the components (we use only
  173.         // 63 bits per element because there is no unsigned long in Java)
  174.         final int neededBits  = offset + 1 - lsb;
  175.         final int neededLongs = (neededBits + 62) / 63;
  176.         encoding = new long[components.length * neededLongs];

  177.         // mix the bits from all components
  178.         int  eIndex = 0;
  179.         int  shift  = 62;
  180.         long word   = 0x0L;
  181.         for (int k = offset; eIndex < encoding.length; --k) {
  182.             for (int vIndex = 0; vIndex < components.length; ++vIndex) {
  183.                 if (getBit(vIndex, k) != 0) {
  184.                     word |= 0x1L << shift;
  185.                 }
  186.                 if (shift-- == 0) {
  187.                     encoding[eIndex++] = word;
  188.                     word  = 0x0L;
  189.                     shift = 62;
  190.                 }
  191.             }
  192.         }

  193.     }

  194.     /** Compares this ordered T-uple with the specified object.

  195.      * <p>The ordering method is detailed in the general description of
  196.      * the class. Its main property is to be consistent with distance:
  197.      * geometrically close T-uples stay close to each other when stored
  198.      * in a sorted collection using this comparison method.</p>

  199.      * <p>T-uples with negative infinite, positive infinite are sorted
  200.      * logically.</p>

  201.      * <p>Some arbitrary choices have been made to handle specific
  202.      * cases. The rationale for these choices is to keep
  203.      * <em>normal</em> and consistent T-uples together.</p>
  204.      * <ul>
  205.      * <li>instances with different dimensions are sorted according to
  206.      * their dimension regardless of their components values</li>
  207.      * <li>instances with {@code Double.NaN} components are sorted
  208.      * after all other ones (evan after instances with positive infinite
  209.      * components</li>
  210.      * <li>instances with both positive and negative infinite components
  211.      * are considered as if they had {@code Double.NaN}
  212.      * components</li>
  213.      * </ul>

  214.      * @param ot T-uple to compare instance with
  215.      * @return a negative integer if the instance is less than the
  216.      * object, zero if they are equal, or a positive integer if the
  217.      * instance is greater than the object

  218.      */
  219.     public int compareTo(final OrderedTuple ot) {
  220.         if (components.length == ot.components.length) {
  221.             if (nan) {
  222.                 return +1;
  223.             } else if (ot.nan) {
  224.                 return -1;
  225.             } else if (negInf || ot.posInf) {
  226.                 return -1;
  227.             } else if (posInf || ot.negInf) {
  228.                 return +1;
  229.             } else {

  230.                 if (offset < ot.offset) {
  231.                     encode(ot.offset);
  232.                 } else if (offset > ot.offset) {
  233.                     ot.encode(offset);
  234.                 }

  235.                 final int limit = FastMath.min(encoding.length, ot.encoding.length);
  236.                 for (int i = 0; i < limit; ++i) {
  237.                     if (encoding[i] < ot.encoding[i]) {
  238.                         return -1;
  239.                     } else if (encoding[i] > ot.encoding[i]) {
  240.                         return +1;
  241.                     }
  242.                 }

  243.                 if (encoding.length < ot.encoding.length) {
  244.                     return -1;
  245.                 } else if (encoding.length > ot.encoding.length) {
  246.                     return +1;
  247.                 } else {
  248.                     return 0;
  249.                 }

  250.             }
  251.         }

  252.         return components.length - ot.components.length;

  253.     }

  254.     /** {@inheritDoc} */
  255.     @Override
  256.     public boolean equals(final Object other) {
  257.         if (this == other) {
  258.             return true;
  259.         } else if (other instanceof OrderedTuple) {
  260.             return compareTo((OrderedTuple) other) == 0;
  261.         } else {
  262.             return false;
  263.         }
  264.     }

  265.     /** {@inheritDoc} */
  266.     @Override
  267.     public int hashCode() {
  268.         // the following constants are arbitrary small primes
  269.         final int multiplier = 37;
  270.         final int trueHash   = 97;
  271.         final int falseHash  = 71;

  272.         // hash fields and combine them
  273.         // (we rely on the multiplier to have different combined weights
  274.         //  for all int fields and all boolean fields)
  275.         int hash = Arrays.hashCode(components);
  276.         hash = hash * multiplier + offset;
  277.         hash = hash * multiplier + lsb;
  278.         hash = hash * multiplier + (posInf ? trueHash : falseHash);
  279.         hash = hash * multiplier + (negInf ? trueHash : falseHash);
  280.         hash = hash * multiplier + (nan    ? trueHash : falseHash);

  281.         return hash;

  282.     }

  283.     /** Get the components array.
  284.      * @return array containing the T-uple components
  285.      */
  286.     public double[] getComponents() {
  287.         return components.clone();
  288.     }

  289.     /** Extract the sign from the bits of a double.
  290.      * @param bits binary representation of the double
  291.      * @return sign bit (zero if positive, non zero if negative)
  292.      */
  293.     private static long sign(final long bits) {
  294.         return bits & SIGN_MASK;
  295.     }

  296.     /** Extract the exponent from the bits of a double.
  297.      * @param bits binary representation of the double
  298.      * @return exponent
  299.      */
  300.     private static int exponent(final long bits) {
  301.         return ((int) ((bits & EXPONENT_MASK) >> 52)) - 1075;
  302.     }

  303.     /** Extract the mantissa from the bits of a double.
  304.      * @param bits binary representation of the double
  305.      * @return mantissa
  306.      */
  307.     private static long mantissa(final long bits) {
  308.         return ((bits & EXPONENT_MASK) == 0) ?
  309.                ((bits & MANTISSA_MASK) << 1) :          // subnormal number
  310.                (IMPLICIT_ONE | (bits & MANTISSA_MASK)); // normal number
  311.     }

  312.     /** Compute the most significant bit of a long.
  313.      * @param l long from which the most significant bit is requested
  314.      * @return scale of the most significant bit of {@code l},
  315.      * or 0 if {@code l} is zero
  316.      * @see #computeLSB
  317.      */
  318.     private static int computeMSB(final long l) {

  319.         long ll = l;
  320.         long mask  = 0xffffffffL;
  321.         int  scale = 32;
  322.         int  msb   = 0;

  323.         while (scale != 0) {
  324.             if ((ll & mask) != ll) {
  325.                 msb |= scale;
  326.                 ll >>= scale;
  327.             }
  328.             scale >>= 1;
  329.             mask >>= scale;
  330.         }

  331.         return msb;

  332.     }

  333.     /** Compute the least significant bit of a long.
  334.      * @param l long from which the least significant bit is requested
  335.      * @return scale of the least significant bit of {@code l},
  336.      * or 63 if {@code l} is zero
  337.      * @see #computeMSB
  338.      */
  339.     private static int computeLSB(final long l) {

  340.         long ll = l;
  341.         long mask  = 0xffffffff00000000L;
  342.         int  scale = 32;
  343.         int  lsb   = 0;

  344.         while (scale != 0) {
  345.             if ((ll & mask) == ll) {
  346.                 lsb |= scale;
  347.                 ll >>= scale;
  348.             }
  349.             scale >>= 1;
  350.             mask >>= scale;
  351.         }

  352.         return lsb;

  353.     }

  354.     /** Get a bit from the mantissa of a double.
  355.      * @param i index of the component
  356.      * @param k scale of the requested bit
  357.      * @return the specified bit (either 0 or 1), after the offset has
  358.      * been added to the double
  359.      */
  360.     private int getBit(final int i, final int k) {
  361.         final long bits = Double.doubleToLongBits(components[i]);
  362.         final int e = exponent(bits);
  363.         if ((k < e) || (k > offset)) {
  364.             return 0;
  365.         } else if (k == offset) {
  366.             return (sign(bits) == 0L) ? 1 : 0;
  367.         } else if (k > (e + 52)) {
  368.             return (sign(bits) == 0L) ? 0 : 1;
  369.         } else {
  370.             final long m = (sign(bits) == 0L) ? mantissa(bits) : -mantissa(bits);
  371.             return (int) ((m >> (k - e)) & 0x1L);
  372.         }
  373.     }

  374. }