-
Notifications
You must be signed in to change notification settings - Fork 5.2k
/
SmartFMap.java
315 lines (275 loc) · 7.76 KB
/
SmartFMap.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
// Copyright 2000-2023 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package com.intellij.util;
import com.intellij.openapi.util.Comparing;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.*;
import java.util.function.BiConsumer;
/**
* An immutable map optimized for storing few entries with relatively rare updates.
*
* @see com.intellij.util.fmap.FMap
*/
public final class SmartFMap<K,V> implements Map<K,V> {
private static final SmartFMap<?, ?> EMPTY = new SmartFMap<>(ArrayUtilRt.EMPTY_OBJECT_ARRAY);
private static final int ARRAY_THRESHOLD = 8;
private final Object myMap; // Object[] for map sizes up to ARRAY_THRESHOLD or Map
private SmartFMap(Object map) {
myMap = map;
}
public static @NotNull<K, V> SmartFMap<K, V> emptyMap() {
//noinspection unchecked
return (SmartFMap<K, V>)EMPTY;
}
public @NotNull SmartFMap<K, V> plus(@NotNull K key, V value) {
return new SmartFMap<>(doPlus(myMap, key, value));
}
private static Object doPlus(Object oldMap, Object key, Object value) {
if (oldMap instanceof Map) {
//noinspection unchecked
Map<Object, Object> newMap = new HashMap<>((Map<Object, Object>)oldMap);
newMap.put(key, value);
return newMap;
}
Object[] array = (Object[])oldMap;
for (int i = 0; i < array.length; i += 2) {
if (key.equals(array[i])) {
Object[] newArray = array.clone();
newArray[i + 1] = value;
return newArray;
}
}
if (array.length == 2 * ARRAY_THRESHOLD) {
Map<Object,Object> map = new HashMap<>();
for (int i = 0; i < array.length; i += 2) {
map.put(array[i], array[i + 1]);
}
map.put(key, value);
return map;
}
Object[] newArray = Arrays.copyOf(array, array.length + 2);
newArray[array.length] = key;
newArray[array.length + 1] = value;
return newArray;
}
public SmartFMap<K, V> minus(@NotNull K key) {
if (myMap instanceof Map) {
Map<K, V> newMap = new HashMap<>(asMap());
newMap.remove(key);
if (newMap.size() <= ARRAY_THRESHOLD) {
Object[] newArray = new Object[newMap.size() * 2];
int i = 0;
for (K k : newMap.keySet()) {
newArray[i++] = k;
newArray[i++] = newMap.get(k);
}
return new SmartFMap<>(newArray);
}
return new SmartFMap<>(newMap);
}
Object[] array = (Object[])myMap;
for (int i = 0; i < array.length; i += 2) {
if (key.equals(array[i])) {
if (size() == 1) {
return emptyMap();
}
Object[] newArray = new Object[array.length - 2];
System.arraycopy(array, 0, newArray, 0, i);
System.arraycopy(array, i + 2, newArray, i, array.length - i - 2);
return new SmartFMap<>(newArray);
}
}
return this;
}
public SmartFMap<K, V> plusAll(Map<? extends K, ? extends V> m) {
SmartFMap<K, V> result = this;
for (Map.Entry<? extends K, ? extends V> e : m.entrySet()) {
result = result.plus(e.getKey(), e.getValue());
}
return result;
}
public SmartFMap<K, V> minusAll(@NotNull Collection<? extends K> keys) {
SmartFMap<K, V> result = this;
for (K key : keys) {
result = result.minus(key);
}
return result;
}
@Override
public boolean equals(Object obj) {
if (myMap instanceof Map) {
return myMap.equals(obj);
}
if (!(obj instanceof Map)) {
return false;
}
Map<?, ?> map = (Map<?, ?>)obj;
if (size() != map.size()) {
return false;
}
Object[] array = (Object[])myMap;
for (int i = 0; i < array.length; i += 2) {
if (!Comparing.equal(array[i + 1], map.get(array[i]))) {
return false;
}
}
return true;
}
@Override
public int hashCode() {
return entrySet().hashCode();
}
@Override
public boolean containsKey(Object key) {
if (key == null) {
return false;
}
if (myMap instanceof Map) {
return asMap().containsKey(key);
}
Object[] array = (Object[])myMap;
for (int i = 0; i < array.length; i += 2) {
if (key.equals(array[i])) {
return true;
}
}
return false;
}
@Override
public boolean containsValue(Object value) {
return false;
}
@Override
public @Nullable V get(Object key) {
if (key == null) {
return null;
}
if (myMap instanceof Map) {
return asMap().get(key);
}
Object[] array = (Object[])myMap;
for (int i = 0; i < array.length; i += 2) {
if (key.equals(array[i])) {
//noinspection unchecked
return (V)array[i + 1];
}
}
return null;
}
/**
* @deprecated not supported, use {@link #plus(Object, Object)}
*/
@Override
@Deprecated
public V put(K key, V value) {
throw new UnsupportedOperationException();
}
/**
* @deprecated not supported, use {@link #plusAll(Map)}
*/
@Override
@Deprecated
public void putAll(@NotNull Map<? extends K, ? extends V> m) {
throw new UnsupportedOperationException();
}
/**
* @deprecated not supported
*/
@Override
@Deprecated
public void clear() {
throw new UnsupportedOperationException();
}
@Override
public @NotNull Set<K> keySet() {
if (isEmpty()) return Collections.emptySet();
LinkedHashSet<K> result = new LinkedHashSet<>();
for (Entry<K, V> entry : entrySet()) {
result.add(entry.getKey());
}
return Collections.unmodifiableSet(result);
}
@Override
public @NotNull Collection<V> values() {
if (isEmpty()) return Collections.emptyList();
ArrayList<V> result = new ArrayList<>();
for (Entry<K, V> entry : entrySet()) {
result.add(entry.getValue());
}
return Collections.unmodifiableCollection(result);
}
/**
* @deprecated not supported, use {@link #minus(Object)}
*/
@Override
@Deprecated
public V remove(Object key) {
throw new UnsupportedOperationException();
}
@Override
public int size() {
if (myMap instanceof Map) {
return asMap().size();
}
return ((Object[])myMap).length >> 1;
}
private Map<K, V> asMap() {
//noinspection unchecked
return (Map<K, V>)myMap;
}
@Override
public boolean isEmpty() {
return size() == 0;
}
@Override
public @NotNull Set<Entry<K, V>> entrySet() {
if (isEmpty()) return Collections.emptySet();
LinkedHashSet<Entry<K, V>> set = new LinkedHashSet<>();
if (myMap instanceof Map) {
for (Entry<K, V> entry : asMap().entrySet()) {
set.add(new AbstractMap.SimpleImmutableEntry<>(entry));
}
} else {
Object[] array = (Object[])myMap;
for (int i = 0; i < array.length; i += 2) {
//noinspection unchecked
set.add(new AbstractMap.SimpleImmutableEntry<>((K)array[i], (V)array[i + 1]));
}
}
return Collections.unmodifiableSet(set);
}
@Override
public void forEach(@NotNull BiConsumer<? super K, ? super V> action) {
if (myMap instanceof Map) {
asMap().forEach(action);
}
else {
Object[] array = (Object[])myMap;
for (int i = 0; i < array.length; i += 2) {
//noinspection unchecked
action.accept((K)array[i], (V)array[i + 1]);
}
}
}
// copied from AbstractMap
@Override
public String toString() {
Iterator<Entry<K,V>> i = entrySet().iterator();
if (! i.hasNext())
return "{}";
StringBuilder sb = new StringBuilder();
sb.append('{');
while (true) {
Entry<K, V> e = i.next();
K key = e.getKey();
V value = e.getValue();
sb.append(key == this ? "(this Map)" : key);
sb.append('=');
sb.append(value == this ? "(this Map)" : value);
if (!i.hasNext()) {
return sb.append('}').toString();
}
sb.append(", ");
}
}
}