Save This Page
Home » mahout-collections-1.0-src » org.apache.mahout.math.map » [javadoc | source]
    1   /*
    2   Copyright 1999 CERN - European Organization for Nuclear Research.
    3   Permission to use, copy, modify, distribute and sell this software and its documentation for any purpose 
    4   is hereby granted without fee, provided that the above copyright notice appear in all copies and 
    5   that both that copyright notice and this permission notice appear in supporting documentation. 
    6   CERN makes no representations about the suitability of this software for any purpose. 
    7   It is provided "as is" without expressed or implied warranty.
    8   */
    9   package org.apache.mahout.math.map;
   10   
   11   /**
   12    * Provides various hash functions.
   13    *
   14    */
   15   public final class HashFunctions {
   16   
   17     /**
   18      * Utility class pattern: all static members, no inheritance.
   19      */
   20     private HashFunctions() {
   21     }
   22   
   23     /**
   24      * Returns a hashcode for the specified value.
   25      *
   26      * @return a hash code value for the specified value.
   27      */
   28     public static int hash(char value) {
   29       return (int) value;
   30     }
   31   
   32     /**
   33      * Returns a hashcode for the specified value.
   34      *
   35      * @return a hash code value for the specified value.
   36      */
   37     public static int hash(double value) {
   38       long bits = Double.doubleToLongBits(value);
   39       return (int) (bits ^ (bits >>> 32));
   40   
   41       //return (int) Double.doubleToLongBits(value*663608941.737);
   42       // this avoids excessive hashCollisions in the case values are of the form (1.0, 2.0, 3.0, ...)
   43     }
   44   
   45     /**
   46      * Returns a hashcode for the specified value.
   47      *
   48      * @return a hash code value for the specified value.
   49      */
   50     public static int hash(float value) {
   51       return Float.floatToIntBits(value * 663608941.737f);
   52       // this avoids excessive hashCollisions in the case values are of the form (1.0, 2.0, 3.0, ...)
   53     }
   54   
   55     /**
   56      * Returns a hashcode for the specified value.
   57      *
   58      * @return a hash code value for the specified value.
   59      */
   60     public static int hash(int value) {
   61       return value;
   62   
   63       //return value * 0x278DDE6D; // see org.apache.mahout.math.jet.random.engine.DRand
   64   
   65       /*
   66       value &= 0x7FFFFFFF; // make it >=0
   67       int hashCode = 0;
   68       do hashCode = 31*hashCode + value%10;
   69       while ((value /= 10) > 0);
   70   
   71       return 28629151*hashCode; // spread even further; h*31^5
   72       */
   73     }
   74   
   75     /**
   76      * Returns a hashcode for the specified value.
   77      *
   78      * @return a hash code value for the specified value.
   79      */
   80     public static int hash(long value) {
   81       return (int) (value ^ (value >> 32));
   82       /*
   83       value &= 0x7FFFFFFFFFFFFFFFL; // make it >=0 (0x7FFFFFFFFFFFFFFFL==Long.MAX_VALUE)
   84       int hashCode = 0;
   85       do hashCode = 31*hashCode + (int) (value%10);
   86       while ((value /= 10) > 0);
   87   
   88       return 28629151*hashCode; // spread even further; h*31^5
   89       */
   90     }
   91   
   92     /**
   93      * Returns a hashcode for the specified object.
   94      *
   95      * @return a hash code value for the specified object.
   96      */
   97     public static int hash(Object object) {
   98       return object == null ? 0 : object.hashCode();
   99     }
  100   
  101     /**
  102      * Returns a hashcode for the specified value.
  103      *
  104      * @return a hash code value for the specified value.
  105      */
  106     public static int hash(short value) {
  107       return (int) value;
  108     }
  109   
  110     /**
  111      * Returns a hashcode for the specified value.
  112      *
  113      * @return a hash code value for the specified value.
  114      */
  115     public static int hash(boolean value) {
  116       return value ? 1231 : 1237;
  117     }
  118   }

Save This Page
Home » mahout-collections-1.0-src » org.apache.mahout.math.map » [javadoc | source]