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
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package net.sourceforge.phpdt.internal.compiler.util;
13 import net.sourceforge.phpdt.core.compiler.CharOperation;
16 * Hashtable of {char[] --> int}
18 public final class HashtableOfIntValues implements Cloneable {
20 public static final int NO_VALUE = Integer.MIN_VALUE;
22 // to avoid using Enumerations, walk the individual tables skipping nulls
23 public char[] keyTable[];
25 public int valueTable[];
27 public int elementSize; // number of elements in the table
31 public HashtableOfIntValues() {
35 public HashtableOfIntValues(int size) {
38 this.threshold = size; // size represents the expected number of
40 int extraRoom = (int) (size * 1.75f);
41 if (this.threshold == extraRoom)
43 this.keyTable = new char[extraRoom][];
44 this.valueTable = new int[extraRoom];
47 public Object clone() throws CloneNotSupportedException {
48 HashtableOfIntValues result = (HashtableOfIntValues) super.clone();
49 result.elementSize = this.elementSize;
50 result.threshold = this.threshold;
52 int length = this.keyTable.length;
53 result.keyTable = new char[length][];
54 System.arraycopy(this.keyTable, 0, result.keyTable, 0, length);
56 length = this.valueTable.length;
57 result.valueTable = new int[length];
58 System.arraycopy(this.valueTable, 0, result.valueTable, 0, length);
62 public boolean containsKey(char[] key) {
64 int index = CharOperation.hashCode(key) % valueTable.length;
65 int keyLength = key.length;
67 while ((currentKey = keyTable[index]) != null) {
68 if (currentKey.length == keyLength
69 && CharOperation.equals(currentKey, key))
71 index = (index + 1) % keyTable.length;
76 public int get(char[] key) {
78 int index = CharOperation.hashCode(key) % valueTable.length;
79 int keyLength = key.length;
81 while ((currentKey = keyTable[index]) != null) {
82 if (currentKey.length == keyLength
83 && CharOperation.equals(currentKey, key))
84 return valueTable[index];
85 index = (index + 1) % keyTable.length;
90 public int put(char[] key, int value) {
92 int index = CharOperation.hashCode(key) % valueTable.length;
93 int keyLength = key.length;
95 while ((currentKey = keyTable[index]) != null) {
96 if (currentKey.length == keyLength
97 && CharOperation.equals(currentKey, key))
98 return valueTable[index] = value;
99 index = (index + 1) % keyTable.length;
101 keyTable[index] = key;
102 valueTable[index] = value;
104 // assumes the threshold is never equal to the size of the table
105 if (++elementSize > threshold)
110 public int removeKey(char[] key) {
112 int index = CharOperation.hashCode(key) % valueTable.length;
113 int keyLength = key.length;
115 while ((currentKey = keyTable[index]) != null) {
116 if (currentKey.length == keyLength
117 && CharOperation.equals(currentKey, key)) {
118 int value = valueTable[index];
120 keyTable[index] = null;
121 valueTable[index] = NO_VALUE;
125 index = (index + 1) % keyTable.length;
130 private void rehash() {
132 HashtableOfIntValues newHashtable = new HashtableOfIntValues(
133 elementSize * 2); // double the number of expected elements
135 for (int i = keyTable.length; --i >= 0;)
136 if ((currentKey = keyTable[i]) != null)
137 newHashtable.put(currentKey, valueTable[i]);
139 this.keyTable = newHashtable.keyTable;
140 this.valueTable = newHashtable.valueTable;
141 this.threshold = newHashtable.threshold;
148 public String toString() {
149 String s = ""; //$NON-NLS-1$
151 for (int i = 0, length = valueTable.length; i < length; i++)
152 if ((key = keyTable[i]) != null)
153 s += new String(key) + " -> " + valueTable[i] + "\n"; //$NON-NLS-2$ //$NON-NLS-1$