~ubuntu-branches/ubuntu/feisty/libitext-java/feisty

« back to all changes in this revision

Viewing changes to com/lowagie/text/pdf/IntHashtable.java

  • Committer: Bazaar Package Importer
  • Author(s): Gerardo Curiel
  • Date: 2006-09-21 00:08:53 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20060921000853-le9rrpcnw9fc57sm
Tags: 1.4.5-1
* New upstream release
* debian/rules modified due to a new build.xml file
* Patched Pdfgraphics2d.java to remove privative dependencie on com.sun.image.codec.jpeg.*
  (more information on
  http://developer.classpath.org/mediation/ClasspathMigration#head-d4ee9efe53a641e29ffdcd96e985bf38bbc671c1 )
* ant/.ant.properties paths fixed
* Build package with the packages provided by java-gcj-compat-dev
* Removed unused README.Debian
* Removed unused debian/patches/01libitext-java-addbuildscript.patch

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// IntHashtable - a Hashtable that uses ints as the keys
 
2
//
 
3
// This is 90% based on JavaSoft's java.util.Hashtable.
 
4
//
 
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/
 
7
 
 
8
package com.lowagie.text.pdf;
 
9
 
 
10
import java.util.Arrays;
 
11
import java.util.NoSuchElementException;
 
12
import java.util.Iterator;
 
13
 
 
14
/// A Hashtable that uses ints as the keys.
 
15
// <P>
 
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.
 
18
// <P>
 
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>
 
21
// <P>
 
22
// @see java.util.Hashtable
 
23
 
 
24
public class IntHashtable implements Cloneable {
 
25
    /// The hash table data.
 
26
    private IntHashtableEntry table[];
 
27
    
 
28
    /// The total number of entries in the hash table.
 
29
    private int count;
 
30
    
 
31
    /// Rehashes the table when count exceeds this threshold.
 
32
    private int threshold;
 
33
    
 
34
    /// The load factor for the hashtable.
 
35
    private float loadFactor;
 
36
    
 
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
 
42
    //          a bigger one.
 
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 );
 
53
    }
 
54
    
 
55
    /// Constructs a new, empty hashtable with the specified initial
 
56
    // capacity.
 
57
    // @param initialCapacity the initial number of buckets
 
58
    public IntHashtable( int initialCapacity ) {
 
59
        this( initialCapacity, 0.75f );
 
60
    }
 
61
    
 
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
 
64
    // full.
 
65
    public IntHashtable() {
 
66
        this( 101, 0.75f );
 
67
    }
 
68
    
 
69
    /// Returns the number of elements contained in the hashtable.
 
70
    public int size() {
 
71
        return count;
 
72
    }
 
73
    
 
74
    /// Returns true if the hashtable contains no elements.
 
75
    public boolean isEmpty() {
 
76
        return count == 0;
 
77
    }
 
78
    
 
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 )
 
90
                    return true;
 
91
            }
 
92
        }
 
93
        return false;
 
94
    }
 
95
    
 
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;
 
101
        int hash = key;
 
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 )
 
105
                return true;
 
106
        }
 
107
        return false;
 
108
    }
 
109
    
 
110
    /// Gets the object associated with the specified key in the
 
111
    // hashtable.
 
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;
 
118
        int hash = key;
 
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 )
 
122
                return e.value;
 
123
        }
 
124
        return 0;
 
125
    }
 
126
    
 
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;
 
133
        
 
134
        int newCapacity = oldCapacity * 2 + 1;
 
135
        IntHashtableEntry newTable[] = new IntHashtableEntry[newCapacity];
 
136
        
 
137
        threshold = (int) ( newCapacity * loadFactor );
 
138
        table = newTable;
 
139
        
 
140
        for ( int i = oldCapacity ; i-- > 0 ; ) {
 
141
            for ( IntHashtableEntry old = oldTable[i] ; old != null ; ) {
 
142
                IntHashtableEntry e = old;
 
143
                old = old.next;
 
144
                
 
145
                int index = ( e.hash & 0x7FFFFFFF ) % newCapacity;
 
146
                e.next = newTable[index];
 
147
                newTable[index] = e;
 
148
            }
 
149
        }
 
150
    }
 
151
    
 
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
 
158
    // is equal to null.
 
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;
 
164
        int hash = key;
 
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 ) {
 
168
                int old = e.value;
 
169
                e.value = value;
 
170
                return old;
 
171
            }
 
172
        }
 
173
        
 
174
        if ( count >= threshold ) {
 
175
            // Rehash the table if the threshold is exceeded.
 
176
            rehash();
 
177
            return put( key, value );
 
178
        }
 
179
        
 
180
        // Creates the new entry.
 
181
        IntHashtableEntry e = new IntHashtableEntry();
 
182
        e.hash = hash;
 
183
        e.key = key;
 
184
        e.value = value;
 
185
        e.next = tab[index];
 
186
        tab[index] = e;
 
187
        ++count;
 
188
        return 0;
 
189
    }
 
190
    
 
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;
 
197
        int hash = key;
 
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 ) {
 
201
                if ( prev != null )
 
202
                    prev.next = e.next;
 
203
                else
 
204
                    tab[index] = e.next;
 
205
                --count;
 
206
                return e.value;
 
207
            }
 
208
        }
 
209
        return 0;
 
210
    }
 
211
    
 
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; )
 
216
            tab[index] = null;
 
217
        count = 0;
 
218
    }
 
219
    
 
220
    public Object clone() {
 
221
        try {
 
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;
 
227
            }
 
228
            return t;
 
229
        } catch (CloneNotSupportedException e) {
 
230
            // this shouldn't happen, since we are Cloneable
 
231
            throw new InternalError();
 
232
        }
 
233
    }
 
234
 
 
235
    public int[] toOrderedKeys() {
 
236
        int res[] = getKeys();
 
237
        Arrays.sort(res);
 
238
        return res;
 
239
    }
 
240
    
 
241
    public int[] getKeys() {
 
242
        int res[] = new int[count];
 
243
        int ptr = 0;
 
244
        int index = table.length;
 
245
        IntHashtableEntry entry = null;
 
246
        while (true) {
 
247
            if (entry == null)
 
248
                while ((index-- > 0) && ((entry = table[index]) == null));
 
249
            if (entry == null)
 
250
                break;
 
251
            IntHashtableEntry e = entry;
 
252
            entry = e.next;
 
253
            res[ptr++] = e.key;
 
254
        }
 
255
        return res;
 
256
    }
 
257
    
 
258
    public int getOneKey() {
 
259
        if (count == 0)
 
260
            return 0;
 
261
        int index = table.length;
 
262
        IntHashtableEntry entry = null;
 
263
        while ((index-- > 0) && ((entry = table[index]) == null));
 
264
        if (entry == null)
 
265
            return 0;
 
266
        return entry.key;
 
267
    }
 
268
    
 
269
    static class IntHashtableEntry {
 
270
        int hash;
 
271
        int key;
 
272
        int value;
 
273
        IntHashtableEntry next;
 
274
        
 
275
        public int getKey() {
 
276
            return key;
 
277
        }
 
278
        
 
279
        public int getValue() {
 
280
            return value;
 
281
        }
 
282
        
 
283
        protected Object clone() {
 
284
            IntHashtableEntry entry = new IntHashtableEntry();
 
285
            entry.hash = hash;
 
286
            entry.key = key;
 
287
            entry.value = value;
 
288
            entry.next = (next != null) ? (IntHashtableEntry)next.clone() : null;
 
289
            return entry;
 
290
        }
 
291
    }    
 
292
 
 
293
    public Iterator getEntryIterator() {
 
294
        return new IntHashtableIterator(table);
 
295
    }
 
296
    
 
297
    static class IntHashtableIterator implements Iterator {
 
298
        //    boolean keys;
 
299
        int index;
 
300
        IntHashtableEntry table[];
 
301
        IntHashtableEntry entry;
 
302
        
 
303
        IntHashtableIterator(IntHashtableEntry table[] /* , boolean keys */) {
 
304
            this.table = table;
 
305
            //  this.keys = keys;
 
306
            this.index = table.length;
 
307
        }
 
308
        
 
309
        public boolean hasNext() {
 
310
            if (entry != null) {
 
311
                return true;
 
312
            }
 
313
            while (index-- > 0) {
 
314
                if ((entry = table[index]) != null) {
 
315
                    return true;
 
316
                }
 
317
            }
 
318
            return false;
 
319
        }
 
320
        
 
321
        public Object next() {
 
322
            if (entry == null) {
 
323
                while ((index-- > 0) && ((entry = table[index]) == null));
 
324
            }
 
325
            if (entry != null) {
 
326
                IntHashtableEntry e = entry;
 
327
                entry = e.next;
 
328
                return e;
 
329
            }
 
330
            throw new NoSuchElementException("IntHashtableIterator");
 
331
        }
 
332
        
 
333
        public void remove() {
 
334
            throw new UnsupportedOperationException("remove() not supported.");
 
335
        }
 
336
        
 
337
    }
 
338
}