Templates were updated by pteague. Also a little change by ed_mann.
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / compiler / util / HashtableOfInt.java
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
7  * 
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package net.sourceforge.phpdt.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
21         public Object[] valueTable;
22
23         int elementSize; // number of elements in the table
24
25         int threshold;
26
27         public HashtableOfInt() {
28                 this(13);
29         }
30
31         public HashtableOfInt(int size) {
32                 this.elementSize = 0;
33                 this.threshold = size; // size represents the expected number of
34                                                                 // elements
35                 int extraRoom = (int) (size * 1.75f);
36                 if (this.threshold == extraRoom)
37                         extraRoom++;
38                 this.keyTable = new int[extraRoom];
39                 this.valueTable = new Object[extraRoom];
40         }
41
42         public boolean containsKey(int key) {
43                 int index = key % valueTable.length;
44                 int currentKey;
45                 while ((currentKey = keyTable[index]) != 0) {
46                         if (currentKey == key)
47                                 return true;
48                         index = (index + 1) % keyTable.length;
49                 }
50                 return false;
51         }
52
53         public Object get(int key) {
54                 int index = key % valueTable.length;
55                 int currentKey;
56                 while ((currentKey = keyTable[index]) != 0) {
57                         if (currentKey == key)
58                                 return valueTable[index];
59                         index = (index + 1) % keyTable.length;
60                 }
61                 return null;
62         }
63
64         public Object put(int key, Object value) {
65                 int index = key % valueTable.length;
66                 int currentKey;
67                 while ((currentKey = keyTable[index]) != 0) {
68                         if (currentKey == key)
69                                 return valueTable[index] = value;
70                         index = (index + 1) % keyTable.length;
71                 }
72                 keyTable[index] = key;
73                 valueTable[index] = value;
74
75                 // assumes the threshold is never equal to the size of the table
76                 if (++elementSize > threshold)
77                         rehash();
78                 return value;
79         }
80
81         private void rehash() {
82                 HashtableOfInt newHashtable = new HashtableOfInt(elementSize * 2); // double
83                                                                                                                                                         // the
84                                                                                                                                                         // number
85                                                                                                                                                         // of
86                                                                                                                                                         // expected
87                                                                                                                                                         // elements
88                 int currentKey;
89                 for (int i = keyTable.length; --i >= 0;)
90                         if ((currentKey = keyTable[i]) != 0)
91                                 newHashtable.put(currentKey, valueTable[i]);
92
93                 this.keyTable = newHashtable.keyTable;
94                 this.valueTable = newHashtable.valueTable;
95                 this.threshold = newHashtable.threshold;
96         }
97
98         public int size() {
99                 return elementSize;
100         }
101
102         public String toString() {
103                 String s = ""; //$NON-NLS-1$
104                 Object object;
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$
108                 return s;
109         }
110 }