-
-
Notifications
You must be signed in to change notification settings - Fork 439
Expand file tree
/
Copy pathVariablesMap.java
More file actions
355 lines (302 loc) · 9.82 KB
/
VariablesMap.java
File metadata and controls
355 lines (302 loc) · 9.82 KB
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
package ch.njol.skript.variables;
import ch.njol.skript.lang.Variable;
import ch.njol.util.StringUtils;
import org.jetbrains.annotations.Nullable;
import org.skriptlang.skript.util.IndexTrackingTreeMap;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.TreeMap;
/**
* A map for storing variables in a sorted and efficient manner.
*/
final class VariablesMap {
/**
* The comparator for comparing variable names.
*/
static final Comparator<String> VARIABLE_NAME_COMPARATOR = (s1, s2) -> {
if (s1 == null)
return s2 == null ? 0 : -1;
if (s2 == null)
return 1;
int i = 0;
int j = 0;
boolean lastNumberNegative = false;
boolean afterDecimalPoint = false;
while (i < s1.length() && j < s2.length()) {
char c1 = s1.charAt(i);
char c2 = s2.charAt(j);
if ('0' <= c1 && c1 <= '9' && '0' <= c2 && c2 <= '9') {
// Numbers/digits are treated differently from other characters.
// The index after the last digit
int i2 = StringUtils.findLastDigit(s1, i);
int j2 = StringUtils.findLastDigit(s2, j);
// Amount of leading zeroes
int z1 = 0;
int z2 = 0;
// Skip leading zeroes (except for the last if all 0's)
if (!afterDecimalPoint) {
if (c1 == '0') {
while (i < i2 - 1 && s1.charAt(i) == '0') {
i++;
z1++;
}
}
if (c2 == '0') {
while (j < j2 - 1 && s2.charAt(j) == '0') {
j++;
z2++;
}
}
}
// Keep in mind that c1 and c2 may not have the right value (e.g. s1.charAt(i)) for the rest of this block
// If the number is prefixed by a '-', it should be treated as negative, thus inverting the order.
// If the previous number was negative, and the only thing separating them was a '.',
// then this number should also be in inverted order.
boolean previousNegative = lastNumberNegative;
// i - z1 contains the first digit, so i - z1 - 1 may contain a `-` indicating this number is negative
lastNumberNegative = i - z1 > 0 && s1.charAt(i - z1 - 1) == '-';
int isPositive = (lastNumberNegative | previousNegative) ? -1 : 1;
// Different length numbers (99 > 9)
if (!afterDecimalPoint && i2 - i != j2 - j)
return ((i2 - i) - (j2 - j)) * isPositive;
// Iterate over the digits
while (i < i2 && j < j2) {
char d1 = s1.charAt(i);
char d2 = s2.charAt(j);
// If the digits differ, return a value dependent on the sign
if (d1 != d2)
return (d1 - d2) * isPositive;
i++;
j++;
}
// Different length numbers (1.99 > 1.9)
if (afterDecimalPoint && i2 - i != j2 - j)
return ((i2 - i) - (j2 - j)) * isPositive;
// If the numbers are equal, but either has leading zeroes,
// more leading zeroes is a lesser number (01 < 1)
if (z1 != z2)
return (z1 - z2) * isPositive;
afterDecimalPoint = true;
} else {
// Normal characters
if (c1 != c2)
return c1 - c2;
// Reset the last number flags if we're exiting a number.
if (c1 != '.') {
lastNumberNegative = false;
afterDecimalPoint = false;
}
i++;
j++;
}
}
if (i < s1.length())
return lastNumberNegative ? -1 : 1;
if (j < s2.length())
return lastNumberNegative ? 1 : -1;
return 0;
};
/**
* The map that stores all non-list variables.
*/
final HashMap<String, Object> hashMap = new HashMap<>();
/**
* The tree of variables, branched by the list structure of the variables.
*/
final TreeMap<String, Object> treeMap = new TreeMap<>();
/**
* Returns the internal value of the requested variable.
* <p>
* <b>Do not modify the returned value!</b>
*
* @param name the name of the variable, possibly a list variable.
* @return an {@link Object} for a normal variable or a
* {@code Map<String, Object>} for a list variable,
* or {@code null} if the variable is not set.
*/
@SuppressWarnings("unchecked")
@Nullable
Object getVariable(String name) {
if (!name.endsWith("*")) {
// Not a list variable, quick access from the hash map
return hashMap.get(name);
} else {
// List variable, search the tree branches
String[] split = Variables.splitVariableName(name);
Map<String, Object> parent = treeMap;
// Iterate over the parts of the variable name
for (int i = 0; i < split.length; i++) {
String n = split[i];
if (n.equals("*")) {
// End of variable name, return map
assert i == split.length - 1;
return parent;
}
// Check if the current (sub-)tree has the expected child node
Object childNode = parent.get(n);
if (childNode == null)
return null;
// Continue the iteration if the child node is a tree itself
if (childNode instanceof Map) {
// Continue iterating with the subtree
parent = (Map<String, Object>) childNode;
assert i != split.length - 1;
} else {
// ..., otherwise the list variable doesn't exist here
return null;
}
}
return null;
}
}
/**
* Sets the given variable to the given value.
* <p>
* This method accepts list variables,
* but these may only be set to {@code null}.
*
* @param name the variable name.
* @param value the variable value, {@code null} to delete the variable.
*/
@SuppressWarnings("unchecked")
void setVariable(String name, @Nullable Object value) {
// First update the hash map easily
if (!name.endsWith("*")) {
if (value == null)
hashMap.remove(name);
else
hashMap.put(name, value);
}
// Then update the tree map by going down the branches
String[] split = Variables.splitVariableName(name);
TreeMap<String, Object> parent = treeMap;
// Iterate over the parts of the variable name
for (int i = 0; i < split.length; i++) {
String childNodeName = split[i];
Object childNode = parent.get(childNodeName);
if (childNode == null) {
// Expected child node not found
if (i == split.length - 1) {
// End of the variable name reached, set variable if needed
if (value != null)
parent.put(childNodeName, value);
break;
} else if (value != null) {
// Create child node, add it to parent and continue iteration
childNode = new IndexTrackingTreeMap<>(VARIABLE_NAME_COMPARATOR);
parent.put(childNodeName, childNode);
parent = (TreeMap<String, Object>) childNode;
} else {
// Want to set variable to null, bu variable is already null
break;
}
} else if (childNode instanceof TreeMap) {
// Child node found
TreeMap<String, Object> childNodeMap = ((TreeMap<String, Object>) childNode);
if (i == split.length - 1) {
// End of variable name reached, adjust child node accordingly
if (value == null)
childNodeMap.remove(null);
else
childNodeMap.put(null, value);
break;
} else if (i == split.length - 2 && split[i + 1].equals("*")) {
// Second to last part of variable name
assert value == null;
// Delete all indices of the list variable from hashMap
deleteFromHashMap(StringUtils.join(split, Variable.SEPARATOR, 0, i + 1), childNodeMap);
// If the list variable itself has a value ,
// e.g. list `{mylist::3}` while variable `{mylist}` also has a value,
// then adjust the parent for that
Object currentChildValue = childNodeMap.get(null);
if (currentChildValue == null)
parent.remove(childNodeName);
else
parent.put(childNodeName, currentChildValue);
break;
} else {
// Continue iteration
parent = childNodeMap;
}
} else {
// Ran into leaf node
if (i == split.length - 1) {
// If we arrived at the end of the variable name, update parent
if (value == null)
parent.remove(childNodeName);
else
parent.put(childNodeName, value);
break;
} else if (value != null) {
// Need to continue iteration, create new child node and put old value in it
TreeMap<String, Object> newChildNodeMap = new IndexTrackingTreeMap<>(VARIABLE_NAME_COMPARATOR);
newChildNodeMap.put(null, childNode);
// Add new child node to parent
parent.put(childNodeName, newChildNodeMap);
parent = newChildNodeMap;
} else {
break;
}
}
}
}
/**
* Deletes all indices of a list variable from the {@link #hashMap}.
*
* @param parent the list variable prefix,
* e.g. {@code list} for {@code list::*}.
* @param current the map of the list variable.
*/
@SuppressWarnings("unchecked")
void deleteFromHashMap(String parent, TreeMap<String, Object> current) {
for (Entry<String, Object> e : current.entrySet()) {
if (e.getKey() == null)
continue;
String childName = parent + Variable.SEPARATOR + e.getKey();
// Remove from hashMap
hashMap.remove(childName);
// Recurse if needed
Object val = e.getValue();
if (val instanceof TreeMap) {
deleteFromHashMap(childName, (TreeMap<String, Object>) val);
}
}
}
/**
* Creates a copy of this map.
*
* @return the copy.
*/
public VariablesMap copy() {
VariablesMap copy = new VariablesMap();
copy.hashMap.putAll(hashMap);
TreeMap<String, Object> treeMapCopy = copyTreeMap(treeMap);
copy.treeMap.putAll(treeMapCopy);
return copy;
}
/**
* Makes a deep copy of the given {@link TreeMap}.
* <p>
* The 'deep copy' means that each subtree of the given tree is copied
* as well.
*
* @param original the original tree map.
* @return the copy.
*/
@SuppressWarnings("unchecked")
private static TreeMap<String, Object> copyTreeMap(TreeMap<String, Object> original) {
TreeMap<String, Object> copy = new IndexTrackingTreeMap<>(VARIABLE_NAME_COMPARATOR);
for (Entry<String, Object> child : original.entrySet()) {
String key = child.getKey();
Object value = child.getValue();
// Copy by recursion if the child is a TreeMap
if (value instanceof TreeMap) {
value = copyTreeMap((TreeMap<String, Object>) value);
}
copy.put(key, value);
}
return copy;
}
}