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