jaulib v1.3.0
Jau Support Library (C++, Java, ..)
Hash64.java
Go to the documentation of this file.
1/**
2 * Author: Sven Gothel <sgothel@jausoft.com>
3 * Copyright (c) 2020 Gothel Software e.K.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be
14 * included in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24package org.jau.util;
25
26public class Hash64 {
27
28 /**
29 * <ul>
30 * <li>31 - the traditional 'Java' prime for {@code 31 * x == (x << 5) - x}</li>
31 * <li>92821 - result of Hans-Peter Stoerr's finding</li>
32 * </ul>
33 * @param prime
34 * @param a
35 * @return
36 */
37 public static long hash(final long prime, final byte a[]) {
38 if (a == null) {
39 return 0;
40 }
41 long h = 1;
42 for (int i=0; i<a.length; i++) {
43 h = prime * h + a[i];
44 }
45 return h;
46 }
47
48 public static long hash(final long prime, final int a, final int b) {
49 final long h = prime + a;
50 return prime * h + b;
51 }
52
53 public static long hash(final long prime, final long a, final long b) {
54 final long h = prime + a;
55 return prime * h + b;
56 }
57
58 /**
59 * Generates a 64bit equally distributed hash value
60 * from <code>addr</code> and <code>size</code> avoiding XOR collisions.
61 */
62 public static long hash31(final long a, final long b) {
63 // 31 * x == (x << 5) - x
64 final long hash = 31 + a;
65 return ((hash << 5) - hash) + b;
66 }
67}
static long hash(final long prime, final byte a[])
Definition: Hash64.java:37
static long hash(final long prime, final int a, final int b)
Definition: Hash64.java:48
static long hash(final long prime, final long a, final long b)
Definition: Hash64.java:53
static long hash31(final long a, final long b)
Generates a 64bit equally distributed hash value from addr and size avoiding XOR collisions.
Definition: Hash64.java:62