001/*
002        AUTOMATICALLY GENERATED BY jTemp FROM
003        /Users/jsh2/Work/openimaj/target/checkout/machine-learning/nearest-neighbour/src/main/jtemp/org/openimaj/lsh/functions/#T#HashFunction.jtemp
004*/
005/**
006 * Copyright (c) 2011, The University of Southampton and the individual contributors.
007 * All rights reserved.
008 *
009 * Redistribution and use in source and binary forms, with or without modification,
010 * are permitted provided that the following conditions are met:
011 *
012 *   *  Redistributions of source code must retain the above copyright notice,
013 *      this list of conditions and the following disclaimer.
014 *
015 *   *  Redistributions in binary form must reproduce the above copyright notice,
016 *      this list of conditions and the following disclaimer in the documentation
017 *      and/or other materials provided with the distribution.
018 *
019 *   *  Neither the name of the University of Southampton nor the names of its
020 *      contributors may be used to endorse or promote products derived from this
021 *      software without specific prior written permission.
022 *
023 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
024 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
025 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
026 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
027 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
028 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
029 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
030 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
031 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
032 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
033 */
034package org.openimaj.lsh.functions;
035
036import cern.jet.random.engine.MersenneTwister;
037
038import org.openimaj.feature.ShortFV;
039import org.openimaj.feature.SparseShortFV;
040import org.openimaj.util.array.SparseShortArray;
041
042/**
043 * Base {@link RandomisedHashFunction} for hashing short arrays.
044 * 
045 * @author Jonathon Hare (jsh2@ecs.soton.ac.uk)
046 */
047public abstract class ShortHashFunction extends RandomisedHashFunction<short[]> {
048        /**
049         * Default constructor
050         * 
051         * @param factory
052         *            factory to use
053         * @param rng
054         *            random generator
055         */
056        ShortHashFunction(MersenneTwister rng) {
057                super(rng);
058        }
059        
060        /**
061         * Compute the hash code for the feature vector.
062         * 
063         * @param feature
064         *            the feature vector
065         * 
066         * @return the hash code
067         */
068        public final int computeHashCode(ShortFV feature) {
069                return computeHashCode(feature.values);
070        }
071        
072        /**
073         * Compute the hash code for the sparse array. This method 
074         * converts the sparse array to a dense one and computes the
075         * hash code from that. Subclasses should override this method
076         * for improved performance.
077         * 
078         * @param array
079         *            the sparse array
080         * 
081         * @return the hash code
082         */
083        public int computeHashCode(SparseShortArray array) {
084                return computeHashCode(array.toArray());
085        }
086        
087        /**
088         * Compute the hash code for the sparse feature vector.
089         * 
090         * @param feature
091         *            the sparse feature
092         * 
093         * @return the hash code
094         */
095        public final int computeHashCode(SparseShortFV feature) {
096                return computeHashCode(feature.values);
097        }
098}