1
// IntHashtable - a Hashtable that uses ints as the keys
3
// This is 90% based on JavaSoft's java.util.Hashtable.
5
// Visit the ACME Labs Java page for up-to-date versions of this and other
6
// fine Java utilities: http://www.acme.com/java/
8
package com.lowagie.text.pdf;
10
import java.util.Arrays;
11
import java.util.NoSuchElementException;
12
import java.util.Iterator;
14
/// A Hashtable that uses ints as the keys.
16
// Use just like java.util.Hashtable, except that the keys must be ints.
17
// This is much faster than creating a new Integer for each access.
19
// <A HREF="/resources/classes/Acme/IntHashtable.java">Fetch the software.</A><BR>
20
// <A HREF="/resources/classes/Acme.tar.gz">Fetch the entire Acme package.</A>
22
// @see java.util.Hashtable
24
public class IntHashtable implements Cloneable {
25
/// The hash table data.
26
private IntHashtableEntry table[];
28
/// The total number of entries in the hash table.
31
/// Rehashes the table when count exceeds this threshold.
32
private int threshold;
34
/// The load factor for the hashtable.
35
private float loadFactor;
37
/// Constructs a new, empty hashtable with the specified initial
38
// capacity and the specified load factor.
39
// @param initialCapacity the initial number of buckets
40
// @param loadFactor a number between 0.0 and 1.0, it defines
41
// the threshold for rehashing the hashtable into
43
// @exception IllegalArgumentException If the initial capacity
44
// is less than or equal to zero.
45
// @exception IllegalArgumentException If the load factor is
46
// less than or equal to zero.
47
public IntHashtable( int initialCapacity, float loadFactor ) {
48
if ( initialCapacity <= 0 || loadFactor <= 0.0 )
49
throw new IllegalArgumentException();
50
this.loadFactor = loadFactor;
51
table = new IntHashtableEntry[initialCapacity];
52
threshold = (int) ( initialCapacity * loadFactor );
55
/// Constructs a new, empty hashtable with the specified initial
57
// @param initialCapacity the initial number of buckets
58
public IntHashtable( int initialCapacity ) {
59
this( initialCapacity, 0.75f );
62
/// Constructs a new, empty hashtable. A default capacity and load factor
63
// is used. Note that the hashtable will automatically grow when it gets
65
public IntHashtable() {
69
/// Returns the number of elements contained in the hashtable.
74
/// Returns true if the hashtable contains no elements.
75
public boolean isEmpty() {
79
/// Returns true if the specified object is an element of the hashtable.
80
// This operation is more expensive than the containsKey() method.
81
// @param value the value that we are looking for
82
// @exception NullPointerException If the value being searched
83
// for is equal to null.
84
// @see IntHashtable#containsKey
85
public boolean contains( int value ) {
86
IntHashtableEntry tab[] = table;
87
for ( int i = tab.length ; i-- > 0 ; ) {
88
for ( IntHashtableEntry e = tab[i] ; e != null ; e = e.next ) {
89
if ( e.value == value )
96
/// Returns true if the collection contains an element for the key.
97
// @param key the key that we are looking for
98
// @see IntHashtable#contains
99
public boolean containsKey( int key ) {
100
IntHashtableEntry tab[] = table;
102
int index = ( hash & 0x7FFFFFFF ) % tab.length;
103
for ( IntHashtableEntry e = tab[index] ; e != null ; e = e.next ) {
104
if ( e.hash == hash && e.key == key )
110
/// Gets the object associated with the specified key in the
112
// @param key the specified key
113
// @returns the element for the key or null if the key
114
// is not defined in the hash table.
115
// @see IntHashtable#put
116
public int get( int key ) {
117
IntHashtableEntry tab[] = table;
119
int index = ( hash & 0x7FFFFFFF ) % tab.length;
120
for ( IntHashtableEntry e = tab[index] ; e != null ; e = e.next ) {
121
if ( e.hash == hash && e.key == key )
127
/// Rehashes the content of the table into a bigger table.
128
// This method is called automatically when the hashtable's
129
// size exceeds the threshold.
130
protected void rehash() {
131
int oldCapacity = table.length;
132
IntHashtableEntry oldTable[] = table;
134
int newCapacity = oldCapacity * 2 + 1;
135
IntHashtableEntry newTable[] = new IntHashtableEntry[newCapacity];
137
threshold = (int) ( newCapacity * loadFactor );
140
for ( int i = oldCapacity ; i-- > 0 ; ) {
141
for ( IntHashtableEntry old = oldTable[i] ; old != null ; ) {
142
IntHashtableEntry e = old;
145
int index = ( e.hash & 0x7FFFFFFF ) % newCapacity;
146
e.next = newTable[index];
152
/// Puts the specified element into the hashtable, using the specified
153
// key. The element may be retrieved by doing a get() with the same key.
154
// The key and the element cannot be null.
155
// @param key the specified key in the hashtable
156
// @param value the specified element
157
// @exception NullPointerException If the value of the element
159
// @see IntHashtable#get
160
// @return the old value of the key, or null if it did not have one.
161
public int put( int key, int value ) {
162
// Makes sure the key is not already in the hashtable.
163
IntHashtableEntry tab[] = table;
165
int index = ( hash & 0x7FFFFFFF ) % tab.length;
166
for ( IntHashtableEntry e = tab[index] ; e != null ; e = e.next ) {
167
if ( e.hash == hash && e.key == key ) {
174
if ( count >= threshold ) {
175
// Rehash the table if the threshold is exceeded.
177
return put( key, value );
180
// Creates the new entry.
181
IntHashtableEntry e = new IntHashtableEntry();
191
/// Removes the element corresponding to the key. Does nothing if the
192
// key is not present.
193
// @param key the key that needs to be removed
194
// @return the value of key, or null if the key was not found.
195
public int remove( int key ) {
196
IntHashtableEntry tab[] = table;
198
int index = ( hash & 0x7FFFFFFF ) % tab.length;
199
for ( IntHashtableEntry e = tab[index], prev = null ; e != null ; prev = e, e = e.next ) {
200
if ( e.hash == hash && e.key == key ) {
212
/// Clears the hash table so that it has no more elements in it.
213
public void clear() {
214
IntHashtableEntry tab[] = table;
215
for ( int index = tab.length; --index >= 0; )
220
public Object clone() {
222
IntHashtable t = (IntHashtable)super.clone();
223
t.table = new IntHashtableEntry[table.length];
224
for (int i = table.length ; i-- > 0 ; ) {
225
t.table[i] = (table[i] != null)
226
? (IntHashtableEntry)table[i].clone() : null;
229
} catch (CloneNotSupportedException e) {
230
// this shouldn't happen, since we are Cloneable
231
throw new InternalError();
235
public int[] toOrderedKeys() {
236
int res[] = getKeys();
241
public int[] getKeys() {
242
int res[] = new int[count];
244
int index = table.length;
245
IntHashtableEntry entry = null;
248
while ((index-- > 0) && ((entry = table[index]) == null));
251
IntHashtableEntry e = entry;
258
public int getOneKey() {
261
int index = table.length;
262
IntHashtableEntry entry = null;
263
while ((index-- > 0) && ((entry = table[index]) == null));
269
static class IntHashtableEntry {
273
IntHashtableEntry next;
275
public int getKey() {
279
public int getValue() {
283
protected Object clone() {
284
IntHashtableEntry entry = new IntHashtableEntry();
288
entry.next = (next != null) ? (IntHashtableEntry)next.clone() : null;
293
public Iterator getEntryIterator() {
294
return new IntHashtableIterator(table);
297
static class IntHashtableIterator implements Iterator {
300
IntHashtableEntry table[];
301
IntHashtableEntry entry;
303
IntHashtableIterator(IntHashtableEntry table[] /* , boolean keys */) {
306
this.index = table.length;
309
public boolean hasNext() {
313
while (index-- > 0) {
314
if ((entry = table[index]) != null) {
321
public Object next() {
323
while ((index-- > 0) && ((entry = table[index]) == null));
326
IntHashtableEntry e = entry;
330
throw new NoSuchElementException("IntHashtableIterator");
333
public void remove() {
334
throw new UnsupportedOperationException("remove() not supported.");