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 of {Object --> int }
014: */
015: public final class HashtableOfObjectToInt implements Cloneable {
016:
017: // to avoid using Enumerations, walk the individual tables skipping nulls
018: public Object[] keyTable;
019: public int[] valueTable;
020:
021: public int elementSize; // number of elements in the table
022: int threshold;
023:
024: public HashtableOfObjectToInt() {
025: this (13);
026: }
027:
028: public HashtableOfObjectToInt(int size) {
029:
030: this .elementSize = 0;
031: this .threshold = size; // size represents the expected number of elements
032: int extraRoom = (int) (size * 1.75f);
033: if (this .threshold == extraRoom)
034: extraRoom++;
035: this .keyTable = new Object[extraRoom];
036: this .valueTable = new int[extraRoom];
037: }
038:
039: public Object clone() throws CloneNotSupportedException {
040: HashtableOfObjectToInt result = (HashtableOfObjectToInt) super
041: .clone();
042: result.elementSize = this .elementSize;
043: result.threshold = this .threshold;
044:
045: int length = this .keyTable.length;
046: result.keyTable = new Object[length];
047: System.arraycopy(this .keyTable, 0, result.keyTable, 0, length);
048:
049: length = this .valueTable.length;
050: result.valueTable = new int[length];
051: System.arraycopy(this .valueTable, 0, result.valueTable, 0,
052: length);
053: return result;
054: }
055:
056: public boolean containsKey(Object key) {
057: int length = this .keyTable.length, index = (key.hashCode() & 0x7FFFFFFF)
058: % length;
059: Object currentKey;
060: while ((currentKey = this .keyTable[index]) != null) {
061: if (currentKey.equals(key))
062: return true;
063: if (++index == length) {
064: index = 0;
065: }
066: }
067: return false;
068: }
069:
070: public int get(Object key) {
071: int length = this .keyTable.length, index = (key.hashCode() & 0x7FFFFFFF)
072: % length;
073: Object currentKey;
074: while ((currentKey = this .keyTable[index]) != null) {
075: if (currentKey.equals(key))
076: return this .valueTable[index];
077: if (++index == length) {
078: index = 0;
079: }
080: }
081: return -1;
082: }
083:
084: public void keysToArray(Object[] array) {
085: int index = 0;
086: for (int i = 0, length = this .keyTable.length; i < length; i++) {
087: if (this .keyTable[i] != null)
088: array[index++] = this .keyTable[i];
089: }
090: }
091:
092: public int put(Object key, int value) {
093: int length = this .keyTable.length, index = (key.hashCode() & 0x7FFFFFFF)
094: % length;
095: Object currentKey;
096: while ((currentKey = this .keyTable[index]) != null) {
097: if (currentKey.equals(key))
098: return this .valueTable[index] = value;
099: if (++index == length) {
100: index = 0;
101: }
102: }
103: this .keyTable[index] = key;
104: this .valueTable[index] = value;
105:
106: // assumes the threshold is never equal to the size of the table
107: if (++elementSize > threshold)
108: rehash();
109: return value;
110: }
111:
112: public int removeKey(Object key) {
113: int length = this .keyTable.length, index = (key.hashCode() & 0x7FFFFFFF)
114: % length;
115: Object currentKey;
116: while ((currentKey = this .keyTable[index]) != null) {
117: if (currentKey.equals(key)) {
118: int value = this .valueTable[index];
119: elementSize--;
120: this .keyTable[index] = null;
121: rehash();
122: return value;
123: }
124: if (++index == length) {
125: index = 0;
126: }
127: }
128: return -1;
129: }
130:
131: private void rehash() {
132:
133: HashtableOfObjectToInt newHashtable = new HashtableOfObjectToInt(
134: elementSize * 2); // double the number of expected elements
135: Object currentKey;
136: for (int i = this .keyTable.length; --i >= 0;)
137: if ((currentKey = this .keyTable[i]) != null)
138: newHashtable.put(currentKey, this .valueTable[i]);
139:
140: this .keyTable = newHashtable.keyTable;
141: this .valueTable = newHashtable.valueTable;
142: this .threshold = newHashtable.threshold;
143: }
144:
145: public int size() {
146: return elementSize;
147: }
148:
149: public String toString() {
150: String s = ""; //$NON-NLS-1$
151: Object key;
152: for (int i = 0, length = this .keyTable.length; i < length; i++)
153: if ((key = this .keyTable[i]) != null)
154: s += key + " -> " + this .valueTable[i] + "\n"; //$NON-NLS-2$ //$NON-NLS-1$
155: return s;
156: }
157: }
|