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 long keys.
014: */
015:
016: public final class HashtableOfLong {
017: // to avoid using Enumerations, walk the individual tables skipping nulls
018: public long[] keyTable;
019: public Object[] valueTable;
020:
021: public int elementSize; // number of elements in the table
022: int threshold;
023:
024: public HashtableOfLong() {
025: this (13);
026: }
027:
028: public HashtableOfLong(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 long[extraRoom];
035: this .valueTable = new Object[extraRoom];
036: }
037:
038: public boolean containsKey(long key) {
039: int length = keyTable.length, index = ((int) (key >>> 32))
040: % length;
041: long currentKey;
042: while ((currentKey = keyTable[index]) != 0) {
043: if (currentKey == key)
044: return true;
045: if (++index == length) {
046: index = 0;
047: }
048: }
049: return false;
050: }
051:
052: public Object get(long key) {
053: int length = keyTable.length, index = ((int) (key >>> 32))
054: % length;
055: long currentKey;
056: while ((currentKey = keyTable[index]) != 0) {
057: if (currentKey == key)
058: return valueTable[index];
059: if (++index == length) {
060: index = 0;
061: }
062: }
063: return null;
064: }
065:
066: public Object put(long key, Object value) {
067: int length = keyTable.length, index = ((int) (key >>> 32))
068: % length;
069: long currentKey;
070: while ((currentKey = keyTable[index]) != 0) {
071: if (currentKey == key)
072: return valueTable[index] = value;
073: if (++index == length) {
074: index = 0;
075: }
076: }
077: keyTable[index] = key;
078: valueTable[index] = value;
079:
080: // assumes the threshold is never equal to the size of the table
081: if (++elementSize > threshold)
082: rehash();
083: return value;
084: }
085:
086: private void rehash() {
087: HashtableOfLong newHashtable = new HashtableOfLong(
088: elementSize * 2); // double the number of expected elements
089: long currentKey;
090: for (int i = keyTable.length; --i >= 0;)
091: if ((currentKey = keyTable[i]) != 0)
092: newHashtable.put(currentKey, valueTable[i]);
093:
094: this .keyTable = newHashtable.keyTable;
095: this .valueTable = newHashtable.valueTable;
096: this .threshold = newHashtable.threshold;
097: }
098:
099: public int size() {
100: return elementSize;
101: }
102:
103: public String toString() {
104: String s = ""; //$NON-NLS-1$
105: Object object;
106: for (int i = 0, length = valueTable.length; i < length; i++)
107: if ((object = valueTable[i]) != null)
108: s += keyTable[i] + " -> " + object.toString() + "\n"; //$NON-NLS-2$ //$NON-NLS-1$
109: return s;
110: }
111: }
|