-
Notifications
You must be signed in to change notification settings - Fork 668
/
PreHashedMap.java
319 lines (279 loc) · 11.1 KB
/
PreHashedMap.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
/*
* Copyright (c) 2004, 2012, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package sun.util;
import java.util.AbstractMap;
import java.util.AbstractSet;
import java.util.Iterator;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Set;
/**
* A precomputed hash map.
*
* <p> Subclasses of this class are of the following form:
*
* <blockquote><pre>
* class FooMap
* extends sun.util.PreHashedMap<String>
* {
*
* private FooMap() {
* super(ROWS, SIZE, SHIFT, MASK);
* }
*
* protected void init(Object[] ht) {
* ht[0] = new Object[] { "key-1", value_1 };
* ht[1] = new Object[] { "key-2", value_2, new Object { "key-3", value_3 } };
* ...
* }
*
* }</pre></blockquote>
*
* <p> The {@code init} method is invoked by the {@code PreHashedMap}
* constructor with an object array long enough for the map's rows. The method
* must construct the hash chain for each row and store it in the appropriate
* element of the array.
*
* <p> Each entry in the map is represented by a unique hash-chain node. The
* final node of a hash chain is a two-element object array whose first element
* is the entry's key and whose second element is the entry's value. A
* non-final node of a hash chain is a three-element object array whose first
* two elements are the entry's key and value and whose third element is the
* next node in the chain.
*
* <p> Instances of this class are mutable and are not safe for concurrent
* access. They may be made immutable and thread-safe via the appropriate
* methods in the {@link java.util.Collections} utility class.
*
* <p> In the JDK build, subclasses of this class are typically created via the
* {@code Hasher} program in the {@code make/tools/Hasher} directory.
*
* @author Mark Reinhold
* @see java.util.AbstractMap
* @since 1.5
*/
/*
* 特制的Map工具类,专为标准字符集类StandardCharsets打造。
*
* 该Map中的数据按行排列,每一行可能包含多个键值对。
* 同一行出现的键的哈希值是相同的,不在同一行的键的哈希值不同。
*
* 例如:
* 1 key1-value1
* 2 key2-value2, key3-value3, key4-value4
* 3 key5-value6, key5-value6
*
* 这6个键值对数据可以存储为:
* Object[] ht = new Object[4];
* ht[1] = new Object{key1, value1};
* ht[2] = new Object{key2, value2, new Object{key3, value3}, new Object{key4, value4}};
* ht[2] = new Object{key5, value5, new Object{key6, value6}};
*/
public abstract class PreHashedMap<V> extends AbstractMap<String, V> {
private final int rows; // 掩码
private final int size; // 键值对数量
private final int shift;
private final int mask; // 映射到行数的掩码
private final Object[] ht; // 包含了很多行的键值对数据,数组中每个元素是一行,每行可能包含多个键值对
/**
* Creates a new map.
*
* <p> This constructor invokes the {@link #init init} method, passing it a
* newly-constructed row array that is {@code rows} elements long.
*
* @param rows The number of rows in the map
* @param size The number of entries in the map
* @param shift The value by which hash codes are right-shifted
* @param mask The value with which hash codes are masked after being shifted
*/
protected PreHashedMap(int rows, int size, int shift, int mask) {
this.rows = rows;
this.size = size;
this.shift = shift;
this.mask = mask;
this.ht = new Object[rows];
init(ht);
}
/**
* Initializes this map.
*
* <p> This method must construct the map's hash chains and store them into
* the appropriate elements of the given hash-table row array.
*
* @param ht The row array to be initialized
*/
// 初始化Map,由子类实现
protected abstract void init(Object[] ht);
// 根据key返回value
public V get(Object k) {
// 先把key按照指定的规则哈希化
int h = (k.hashCode() >> shift) & mask;
// 根据键的哈希值返回整行数据
Object[] a = (Object[]) ht[h];
if(a == null)
return null;
for(; ; ) {
// key匹配,则返回value
if(a[0].equals(k))
return toV(a[1]);
if(a.length < 3)
return null;
// 尝试其他的可能
a = (Object[]) a[2];
}
}
/**
* @throws UnsupportedOperationException If the given key is not part of this map's initial key set
*/
// 设置键值对,并返回成功设置的value
public V put(String k, V v) {
// 先把key按照指定的规则哈希化
int h = (k.hashCode() >> shift) & mask;
Object[] a = (Object[]) ht[h];
if(a == null)
throw new UnsupportedOperationException(k);
for(; ; ) {
if(a[0].equals(k)) {
V ov = toV(a[1]);
a[1] = v;
return ov;
}
if(a.length < 3)
throw new UnsupportedOperationException(k);
a = (Object[]) a[2];
}
}
// 返回key的集合
public Set<String> keySet() {
return new AbstractSet<>() {
public int size() {
return size;
}
public Iterator<String> iterator() {
return new Iterator<>() {
Object[] a = null; // value
String cur = null; // key
private int i = -1;
// 是否还有下一个key
public boolean hasNext() {
if(cur != null)
return true;
return findNext();
}
// 反回下一个key
public String next() {
if(cur == null) {
if(!findNext())
throw new NoSuchElementException();
}
String s = cur;
cur = null;
return s;
}
public void remove() {
throw new UnsupportedOperationException();
}
// 按行遍历查找key
private boolean findNext() {
if(a != null) {
// 如果同一行存在多个键值对
if(a.length == 3) {
a = (Object[]) a[2];
cur = (String) a[0];
return true;
}
// 否则到下一行遍历
i++;
a = null;
}
cur = null;
if(i >= rows)
return false;
if(i < 0 || ht[i] == null) {
do {
if(++i >= rows)
return false;
} while(ht[i] == null);
}
a = (Object[]) ht[i];
cur = (String) a[0];
return true;
}
};
}
};
}
// 返回键值对实体的集合
public Set<Entry<String, V>> entrySet() {
return new AbstractSet<>() {
public int size() {
return size;
}
public Iterator<Entry<String, V>> iterator() {
return new Iterator<>() {
final Iterator<String> i = keySet().iterator();
// 有没有entry取决于有没有key
public boolean hasNext() {
return i.hasNext();
}
public Map.Entry<String, V> next() {
return new Map.Entry<>() {
String k = i.next();
public String getKey() {
return k;
}
public V getValue() {
return get(k);
}
public int hashCode() {
V v = get(k);
return (k.hashCode() + (v == null ? 0 : v.hashCode()));
}
public boolean equals(Object ob) {
if(ob == this)
return true;
if(!(ob instanceof Map.Entry))
return false;
Map.Entry<?, ?> that = (Map.Entry<?, ?>) ob;
return (this.getKey() == null ? that.getKey() == null : this.getKey().equals(that.getKey()))
&& (this.getValue() == null ? that.getValue() == null : this.getValue().equals(that.getValue()));
}
public V setValue(V v) {
throw new UnsupportedOperationException();
}
};
}
public void remove() {
throw new UnsupportedOperationException();
}
};
}
};
}
@SuppressWarnings("unchecked")
private V toV(Object x) {
return (V) x;
}
}