KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > core > builder > StringSet


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.core.builder;
12
13 public class StringSet {
14
15 // to avoid using Enumerations, walk the individual values skipping nulls
16
public String JavaDoc[] values;
17 public int elementSize; // number of elements in the table
18
public int threshold;
19
20 public StringSet(int size) {
21     this.elementSize = 0;
22     this.threshold = size; // size represents the expected number of elements
23
int extraRoom = (int) (size * 1.5f);
24     if (this.threshold == extraRoom)
25         extraRoom++;
26     this.values = new String JavaDoc[extraRoom];
27 }
28
29 public boolean add(String JavaDoc value) {
30     int length = this.values.length;
31     int index = (value.hashCode() & 0x7FFFFFFF) % length;
32     String JavaDoc current;
33     while ((current = this.values[index]) != null) {
34         if (value.equals(current)) return false; // did not add it since it already existed
35
if (++index == length) index = 0;
36     }
37     this.values[index] = value;
38
39     // assumes the threshold is never equal to the size of the table
40
if (++elementSize > threshold) rehash();
41     return true;
42 }
43
44 public void clear() {
45     for (int i = this.values.length; --i >= 0;)
46         this.values[i] = null;
47     this.elementSize = 0;
48 }
49
50 public boolean includes(String JavaDoc value) {
51     int length = values.length;
52     int index = (value.hashCode() & 0x7FFFFFFF) % length;
53     String JavaDoc current;
54     while ((current = this.values[index]) != null) {
55         if (value.equals(current)) return true;
56         if (++index == length) index = 0;
57     }
58     return false;
59 }
60
61 private void rehash() {
62     StringSet newSet = new StringSet(elementSize * 2); // double the number of expected elements
63
String JavaDoc current;
64     for (int i = this.values.length; --i >= 0;)
65         if ((current = this.values[i]) != null)
66             newSet.add(current);
67
68     this.values = newSet.values;
69     this.elementSize = newSet.elementSize;
70     this.threshold = newSet.threshold;
71 }
72
73 public String JavaDoc toString() {
74     String JavaDoc s = ""; //$NON-NLS-1$
75
String JavaDoc value;
76     for (int i = 0, l = this.values.length; i < l; i++)
77         if ((value = this.values[i]) != null)
78             s += value + "\n"; //$NON-NLS-1$
79
return s;
80 }
81 }
82
Popular Tags