KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > internal > registry > HashtableOfInt


1 /*******************************************************************************
2  * Copyright (c) 2004, 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.core.internal.registry;
12
13 import java.io.*;
14
15 /**
16  * Hashtable for non-zero int keys.
17  */

18
19 public final class HashtableOfInt {
20     private int[] keyTable;
21     private int[] valueTable;
22     private static final float GROWTH_FACTOR = 1.33f;
23
24     private int elementSize; // number of elements in the table
25
private 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 elements
34
int extraRoom = (int) (size * 1.33f);
35         if (this.threshold == extraRoom)
36             extraRoom++;
37         this.keyTable = new int[extraRoom];
38         this.valueTable = new int[extraRoom];
39     }
40
41     public boolean containsKey(int key) {
42         int index = key % valueTable.length;
43         int currentKey;
44         while ((currentKey = keyTable[index]) != 0) {
45             if (currentKey == key)
46                 return true;
47             index = (index + 1) % keyTable.length;
48         }
49         return false;
50     }
51
52     public int get(int key) {
53         int index = key % valueTable.length;
54         int currentKey;
55         while ((currentKey = keyTable[index]) != 0) {
56             if (currentKey == key)
57                 return valueTable[index];
58             index = (index + 1) % keyTable.length;
59         }
60         return Integer.MIN_VALUE;
61     }
62
63     public int removeKey(int key) {
64         int index = key % valueTable.length;
65         int currentKey;
66         while ((currentKey = keyTable[index]) != 0) {
67             if (currentKey == key) {
68                 return valueTable[index];
69             }
70             index = (index + 1) % keyTable.length;
71         }
72         return Integer.MIN_VALUE;
73     }
74
75     public int put(int key, int value) {
76         int index = key % valueTable.length;
77         int currentKey;
78         while ((currentKey = keyTable[index]) != 0) {
79             if (currentKey == key)
80                 return valueTable[index] = value;
81             index = (index + 1) % keyTable.length;
82         }
83         keyTable[index] = key;
84         valueTable[index] = value;
85
86         // assumes the threshold is never equal to the size of the table
87
if (++elementSize > threshold)
88             rehash();
89         return value;
90     }
91
92     private void rehash() {
93         HashtableOfInt newHashtable = new HashtableOfInt((int) (elementSize * GROWTH_FACTOR)); // double the number of expected elements
94
int currentKey;
95         for (int i = keyTable.length; --i >= 0;)
96             if ((currentKey = keyTable[i]) != 0)
97                 newHashtable.put(currentKey, valueTable[i]);
98
99         this.keyTable = newHashtable.keyTable;
100         this.valueTable = newHashtable.valueTable;
101         this.threshold = newHashtable.threshold;
102     }
103
104     public int size() {
105         return elementSize;
106     }
107
108     public String JavaDoc toString() {
109         String JavaDoc s = ""; //$NON-NLS-1$
110
int object;
111         for (int i = 0, length = valueTable.length; i < length; i++)
112             if ((object = valueTable[i]) != Integer.MIN_VALUE)
113                 s += keyTable[i] + " -> " + object + "\n"; //$NON-NLS-2$ //$NON-NLS-1$
114
return s;
115     }
116
117     public void save(DataOutputStream out) throws IOException {
118         out.writeInt(elementSize);
119         int tableSize = keyTable.length;
120         out.writeInt(tableSize);
121         out.writeInt(threshold);
122         for (int i = 0; i < tableSize; i++) {
123             out.writeInt(keyTable[i]);
124             out.writeInt(valueTable[i]);
125         }
126     }
127
128     public void load(DataInputStream in) throws IOException {
129         elementSize = in.readInt();
130         int tableSize = in.readInt();
131         threshold = in.readInt();
132         boolean fastMode = true;
133         if (((double) tableSize / elementSize) < GROWTH_FACTOR) {
134             keyTable = new int[(int) (elementSize * GROWTH_FACTOR)];
135             valueTable = new int[(int) (elementSize * GROWTH_FACTOR)];
136             elementSize = 0;
137             fastMode = false;
138         } else {
139             keyTable = new int[tableSize];
140             valueTable = new int[tableSize];
141         }
142         for (int i = 0; i < tableSize; i++) {
143             int key = in.readInt();
144             int value = in.readInt();
145             if (fastMode) {
146                 keyTable[i] = key;
147                 valueTable[i] = value;
148             } else {
149                 put(key, value);
150             }
151         }
152     }
153 }
154
Popular Tags