95d636b52524f704ca8dac26bcefd1e4dc582551
[org.ibex.util.git] / src / org / ibex / util / Hash.java
1 // Copyright 2000-2005 the Contributors, as shown in the revision logs.
2 // Licensed under the Apache Public Source License 2.0 ("the License").
3 // You may not use this file except in compliance with the License.
4
5 package org.ibex.util;
6
7 import java.util.*;
8
9 /** Implementation of an unsynchronized hash table, with one or two
10  *  keys, using Radke's quadradic residue linear probing instead of
11  *  buckets to minimize object count (less allocations, faster GC).
12  *  See C. Radke, Communications of the ACM, 1970, 103-105
13  *
14  *  Not threadsafe.
15  */
16 public class Hash implements java.io.Serializable {
17
18     public static final long serialVersionUID = 8177551301264350283L;
19
20     /** this object is inserted as key in a slot when the
21      *  corresponding value is removed -- this ensures that the
22      *  probing sequence for any given key remains the same even if
23      *  other keys are removed.
24      */
25     // FIXME: this should have been static except that that makes it nonserializable
26     private Object placeholder = new java.io.Serializable() { };
27
28     /** the number of entries with at least one non-null key */
29     private int usedslots = 0;
30
31     /** the number of entries with non-null values */
32     protected int size = 0;
33
34     /** when num_slots < loadFactor * size, rehash into a bigger table */
35     private final int loadFactor;
36
37     /** primary keys */
38     private Object[] keys1 = null;
39
40     /** secondary keys; null if no secondary key has ever been added */
41     private Object[] keys2 = null;
42
43     /** the values for the table */
44     private Object[] vals = null;
45     
46     /** the number of entries with a non-null value */
47     public int size() { return size; }
48
49     public Object[] dumpkeys() {
50         Object[] ret = new Object[size];
51         int j = 0;
52         for(int i=0; i<keys1.length; i++)
53             if (keys1[i]!=null)
54                 ret[j++] = keys1[i];
55         return ret;
56     }
57
58     public Object[] vals() {
59         Object[] ret = new Object[size()];
60         int j = 0;
61         for(int i=0; i<vals.length; i++)
62             if (vals[i] != null && vals[i] != placeholder)
63                 ret[j++] = vals[i];
64         return ret;
65     }
66
67     /** empties the table */
68     public void clear() {
69         size = 0;
70         usedslots = 0;
71         for(int i=0; i<vals.length; i++) {
72             vals[i] = null;
73             keys1[i] = null;
74             if (keys2 != null) keys2[i] = null;
75         }
76     }
77
78     /** returns all the primary keys in the table */
79     public java.util.Enumeration enumerateKeys() { return new HashEnum(); }
80
81     public Hash() { this(25, 3); }
82     public Hash(int initialcapacity, int loadFactor) {
83         // using a pseudoprime in the form 4x+3 ensures full coverage
84         initialcapacity = initialcapacity / 4;
85         initialcapacity = 4 * initialcapacity + 3;
86         keys1 = new Object[initialcapacity];
87         vals = new Object[initialcapacity];
88         this.loadFactor = loadFactor;
89     }
90     
91     public void remove(Object k1) { remove(k1, null); }
92     public void remove(Object k1, Object k2) { put_(k1, k2, null); }
93
94     private void rehash() {
95         Object[] oldkeys1 = keys1;
96         Object[] oldkeys2 = keys2;
97         Object[] oldvals = vals;
98         keys1 = new Object[oldvals.length * 2];
99         keys2 = oldkeys2 == null ? null : new Object[oldvals.length * 2];
100         vals = new Object[oldvals.length * 2];
101         size = 0;
102         usedslots = 0;
103         for(int i=0; i<oldvals.length; i++)
104             if (((oldkeys1[i] != null && oldkeys1[i] != placeholder) || (oldkeys2 != null && oldkeys2[i] != null)) && oldvals[i] != null)
105                 put_(oldkeys1[i], oldkeys2 == null ? null : oldkeys2[i], oldvals[i]);
106     }
107
108     public Object get(Object k1) { return get(k1, null); }
109     public Object get(Object k1, Object k2) {
110         if (k2 != null && keys2 == null) return null;
111         int hash = (k1 == null ? 0 : k1.hashCode()) ^ (k2 == null ? 0 : k2.hashCode());
112         int dest = Math.abs(hash) % vals.length;
113         int odest = dest;
114         int tries = 1;
115         boolean plus = true;
116         while (keys1[dest] != null || (keys2 != null && keys2[dest] != null)) {
117             Object hk1 = keys1[dest];
118             Object hk2 = keys2 == null ? null : keys2[dest];
119             if ((k1 == hk1 || (k1 != null && hk1 != null && k1.equals(hk1))) &&
120                 (k2 == hk2 || (k2 != null && hk2 != null && k2.equals(hk2)))) {
121                 return vals[dest];
122             }
123             dest = Math.abs((odest + (plus ? 1 : -1 ) * tries * tries) % vals.length);
124             if (plus) tries++;
125             plus = !plus;
126         }
127         return null;
128     }
129
130     public void put(Object k1, Object v) { put(k1, null, v); }
131     public void put(Object k1, Object k2, Object v) { put_(k1, k2, v); }
132     private void put_(Object k1, Object k2, Object v) {
133         if (usedslots * loadFactor > vals.length) rehash();
134         int hash = (k1 == null ? 0 : k1.hashCode()) ^ (k2 == null ? 0 : k2.hashCode());
135         int dest = Math.abs(hash) % vals.length;
136         int odest = dest;
137         boolean plus = true;
138         int tries = 1;
139         while (true) {
140             Object hk1 = keys1[dest];
141             Object hk2 = keys2 == null ? null : keys2[dest];
142             if (hk1 == null && hk2 == null) {                                         // empty slot
143                 if (v == null) return;
144                 size++;
145                 usedslots++;
146                 break;
147             }
148
149             if ((k1 == hk1 || (k1 != null && hk1 != null && k1.equals(hk1))) &&       // replacing former entry
150                 (k2 == hk2 || (k2 != null && hk2 != null && k2.equals(hk2)))) {
151
152                 // we don't actually remove things from the table; rather, we insert a placeholder
153                 if (v == null) {
154                     k1 = placeholder;
155                     k2 = null;
156                     size--;
157                 }
158                 break;
159             }
160
161             dest = Math.abs((odest + (plus ? 1 : -1 ) * tries * tries) % vals.length);
162             if (plus) tries++;
163             plus = !plus;
164         }
165
166         keys1[dest] = k1;
167         if (k2 != null && keys2 == null) keys2 = new Object[keys1.length];
168         if (keys2 != null) keys2[dest] = k2;
169         vals[dest] = v;
170     }
171
172     private class HashEnum implements java.util.Enumeration {
173         private int iterator = 0;
174         private int found = 0;
175         
176         public boolean hasMoreElements() {
177             return found < usedslots;
178         }
179
180         public Object nextElement() {
181             if (!hasMoreElements()) throw new java.util.NoSuchElementException();
182
183             Object o = null;
184             while (o == null) o = keys1[iterator++];
185             if (o == null) throw new IllegalStateException("Didn't find an element, when I should have.");
186             found++;
187             
188             return o;
189         }
190     }
191 }
192
193