KickJava   Java API By Example, From Geeks To Geeks.

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


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 long keys.
15   */

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