Unify Hash : Hash Code « Development Class « Java

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Class
8. Collections Data Structure
9. Data Type
10. Database SQL JDBC
11. Design Pattern
12. Development Class
13. EJB3
14. Email
15. Event
16. File Input Output
17. Game
18. Generics
19. GWT
20. Hibernate
21. I18N
22. J2EE
23. J2ME
24. JDK 6
25. JNDI LDAP
26. JPA
27. JSP
28. JSTL
29. Language Basics
30. Network Protocol
31. PDF RTF
32. Reflection
33. Regular Expressions
34. Scripting
35. Security
36. Servlets
37. Spring
38. Swing Components
39. Swing JFC
40. SWT JFace Eclipse
41. Threads
42. Tiny Application
43. Velocity
44. Web Services SOA
45. XML
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java » Development Class » Hash CodeScreenshots 
Unify Hash
     
/* UnifyHash Copyright (C) 1999-2002 Jochen Hoenicke.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; see the file COPYING.LESSER.  If not, write to
 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 * $Id: UnifyHash.java.in,v 1.3.2.2 2002/05/28 17:34:24 hoenicke Exp $
 */

///#ifdef JDK12
import java.lang.ref.WeakReference;
import java.lang.ref.ReferenceQueue;
///#endif

import java.util.Comparator;
import java.util.AbstractCollection;
import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.ConcurrentModificationException;
import java.lang.UnsupportedOperationException;

public class UnifyHash extends AbstractCollection {
    /** 
     * the default capacity
     */
    private static final int DEFAULT_CAPACITY = 11;

    /** the default load factor of a HashMap */
    private static final float DEFAULT_LOAD_FACTOR = 0.75F;

///#ifdef JDK12
    private ReferenceQueue queue = new ReferenceQueue();
///#endif

    static class Bucket
///#ifdef JDK12
  extends WeakReference
///#endif
    {
///#ifdef JDK12
  public Bucket(Object o, ReferenceQueue q) {
      super(o, q);
  }
///#else
/// public Bucket(Object o) {
///     this.obj = o;
/// }
///
/// Object obj;
///
/// public Object get() {
///     return obj;
/// }
///#endif

  int hash;
  Bucket next;
    }

    private Bucket[] buckets;
    int modCount = 0;
    int size = 0;
    int threshold;
    float loadFactor;

    public UnifyHash(int initialCapacity, float loadFactor) {
  this.loadFactor = loadFactor;
  buckets = new Bucket[initialCapacity];
  threshold = (int) (loadFactor * initialCapacity);
    }

    public UnifyHash(int initialCapacity) {
  this(initialCapacity, DEFAULT_LOAD_FACTOR);
    }

    public UnifyHash() {
  this(DEFAULT_CAPACITY, DEFAULT_LOAD_FACTOR);
    }

    private void grow() {
  Bucket[] oldBuckets = buckets;
  int newCap = buckets.length * 1;
  threshold = (int) (loadFactor * newCap);
  buckets = new Bucket[newCap];
  for (int i = 0; i < oldBuckets.length; i++) {
      Bucket nextBucket;
      for (Bucket b = oldBuckets[i]; b != null; b = nextBucket) {
    if (i != Math.abs(b.hash % oldBuckets.length))
        throw new RuntimeException(""+i+", hash: "+b.hash+", oldlength: "+oldBuckets.length);
    int newSlot = Math.abs(b.hash % newCap);
    nextBucket = b.next;
    b.next = buckets[newSlot];
    buckets[newSlot= b;
      }
  }
    }

///#ifdef JDK12
    public final void cleanUp() {
  Bucket died;
  while ((died = (Bucket)queue.poll()) != null) {
      int diedSlot = Math.abs(died.hash % buckets.length);
      if (buckets[diedSlot== died)
    buckets[diedSlot= died.next;
      else {
    Bucket b = buckets[diedSlot];
    while (b.next != died)
        b = b.next;
    b.next = died.next;
      }
      size--;
  }
    }
///#endif


    public int size() {
  return size;
    }

    public Iterator iterator() {
///#ifdef JDK12
  cleanUp();
///#endif

  return new Iterator() {
      private int bucket = 0;
      private int known = modCount;
      private Bucket nextBucket;
      private Object nextVal;

      {
    internalNext();
      }

      private void internalNext() {
    while (true) {
        while (nextBucket == null) {
      if (bucket == buckets.length)
          return;
      nextBucket = buckets[bucket++];
        }
        
        nextVal = nextBucket.get();
        if (nextVal != null)
      return;

        nextBucket = nextBucket.next;
    }
      }

      public boolean hasNext() {
    return nextBucket != null;
      }

      public Object next() {
    if (known != modCount)
        throw new ConcurrentModificationException();
    if (nextBucket == null)
        throw new NoSuchElementException();
    Object result = nextVal;
    nextBucket = nextBucket.next;
    internalNext();
    return result;
      }

      public void remove() {
    throw new UnsupportedOperationException();
      }
  };
    }

    public Iterator iterateHashCode(final int hash) {
///#ifdef JDK12
  cleanUp();
///#endif
  return new Iterator() {
      private int known = modCount;
      private Bucket nextBucket
    = buckets[Math.abs(hash % buckets.length)];
      private Object nextVal;

      {
    internalNext();
      }

      private void internalNext() {
    while (nextBucket != null) {
        if (nextBucket.hash == hash) {
      nextVal = nextBucket.get();
      if (nextVal != null)
          return;
        }

        nextBucket = nextBucket.next;
    }
      }

      public boolean hasNext() {
    return nextBucket != null;
      }

      public Object next() {
    if (known != modCount)
        throw new ConcurrentModificationException();
    if (nextBucket == null)
        throw new NoSuchElementException();
    Object result = nextVal;
    nextBucket = nextBucket.next;
    internalNext();
    return result;
      }

      public void remove() {
    throw new UnsupportedOperationException();
      }
  };
    }

    public void put(int hash, Object o) {
  if (size++ > threshold)
      grow();
  modCount++;

  int slot = Math.abs(hash % buckets.length);
///#ifdef JDK12
  Bucket b = new Bucket(o, queue);
///#else
/// Bucket b = new Bucket(o);
///#endif
  b.hash = hash;
  b.next = buckets[slot];
  buckets[slot= b;
    }

    public Object unify(Object o, int hash, Comparator comparator) {
///#ifdef JDK12
  cleanUp();
///#endif
  int slot = Math.abs(hash % buckets.length);
  for (Bucket b = buckets[slot]; b != null; b = b.next) {
      Object old = b.get();
      if (old != null && comparator.compare(o, old== 0)
    return old;
  }

  put(hash, o);
  return o;
    }
}

   
    
    
    
    
  
Related examples in the same category
1. Computing hash codes
2. A hash-code generator and a collection of static hash-code generation methods.
3. MD5 hash generator
4. Hash 32 String
5. Hash 64 String
6. MD5 hashing: Encodes a string
7. MD5 String
8. Hash Code BuilderHash Code Builder
9. HashCode generationHashCode generation
10. Get hash code for primitive data types
11. Return as hash code for the given object
12. Null Safe Hash Code
13. A very efficient java hash algorithm, based on the BuzHash algoritm
14. Easy implementation of hashCode
15. An implementation of the HMACT64 keyed hashing algorithm
16. Gets the hash code of an object returning zero when the object is null
17. Secure Hash
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.