Makefile fixup
[org.ibex.tool.git] / src / org / eclipse / jdt / internal / compiler / util / HashtableOfInt.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 2004 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 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[] 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[extraRoom];
35 }
36 public boolean containsKey(int key) {
37         int index = key % valueTable.length;
38         int currentKey;
39         while ((currentKey = keyTable[index]) != 0) {
40                 if (currentKey == key)
41                         return true;
42                 index = (index + 1) % keyTable.length;
43         }
44         return false;
45 }
46 public Object get(int key) {
47         int index = key % valueTable.length;
48         int currentKey;
49         while ((currentKey = keyTable[index]) != 0) {
50                 if (currentKey == key)  return valueTable[index];
51                 index = (index + 1) % keyTable.length;
52         }
53         return null;
54 }
55 public Object put(int key, Object value) {
56         int index = key % valueTable.length;
57         int currentKey;
58         while ((currentKey = keyTable[index]) != 0) {
59                 if (currentKey == key)  return valueTable[index] = value;
60                 index = (index + 1) % keyTable.length;
61         }
62         keyTable[index] = key;
63         valueTable[index] = value;
64
65         // assumes the threshold is never equal to the size of the table
66         if (++elementSize > threshold)
67                 rehash();
68         return value;
69 }
70 private void rehash() {
71         HashtableOfInt newHashtable = new HashtableOfInt(elementSize * 2); // double the number of expected elements
72         int currentKey;
73         for (int i = keyTable.length; --i >= 0;)
74                 if ((currentKey = keyTable[i]) != 0)
75                         newHashtable.put(currentKey, valueTable[i]);
76
77         this.keyTable = newHashtable.keyTable;
78         this.valueTable = newHashtable.valueTable;
79         this.threshold = newHashtable.threshold;
80 }
81 public int size() {
82         return elementSize;
83 }
84 public String toString() {
85         String s = ""; //$NON-NLS-1$
86         Object object;
87         for (int i = 0, length = valueTable.length; i < length; i++)
88                 if ((object = valueTable[i]) != null)
89                         s += keyTable[i] + " -> " + object.toString() + "\n"; //$NON-NLS-2$ //$NON-NLS-1$
90         return s;
91 }
92 }