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
012import java.util.Random;
013
014/**
015 * This class is the abstract superclass of various hash functions. 
016 * It cannot be instantiated directly, but its subclasses can. Two static 
017 * methods <tt>create()</tt> and <tt>create(seed)</tt> are provided for convenience.
018 * Typical use: <br><pre>
019 *   HashFun hf = new HashFun.create(); // or, alternatively,
020 *   HashFun hf = new Hash32Ward(seed);
021 *   double g = hf.hash(u); // g is in [-1,+1]
022 *   double g = hf.hash(u,v);
023 *   double[] g = hf.hash(u,v,w); </pre>
024 * Omit seed in the constructor call to get a random seed 
025 * hash function of the specified type.
026 */
027
028public abstract class HashFun {
029        
030        static final Random rand = new Random();
031        int seed;
032        
033        protected HashFun() {
034                this.seed = makeRandomSeed();
035        }
036        
037        protected HashFun(int seed){
038                this.seed = seed;
039        }
040        
041        /**
042         * @return A new HashFun object.
043         * Has32Shift is used as the default type.
044         */
045        public static HashFun create() {
046                return new Hash32Shift();
047        }
048        
049
050        /**
051         * Creates a new {@link HashFun} object initialized with seed. 
052         * Has32Shift is used as the default type.
053         * 
054         * @param seed the seed
055         * @return a new {@link HashFun} object
056         */
057        public static HashFun create(int seed) {
058                return new Hash32Shift(seed);
059        }
060        
061        protected int makeRandomSeed() {
062                return 0x000fffff & rand.nextInt();
063        }
064        
065        // these hash functions return either a single value or a vector 
066        // with elements in [0,1]
067        public abstract double hash(int u);                                     // 1D hash function
068        public abstract double[] hash(int u, int v);            // 2D hash function
069        public abstract double[] hash(int u, int v, int w);     // 3D hash function
070        public abstract double[] hash(int[] p);                         // ND hash function
071
072}
073
074