Java Hash Calculate hashPair(int a, int b)

Here you can find the source of hashPair(int a, int b)

Description

Generates a unique number based from two arguments.

License

Open Source License

Parameter

Parameter Description
a the first number of the pair
b the second number of the pair

Return

a unique hash of the two numbers

Declaration

public static long hashPair(int a, int b) 

Method Source Code

//package com.java2s;
/*//w  ww  .  j  ava  2  s  .  co m
 * Copyright (c) 2014 RobotsByTheC. All rights reserved.
 *
 * Open Source Software - may be modified and shared by FRC teams. The code must
 * be accompanied by the BSD license file in the root directory of the project.
 */

public class Main {
    /**
     * Generates a unique number based from two arguments. The arguments must be
     * positive, because this method is implemented using the unmodified
     * Szudzik's function.
     *
     * @param a the first number of the pair
     * @param b the second number of the pair
     * @return a unique hash of the two numbers
     */
    public static long hashPair(int a, int b) {
        if (a >= 0 || b >= 0) {
            return a >= b ? a * a + a + b : a + b * b;
        } else {
            throw new IllegalArgumentException("a and b must be positive.");
        }
    }
}

Related

  1. hashMore(final int hash, final int more)
  2. hashName(CharSequence name)
  3. hashObject(Object obj)
  4. hashOrNull(Object value)
  5. hashOrZero(Object o)
  6. hashpair(Object o1, Object o2)
  7. hashPoint(int hashlength, double fraction)
  8. hashSetSize(final int nrOfElements)
  9. hashThem(Object one, Object two)