jaulib v1.3.0
Jau Support Library (C++, Java, ..)
Hash32.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 Hash32 {
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 int hash(final int prime, final byte a[]) {
38 if (a == null) {
39 return 0;
40 }
41 int 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 int hash(final int prime, final int a, final int b) {
49 final int h = prime + a;
50 return prime * h + b;
51 }
52
53 /**
54 * Generates a 32bit equally distributed hash value using prime 31.
55 */
56 public static int hash31(final int a, final int b) {
57 // 31 * x == (x << 5) - x
58 final int hash = 31 + a;
59 return ((hash << 5) - hash) + b;
60 }
61
62 /**
63 * Generates a 32bit equally distributed identity hash value
64 * from <code>addr</code> avoiding XOR collision using prime 31.
65 */
66 public static int hash31(final long addr) {
67 // avoid xor collisions of low/high parts
68 // 31 * x == (x << 5) - x
69 final int hash = 31 + (int) addr; // lo addr
70 return ((hash << 5) - hash) + (int) ( addr >>> 32 ) ; // hi addr
71 }
72
73 /**
74 * Generates a 32bit equally distributed identity hash value
75 * from <code>addr</code> and <code>size</code> avoiding XOR collision using prime 31.
76 */
77 public static int hash31(final long addr, final long size) {
78 // avoid xor collisions of low/high parts
79 // 31 * x == (x << 5) - x
80 int hash = 31 + (int) addr ; // lo addr
81 hash = ((hash << 5) - hash) + (int) ( addr >>> 32 ) ; // hi addr
82 hash = ((hash << 5) - hash) + (int) size ; // lo size
83 return ((hash << 5) - hash) + (int) ( size >>> 32 ) ; // hi size
84 }
85
86}
static int hash31(final long addr, final long size)
Generates a 32bit equally distributed identity hash value from addr and size avoiding XOR collision u...
Definition: Hash32.java:77
static int hash31(final long addr)
Generates a 32bit equally distributed identity hash value from addr avoiding XOR collision using prim...
Definition: Hash32.java:66
static int hash(final int prime, final byte a[])
Definition: Hash32.java:37
static int hash31(final int a, final int b)
Generates a 32bit equally distributed hash value using prime 31.
Definition: Hash32.java:56
static int hash(final int prime, final int a, final int b)
Definition: Hash32.java:48