001/*******************************************************************************
002 * This software is provided as a supplement to the authors' textbooks on digital
003 *  image processing published by Springer-Verlag in various languages and editions.
004 * Permission to use and distribute this software is granted under the BSD 2-Clause 
005 * "Simplified" License (see http://opensource.org/licenses/BSD-2-Clause). 
006 * Copyright (c) 2006-2016 Wilhelm Burger, Mark J. Burge. All rights reserved. 
007 * Visit http://imagingbook.com for additional details.
008 *******************************************************************************/
009
010package imagingbook.pub.noise.hashing;
011
012/**
013 * Hash function described in Thomas Wang, "Integer Hash Function"
014 * http://www.concentric.net/~Ttwang/tech/inthash.htm (Jan. 2007)
015 */
016public class Hash32ShiftMult extends Hash32 {
017        
018        public Hash32ShiftMult() {
019                super();
020        }
021        
022        public Hash32ShiftMult(int seed) {
023                super(seed);
024        }
025        
026        int hashInt(int key) {
027                return hashIntShiftMult(key) ;
028        }
029        
030        int hashIntShiftMult(int key) {
031                int c2 = 668265261; //=  0x27d4eb2d, which is not a prime, closest prime is 668265263
032                key = (key ^ 61) ^ (key >>> 16);
033                key = key + (key << 3);
034                key = key ^ (key >>> 4);
035                key = key * c2;
036                key = key ^ (key >>> 15);
037                return key;
038        }
039}