KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > core > util > HashSetOfArray


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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.util;
12
13 /**
14  * HashSet of Object[]
15  */

16 public final class HashSetOfArray implements Cloneable JavaDoc {
17     
18     // to avoid using Enumerations, walk the individual tables skipping nulls
19
public Object JavaDoc[][] set;
20
21     public int elementSize; // number of elements in the table
22
int threshold;
23
24     public HashSetOfArray() {
25         this(13);
26     }
27
28     public HashSetOfArray(int size) {
29
30         this.elementSize = 0;
31         this.threshold = size; // size represents the expected number of elements
32
int extraRoom = (int) (size * 1.75f);
33         if (this.threshold == extraRoom)
34             extraRoom++;
35         this.set = new Object JavaDoc[extraRoom][];
36     }
37
38     public Object JavaDoc clone() throws CloneNotSupportedException JavaDoc {
39         HashSetOfArray result = (HashSetOfArray) super.clone();
40         result.elementSize = this.elementSize;
41         result.threshold = this.threshold;
42
43         int length = this.set.length;
44         result.set = new Object JavaDoc[length][];
45         System.arraycopy(this.set, 0, result.set, 0, length);
46
47         return result;
48     }
49
50     public boolean contains(Object JavaDoc[] array) {
51         int length = this.set.length;
52         int index = hashCode(array) % length;
53         int arrayLength = array.length;
54         Object JavaDoc[] currentArray;
55         while ((currentArray = this.set[index]) != null) {
56             if (currentArray.length == arrayLength && Util.equalArraysOrNull(currentArray, array))
57                 return true;
58             if (++index == length) {
59                 index = 0;
60             }
61         }
62         return false;
63     }
64
65     private int hashCode(Object JavaDoc[] element) {
66         return hashCode(element, element.length);
67     }
68     
69     private int hashCode(Object JavaDoc[] element, int length) {
70         int hash = 0;
71         for (int i = length-1; i >= 0; i--)
72             hash = Util.combineHashCodes(hash, element[i].hashCode());
73         return hash & 0x7FFFFFFF;
74     }
75     
76     public Object JavaDoc add(Object JavaDoc[] array) {
77         int length = this.set.length;
78         int index = hashCode(array) % length;
79         int arrayLength = array.length;
80         Object JavaDoc[] currentArray;
81         while ((currentArray = this.set[index]) != null) {
82             if (currentArray.length == arrayLength && Util.equalArraysOrNull(currentArray, array))
83                 return this.set[index] = array;
84             if (++index == length) {
85                 index = 0;
86             }
87         }
88         this.set[index] = array;
89
90         // assumes the threshold is never equal to the size of the table
91
if (++this.elementSize > threshold)
92             rehash();
93         return array;
94     }
95
96     public Object JavaDoc remove(Object JavaDoc[] array) {
97         int length = this.set.length;
98         int index = hashCode(array) % length;
99         int arrayLength = array.length;
100         Object JavaDoc[] currentArray;
101         while ((currentArray = this.set[index]) != null) {
102             if (currentArray.length == arrayLength && Util.equalArraysOrNull(currentArray, array)) {
103                 Object JavaDoc existing = this.set[index];
104                 this.elementSize--;
105                 this.set[index] = null;
106                 rehash();
107                 return existing;
108             }
109             if (++index == length) {
110                 index = 0;
111             }
112         }
113         return null;
114     }
115
116     private void rehash() {
117
118         HashSetOfArray newHashSet = new HashSetOfArray(elementSize * 2); // double the number of expected elements
119
Object JavaDoc[] currentArray;
120         for (int i = this.set.length; --i >= 0;)
121             if ((currentArray = this.set[i]) != null)
122                 newHashSet.add(currentArray);
123
124         this.set = newHashSet.set;
125         this.threshold = newHashSet.threshold;
126     }
127
128     public int size() {
129         return elementSize;
130     }
131
132     public String JavaDoc toString() {
133         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
134         Object JavaDoc[] element;
135         for (int i = 0, length = this.set.length; i < length; i++)
136             if ((element = this.set[i]) != null) {
137                 buffer.append('{');
138                 for (int j = 0, length2 = element.length; j < length2; j++) {
139                     buffer.append(element[j]);
140                     if (j != length2-1)
141                         buffer.append(", "); //$NON-NLS-1$
142
}
143                 buffer.append("}"); //$NON-NLS-1$
144
if (i != length-1)
145                     buffer.append('\n');
146             }
147         return buffer.toString();
148     }
149 }
150
Popular Tags