IntMap provides a simple hashmap from keys to integers : Customized Map « Collections « Java Tutorial

Java Tutorial
1. Language
2. Data Type
3. Operators
4. Statement Control
5. Class Definition
6. Development
7. Reflection
8. Regular Expressions
9. Collections
10. Thread
11. File
12. Generics
13. I18N
14. Swing
15. Swing Event
16. 2D Graphics
17. SWT
18. SWT 2D Graphics
19. Network
20. Database
21. Hibernate
22. JPA
23. JSP
24. JSTL
25. Servlet
26. Web Services SOA
27. EJB3
28. Spring
29. PDF
30. Email
31. J2ME
32. J2EE Application
33. XML
34. Design Pattern
35. Log
36. Security
37. Apache Common
38. Ant
39. JUnit
Java
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 Tutorial » Collections » Customized Map 
9. 33. 18. IntMap provides a simple hashmap from keys to integers
/*
 * Copyright (c) 2001-2008 Caucho Technology, Inc.  All rights reserved.
 *
 * The Apache Software License, Version 1.1
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *
 * 3. The end-user documentation included with the redistribution, if
 *    any, must include the following acknowlegement:
 *       "This product includes software developed by the
 *        Caucho Technology (http://www.caucho.com/)."
 *    Alternately, this acknowlegement may appear in the software itself,
 *    if and wherever such third-party acknowlegements normally appear.
 *
 * 4. The names "Burlap", "Resin", and "Caucho" must not be used to
 *    endorse or promote products derived from this software without prior
 *    written permission. For written permission, please contact
 *    info@caucho.com.
 *
 * 5. Products derived from this software may not be called "Resin"
 *    nor may "Resin" appear in their names without prior written
 *    permission of Caucho Technology.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED.  IN NO EVENT SHALL CAUCHO TECHNOLOGY OR ITS CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * @author Scott Ferguson
 */


/**
 * The IntMap provides a simple hashmap from keys to integers.  The API is
 * an abbreviation of the HashMap collection API.
 *
 * The convenience of IntMap is avoiding all the silly wrapping of
 * integers.
 */
public class IdentityIntMap {
  /**
   * Encoding of a null entry.  Since NULL is equal to Integer.MIN_VALUE, 
   * it's impossible to distinguish between the two.
   */
  public final static int NULL = 0xdeadbeef// Integer.MIN_VALUE + 1;

  private static final Object DELETED = new Object();

  private Object []_keys;
  private int []_values;

  private int _size;
  private int _mask;

  /**
   * Create a new IntMap.  Default size is 16.
   */
  public IdentityIntMap()
  {
    _keys = new Object[256];
    _values = new int[256];

    _mask = _keys.length - 1;
    _size = 0;
  }

  /**
   * Clear the hashmap.
   */
  public void clear()
  {
    Object []keys = _keys;
    int []values = _values;

    for (int i = keys.length - 1; i >= 0; i--) {
      keys[inull;
      values[i0;
    }

    _size = 0;
  }
  /**
   * Returns the current number of entries in the map.
   */
  public int size()
  {
    return _size;
  }

  /**
   * Puts a new value in the property table with the appropriate flags
   */
  public int get(Object key)
  {
    int mask = _mask;
    int hash = System.identityHashCode(key% mask & mask;

    Object []keys = _keys;

    while (true) {
      Object mapKey = keys[hash];

      if (mapKey == null)
        return NULL;
      else if (mapKey == key)
        return _values[hash];

      hash = (hash + 1% mask;
    }
  }

  /**
   * Expands the property table
   */
  private void resize(int newSize)
  {
    Object []newKeys = new Object[newSize];
    int []newValues = new int[newSize];

    int mask = _mask = newKeys.length - 1;

    Object []keys = _keys;
    int values[] = _values;

    for (int i = keys.length - 1; i >= 0; i--) {
      Object key = keys[i];

      if (key == null || key == DELETED)
        continue;

      int hash = System.identityHashCode(key% mask & mask;

      while (true) {
        if (newKeys[hash== null) {
          newKeys[hash= key;
          newValues[hash= values[i];
          break;
        }

        hash = (hash + 1% mask;
      }
    }

    _keys = newKeys;
    _values = newValues;
  }

  /**
   * Puts a new value in the property table with the appropriate flags
   */
  public int put(Object key, int value)
  {
    int mask = _mask;
    int hash = System.identityHashCode(key% mask & mask;

    Object []keys = _keys;

    while (true) {
      Object testKey = keys[hash];

      if (testKey == null || testKey == DELETED) {
        keys[hash= key;
        _values[hash= value;

        _size++;

        if (keys.length <= * _size)
          resize(* keys.length);

        return NULL;
      }
      else if (key != testKey) {
        hash = (hash + 1% mask;

        continue;
      }
      else {
        int old = _values[hash];

        _values[hash= value;

        return old;
      }
    }
  }

  /**
   * Deletes the entry.  Returns true if successful.
   */
  public int remove(Object key)
  {
    int mask = _mask;
    int hash = System.identityHashCode(key% mask & mask;

    while (true) {
      Object mapKey = _keys[hash];

      if (mapKey == null)
        return NULL;
      else if (mapKey == key) {
        _keys[hash= DELETED;

        _size--;

        return _values[hash];
      }

      hash = (hash + 1% mask;
    }
  }

  public String toString()
  {
    StringBuffer sbuf = new StringBuffer();

    sbuf.append("IntMap[");
    boolean isFirst = true;

    for (int i = 0; i <= _mask; i++) {
      if (_keys[i!= null && _keys[i!= DELETED) {
        if (! isFirst)
          sbuf.append(", ");

        isFirst = false;
        sbuf.append(_keys[i]);
        sbuf.append(":");
        sbuf.append(_values[i]);
      }
    }
    sbuf.append("]");

    return sbuf.toString();
  }
}
9. 33. Customized Map
9. 33. 1. Implementation of a bit map of any size, together with static methods to manipulate int, byte and byte[] values as bit maps
9. 33. 2. A fixed size map implementation.
9. 33. 3. A memory-efficient hash map.
9. 33. 4. CaseBlindHashMap - a HashMap extension, using Strings as key values.
9. 33. 5. Clones a map and prefixes the keys in the clone
9. 33. 6. Converts array into a java.util.Map.
9. 33. 7. Ordered Map
9. 33. 8. A hash map that uses primitive ints for the key rather than objects.
9. 33. 9. HashNMap stores multiple values by a single key value. Values can be retrieved using a direct query or by creating an enumeration over the stored elements.
9. 33. 10. A Map where keys are compared by object identity, rather than equals()
9. 33. 11. A java.util.Map implementation using reference values
9. 33. 12. A simple hashmap from keys to integers
9. 33. 13. An IdentityMap that uses reference-equality instead of object-equality
9. 33. 14. Creates a mutable map from two arrays with keys and values
9. 33. 15. Fixed size hash map using String values as keys mapped to primitive int values.
9. 33. 16. Hash map for counting references to Object keys.
9. 33. 17. Hash map using String values as keys mapped to primitive int values.
9. 33. 18. IntMap provides a simple hashmap from keys to integers
9. 33. 19. List ordered map
9. 33. 20. Lookup table that stores a list of strings
9. 33. 21. Map implementation Optimized for Strings keys
9. 33. 22. Map with keys iterated in insertion order
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.