1 /*******************************************************************************
2 * Copyright (c) 2000, 2003 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Common Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/cpl-v10.html
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package net.sourceforge.phpdt.internal.compiler.util;
14 * Hashtable for non-zero long keys.
17 public final class HashtableOfLong {
18 // to avoid using Enumerations, walk the individual tables skipping nulls
19 public long[] keyTable;
21 public Object[] valueTable;
23 int elementSize; // number of elements in the table
27 public HashtableOfLong() {
31 public HashtableOfLong(int size) {
33 this.threshold = size; // size represents the expected number of
35 int extraRoom = (int) (size * 1.75f);
36 if (this.threshold == extraRoom)
38 this.keyTable = new long[extraRoom];
39 this.valueTable = new Object[extraRoom];
42 public boolean containsKey(long key) {
43 int index = ((int) (key >>> 32)) % valueTable.length;
45 while ((currentKey = keyTable[index]) != 0) {
46 if (currentKey == key)
48 index = (index + 1) % keyTable.length;
53 public Object get(long key) {
54 int index = ((int) (key >>> 32)) % valueTable.length;
56 while ((currentKey = keyTable[index]) != 0) {
57 if (currentKey == key)
58 return valueTable[index];
59 index = (index + 1) % keyTable.length;
64 public Object put(long key, Object value) {
65 int index = ((int) (key >>> 32)) % valueTable.length;
67 while ((currentKey = keyTable[index]) != 0) {
68 if (currentKey == key)
69 return valueTable[index] = value;
70 index = (index + 1) % keyTable.length;
72 keyTable[index] = key;
73 valueTable[index] = value;
75 // assumes the threshold is never equal to the size of the table
76 if (++elementSize > threshold)
81 private void rehash() {
82 HashtableOfLong newHashtable = new HashtableOfLong(elementSize * 2); // double
89 for (int i = keyTable.length; --i >= 0;)
90 if ((currentKey = keyTable[i]) != 0)
91 newHashtable.put(currentKey, valueTable[i]);
93 this.keyTable = newHashtable.keyTable;
94 this.valueTable = newHashtable.valueTable;
95 this.threshold = newHashtable.threshold;
102 public String toString() {
103 String s = ""; //$NON-NLS-1$
105 for (int i = 0, length = valueTable.length; i < length; i++)
106 if ((object = valueTable[i]) != null)
107 s += keyTable[i] + " -> " + object.toString() + "\n"; //$NON-NLS-2$ //$NON-NLS-1$