Class ExactMatchDistance

  • All Implemented Interfaces:
    SequenceDistanceMeasurer, SequenceDistanceMeasurerDouble

    public final class ExactMatchDistance
    extends Object

    ExactMatch distance (or Hamming Distance) of a pair of non-binary strings (or more generally sequences) is the number of sequence (or string) positions where the two sequences differ. This class supports comparison of Java String objects, as well as comparisons of arrays of any of Java's eight primitive types, and arrays of any object type. It is the count of the number of positions for which the two sequences contain different elements.

    Most other implementations of Hamming distance require the two strings to be of the same length. Ours does not. If the two sequences (i.e., arrays or Strings) are of different lengths, then the difference in length affects distance. E.g., although "abbd" is a distance of 1 from "abcd" since they differ in one position, "abbd" is a distance of 4 from "abcdefg" (the one difference, plus the three extra characters).

    Runtime: O(n), where n is the length of the shorter of the two sequences.

    If your sequences are guaranteed not to have duplicates, and to contain the same set of elements, then consider instead using the ExactMatchDistance class, which assumes permutations of the integers from 0 to N-1.

    Exact match distance was introduced specifically for permutations in:
    S. Ronald, "More distance functions for order-based encodings," in Proc. IEEE CEC. IEEE Press, 1998, pp. 558–563.

    • Constructor Summary

      Constructors 
      Constructor Description
      ExactMatchDistance()
      Constructs the distance measurer as specified in the class documentation.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      int distance​(boolean[] s1, boolean[] s2)
      Measures the distance between two arrays.
      int distance​(byte[] s1, byte[] s2)
      Measures the distance between two arrays.
      int distance​(char[] s1, char[] s2)
      Measures the distance between two arrays.
      int distance​(double[] s1, double[] s2)
      Measures the distance between two arrays.
      int distance​(float[] s1, float[] s2)
      Measures the distance between two arrays.
      int distance​(int[] s1, int[] s2)
      Measures the distance between two arrays.
      int distance​(long[] s1, long[] s2)
      Measures the distance between two arrays.
      int distance​(short[] s1, short[] s2)
      Measures the distance between two arrays.
      int distance​(Object[] s1, Object[] s2)
      Measures the distance between two arrays of objects.
      int distance​(String s1, String s2)
      Measures the distance between two Strings.
      <T> int distance​(List<T> s1, List<T> s2)
      Measures the distance between two lists of objects.
      double distancef​(boolean[] s1, boolean[] s2)
      Measures the distance between two arrays.
      double distancef​(byte[] s1, byte[] s2)
      Measures the distance between two arrays.
      double distancef​(char[] s1, char[] s2)
      Measures the distance between two arrays.
      double distancef​(double[] s1, double[] s2)
      Measures the distance between two arrays.
      double distancef​(float[] s1, float[] s2)
      Measures the distance between two arrays.
      double distancef​(int[] s1, int[] s2)
      Measures the distance between two arrays.
      double distancef​(long[] s1, long[] s2)
      Measures the distance between two arrays.
      double distancef​(short[] s1, short[] s2)
      Measures the distance between two arrays.
      double distancef​(Object[] s1, Object[] s2)
      Measures the distance between two arrays of objects.
      double distancef​(String s1, String s2)
      Measures the distance between two Strings.
      <T> double distancef​(List<T> s1, List<T> s2)
      Measures the distance between two lists of objects.
    • Constructor Detail

      • ExactMatchDistance

        public ExactMatchDistance()
        Constructs the distance measurer as specified in the class documentation.
    • Method Detail

      • distance

        public int distance​(int[] s1,
                            int[] s2)
        Measures the distance between two arrays.
        Parameters:
        s1 - First array.
        s2 - Second array.
        Returns:
        distance between s1 and s2
      • distance

        public int distance​(long[] s1,
                            long[] s2)
        Measures the distance between two arrays.
        Parameters:
        s1 - First array.
        s2 - Second array.
        Returns:
        distance between s1 and s2
      • distance

        public int distance​(short[] s1,
                            short[] s2)
        Measures the distance between two arrays.
        Parameters:
        s1 - First array.
        s2 - Second array.
        Returns:
        distance between s1 and s2
      • distance

        public int distance​(byte[] s1,
                            byte[] s2)
        Measures the distance between two arrays.
        Parameters:
        s1 - First array.
        s2 - Second array.
        Returns:
        distance between s1 and s2
      • distance

        public int distance​(char[] s1,
                            char[] s2)
        Measures the distance between two arrays.
        Parameters:
        s1 - First array.
        s2 - Second array.
        Returns:
        distance between s1 and s2
      • distance

        public int distance​(boolean[] s1,
                            boolean[] s2)
        Measures the distance between two arrays.
        Parameters:
        s1 - First array.
        s2 - Second array.
        Returns:
        distance between s1 and s2
      • distance

        public int distance​(double[] s1,
                            double[] s2)
        Measures the distance between two arrays.
        Parameters:
        s1 - First array.
        s2 - Second array.
        Returns:
        distance between s1 and s2
      • distance

        public int distance​(float[] s1,
                            float[] s2)
        Measures the distance between two arrays.
        Parameters:
        s1 - First array.
        s2 - Second array.
        Returns:
        distance between s1 and s2
      • distance

        public int distance​(String s1,
                            String s2)
        Measures the distance between two Strings.
        Parameters:
        s1 - First String.
        s2 - Second String.
        Returns:
        distance between s1 and s2
      • distance

        public int distance​(Object[] s1,
                            Object[] s2)
        Measures the distance between two arrays of objects. The objects in the arrays must be of a class that has overridden the Object.equals method.
        Parameters:
        s1 - First array.
        s2 - Second array.
        Returns:
        distance between s1 and s2
      • distance

        public <T> int distance​(List<T> s1,
                                List<T> s2)
        Measures the distance between two lists of objects. The objects in the lists must be of a class that has overridden the Object.equals method.
        Type Parameters:
        T - Type of List elements.
        Parameters:
        s1 - First list.
        s2 - Second list.
        Returns:
        distance between s1 and s2
      • distancef

        public final double distancef​(long[] s1,
                                      long[] s2)
        Measures the distance between two arrays.
        Specified by:
        distancef in interface SequenceDistanceMeasurerDouble
        Parameters:
        s1 - First array.
        s2 - Second array.
        Returns:
        distance between s1 and s2
      • distancef

        public final double distancef​(int[] s1,
                                      int[] s2)
        Measures the distance between two arrays.
        Specified by:
        distancef in interface SequenceDistanceMeasurerDouble
        Parameters:
        s1 - First array.
        s2 - Second array.
        Returns:
        distance between s1 and s2
      • distancef

        public final double distancef​(short[] s1,
                                      short[] s2)
        Measures the distance between two arrays.
        Specified by:
        distancef in interface SequenceDistanceMeasurerDouble
        Parameters:
        s1 - First array.
        s2 - Second array.
        Returns:
        distance between s1 and s2
      • distancef

        public final double distancef​(byte[] s1,
                                      byte[] s2)
        Measures the distance between two arrays.
        Specified by:
        distancef in interface SequenceDistanceMeasurerDouble
        Parameters:
        s1 - First array.
        s2 - Second array.
        Returns:
        distance between s1 and s2
      • distancef

        public final double distancef​(char[] s1,
                                      char[] s2)
        Measures the distance between two arrays.
        Specified by:
        distancef in interface SequenceDistanceMeasurerDouble
        Parameters:
        s1 - First array.
        s2 - Second array.
        Returns:
        distance between s1 and s2
      • distancef

        public final double distancef​(double[] s1,
                                      double[] s2)
        Measures the distance between two arrays.
        Specified by:
        distancef in interface SequenceDistanceMeasurerDouble
        Parameters:
        s1 - First array.
        s2 - Second array.
        Returns:
        distance between s1 and s2
      • distancef

        public final double distancef​(float[] s1,
                                      float[] s2)
        Measures the distance between two arrays.
        Specified by:
        distancef in interface SequenceDistanceMeasurerDouble
        Parameters:
        s1 - First array.
        s2 - Second array.
        Returns:
        distance between s1 and s2
      • distancef

        public final double distancef​(boolean[] s1,
                                      boolean[] s2)
        Measures the distance between two arrays.
        Specified by:
        distancef in interface SequenceDistanceMeasurerDouble
        Parameters:
        s1 - First array.
        s2 - Second array.
        Returns:
        distance between s1 and s2
      • distancef

        public final double distancef​(String s1,
                                      String s2)
        Measures the distance between two Strings.
        Specified by:
        distancef in interface SequenceDistanceMeasurerDouble
        Parameters:
        s1 - First String.
        s2 - Second String.
        Returns:
        distance between s1 and s2
      • distancef

        public final double distancef​(Object[] s1,
                                      Object[] s2)
        Measures the distance between two arrays of objects. The objects in the arrays must be of a class that has overridden the Object.equals method.
        Specified by:
        distancef in interface SequenceDistanceMeasurerDouble
        Parameters:
        s1 - First array.
        s2 - Second array.
        Returns:
        distance between s1 and s2
      • distancef

        public final <T> double distancef​(List<T> s1,
                                          List<T> s2)
        Measures the distance between two lists of objects. The objects in the lists must be of a class that has overridden the Object.equals method.
        Specified by:
        distancef in interface SequenceDistanceMeasurerDouble
        Type Parameters:
        T - Type of List elements.
        Parameters:
        s1 - First list.
        s2 - Second list.
        Returns:
        distance between s1 and s2