author | egahlin |
Fri, 24 May 2019 19:39:31 +0200 | |
branch | JEP-349-branch |
changeset 57372 | 50ca040843ea |
parent 57360 | 5d043a159d5c |
permissions | -rw-r--r-- |
50113 | 1 |
/* |
57360 | 2 |
* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved. |
50113 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. Oracle designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
|
10 |
* |
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
24 |
*/ |
|
25 |
||
26 |
package jdk.jfr.consumer; |
|
27 |
||
57360 | 28 |
import java.util.BitSet; |
29 |
import java.util.function.Consumer; |
|
30 |
import java.util.function.LongConsumer; |
|
31 |
||
32 |
@SuppressWarnings("unchecked") |
|
57372
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57360
diff
changeset
|
33 |
public final class LongMap<T> { |
57360 | 34 |
private static final int MAXIMUM_CAPACITY = 1 << 30; |
35 |
private static final long[] EMPTY_KEYS = new long[0]; |
|
36 |
private static final Object[] EMPTY_OBJECTS = new Object[0]; |
|
37 |
private static final int DEFAULT_SIZE = 32; |
|
38 |
private static final Object NULL_OBJECT = new Object(); |
|
39 |
||
40 |
private final int bitCount; |
|
41 |
private BitSet bitSet; |
|
42 |
private long[] keys = EMPTY_KEYS; |
|
43 |
private T[] objects = (T[]) EMPTY_OBJECTS; |
|
44 |
private int count; |
|
45 |
private int shift; |
|
46 |
||
47 |
public LongMap() { |
|
48 |
this.bitCount = 0; |
|
49 |
} |
|
50 |
||
51 |
public LongMap(int markBits) { |
|
52 |
this.bitCount = markBits; |
|
53 |
this.bitSet = new BitSet(); |
|
54 |
} |
|
55 |
||
56 |
// Should be 2^n |
|
57 |
private void initialize(int capacity) { |
|
58 |
keys = new long[capacity]; |
|
59 |
objects = (T[]) new Object[capacity]; |
|
60 |
shift = 64 - (31 - Integer.numberOfLeadingZeros(capacity)); |
|
61 |
} |
|
62 |
||
63 |
public void claimBits() { |
|
64 |
// flip last bit back and forth to make bitset expand to max size |
|
65 |
int lastBit = bitSetIndex(objects.length - 1, bitCount -1); |
|
66 |
bitSet.flip(lastBit); |
|
67 |
bitSet.flip(lastBit); |
|
68 |
} |
|
50113 | 69 |
|
57360 | 70 |
public void setId(long id, int bitIndex) { |
71 |
int bitSetIndex = bitSetIndex(tableIndexOf(id), bitIndex); |
|
72 |
bitSet.set(bitSetIndex, true); |
|
73 |
} |
|
74 |
||
75 |
public void clearId(long id, int bitIndex) { |
|
76 |
int bitSetIndex = bitSetIndex(tableIndexOf(id), bitIndex); |
|
77 |
bitSet.set(bitSetIndex, false); |
|
78 |
} |
|
79 |
||
80 |
public boolean isSetId(long id, int bitIndex) { |
|
81 |
int bitSetIndex = bitSetIndex(tableIndexOf(id), bitIndex); |
|
82 |
return bitSet.get(bitSetIndex); |
|
83 |
} |
|
84 |
||
85 |
private int bitSetIndex(int tableIndex, int bitIndex) { |
|
86 |
return bitCount * tableIndex + bitIndex; |
|
87 |
} |
|
50113 | 88 |
|
57360 | 89 |
private int tableIndexOf(long id) { |
90 |
int index = index(id); |
|
91 |
while (true) { |
|
92 |
if (objects[index] == null) { |
|
93 |
throw new InternalError("Unknown id"); |
|
94 |
} |
|
95 |
if (keys[index] == id) { |
|
96 |
return index; |
|
97 |
} |
|
98 |
index++; |
|
99 |
if (index == keys.length) { |
|
100 |
index = 0; |
|
101 |
} |
|
102 |
} |
|
103 |
} |
|
104 |
||
105 |
public boolean hasKey(long id) { |
|
106 |
int index = index(id); |
|
107 |
while (true) { |
|
108 |
if (objects[index] == null) { |
|
109 |
return false; |
|
110 |
} |
|
111 |
if (keys[index] == id) { |
|
112 |
return true; |
|
113 |
} |
|
114 |
index++; |
|
115 |
if (index == keys.length) { |
|
116 |
index = 0; |
|
117 |
} |
|
118 |
} |
|
50113 | 119 |
} |
120 |
||
57360 | 121 |
public void expand(int size) { |
122 |
int l = 4 * size / 3; |
|
123 |
if (l <= keys.length) { |
|
124 |
return; |
|
125 |
} |
|
126 |
int n = tableSizeFor(l); |
|
127 |
LongMap<T> temp = new LongMap<>(bitCount); |
|
128 |
temp.initialize(n); |
|
129 |
// Optimization, avoid growing while copying bits |
|
130 |
if (bitCount > 0 && !bitSet.isEmpty()) { |
|
131 |
temp.claimBits(); |
|
132 |
claimBits(); |
|
133 |
} |
|
134 |
for (int tIndex = 0; tIndex < keys.length; tIndex++) { |
|
135 |
T o = objects[tIndex]; |
|
136 |
if (o != null) { |
|
137 |
long key = keys[tIndex]; |
|
138 |
temp.put(key, o); |
|
139 |
if (bitCount != 0) { |
|
140 |
for (int bIndex = 0; bIndex < bitCount; bIndex++) { |
|
141 |
boolean bitValue = isSetId(key, bIndex); |
|
142 |
if (bitValue) { |
|
143 |
temp.setId(key, bIndex); |
|
144 |
} |
|
145 |
} |
|
146 |
} |
|
147 |
} |
|
148 |
} |
|
149 |
keys = temp.keys; |
|
150 |
objects = temp.objects; |
|
151 |
shift = temp.shift; |
|
152 |
bitSet = temp.bitSet; |
|
50113 | 153 |
} |
154 |
||
57360 | 155 |
public void put(long id, T object) { |
156 |
if (keys == EMPTY_KEYS) { |
|
157 |
// Lazy initialization |
|
158 |
initialize(DEFAULT_SIZE); |
|
159 |
} |
|
160 |
if (count > 3 * keys.length / 4) { |
|
161 |
expand(2 * keys.length); |
|
162 |
} |
|
163 |
if (object == null) { |
|
164 |
object = (T) NULL_OBJECT; |
|
165 |
} |
|
166 |
||
167 |
int index = index(id); |
|
168 |
// probe for empty slot |
|
169 |
while (true) { |
|
170 |
if (objects[index] == null) { |
|
171 |
keys[index] = id; |
|
172 |
objects[index] = object; |
|
173 |
count++; |
|
174 |
return; |
|
175 |
} |
|
176 |
// if it already exists, replace |
|
177 |
if (keys[index] == id) { |
|
178 |
objects[index] = object; |
|
179 |
return; |
|
180 |
} |
|
181 |
index++; |
|
182 |
if (index == keys.length) { |
|
183 |
index = 0; |
|
184 |
} |
|
185 |
} |
|
186 |
} |
|
187 |
public T getAt(int tableIndex) { |
|
188 |
T o = objects[tableIndex]; |
|
189 |
return o == NULL_OBJECT ? null : o; |
|
50113 | 190 |
} |
191 |
||
57360 | 192 |
public T get(long id) { |
193 |
if (keys == EMPTY_KEYS) { |
|
194 |
return null; |
|
195 |
} |
|
196 |
int index = index(id); |
|
197 |
while (true) { |
|
198 |
if (objects[index] == null) { |
|
199 |
return null; |
|
200 |
} |
|
201 |
if (keys[index] == id) { |
|
202 |
return getAt(index); |
|
203 |
} |
|
204 |
index++; |
|
205 |
if (index == keys.length) { |
|
206 |
index = 0; |
|
207 |
} |
|
208 |
} |
|
209 |
} |
|
210 |
||
211 |
private int index(long id) { |
|
212 |
// int hash = (int) (id ^ (id >>> 32)); |
|
213 |
// return hash & (keys.length - 1); |
|
214 |
return (int) ((id * -7046029254386353131L) >>> shift); |
|
215 |
} |
|
216 |
||
217 |
// Copied from HashMap::tableSizeFor |
|
218 |
private static final int tableSizeFor(int cap) { |
|
219 |
int n = -1 >>> Integer.numberOfLeadingZeros(cap - 1); |
|
220 |
return (n < 0) ? 1 : (n >= MAXIMUM_CAPACITY) ? MAXIMUM_CAPACITY : n + 1; |
|
221 |
} |
|
222 |
||
223 |
public void forEachKey(LongConsumer keyTraverser) { |
|
224 |
for (int i = 0; i < keys.length; i++) { |
|
225 |
if (objects[i] != null) { |
|
226 |
keyTraverser.accept(keys[i]); |
|
227 |
} |
|
228 |
} |
|
229 |
} |
|
230 |
||
231 |
public void forEach(Consumer<T> consumer) { |
|
232 |
for (int i = 0; i < keys.length; i++) { |
|
233 |
T o = objects[i]; |
|
234 |
if (o != null) { |
|
235 |
consumer.accept(o); |
|
236 |
} |
|
237 |
} |
|
238 |
} |
|
239 |
||
240 |
public int size() { |
|
241 |
return count; |
|
242 |
} |
|
243 |
||
244 |
public String toString() { |
|
245 |
StringBuilder sb = new StringBuilder(); |
|
246 |
for (int i = 0; i < objects.length; i++) { |
|
247 |
sb.append(i); |
|
248 |
sb.append(": id="); |
|
249 |
sb.append(keys[i]); |
|
250 |
sb.append(" "); |
|
251 |
sb.append(objects[i]); |
|
252 |
sb.append("\n"); |
|
253 |
} |
|
254 |
return sb.toString(); |
|
50113 | 255 |
} |
256 |
} |