1 /* 2 * Copyright (C) 2014 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package android.hardware.camera2.utils; 18 19 import android.compat.annotation.UnsupportedAppUsage; 20 import android.os.Build; 21 22 /** 23 * Provide hashing functions using the Modified Bernstein hash 24 */ 25 public final class HashCodeHelpers { 26 27 /** 28 * Hash every element uniformly using the Modified Bernstein hash. 29 * 30 * <p>Useful to implement a {@link Object#hashCode} for uniformly distributed data.</p> 31 * 32 * @param array a non-{@code null} array of integers 33 * 34 * @return the numeric hash code 35 */ 36 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) hashCode(int... array)37 public static int hashCode(int... array) { 38 if (array == null) { 39 return 0; 40 } 41 42 /* 43 * Note that we use 31 here instead of 33 since it's preferred in Effective Java 44 * and used elsewhere in the runtime (e.g. Arrays#hashCode) 45 * 46 * That being said 33 and 31 are nearly identical in terms of their usefulness 47 * according to http://svn.apache.org/repos/asf/apr/apr/trunk/tables/apr_hash.c 48 */ 49 int h = 1; 50 for (int x : array) { 51 // Strength reduction; in case the compiler has illusions about divisions being faster 52 h = ((h << 5) - h) ^ x; // (h * 31) XOR x 53 } 54 55 return h; 56 } 57 58 /** 59 * Hash every element uniformly using the Modified Bernstein hash. 60 * 61 * <p>Useful to implement a {@link Object#hashCode} for uniformly distributed data.</p> 62 * 63 * @param array a non-{@code null} array of floats 64 * 65 * @return the numeric hash code 66 */ hashCode(float... array)67 public static int hashCode(float... array) { 68 if (array == null) { 69 return 0; 70 } 71 72 int h = 1; 73 for (float f : array) { 74 int x = Float.floatToIntBits(f); 75 h = ((h << 5) - h) ^ x; // (h * 31) XOR x 76 } 77 78 return h; 79 } 80 81 /** 82 * Hash every element uniformly using the Modified Bernstein hash. 83 * 84 * <p>Useful to implement a {@link Object#hashCode} for uniformly distributed data.</p> 85 * 86 * @param array a non-{@code null} array of objects 87 * 88 * @return the numeric hash code 89 */ hashCodeGeneric(T... array)90 public static <T> int hashCodeGeneric(T... array) { 91 if (array == null) { 92 return 0; 93 } 94 95 int h = 1; 96 for (T o : array) { 97 int x = (o == null) ? 0 : o.hashCode(); 98 h = ((h << 5) - h) ^ x; // (h * 31) XOR x 99 } 100 101 return h; 102 } 103 104 } 105