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[];
24 public int valueTable[];
26 public int elementSize; // number of elements in the table
29 public HashtableOfIntValues() {
33 public HashtableOfIntValues(int size) {
36 this.threshold = size; // size represents the expected number of elements
37 int extraRoom = (int) (size * 1.75f);
38 if (this.threshold == extraRoom)
40 this.keyTable = new char[extraRoom][];
41 this.valueTable = new int[extraRoom];
44 public Object clone() throws CloneNotSupportedException {
45 HashtableOfIntValues result = (HashtableOfIntValues) super.clone();
46 result.elementSize = this.elementSize;
47 result.threshold = this.threshold;
49 int length = this.keyTable.length;
50 result.keyTable = new char[length][];
51 System.arraycopy(this.keyTable, 0, result.keyTable, 0, length);
53 length = this.valueTable.length;
54 result.valueTable = new int[length];
55 System.arraycopy(this.valueTable, 0, result.valueTable, 0, length);
59 public boolean containsKey(char[] key) {
61 int index = CharOperation.hashCode(key) % valueTable.length;
62 int keyLength = key.length;
64 while ((currentKey = keyTable[index]) != null) {
65 if (currentKey.length == keyLength && CharOperation.equals(currentKey, key))
67 index = (index + 1) % keyTable.length;
72 public int get(char[] key) {
74 int index = CharOperation.hashCode(key) % valueTable.length;
75 int keyLength = key.length;
77 while ((currentKey = keyTable[index]) != null) {
78 if (currentKey.length == keyLength && CharOperation.equals(currentKey, key))
79 return valueTable[index];
80 index = (index + 1) % keyTable.length;
85 public int put(char[] key, int value) {
87 int index = CharOperation.hashCode(key) % valueTable.length;
88 int keyLength = key.length;
90 while ((currentKey = keyTable[index]) != null) {
91 if (currentKey.length == keyLength && CharOperation.equals(currentKey, key))
92 return valueTable[index] = value;
93 index = (index + 1) % keyTable.length;
95 keyTable[index] = key;
96 valueTable[index] = value;
98 // assumes the threshold is never equal to the size of the table
99 if (++elementSize > threshold)
104 public int removeKey(char[] key) {
106 int index = CharOperation.hashCode(key) % valueTable.length;
107 int keyLength = key.length;
109 while ((currentKey = keyTable[index]) != null) {
110 if (currentKey.length == keyLength && CharOperation.equals(currentKey, key)) {
111 int value = valueTable[index];
113 keyTable[index] = null;
114 valueTable[index] = NO_VALUE;
118 index = (index + 1) % keyTable.length;
123 private void rehash() {
125 HashtableOfIntValues newHashtable = new HashtableOfIntValues(elementSize * 2); // double the number of expected elements
127 for (int i = keyTable.length; --i >= 0;)
128 if ((currentKey = keyTable[i]) != null)
129 newHashtable.put(currentKey, valueTable[i]);
131 this.keyTable = newHashtable.keyTable;
132 this.valueTable = newHashtable.valueTable;
133 this.threshold = newHashtable.threshold;
140 public String toString() {
141 String s = ""; //$NON-NLS-1$
143 for (int i = 0, length = valueTable.length; i < length; i++)
144 if ((key = keyTable[i]) != null)
145 s += new String(key) + " -> " + valueTable[i] + "\n"; //$NON-NLS-2$ //$NON-NLS-1$