Source Code Cross Referenced for IntKeyIntChainedHashMap.java in  » Web-Framework » rife-1.6.1 » com » uwyn » rife » pcj » map » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Web Framework » rife 1.6.1 » com.uwyn.rife.pcj.map 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         *  Primitive Collections for Java.
003:         *  Copyright (C) 2002  S�ren Bak
004:         *
005:         *  This library is free software; you can redistribute it and/or
006:         *  modify it under the terms of the GNU Lesser General Public
007:         *  License as published by the Free Software Foundation; either
008:         *  version 2.1 of the License, or (at your option) any later version.
009:         *
010:         *  This library is distributed in the hope that it will be useful,
011:         *  but WITHOUT ANY WARRANTY; without even the implied warranty of
012:         *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013:         *  Lesser General Public License for more details.
014:         *
015:         *  You should have received a copy of the GNU Lesser General Public
016:         *  License along with this library; if not, write to the Free Software
017:         *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
018:         */
019:        package com.uwyn.rife.pcj.map;
020:
021:        import java.io.IOException;
022:        import java.io.ObjectInputStream;
023:        import java.io.ObjectOutputStream;
024:        import java.io.Serializable;
025:
026:        import com.uwyn.rife.pcj.AbstractIntCollection;
027:        import com.uwyn.rife.pcj.IntCollection;
028:        import com.uwyn.rife.pcj.IntIterator;
029:        import com.uwyn.rife.pcj.hash.DefaultIntHashFunction;
030:        import com.uwyn.rife.pcj.hash.IntHashFunction;
031:        import com.uwyn.rife.pcj.set.AbstractIntSet;
032:        import com.uwyn.rife.pcj.set.IntSet;
033:        import com.uwyn.rife.pcj.util.Exceptions;
034:
035:        /**
036:         *  This class represents chained hash table based maps from
037:         *  int values to int values.
038:         *
039:         *  @see        IntKeyIntOpenHashMap
040:         *  @see        java.util.Map
041:         *
042:         *  @author     Søren Bak
043:         *  @version    1.4     21-08-2003 19:48
044:         *  @since      1.0
045:         */
046:        public class IntKeyIntChainedHashMap extends AbstractIntKeyIntMap
047:                implements  IntKeyIntMap, Cloneable, Serializable {
048:
049:            private static final long serialVersionUID = 2147225957064033301L;
050:
051:            /** Constant indicating relative growth policy. */
052:            private static final int GROWTH_POLICY_RELATIVE = 0;
053:
054:            /** Constant indicating absolute growth policy. */
055:            private static final int GROWTH_POLICY_ABSOLUTE = 1;
056:
057:            /**
058:             *  The default growth policy of this map.
059:             *  @see    #GROWTH_POLICY_RELATIVE
060:             *  @see    #GROWTH_POLICY_ABSOLUTE
061:             */
062:            private static final int DEFAULT_GROWTH_POLICY = GROWTH_POLICY_RELATIVE;
063:
064:            /** The default factor with which to increase the capacity of this map. */
065:            public static final double DEFAULT_GROWTH_FACTOR = 1.0;
066:
067:            /** The default chunk size with which to increase the capacity of this map. */
068:            public static final int DEFAULT_GROWTH_CHUNK = 10;
069:
070:            /** The default capacity of this map. */
071:            public static final int DEFAULT_CAPACITY = 11;
072:
073:            /** The default load factor of this map. */
074:            public static final double DEFAULT_LOAD_FACTOR = 0.75;
075:
076:            /**
077:             *  The hash function used to hash keys in this map.
078:             *  @serial
079:             */
080:            private IntHashFunction keyhash;
081:
082:            /**
083:             *  The size of this map.
084:             *  @serial
085:             */
086:            private int size;
087:
088:            /** The hash table backing up this map. Contains linked entry values. */
089:            private transient Entry[] data;
090:
091:            /**
092:             *  The growth policy of this map (0 is relative growth, 1 is absolute growth).
093:             *  @serial
094:             */
095:            private int growthPolicy;
096:
097:            /**
098:             *  The growth factor of this map, if the growth policy is
099:             *  relative.
100:             *  @serial
101:             */
102:            private double growthFactor;
103:
104:            /**
105:             *  The growth chunk size of this map, if the growth policy is
106:             *  absolute.
107:             *  @serial
108:             */
109:            private int growthChunk;
110:
111:            /**
112:             *  The load factor of this map.
113:             *  @serial
114:             */
115:            private double loadFactor;
116:
117:            /**
118:             *  The next size at which to expand the data[].
119:             *  @serial
120:             */
121:            private int expandAt;
122:
123:            /** A set view of the keys of this map. */
124:            private transient IntSet keys;
125:
126:            /** A collection view of the values of this map. */
127:            private transient IntCollection values;
128:
129:            /** Indicates whether last call to containsKey() had a corresponding value. */
130:            private transient boolean hasLastValue;
131:
132:            /** Value corresponding to the key of the last call of containsKey(). */
133:            private transient int lastValue;
134:
135:            private IntKeyIntChainedHashMap(IntHashFunction keyhash,
136:                    int capacity, int growthPolicy, double growthFactor,
137:                    int growthChunk, double loadFactor) {
138:                if (keyhash == null)
139:                    Exceptions.nullArgument("hash function");
140:                if (capacity < 0)
141:                    Exceptions.negativeArgument("capacity", String
142:                            .valueOf(capacity));
143:                if (growthFactor < 0.0)
144:                    Exceptions.negativeArgument("growthFactor", String
145:                            .valueOf(growthFactor));
146:                if (growthChunk < 0)
147:                    Exceptions.negativeArgument("growthChunk", String
148:                            .valueOf(growthChunk));
149:                if (loadFactor <= 0.0)
150:                    Exceptions.negativeOrZeroArgument("loadFactor", String
151:                            .valueOf(loadFactor));
152:                this .keyhash = keyhash;
153:                data = new Entry[capacity];
154:                size = 0;
155:                expandAt = (int) Math.round(loadFactor * capacity);
156:                this .growthPolicy = growthPolicy;
157:                this .growthFactor = growthFactor;
158:                this .growthChunk = growthChunk;
159:                this .loadFactor = loadFactor;
160:                hasLastValue = false;
161:            }
162:
163:            private IntKeyIntChainedHashMap(int capacity, int growthPolicy,
164:                    double growthFactor, int growthChunk, double loadFactor) {
165:                this (DefaultIntHashFunction.INSTANCE, capacity, growthPolicy,
166:                        growthFactor, growthChunk, loadFactor);
167:            }
168:
169:            /**
170:             *  Creates a new hash map with capacity 11, a relative
171:             *  growth factor of 1.0, and a load factor of 75%.
172:             */
173:            public IntKeyIntChainedHashMap() {
174:                this (DEFAULT_CAPACITY);
175:            }
176:
177:            /**
178:             *  Creates a new hash map with the same mappings as a specified map.
179:             *
180:             *  @param      map
181:             *              the map whose mappings to put into the new map.
182:             *
183:             *  @throws     NullPointerException
184:             *              if <tt>map</tt> is <tt>null</tt>.
185:             */
186:            public IntKeyIntChainedHashMap(IntKeyIntMap map) {
187:                this ();
188:                putAll(map);
189:            }
190:
191:            /**
192:             *  Creates a new hash map with a specified capacity, a relative
193:             *  growth factor of 1.0, and a load factor of 75%.
194:             *
195:             *  @param      capacity
196:             *              the initial capacity of the map.
197:             *
198:             *  @throws     IllegalArgumentException
199:             *              if <tt>capacity</tt> is negative.
200:             */
201:            public IntKeyIntChainedHashMap(int capacity) {
202:                this (capacity, DEFAULT_GROWTH_POLICY, DEFAULT_GROWTH_FACTOR,
203:                        DEFAULT_GROWTH_CHUNK, DEFAULT_LOAD_FACTOR);
204:            }
205:
206:            /**
207:             *  Creates a new hash map with a capacity of 11, a relative
208:             *  growth factor of 1.0, and a specified load factor.
209:             *
210:             *  @param      loadFactor
211:             *              the load factor of the map.
212:             *
213:             *  @throws     IllegalArgumentException
214:             *              if <tt>capacity</tt> is negative.
215:             */
216:            public IntKeyIntChainedHashMap(double loadFactor) {
217:                this (DEFAULT_CAPACITY, DEFAULT_GROWTH_POLICY,
218:                        DEFAULT_GROWTH_FACTOR, DEFAULT_GROWTH_CHUNK, loadFactor);
219:            }
220:
221:            /**
222:             *  Creates a new hash map with a specified capacity and
223:             *  load factor, and a relative growth factor of 1.0.
224:             *
225:             *  @param      capacity
226:             *              the initial capacity of the map.
227:             *
228:             *  @param      loadFactor
229:             *              the load factor of the map.
230:             *
231:             *  @throws     IllegalArgumentException
232:             *              if <tt>capacity</tt> is negative;
233:             *              if <tt>loadFactor</tt> is not positive.
234:             */
235:            public IntKeyIntChainedHashMap(int capacity, double loadFactor) {
236:                this (capacity, DEFAULT_GROWTH_POLICY, DEFAULT_GROWTH_FACTOR,
237:                        DEFAULT_GROWTH_CHUNK, loadFactor);
238:            }
239:
240:            /**
241:             *  Creates a new hash map with a specified capacity,
242:             *  load factor, and relative growth factor.
243:             *
244:             *  <p>The map capacity increases to <tt>capacity()*(1+growthFactor)</tt>.
245:             *  This strategy is good for avoiding many capacity increases, but
246:             *  the amount of wasted memory is approximately the size of the map.
247:             *
248:             *  @param      capacity
249:             *              the initial capacity of the map.
250:             *
251:             *  @param      loadFactor
252:             *              the load factor of the map.
253:             *
254:             *  @param      growthFactor
255:             *              the relative amount with which to increase the
256:             *              the capacity when a capacity increase is needed.
257:             *
258:             *  @throws     IllegalArgumentException
259:             *              if <tt>capacity</tt> is negative;
260:             *              if <tt>loadFactor</tt> is not positive;
261:             *              if <tt>growthFactor</tt> is not positive.
262:             */
263:            public IntKeyIntChainedHashMap(int capacity, double loadFactor,
264:                    double growthFactor) {
265:                this (capacity, GROWTH_POLICY_RELATIVE, growthFactor,
266:                        DEFAULT_GROWTH_CHUNK, loadFactor);
267:            }
268:
269:            /**
270:             *  Creates a new hash map with a specified capacity,
271:             *  load factor, and absolute growth factor.
272:             *
273:             *  <p>The map capacity increases to <tt>capacity()+growthChunk</tt>.
274:             *  This strategy is good for avoiding wasting memory. However, an
275:             *  overhead is potentially introduced by frequent capacity increases.
276:             *
277:             *  @param      capacity
278:             *              the initial capacity of the map.
279:             *
280:             *  @param      loadFactor
281:             *              the load factor of the map.
282:             *
283:             *  @param      growthChunk
284:             *              the absolute amount with which to increase the
285:             *              the capacity when a capacity increase is needed.
286:             *
287:             *  @throws     IllegalArgumentException
288:             *              if <tt>capacity</tt> is negative;
289:             *              if <tt>loadFactor</tt> is not positive;
290:             *              if <tt>growthChunk</tt> is not positive;
291:             */
292:            public IntKeyIntChainedHashMap(int capacity, double loadFactor,
293:                    int growthChunk) {
294:                this (capacity, GROWTH_POLICY_ABSOLUTE, DEFAULT_GROWTH_FACTOR,
295:                        growthChunk, loadFactor);
296:            }
297:
298:            // ---------------------------------------------------------------
299:            //      Constructors with hash function argument
300:            // ---------------------------------------------------------------
301:
302:            /**
303:             *  Creates a new hash map with capacity 11, a relative
304:             *  growth factor of 1.0, and a load factor of 75%.
305:             *
306:             *  @param      keyhash
307:             *              the hash function to use when hashing keys.
308:             *
309:             *  @throws     NullPointerException
310:             *              if <tt>keyhash</tt> is <tt>null</tt>.
311:             */
312:            public IntKeyIntChainedHashMap(IntHashFunction keyhash) {
313:                this (keyhash, DEFAULT_CAPACITY, DEFAULT_GROWTH_POLICY,
314:                        DEFAULT_GROWTH_FACTOR, DEFAULT_GROWTH_CHUNK,
315:                        DEFAULT_LOAD_FACTOR);
316:            }
317:
318:            /**
319:             *  Creates a new hash map with a specified capacity, a relative
320:             *  growth factor of 1.0, and a load factor of 75%.
321:             *
322:             *  @param      keyhash
323:             *              the hash function to use when hashing keys.
324:             *
325:             *  @param      capacity
326:             *              the initial capacity of the map.
327:             *
328:             *  @throws     IllegalArgumentException
329:             *              if <tt>capacity</tt> is negative.
330:             *
331:             *  @throws     NullPointerException
332:             *              if <tt>keyhash</tt> is <tt>null</tt>.
333:             */
334:            public IntKeyIntChainedHashMap(IntHashFunction keyhash, int capacity) {
335:                this (keyhash, capacity, DEFAULT_GROWTH_POLICY,
336:                        DEFAULT_GROWTH_FACTOR, DEFAULT_GROWTH_CHUNK,
337:                        DEFAULT_LOAD_FACTOR);
338:            }
339:
340:            /**
341:             *  Creates a new hash map with a capacity of 11, a relative
342:             *  growth factor of 1.0, and a specified load factor.
343:             *
344:             *  @param      keyhash
345:             *              the hash function to use when hashing keys.
346:             *
347:             *  @param      loadFactor
348:             *              the load factor of the map.
349:             *
350:             *  @throws     IllegalArgumentException
351:             *              if <tt>capacity</tt> is negative.
352:             *
353:             *  @throws     NullPointerException
354:             *              if <tt>keyhash</tt> is <tt>null</tt>.
355:             */
356:            public IntKeyIntChainedHashMap(IntHashFunction keyhash,
357:                    double loadFactor) {
358:                this (keyhash, DEFAULT_CAPACITY, DEFAULT_GROWTH_POLICY,
359:                        DEFAULT_GROWTH_FACTOR, DEFAULT_GROWTH_CHUNK, loadFactor);
360:            }
361:
362:            /**
363:             *  Creates a new hash map with a specified capacity and
364:             *  load factor, and a relative growth factor of 1.0.
365:             *
366:             *  @param      keyhash
367:             *              the hash function to use when hashing keys.
368:             *
369:             *  @param      capacity
370:             *              the initial capacity of the map.
371:             *
372:             *  @param      loadFactor
373:             *              the load factor of the map.
374:             *
375:             *  @throws     IllegalArgumentException
376:             *              if <tt>capacity</tt> is negative;
377:             *              if <tt>loadFactor</tt> is not positive.
378:             *
379:             *  @throws     NullPointerException
380:             *              if <tt>keyhash</tt> is <tt>null</tt>.
381:             */
382:            public IntKeyIntChainedHashMap(IntHashFunction keyhash,
383:                    int capacity, double loadFactor) {
384:                this (keyhash, capacity, DEFAULT_GROWTH_POLICY,
385:                        DEFAULT_GROWTH_FACTOR, DEFAULT_GROWTH_CHUNK, loadFactor);
386:            }
387:
388:            /**
389:             *  Creates a new hash map with a specified capacity,
390:             *  load factor, and relative growth factor.
391:             *
392:             *  <p>The map capacity increases to <tt>capacity()*(1+growthFactor)</tt>.
393:             *  This strategy is good for avoiding many capacity increases, but
394:             *  the amount of wasted memory is approximately the size of the map.
395:             *
396:             *  @param      keyhash
397:             *              the hash function to use when hashing keys.
398:             *
399:             *  @param      capacity
400:             *              the initial capacity of the map.
401:             *
402:             *  @param      loadFactor
403:             *              the load factor of the map.
404:             *
405:             *  @param      growthFactor
406:             *              the relative amount with which to increase the
407:             *              the capacity when a capacity increase is needed.
408:             *
409:             *  @throws     IllegalArgumentException
410:             *              if <tt>capacity</tt> is negative;
411:             *              if <tt>loadFactor</tt> is not positive;
412:             *              if <tt>growthFactor</tt> is not positive.
413:             *
414:             *  @throws     NullPointerException
415:             *              if <tt>keyhash</tt> is <tt>null</tt>.
416:             */
417:            public IntKeyIntChainedHashMap(IntHashFunction keyhash,
418:                    int capacity, double loadFactor, double growthFactor) {
419:                this (keyhash, capacity, GROWTH_POLICY_RELATIVE, growthFactor,
420:                        DEFAULT_GROWTH_CHUNK, loadFactor);
421:            }
422:
423:            /**
424:             *  Creates a new hash map with a specified capacity,
425:             *  load factor, and absolute growth factor.
426:             *
427:             *  <p>The map capacity increases to <tt>capacity()+growthChunk</tt>.
428:             *  This strategy is good for avoiding wasting memory. However, an
429:             *  overhead is potentially introduced by frequent capacity increases.
430:             *
431:             *  @param      keyhash
432:             *              the hash function to use when hashing keys.
433:             *
434:             *  @param      capacity
435:             *              the initial capacity of the map.
436:             *
437:             *  @param      loadFactor
438:             *              the load factor of the map.
439:             *
440:             *  @param      growthChunk
441:             *              the absolute amount with which to increase the
442:             *              the capacity when a capacity increase is needed.
443:             *
444:             *  @throws     IllegalArgumentException
445:             *              if <tt>capacity</tt> is negative;
446:             *              if <tt>loadFactor</tt> is not positive;
447:             *              if <tt>growthChunk</tt> is not positive;
448:             *
449:             *  @throws     NullPointerException
450:             *              if <tt>keyhash</tt> is <tt>null</tt>.
451:             */
452:            public IntKeyIntChainedHashMap(IntHashFunction keyhash,
453:                    int capacity, double loadFactor, int growthChunk) {
454:                this (keyhash, capacity, GROWTH_POLICY_ABSOLUTE,
455:                        DEFAULT_GROWTH_FACTOR, growthChunk, loadFactor);
456:            }
457:
458:            // ---------------------------------------------------------------
459:            //      Hash table management
460:            // ---------------------------------------------------------------
461:
462:            private void ensureCapacity(int elements) {
463:                if (elements >= expandAt) {
464:                    int newcapacity;
465:                    if (growthPolicy == GROWTH_POLICY_RELATIVE)
466:                        newcapacity = (int) (data.length * (1.0 + growthFactor));
467:                    else
468:                        newcapacity = data.length + growthChunk;
469:                    if (newcapacity * loadFactor < elements)
470:                        newcapacity = (int) Math
471:                                .round(((double) elements / loadFactor));
472:                    newcapacity = com.uwyn.rife.pcj.hash.Primes
473:                            .nextPrime(newcapacity);
474:                    expandAt = (int) Math.round(loadFactor * newcapacity);
475:
476:                    Entry[] newdata = new Entry[newcapacity];
477:
478:                    //  re-hash
479:                    for (int i = 0; i < data.length; i++) {
480:                        Entry e = data[i];
481:                        while (e != null) {
482:                            int index = Math.abs(keyhash.hash(e.key))
483:                                    % newdata.length;
484:                            Entry next = e.next;
485:                            e.next = newdata[index];
486:                            newdata[index] = e;
487:                            e = next;
488:                        }
489:                    }
490:
491:                    data = newdata;
492:                }
493:            }
494:
495:            @SuppressWarnings("unused")
496:            private Entry addList(Entry list, Entry v) {
497:                v.next = list;
498:                return v;
499:            }
500:
501:            private Entry removeList(Entry list, Entry e) {
502:                if (list == e) {
503:                    list = e.next;
504:                    e.next = null;
505:                    return list;
506:                }
507:                Entry listStart = list;
508:                while (list.next != e)
509:                    list = list.next;
510:                list.next = e.next;
511:                e.next = null;
512:                return listStart;
513:            }
514:
515:            private Entry searchList(Entry list, int key) {
516:                while (list != null) {
517:                    if (list.key == key)
518:                        return list;
519:                    list = list.next;
520:                }
521:                return null;
522:            }
523:
524:            private Entry getEntry(int key) {
525:                int index = Math.abs(keyhash.hash(key)) % data.length;
526:                return searchList(data[index], key);
527:            }
528:
529:            // ---------------------------------------------------------------
530:            //      Operations not supported by abstract implementation
531:            // ---------------------------------------------------------------
532:
533:            public IntSet keySet() {
534:                if (keys == null)
535:                    keys = new KeySet();
536:                return keys;
537:            }
538:
539:            public int lget() {
540:                if (!hasLastValue)
541:                    Exceptions.noLastElement();
542:                return lastValue;
543:            }
544:
545:            public int put(int key, int value) {
546:                int result;
547:                int index = Math.abs(keyhash.hash(key)) % data.length;
548:                Entry e = searchList(data[index], key);
549:                if (e == null) {
550:                    result = MapDefaults.defaultInt();
551:                    e = new Entry(key, value);
552:                    e.next = data[index];
553:                    data[index] = e;
554:                    //  Capacity is increased after insertion in order to
555:                    //  avoid recalculation of index
556:                    ensureCapacity(size + 1);
557:                    size++;
558:                } else {
559:                    result = e.value;
560:                    e.value = value;
561:                }
562:                return result;
563:            }
564:
565:            public IntCollection values() {
566:                if (values == null)
567:                    values = new ValueCollection();
568:                return values;
569:            }
570:
571:            /**
572:             *  Returns a clone of this hash map.
573:             *
574:             *  @return     a clone of this hash map.
575:             *
576:             *  @since      1.1
577:             */
578:            public Object clone() {
579:                try {
580:                    IntKeyIntChainedHashMap c = (IntKeyIntChainedHashMap) super 
581:                            .clone();
582:                    c.data = new Entry[data.length];
583:                    for (int i = 0; i < data.length; i++)
584:                        c.data[i] = cloneList(data[i]);
585:                    //  The views should not refer to this map's views
586:                    c.values = null;
587:                    c.keys = null;
588:                    return c;
589:                } catch (CloneNotSupportedException e) {
590:                    Exceptions.cloning();
591:                    return null;
592:                }
593:            }
594:
595:            private Entry cloneList(Entry e) {
596:                if (e == null)
597:                    return null;
598:                Entry ne = new Entry(e.getKey(), e.getValue());
599:                ne.next = cloneList(e.next);
600:                return ne;
601:            }
602:
603:            private static class Entry {
604:                int key;
605:                int value;
606:                Entry next;
607:
608:                Entry(int key, int value) {
609:                    this .key = key;
610:                    this .value = value;
611:                }
612:
613:                public int getKey() {
614:                    return key;
615:                }
616:
617:                public int getValue() {
618:                    return value;
619:                }
620:
621:                public boolean equals(Object obj) {
622:                    if (!(obj instanceof  Entry))
623:                        return false;
624:                    Entry e = (Entry) obj;
625:                    return e.getKey() == key && e.getValue() == value;
626:                }
627:            }
628:
629:            public IntKeyIntMapIterator entries() {
630:                return new IntKeyIntMapIterator() {
631:                    Entry currEntry = null;
632:                    int nextList = nextList(0);
633:                    Entry nextEntry = nextList == -1 ? null : data[nextList];
634:
635:                    int nextList(int index) {
636:                        while (index < data.length && data[index] == null)
637:                            index++;
638:                        return index < data.length ? index : -1;
639:                    }
640:
641:                    public boolean hasNext() {
642:                        return nextEntry != null;
643:                    }
644:
645:                    public void next() {
646:                        if (nextEntry == null)
647:                            Exceptions.endOfIterator();
648:                        currEntry = nextEntry;
649:
650:                        //  Find next
651:                        nextEntry = nextEntry.next;
652:                        if (nextEntry == null) {
653:                            nextList = nextList(nextList + 1);
654:                            if (nextList != -1)
655:                                nextEntry = data[nextList];
656:                        }
657:                    }
658:
659:                    public int getKey() {
660:                        if (currEntry == null)
661:                            Exceptions.noElementToGet();
662:                        return currEntry.getKey();
663:                    }
664:
665:                    public int getValue() {
666:                        if (currEntry == null)
667:                            Exceptions.noElementToGet();
668:                        return currEntry.getValue();
669:                    }
670:
671:                    public void remove() {
672:                        if (currEntry == null)
673:                            Exceptions.noElementToRemove();
674:                        IntKeyIntChainedHashMap.this .remove(currEntry.getKey());
675:                        currEntry = null;
676:                    }
677:
678:                };
679:            }
680:
681:            private class KeySet extends AbstractIntSet {
682:
683:                public void clear() {
684:                    IntKeyIntChainedHashMap.this .clear();
685:                }
686:
687:                public boolean contains(int v) {
688:                    return getEntry(v) != null;
689:                }
690:
691:                public IntIterator iterator() {
692:                    return new IntIterator() {
693:                        Entry currEntry = null;
694:                        int nextList = nextList(0);
695:                        Entry nextEntry = nextList == -1 ? null
696:                                : data[nextList];
697:
698:                        int nextList(int index) {
699:                            while (index < data.length && data[index] == null)
700:                                index++;
701:                            return index < data.length ? index : -1;
702:                        }
703:
704:                        public boolean hasNext() {
705:                            return nextEntry != null;
706:                        }
707:
708:                        public int next() {
709:                            if (nextEntry == null)
710:                                Exceptions.endOfIterator();
711:                            currEntry = nextEntry;
712:
713:                            //  Find next
714:                            nextEntry = nextEntry.next;
715:                            if (nextEntry == null) {
716:                                nextList = nextList(nextList + 1);
717:                                if (nextList != -1)
718:                                    nextEntry = data[nextList];
719:                            }
720:                            return currEntry.key;
721:                        }
722:
723:                        public void remove() {
724:                            if (currEntry == null)
725:                                Exceptions.noElementToRemove();
726:                            IntKeyIntChainedHashMap.this .remove(currEntry
727:                                    .getKey());
728:                            currEntry = null;
729:                        }
730:                    };
731:                }
732:
733:                public boolean remove(int v) {
734:                    boolean result = containsKey(v);
735:                    if (result)
736:                        IntKeyIntChainedHashMap.this .remove(v);
737:                    return result;
738:                }
739:
740:                public int size() {
741:                    return size;
742:                }
743:
744:            }
745:
746:            private class ValueCollection extends AbstractIntCollection {
747:
748:                public void clear() {
749:                    IntKeyIntChainedHashMap.this .clear();
750:                }
751:
752:                public boolean contains(int v) {
753:                    return containsValue(v);
754:                }
755:
756:                public IntIterator iterator() {
757:                    return new IntIterator() {
758:                        Entry currEntry = null;
759:                        int nextList = nextList(0);
760:                        Entry nextEntry = nextList == -1 ? null
761:                                : data[nextList];
762:
763:                        int nextList(int index) {
764:                            while (index < data.length && data[index] == null)
765:                                index++;
766:                            return index < data.length ? index : -1;
767:                        }
768:
769:                        public boolean hasNext() {
770:                            return nextEntry != null;
771:                        }
772:
773:                        public int next() {
774:                            if (nextEntry == null)
775:                                Exceptions.endOfIterator();
776:                            currEntry = nextEntry;
777:
778:                            //  Find next
779:                            nextEntry = nextEntry.next;
780:                            if (nextEntry == null) {
781:                                nextList = nextList(nextList + 1);
782:                                if (nextList != -1)
783:                                    nextEntry = data[nextList];
784:                            }
785:                            return currEntry.value;
786:                        }
787:
788:                        public void remove() {
789:                            if (currEntry == null)
790:                                Exceptions.noElementToRemove();
791:                            IntKeyIntChainedHashMap.this .remove(currEntry
792:                                    .getKey());
793:                            currEntry = null;
794:                        }
795:                    };
796:                }
797:
798:                public int size() {
799:                    return size;
800:                }
801:
802:            }
803:
804:            // ---------------------------------------------------------------
805:            //      Operations overwritten for efficiency
806:            // ---------------------------------------------------------------
807:
808:            public void clear() {
809:                java.util.Arrays.fill(data, null);
810:                size = 0;
811:            }
812:
813:            public boolean containsKey(int key) {
814:                Entry e = getEntry(key);
815:                if (e == null)
816:                    hasLastValue = false;
817:                else {
818:                    hasLastValue = true;
819:                    lastValue = e.value;
820:                }
821:                return hasLastValue;
822:            }
823:
824:            public boolean containsValue(int value) {
825:                for (int i = 0; i < data.length; i++) {
826:                    Entry e = data[i];
827:                    while (e != null) {
828:                        if (e.value == value)
829:                            return true;
830:                        e = e.next;
831:                    }
832:                }
833:                return false;
834:            }
835:
836:            public int get(int key) {
837:                int index = Math.abs(keyhash.hash(key)) % data.length;
838:                Entry e = searchList(data[index], key);
839:                return e != null ? e.value : MapDefaults.defaultInt();
840:            }
841:
842:            public boolean isEmpty() {
843:                return size == 0;
844:            }
845:
846:            public int remove(int key) {
847:                int index = Math.abs(keyhash.hash(key)) % data.length;
848:                Entry e = searchList(data[index], key);
849:                int value;
850:                if (e != null) {
851:                    //  This can be improved to one iteration
852:                    data[index] = removeList(data[index], e);
853:                    value = e.value;
854:                    size--;
855:                } else
856:                    value = MapDefaults.defaultInt();
857:                return value;
858:            }
859:
860:            public int size() {
861:                return size;
862:            }
863:
864:            public int tget(int key) {
865:                int index = Math.abs(keyhash.hash(key)) % data.length;
866:                Entry e = searchList(data[index], key);
867:                if (e == null)
868:                    Exceptions.noSuchMapping(String.valueOf(key));
869:                return e.value;
870:            }
871:
872:            // ---------------------------------------------------------------
873:            //      Serialization
874:            // ---------------------------------------------------------------
875:
876:            /**
877:             *  @serialData     Default fields; the capacity of the
878:             *                  map (<tt>int</tt>); the maps's entries
879:             *                  (<tt>int</tt>, <tt>int</tt>).
880:             *
881:             *  @since          1.1
882:             */
883:            private void writeObject(ObjectOutputStream s) throws IOException {
884:                s.defaultWriteObject();
885:                s.writeInt(data.length);
886:                IntKeyIntMapIterator i = entries();
887:                while (i.hasNext()) {
888:                    i.next();
889:                    s.writeInt(i.getKey());
890:                    s.writeInt(i.getValue());
891:                }
892:            }
893:
894:            /**
895:             *  @since          1.1
896:             */
897:            private void readObject(ObjectInputStream s) throws IOException,
898:                    ClassNotFoundException {
899:                s.defaultReadObject();
900:                data = new Entry[s.readInt()];
901:                for (int i = 0; i < size; i++) {
902:                    int key = s.readInt();
903:                    int value = s.readInt();
904:                    int index = Math.abs(keyhash.hash(key)) % data.length;
905:                    Entry e = new Entry(key, value);
906:                    e.next = data[index];
907:                    data[index] = e;
908:                }
909:            }
910:
911:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.