home *** CD-ROM | disk | FTP | other *** search
Java Source | 1996-09-10 | 2.6 KB | 85 lines |
- // Copyright(c) 1996 ObjectSpace, Inc.
- // Portions Copyright(c) 1995, 1996 Hewlett-Packard Company.
-
- package jgl;
-
- /**
- * The Hashing class contains generic hashing algorithms.
- * <p>
- * @version 1.1
- * @author ObjectSpace, Inc.
- */
-
- public final class Hashing
- {
- /**
- * Compute an hash value for an ordered container.
- */
- public static int orderedHash( Container c )
- {
- return orderedHash( c.start(), c.finish() );
- }
-
- /**
- * Compute a hash value for a range of elements in an ordered container
- * Hashing on an ordered container requires that all
- * elements in the container that are used in the hash
- * have the position taken into account.
- * The hashing scheme uses all contained elements if the size
- * of the range is less than 16. If the size of the range
- * is > 16, only representative samples are used to increase
- * performance. Position is taken into account for each element
- * used in the hash by taking the position modulo 16 plus one
- * and using this result as a divisor on the actual hash code
- * of the element.
- * @param first An iterator positioned at the first element of the sequence.
- * @param last An iterator positioned immediately after the last element of the sequence.
- */
- public static int orderedHash( ForwardIterator first, ForwardIterator last )
- {
- int h = 0;
- int length = first.distance( last );
- int skip = length < 16 ? 1 : length / 16;
- int position = 0;
- while( ( !first.equals( last ) ) && ( first.distance( last ) > 0 ) )
- {
- if( first.get() != null )
- h ^= ( first.get().hashCode() / ((position % 16) + 1) );
- position++;
- first.advance( skip );
- }
- return h;
- }
-
- /**
- * Compute an hash value for an unordered container.
- */
- public static int unorderedHash( Container c )
- {
- return unorderedHash( c.start(), c.finish() );
- }
-
- /**
- * Compute a hash value for a range of elements in an
- * uordered container.
- * Hashing on an unordered container requires that all
- * elements in the container are used in the hash.
- * A simple XOR scheme is used over all elements to
- * ensure that equality is maintained over like ranges.
- * @param first An iterator positioned at the first element of the sequence.
- * @param last An iterator positioned immediately after the last element of the sequence.
- */
- public static int unorderedHash( ForwardIterator first, ForwardIterator last )
- {
- int h = 0;
- while( !first.equals( last ) )
- {
- if( first.get() != null )
- h ^= first.get().hashCode();
- first.advance();
- }
- return h;
- }
- }
-
-