Source Code Cross Referenced for FloatKeyChainedHashMap.java in  » Development » PCJ » bak » 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 » Development » PCJ » bak.pcj.map 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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