Source Code Cross Referenced for GeneralHashTable.java in  » Scripting » Kawa » gnu » kawa » util » 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 » Scripting » Kawa » gnu.kawa.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        // Copyright (c) 2005  Per M.A. Bothner.
002:        // This is free software;  for terms and warranty disclaimer see COPYING.
003:
004:        package gnu.kawa.util;
005:
006:        /** A generic hash table.
007:         * Supports deletions, and re-allocates the table when too big.
008:         * The equivalence relation can be customized. */
009:
010:        public class GeneralHashTable
011:        // FUTURE: implements java.util.Map
012:        {
013:            protected HashNode[] table;
014:            protected int mask;
015:            protected int num_bindings;
016:
017:            public GeneralHashTable() {
018:                this (64);
019:            }
020:
021:            public GeneralHashTable(int capacity) {
022:                int log2Size = 4;
023:                while (capacity > (1 << log2Size))
024:                    log2Size++;
025:                capacity = 1 << log2Size;
026:                table = new HashNode[capacity];
027:                mask = capacity - 1;
028:            }
029:
030:            /** Allocate a new node in the hash table. */
031:            protected HashNode makeEntry(Object key, int hash, Object value) {
032:                HashNode node = new HashNode();
033:                node.key = key;
034:                node.hash = hash;
035:                node.value = value;
036:                return node;
037:            }
038:
039:            /** Calculate hash code of a key.
040:             * You may need to override this if you override the <code>matches</code> method.
041:             */
042:            public int hash(Object key) {
043:                // FIXME
044:                return key == null ? 0 : key.hashCode();
045:            }
046:
047:            public int hash(HashNode node) {
048:                //return hash(node.getKey());
049:                return node.hash;
050:            }
051:
052:            public boolean matches(Object key, int hash, HashNode node) {
053:                return node.hash == hash && matches(node.getKey(), key);
054:            }
055:
056:            /** Compare two keys for equivalence.
057:             * Override this and the {@link #hash(Object)} method if you want
058:             * a different equivalence relation.
059:             */
060:            public boolean matches(Object value1, Object value2) {
061:                // FIXME
062:                return value1 == value2
063:                        || (value1 != null && value1.equals(value2));
064:            }
065:
066:            public Object get(Object key, Object defaultValue) {
067:                int hash = hash(key);
068:                int index = hash & this .mask;
069:                for (HashNode node = table[index]; node != null; node = node.next) {
070:                    if (matches(key, hash, node))
071:                        return node.getValue();
072:                }
073:                return defaultValue;
074:            }
075:
076:            public HashNode getNode(Object key) {
077:                int hash = hash(key);
078:                int index = hash & this .mask;
079:                for (HashNode node = table[index]; node != null; node = node.next) {
080:                    if (matches(key, hash, node))
081:                        return node;
082:                }
083:                return null;
084:            }
085:
086:            public Object put(Object key, Object value) {
087:                return put(key, hash(key), value);
088:            }
089:
090:            public Object put(Object key, int hash, Object value) {
091:                int index = hash & mask;
092:                HashNode first = table[index];
093:                HashNode node = first;
094:                for (;;) {
095:                    if (node == null) {
096:                        if (++num_bindings >= table.length) {
097:                            rehash();
098:                            index = hash & mask;
099:                            first = table[index];
100:                        }
101:                        node = makeEntry(key, hash, value);
102:                        node.next = first;
103:                        table[index] = node;
104:                        return null;
105:                    } else if (matches(key, hash, node)) {
106:                        return node.setValue(value);
107:                    }
108:                    node = node.next;
109:                }
110:            }
111:
112:            public Object remove(Object key) {
113:                int hash = hash(key);
114:                int index = hash & this .mask;
115:                HashNode prev = null;
116:                HashNode node = table[index];
117:                while (node != null) {
118:                    HashNode next = node.next;
119:                    if (matches(key, hash, node)) {
120:                        if (prev == null)
121:                            table[index] = next;
122:                        else
123:                            prev.next = node;
124:                        num_bindings--;
125:                        return node.getValue();
126:                    }
127:                    prev = node;
128:                    node = next;
129:                }
130:                return null;
131:            }
132:
133:            protected void rehash() {
134:                HashNode[] oldTable = table;
135:                int oldCapacity = oldTable.length;
136:                int newCapacity = 2 * oldCapacity;
137:                HashNode[] newTable = new HashNode[newCapacity];
138:                int newMask = newCapacity - 1;
139:                for (int i = oldCapacity; --i >= 0;) {
140:                    HashNode chain = oldTable[i];
141:                    if (chain != null && chain.next != null) {
142:                        // Reverse the old chain in place, so that after re-hashing the
143:                        // new chain has the same order.. This is useful for some
144:                        // subclasses (specifically gnu.expr.NameLookup), and it is
145:                        // cheap to so here where extra cache misses are unlikely.
146:                        HashNode prev = null;
147:                        do {
148:                            HashNode node = chain;
149:                            chain = node.next;
150:                            node.next = prev;
151:                            prev = node;
152:                        } while (chain != null);
153:                        chain = prev;
154:                    }
155:
156:                    for (HashNode element = chain; element != null;) {
157:                        HashNode next = element.next;
158:                        int hash = hash(element);
159:                        int j = hash & newMask;
160:                        HashNode head = newTable[j];
161:                        element.next = head;
162:                        newTable[j] = element;
163:                        element = next;
164:                    }
165:                }
166:                table = newTable;
167:                mask = newMask;
168:            }
169:
170:            public void clear() {
171:                HashNode[] t = this .table;
172:                for (int i = t.length; --i >= 0;)
173:                    t[i] = null;
174:                num_bindings = 0;
175:            }
176:
177:            public int size() {
178:                return num_bindings;
179:            }
180:
181:            protected static HashNode next(HashNode node) {
182:                return node.next;
183:            }
184:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.