KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > compiler > util > HashtableOfInt


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.compiler.util;
12  
13  /**
14   * Hashtable for non-zero int keys.
15   */

16   
17 public final class HashtableOfInt {
18     // to avoid using Enumerations, walk the individual tables skipping nulls
19
public int[] keyTable;
20     public Object JavaDoc[] valueTable;
21
22     public int elementSize; // number of elements in the table
23
int threshold;
24 public HashtableOfInt() {
25     this(13);
26 }
27 public HashtableOfInt(int size) {
28     this.elementSize = 0;
29     this.threshold = size; // size represents the expected number of elements
30
int extraRoom = (int) (size * 1.75f);
31     if (this.threshold == extraRoom)
32         extraRoom++;
33     this.keyTable = new int[extraRoom];
34     this.valueTable = new Object JavaDoc[extraRoom];
35 }
36 public boolean containsKey(int key) {
37     int length = keyTable.length, index = key % length;
38     int currentKey;
39     while ((currentKey = keyTable[index]) != 0) {
40         if (currentKey == key)
41             return true;
42         if (++index == length) {
43             index = 0;
44         }
45     }
46     return false;
47 }
48 public Object JavaDoc get(int key) {
49     int length = keyTable.length, index = key % length;
50     int currentKey;
51     while ((currentKey = keyTable[index]) != 0) {
52         if (currentKey == key) return valueTable[index];
53         if (++index == length) {
54             index = 0;
55         }
56     }
57     return null;
58 }
59 public Object JavaDoc put(int key, Object JavaDoc value) {
60     int length = keyTable.length, index = key % length;
61     int currentKey;
62     while ((currentKey = keyTable[index]) != 0) {
63         if (currentKey == key) return valueTable[index] = value;
64         if (++index == length) {
65             index = 0;
66         }
67     }
68     keyTable[index] = key;
69     valueTable[index] = value;
70
71     // assumes the threshold is never equal to the size of the table
72
if (++elementSize > threshold)
73         rehash();
74     return value;
75 }
76 private void rehash() {
77     HashtableOfInt newHashtable = new HashtableOfInt(elementSize * 2); // double the number of expected elements
78
int currentKey;
79     for (int i = keyTable.length; --i >= 0;)
80         if ((currentKey = keyTable[i]) != 0)
81             newHashtable.put(currentKey, valueTable[i]);
82
83     this.keyTable = newHashtable.keyTable;
84     this.valueTable = newHashtable.valueTable;
85     this.threshold = newHashtable.threshold;
86 }
87 public int size() {
88     return elementSize;
89 }
90 public String JavaDoc toString() {
91     String JavaDoc s = ""; //$NON-NLS-1$
92
Object JavaDoc object;
93     for (int i = 0, length = valueTable.length; i < length; i++)
94         if ((object = valueTable[i]) != null)
95             s += keyTable[i] + " -> " + object.toString() + "\n"; //$NON-NLS-2$ //$NON-NLS-1$
96
return s;
97 }
98 }
99
Popular Tags