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 */
016
017public class Hash32Shift extends Hash32 {
018
019        public Hash32Shift() {
020                super();
021        }
022        
023        public Hash32Shift(int seed) {
024                super(seed);
025        }
026        
027        @Override
028        int hashInt(int key) {
029                return hashIntShift(key);
030        }
031        
032        int hashIntShift(int key) {
033                key = ~key + (key << 15); // key = (key << 15) - key - 1;
034                key = key ^ (key >>> 12);
035                key = key + (key << 2);
036                key = key ^ (key >>> 4);
037                key = key * 2057; // key = (key + (key << 3)) + (key << 11);
038                key = key ^ (key >>> 16);
039                return key;
040        }
041
042}