File: SimpleHashtable.java

package info (click to toggle)
tomcat6 6.0.41-3
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 20,640 kB
  • sloc: java: 175,884; xml: 40,992; jsp: 1,943; sh: 1,262; makefile: 101
file content (325 lines) | stat: -rw-r--r-- 8,872 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
/*
 *  Licensed to the Apache Software Foundation (ASF) under one or more
 *  contributor license agreements.  See the NOTICE file distributed with
 *  this work for additional information regarding copyright ownership.
 *  The ASF licenses this file to You under the Apache License, Version 2.0
 *  (the "License"); you may not use this file except in compliance with
 *  the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package org.apache.tomcat.util.collections;

import java.util.Enumeration;

/* **************************************** Stolen from Crimson ******************** */
/* From Crimson/Parser - in a perfect world we'll just have a common set of
   utilities, and all apache project will just use those.

*/

// can't be replaced using a Java 2 "Collections" API
// since this package must also run on JDK 1.1


/**
 * This class implements a special purpose hashtable.  It works like a
 * normal <code>java.util.Hashtable</code> except that: <OL>
 *
 *	<LI> Keys to "get" are strings which are known to be interned,
 *	so that "==" is used instead of "String.equals".  (Interning
 *	could be document-relative instead of global.)
 *
 *	<LI> It's not synchronized, since it's to be used only by
 *	one thread at a time.
 *
 *	<LI> The keys () enumerator allocates no memory, with live
 *	updates to the data disallowed.
 *
 *	<LI> It's got fewer bells and whistles:  fixed threshold and
 *	load factor, no JDK 1.2 collection support, only keys can be
 *	enumerated, things can't be removed, simpler inheritance; more.
 *
 *	</OL>
 *
 * <P> The overall result is that it's less expensive to use these in
 * performance-critical locations, in terms both of CPU and memory,
 * than <code>java.util.Hashtable</code> instances.  In this package
 * it makes a significant difference when normalizing attributes,
 * which is done for each start-element construct.
 *
 *@deprecated
 */
public final class SimpleHashtable implements Enumeration
{
    
    private static org.apache.juli.logging.Log log=
        org.apache.juli.logging.LogFactory.getLog( SimpleHashtable.class );
    
    // entries ...
    private Entry		table[];

    // currently enumerated key
    private Entry		current = null;
    private int			currentBucket = 0;

    // number of elements in hashtable
    private int			count;
    private int			threshold;

    private static final float	loadFactor = 0.75f;


    /**
     * Constructs a new, empty hashtable with the specified initial 
     * capacity.
     *
     * @param      initialCapacity   the initial capacity of the hashtable.
     */
    public SimpleHashtable(int initialCapacity) {
	if (initialCapacity < 0)
	    throw new IllegalArgumentException("Illegal Capacity: "+
                                               initialCapacity);
        if (initialCapacity==0)
            initialCapacity = 1;
	table = new Entry[initialCapacity];
	threshold = (int)(initialCapacity * loadFactor);
    }

    /**
     * Constructs a new, empty hashtable with a default capacity.
     */
    public SimpleHashtable() {
	this(11);
    }

    /**
     */
    public void clear ()
    {
	count = 0;
	currentBucket = 0;
	current = null;
	for (int i = 0; i < table.length; i++)
	    table [i] = null;
    }

    /**
     * Returns the number of keys in this hashtable.
     *
     * @return  the number of keys in this hashtable.
     */
    public int size() {
	return count;
    }

    /**
     * Returns an enumeration of the keys in this hashtable.
     *
     * @return  an enumeration of the keys in this hashtable.
     * @see     Enumeration
     */
    public Enumeration keys() {
	currentBucket = 0;
	current = null;
	hasMoreElements();
	return this;
    }

    /**
     * Used to view this as an enumeration; returns true if there
     * are more keys to be enumerated.
     */
    public boolean hasMoreElements ()
    {
	if (current != null)
	    return true;
	while (currentBucket < table.length) {
	    current = table [currentBucket++];
	    if (current != null)
		return true;
	}
	return false;
    }

    /**
     * Used to view this as an enumeration; returns the next key
     * in the enumeration.
     */
    public Object nextElement ()
    {
	Object retval;

	if (current == null)
	    throw new IllegalStateException ();
	retval = current.key;
	current = current.next;
	// Advance to the next position ( we may call next after next,
	// without hasMore )
	hasMoreElements();
	return retval;
    }


    /**
     * Returns the value to which the specified key is mapped in this hashtable.
     */
    public Object getInterned (String key) {
	Entry tab[] = table;
	int hash = key.hashCode();
	int index = (hash & 0x7FFFFFFF) % tab.length;
	for (Entry e = tab[index] ; e != null ; e = e.next) {
	    if ((e.hash == hash) && (e.key == key))
		return e.value;
	}
	return null;
    }

    /**
     * Returns the value to which the specified key is mapped in this
     * hashtable ... the key isn't necessarily interned, though.
     */
    public Object get(String key) {
	Entry tab[] = table;
	int hash = key.hashCode();
	int index = (hash & 0x7FFFFFFF) % tab.length;
	for (Entry e = tab[index] ; e != null ; e = e.next) {
	    if ((e.hash == hash) && e.key.equals(key))
		return e.value;
	}
	return null;
    }

    /**
     * Increases the capacity of and internally reorganizes this 
     * hashtable, in order to accommodate and access its entries more 
     * efficiently.  This method is called automatically when the 
     * number of keys in the hashtable exceeds this hashtable's capacity 
     * and load factor. 
     */
    private void rehash() {
	int oldCapacity = table.length;
	Entry oldMap[] = table;

	int newCapacity = oldCapacity * 2 + 1;
	Entry newMap[] = new Entry[newCapacity];

	threshold = (int)(newCapacity * loadFactor);
	table = newMap;

	/*
	System.out.pr intln("rehash old=" + oldCapacity
		+ ", new=" + newCapacity
		+ ", thresh=" + threshold
		+ ", count=" + count);
	*/

	for (int i = oldCapacity ; i-- > 0 ;) {
	    for (Entry old = oldMap[i] ; old != null ; ) {
		Entry e = old;
		old = old.next;

		int index = (e.hash & 0x7FFFFFFF) % newCapacity;
		e.next = newMap[index];
		newMap[index] = e;
	    }
	}
    }

    /**
     * Maps the specified <code>key</code> to the specified 
     * <code>value</code> in this hashtable. Neither the key nor the 
     * value can be <code>null</code>. 
     *
     * <P>The value can be retrieved by calling the <code>get</code> method 
     * with a key that is equal to the original key. 
     */
    public Object put(Object key, Object value) {
	// Make sure the value is not null
	if (value == null) {
	    throw new NullPointerException();
	}

	// Makes sure the key is not already in the hashtable.
	Entry tab[] = table;
	int hash = key.hashCode();
	int index = (hash & 0x7FFFFFFF) % tab.length;
	for (Entry e = tab[index] ; e != null ; e = e.next) {
	    // if ((e.hash == hash) && e.key.equals(key)) {
	    if ((e.hash == hash) && (e.key == key)) {
		Object old = e.value;
		e.value = value;
		return old;
	    }
	}

	if (count >= threshold) {
	    // Rehash the table if the threshold is exceeded
	    rehash();

            tab = table;
            index = (hash & 0x7FFFFFFF) % tab.length;
	} 

	// Creates the new entry.
	Entry e = new Entry(hash, key, value, tab[index]);
	tab[index] = e;
	count++;
	return null;
    }

    public Object remove(Object key) {
	Entry tab[] = table;
	Entry prev=null;
	int hash = key.hashCode();
	int index = (hash & 0x7FFFFFFF) % tab.length;
	if( dL > 0 ) d("Idx " + index +  " " + tab[index] );
	for (Entry e = tab[index] ; e != null ; prev=e, e = e.next) {
	    if( dL > 0 ) d("> " + prev + " " + e.next + " " + e + " " + e.key);
	    if ((e.hash == hash) && e.key.equals(key)) {
		if( prev!=null ) {
		    prev.next=e.next;
		} else {
		    tab[index]=e.next;
		}
		if( dL > 0 ) d("Removing from list " + tab[index] + " " + prev +
			       " " + e.value);
		count--;
		Object res=e.value;
		e.value=null;
		return res;
	    }
	}
	return null;
    }

    /**
     * Hashtable collision list.
     */
    private static class Entry {
	int	hash;
	Object	key;
	Object	value;
	Entry	next;

	protected Entry(int hash, Object key, Object value, Entry next) {
	    this.hash = hash;
	    this.key = key;
	    this.value = value;
	    this.next = next;
	}
    }

    private static final int dL=0;
    private void d(String s ) {
	if (log.isDebugEnabled())
            log.debug( "SimpleHashtable: " + s );
    }
}