KickJava   Java API By Example, From Geeks To Geeks.

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


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 of {Object --> int[] }
15  */

16 public final class HashtableOfObjectToIntArray implements Cloneable JavaDoc {
17     
18     // to avoid using Enumerations, walk the individual tables skipping nulls
19
public Object JavaDoc[] keyTable;
20     public int[][] valueTable;
21
22     public int elementSize; // number of elements in the table
23
int threshold;
24
25     public HashtableOfObjectToIntArray() {
26         this(13);
27     }
28
29     public HashtableOfObjectToIntArray(int size) {
30
31         this.elementSize = 0;
32         this.threshold = size; // size represents the expected number of elements
33
int extraRoom = (int) (size * 1.75f);
34         if (this.threshold == extraRoom)
35             extraRoom++;
36         this.keyTable = new Object JavaDoc[extraRoom];
37         this.valueTable = new int[extraRoom][];
38     }
39
40     public Object JavaDoc clone() throws CloneNotSupportedException JavaDoc {
41         HashtableOfObjectToIntArray result = (HashtableOfObjectToIntArray) super.clone();
42         result.elementSize = this.elementSize;
43         result.threshold = this.threshold;
44
45         int length = this.keyTable.length;
46         result.keyTable = new Object JavaDoc[length];
47         System.arraycopy(this.keyTable, 0, result.keyTable, 0, length);
48
49         length = this.valueTable.length;
50         result.valueTable = new int[length][];
51         System.arraycopy(this.valueTable, 0, result.valueTable, 0, length);
52         return result;
53     }
54
55     public boolean containsKey(Object JavaDoc key) {
56         int length = this.keyTable.length,
57             index = (key.hashCode()& 0x7FFFFFFF) % length;
58         Object JavaDoc currentKey;
59         while ((currentKey = this.keyTable[index]) != null) {
60             if (currentKey.equals(key))
61                 return true;
62             if (++index == length) {
63                 index = 0;
64             }
65         }
66         return false;
67     }
68
69     public int[] get(Object JavaDoc key) {
70         int length = this.keyTable.length,
71             index = (key.hashCode()& 0x7FFFFFFF) % length;
72         Object JavaDoc currentKey;
73         while ((currentKey = this.keyTable[index]) != null) {
74             if (currentKey.equals(key))
75                 return this.valueTable[index];
76             if (++index == length) {
77                 index = 0;
78             }
79         }
80         return null;
81     }
82
83     public void keysToArray(Object JavaDoc[] array) {
84         int index = 0;
85         for (int i=0, length=this.keyTable.length; i<length; i++) {
86             if (this.keyTable[i] != null)
87                 array[index++] = this.keyTable[i];
88         }
89     }
90
91     public int[] put(Object JavaDoc key, int[] value) {
92         int length = this.keyTable.length,
93             index = (key.hashCode()& 0x7FFFFFFF) % length;
94         Object JavaDoc currentKey;
95         while ((currentKey = this.keyTable[index]) != null) {
96             if (currentKey.equals(key))
97                 return this.valueTable[index] = value;
98             if (++index == length) {
99                 index = 0;
100             }
101         }
102         this.keyTable[index] = key;
103         this.valueTable[index] = value;
104
105         // assumes the threshold is never equal to the size of the table
106
if (++elementSize > threshold)
107             rehash();
108         return value;
109     }
110
111     public int[] removeKey(Object JavaDoc key) {
112         int length = this.keyTable.length,
113             index = (key.hashCode()& 0x7FFFFFFF) % length;
114         Object JavaDoc currentKey;
115         while ((currentKey = this.keyTable[index]) != null) {
116             if (currentKey.equals(key)) {
117                 int[] value = this.valueTable[index];
118                 elementSize--;
119                 this.keyTable[index] = null;
120                 rehash();
121                 return value;
122             }
123             if (++index == length) {
124                 index = 0;
125             }
126         }
127         return null;
128     }
129
130     private void rehash() {
131
132         HashtableOfObjectToIntArray newHashtable = new HashtableOfObjectToIntArray(elementSize * 2); // double the number of expected elements
133
Object JavaDoc currentKey;
134         for (int i = this.keyTable.length; --i >= 0;)
135             if ((currentKey = this.keyTable[i]) != null)
136                 newHashtable.put(currentKey, this.valueTable[i]);
137
138         this.keyTable = newHashtable.keyTable;
139         this.valueTable = newHashtable.valueTable;
140         this.threshold = newHashtable.threshold;
141     }
142
143     public int size() {
144         return elementSize;
145     }
146     
147     public String JavaDoc toString() {
148         String JavaDoc s = ""; //$NON-NLS-1$
149
Object JavaDoc key;
150         for (int i = 0, length = this.keyTable.length; i < length; i++)
151             if ((key = this.keyTable[i]) != null)
152                 s += key + " -> " + this.valueTable[i] + "\n"; //$NON-NLS-2$ //$NON-NLS-1$
153
return s;
154     }
155 }
156
Popular Tags