ae95978cf8bc7abbd0ab023fa1e33a0f35eb0f40
[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[] vals() {
50         Object[] ret = new Object[size()];
51         int j = 0;
52         for(int i=0; i<vals.length; i++)
53             if (vals[i] != null && vals[i] != placeholder)
54                 ret[j++] = vals[i];
55         return ret;
56     }
57
58     /** empties the table */
59     public void clear() {
60         size = 0;
61         usedslots = 0;
62         for(int i=0; i<vals.length; i++) {
63             vals[i] = null;
64             keys1[i] = null;
65             if (keys2 != null) keys2[i] = null;
66         }
67     }
68
69     /** returns all the primary keys in the table */
70     public Enumeration keys() { return new HashEnum(); }
71
72     public Hash() { this(25, 3); }
73     public Hash(int initialcapacity, int loadFactor) {
74         // using a pseudoprime in the form 4x+3 ensures full coverage
75         initialcapacity = initialcapacity / 4;
76         initialcapacity = 4 * initialcapacity + 3;
77         keys1 = new Object[initialcapacity];
78         vals = new Object[initialcapacity];
79         this.loadFactor = loadFactor;
80     }
81     
82     public void remove(Object k1) { remove(k1, null); }
83     public void remove(Object k1, Object k2) { put_(k1, k2, null); }
84
85     private void rehash() {
86         Object[] oldkeys1 = keys1;
87         Object[] oldkeys2 = keys2;
88         Object[] oldvals = vals;
89         keys1 = new Object[oldvals.length * 2];
90         keys2 = oldkeys2 == null ? null : new Object[oldvals.length * 2];
91         vals = new Object[oldvals.length * 2];
92         size = 0;
93         usedslots = 0;
94         for(int i=0; i<oldvals.length; i++)
95             if (((oldkeys1[i] != null && oldkeys1[i] != placeholder) || (oldkeys2 != null && oldkeys2[i] != null)) && oldvals[i] != null)
96                 put_(oldkeys1[i], oldkeys2 == null ? null : oldkeys2[i], oldvals[i]);
97     }
98
99     public Object get(Object k1) { return get(k1, null); }
100     public Object get(Object k1, Object k2) {
101         if (k2 != null && keys2 == null) return null;
102         int hash = (k1 == null ? 0 : k1.hashCode()) ^ (k2 == null ? 0 : k2.hashCode());
103         int dest = Math.abs(hash) % vals.length;
104         int odest = dest;
105         int tries = 1;
106         boolean plus = true;
107         while (keys1[dest] != null || (keys2 != null && keys2[dest] != null)) {
108             Object hk1 = keys1[dest];
109             Object hk2 = keys2 == null ? null : keys2[dest];
110             if ((k1 == hk1 || (k1 != null && hk1 != null && k1.equals(hk1))) &&
111                 (k2 == hk2 || (k2 != null && hk2 != null && k2.equals(hk2)))) {
112                 return vals[dest];
113             }
114             dest = Math.abs((odest + (plus ? 1 : -1 ) * tries * tries) % vals.length);
115             if (plus) tries++;
116             plus = !plus;
117         }
118         return null;
119     }
120
121     public void put(Object k1, Object v) { put(k1, null, v); }
122     public void put(Object k1, Object k2, Object v) { put_(k1, k2, v); }
123     private void put_(Object k1, Object k2, Object v) {
124         if (usedslots * loadFactor > vals.length) rehash();
125         int hash = (k1 == null ? 0 : k1.hashCode()) ^ (k2 == null ? 0 : k2.hashCode());
126         int dest = Math.abs(hash) % vals.length;
127         int odest = dest;
128         boolean plus = true;
129         int tries = 1;
130         while (true) {
131             Object hk1 = keys1[dest];
132             Object hk2 = keys2 == null ? null : keys2[dest];
133             if (hk1 == null && hk2 == null) {                                         // empty slot
134                 if (v == null) return;
135                 size++;
136                 usedslots++;
137                 break;
138             }
139
140             if ((k1 == hk1 || (k1 != null && hk1 != null && k1.equals(hk1))) &&       // replacing former entry
141                 (k2 == hk2 || (k2 != null && hk2 != null && k2.equals(hk2)))) {
142
143                 // we don't actually remove things from the table; rather, we insert a placeholder
144                 if (v == null) {
145                     k1 = placeholder;
146                     k2 = null;
147                     size--;
148                 }
149                 break;
150             }
151
152             dest = Math.abs((odest + (plus ? 1 : -1 ) * tries * tries) % vals.length);
153             if (plus) tries++;
154             plus = !plus;
155         }
156
157         keys1[dest] = k1;
158         if (k2 != null && keys2 == null) keys2 = new Object[keys1.length];
159         if (keys2 != null) keys2[dest] = k2;
160         vals[dest] = v;
161     }
162
163     private class HashEnum implements java.util.Enumeration {
164         private int iterator = 0;
165         private int found = 0;
166         
167         public boolean hasMoreElements() {
168             return found < usedslots;
169         }
170
171         public Object nextElement() {
172             if (!hasMoreElements()) throw new java.util.NoSuchElementException();
173
174             Object o = null;
175             while (o == null) o = keys1[iterator++];
176             if (o == null) throw new IllegalStateException("Didn't find an element, when I should have.");
177             found++;
178             
179             return o;
180         }
181     }
182 }
183
184