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.core.util;
13 import net.sourceforge.phpdt.core.compiler.CharOperation;
15 public final class SimpleWordSet {
17 // to avoid using Enumerations, walk the individual values skipping nulls
18 public char[][] words;
20 public int elementSize; // number of elements in the table
24 public SimpleWordSet(int size) {
26 this.threshold = size; // size represents the expected number of
28 int extraRoom = (int) (size * 1.5f);
29 if (this.threshold == extraRoom)
31 this.words = new char[extraRoom][];
34 public char[] add(char[] word) {
35 int length = this.words.length;
36 int index = CharOperation.hashCode(word) % length;
38 while ((current = words[index]) != null) {
39 if (CharOperation.equals(current, word))
41 if (++index == length)
46 // assumes the threshold is never equal to the size of the table
47 if (++elementSize > threshold)
52 public boolean includes(char[] word) {
53 int length = this.words.length;
54 int index = CharOperation.hashCode(word) % length;
56 while ((current = words[index]) != null) {
57 if (CharOperation.equals(current, word))
59 if (++index == length)
65 private void rehash() {
66 SimpleWordSet newSet = new SimpleWordSet(elementSize * 2); // double
72 for (int i = words.length; --i >= 0;)
73 if ((current = words[i]) != null)
76 this.words = newSet.words;
77 this.elementSize = newSet.elementSize;
78 this.threshold = newSet.threshold;