001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.jdt.internal.compiler.util;
011:
012: /**
013: * Hashtable for non-zero int keys.
014: */
015:
016: public final class HashtableOfInt {
017: // to avoid using Enumerations, walk the individual tables skipping nulls
018: public int[] keyTable;
019: public Object[] valueTable;
020:
021: public int elementSize; // number of elements in the table
022: int threshold;
023:
024: public HashtableOfInt() {
025: this (13);
026: }
027:
028: public HashtableOfInt(int size) {
029: this .elementSize = 0;
030: this .threshold = size; // size represents the expected number of elements
031: int extraRoom = (int) (size * 1.75f);
032: if (this .threshold == extraRoom)
033: extraRoom++;
034: this .keyTable = new int[extraRoom];
035: this .valueTable = new Object[extraRoom];
036: }
037:
038: public boolean containsKey(int key) {
039: int length = keyTable.length, index = key % length;
040: int currentKey;
041: while ((currentKey = keyTable[index]) != 0) {
042: if (currentKey == key)
043: return true;
044: if (++index == length) {
045: index = 0;
046: }
047: }
048: return false;
049: }
050:
051: public Object get(int key) {
052: int length = keyTable.length, index = key % length;
053: int currentKey;
054: while ((currentKey = keyTable[index]) != 0) {
055: if (currentKey == key)
056: return valueTable[index];
057: if (++index == length) {
058: index = 0;
059: }
060: }
061: return null;
062: }
063:
064: public Object put(int key, Object value) {
065: int length = keyTable.length, index = key % length;
066: int currentKey;
067: while ((currentKey = keyTable[index]) != 0) {
068: if (currentKey == key)
069: return valueTable[index] = value;
070: if (++index == length) {
071: index = 0;
072: }
073: }
074: keyTable[index] = key;
075: valueTable[index] = value;
076:
077: // assumes the threshold is never equal to the size of the table
078: if (++elementSize > threshold)
079: rehash();
080: return value;
081: }
082:
083: private void rehash() {
084: HashtableOfInt newHashtable = new HashtableOfInt(
085: elementSize * 2); // double the number of expected elements
086: int currentKey;
087: for (int i = keyTable.length; --i >= 0;)
088: if ((currentKey = keyTable[i]) != 0)
089: newHashtable.put(currentKey, valueTable[i]);
090:
091: this .keyTable = newHashtable.keyTable;
092: this .valueTable = newHashtable.valueTable;
093: this .threshold = newHashtable.threshold;
094: }
095:
096: public int size() {
097: return elementSize;
098: }
099:
100: public String toString() {
101: String s = ""; //$NON-NLS-1$
102: Object object;
103: for (int i = 0, length = valueTable.length; i < length; i++)
104: if ((object = valueTable[i]) != null)
105: s += keyTable[i] + " -> " + object.toString() + "\n"; //$NON-NLS-2$ //$NON-NLS-1$
106: return s;
107: }
108: }
|