1 /*******************************************************************************
2 * Copyright (c) 2000, 2001, 2002 International Business Machines Corp. and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Common Public License v0.5
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/cpl-v05.html
9 * IBM Corporation - initial API and implementation
10 ******************************************************************************/
11 package net.sourceforge.phpdt.internal.compiler.util;
14 * Hashtable for non-zero int keys.
17 public final class HashtableOfInt {
18 // to avoid using Enumerations, walk the individual tables skipping nulls
19 public int[] keyTable;
20 public Object[] valueTable;
22 int elementSize; // number of elements in the table
24 public HashtableOfInt() {
27 public HashtableOfInt(int size) {
29 this.threshold = size; // size represents the expected number of elements
30 int extraRoom = (int) (size * 1.75f);
31 if (this.threshold == extraRoom)
33 this.keyTable = new int[extraRoom];
34 this.valueTable = new Object[extraRoom];
36 public boolean containsKey(int key) {
37 int index = key % valueTable.length;
39 while ((currentKey = keyTable[index]) != 0) {
40 if (currentKey == key)
42 index = (index + 1) % keyTable.length;
46 public Object get(int key) {
47 int index = key % valueTable.length;
49 while ((currentKey = keyTable[index]) != 0) {
50 if (currentKey == key) return valueTable[index];
51 index = (index + 1) % keyTable.length;
55 public Object put(int key, Object value) {
56 int index = key % valueTable.length;
58 while ((currentKey = keyTable[index]) != 0) {
59 if (currentKey == key) return valueTable[index] = value;
60 index = (index + 1) % keyTable.length;
62 keyTable[index] = key;
63 valueTable[index] = value;
65 // assumes the threshold is never equal to the size of the table
66 if (++elementSize > threshold)
70 private void rehash() {
71 HashtableOfInt newHashtable = new HashtableOfInt(elementSize * 2); // double the number of expected elements
73 for (int i = keyTable.length; --i >= 0;)
74 if ((currentKey = keyTable[i]) != 0)
75 newHashtable.put(currentKey, valueTable[i]);
77 this.keyTable = newHashtable.keyTable;
78 this.valueTable = newHashtable.valueTable;
79 this.threshold = newHashtable.threshold;
84 public String toString() {
85 String s = ""; //$NON-NLS-1$
87 for (int i = 0, length = valueTable.length; i < length; i++)
88 if ((object = valueTable[i]) != null)
89 s += keyTable[i] + " -> " + object.toString() + "\n"; //$NON-NLS-2$ //$NON-NLS-1$