001/* 002 * Java Genetic Algorithm Library (jenetics-7.2.0). 003 * Copyright (c) 2007-2023 Franz Wilhelmstötter 004 * 005 * Licensed under the Apache License, Version 2.0 (the "License"); 006 * you may not use this file except in compliance with the License. 007 * You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 * 017 * Author: 018 * Franz Wilhelmstötter (franz.wilhelmstoetter@gmail.com) 019 */ 020package io.jenetics.internal.util; 021 022import java.util.Arrays; 023import java.util.Objects; 024 025/** 026 * Static methods for simple and efficient hash-code calculation. 027 * 028 * @author <a href="mailto:franz.wilhelmstoetter@gmail.com">Franz Wilhelmstötter</a> 029 * @version 4.3 030 * @since 4.3 031 */ 032public final class Hashes { 033 034 private static final int P1 = 47; 035 private static final int P2 = 103; 036 private static final int P3 = 197; 037 038 private Hashes() { 039 } 040 041 public static int hash(final Object value, final int seed) { 042 return seed + P2*Objects.hashCode(value) + P3; 043 } 044 045 public static int hash(final Object value) { 046 return hash(value, P1); 047 } 048 049 public static int hash(final Object[] value, final int seed) { 050 return seed + P2*Arrays.hashCode(value) + P3; 051 } 052 053 public static int hash(final Object[] value) { 054 return hash(value, P1); 055 } 056 057 public static int hash(final byte value, final int seed) { 058 return seed + P2*Byte.hashCode(value) + P3; 059 } 060 061 public static int hash(final byte value) { 062 return hash(value, P1); 063 } 064 065 public static int hash(final byte[] value, final int seed) { 066 return seed + P2*Arrays.hashCode(value) + P3; 067 } 068 069 public static int hash(final byte[] value) { 070 return hash(value, P1); 071 } 072 073 public static int hash(final char value, final int seed) { 074 return seed + P2*Character.hashCode(value) + P3; 075 } 076 077 public static int hash(final char value) { 078 return hash(value, P1); 079 } 080 081 public static int hash(final char[] value, final int seed) { 082 return seed + P2*Arrays.hashCode(value) + P3; 083 } 084 085 public static int hash(final char[] value) { 086 return hash(value, P1); 087 } 088 089 public static int hash(final short value, final int seed) { 090 return seed + P2*Short.hashCode(value) + P3; 091 } 092 093 public static int hash(final short value) { 094 return hash(value, P1); 095 } 096 097 public static int hash(final short[] value, final int seed) { 098 return seed + P2*Arrays.hashCode(value) + P3; 099 } 100 101 public static int hash(final short[] value) { 102 return hash(value, P1); 103 } 104 105 public static int hash(final int value, final int seed) { 106 return seed + P2*Integer.hashCode(value) + P3; 107 } 108 109 public static int hash(final int value) { 110 return hash(value, P1); 111 } 112 113 public static int hash(final int[] value, final int seed) { 114 return seed + P2*Arrays.hashCode(value) + P3; 115 } 116 117 public static int hash(final int[] value) { 118 return hash(value, P1); 119 } 120 121 public static int hash(final long value, final int seed) { 122 return seed + P2*Long.hashCode(value) + P3; 123 } 124 125 public static int hash(final long value) { 126 return hash(value, P1); 127 } 128 129 public static int hash(final long[] value, final int seed) { 130 return seed + P2*Arrays.hashCode(value) + P3; 131 } 132 133 public static int hash(final long[] value) { 134 return hash(value, P1); 135 } 136 137 public static int hash(final float value, final int seed) { 138 return seed + P2*Float.hashCode(value) + P3; 139 } 140 141 public static int hash(final float value) { 142 return hash(value, P1); 143 } 144 145 public static int hash(final float[] value, final int seed) { 146 return seed + P2*Arrays.hashCode(value) + P3; 147 } 148 149 public static int hash(final float[] value) { 150 return hash(value, P1); 151 } 152 153 public static int hash(final double value, final int seed) { 154 return seed + P2*Double.hashCode(value) + P3; 155 } 156 157 public static int hash(final double value) { 158 return hash(value, P1); 159 } 160 161 public static int hash(final double[] value, final int seed) { 162 return seed + P2*Arrays.hashCode(value) + P3; 163 } 164 165 public static int hash(final double[] value) { 166 return hash(value, P1); 167 } 168 169}