Download Jar. Libraryless. Click here for Pure Java version (1898L/13K).
1 | !7 |
2 | |
3 | unset flag SymbolAsString. |
4 | |
5 | sclass Symbol {
|
6 | S text; |
7 | |
8 | *() {}
|
9 | *(S *text, bool dummy) {} // weird signature to prevent accidental calling
|
10 | |
11 | public int hashCode() { ret main.hashCode(text); }
|
12 | toString { ret text; }
|
13 | public bool equals(O o) {
|
14 | ret this == o; |
15 | } |
16 | } |
17 | |
18 | sclass SymbolTester {
|
19 | S text; |
20 | |
21 | *() {}
|
22 | *(S *text) {}
|
23 | |
24 | public int hashCode() { ret main.hashCode(text); }
|
25 | public bool equals(O o) {
|
26 | ret o instanceof Symbol && eq(text, o/Symbol.text); |
27 | } |
28 | } |
29 | |
30 | static new WeakHashMap2<Symbol, Bool> symbolMap; |
31 | |
32 | static Symbol symbol(S s) {
|
33 | if (s == null) null; |
34 | synchronized(symbolMap) {
|
35 | // This is a trick that works because of how WeakHashMap compares keys. |
36 | SymbolTester test = new(s); |
37 | WeakHashMap2.Entry<Symbol, Bool> e = symbolMap.getEntry(test); |
38 | Symbol sym = e == null ? null : (Symbol) e.get(); |
39 | //O e = call(symbolMap, 'getEntry, test); |
40 | //Symbol sym = e == null ? null : ((WeakReference<Symbol>) e).get(); |
41 | if (sym == null) |
42 | symbolMap.put(sym = Symbol(s, false), Bool.TRUE); |
43 | ret sym; |
44 | } |
45 | } |
46 | |
47 | p {
|
48 | S s1 = "a", s2 = lower("A");
|
49 | assertNotIdentical(s1, s2); // Now more clearly showing the improvement |
50 | Symbol a = symbol(s1); |
51 | Symbol a2 = symbol(s2); |
52 | assertEquals("a", str(a));
|
53 | assertEquals("a", str(a2));
|
54 | assertIdentical(a, a2); |
55 | Symbol b = symbol("b");
|
56 | assertEquals("b", str(b));
|
57 | print("OK");
|
58 | } |
Began life as a copy of #1010607
download show line numbers debug dex old transpilations
Travelled to 13 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt
No comments. add comment
| Snippet ID: | #1011691 |
| Snippet name: | Test WeakHashMap2 (OK) |
| Eternal ID of this version: | #1011691/7 |
| Text MD5: | 09005e451e7295d9cbcd6f68f8719a12 |
| Transpilation MD5: | 1c4e03eeebe5d517080c1a99f12a5095 |
| Author: | stefan |
| Category: | javax |
| Type: | JavaX source code (desktop) |
| Public (visible to everyone): | Yes |
| Archived (hidden from active list): | No |
| Created/modified: | 2017-12-10 05:32:50 |
| Source code size: | 1436 bytes / 58 lines |
| Pitched / IR pitched: | No / No |
| Views / Downloads: | 794 / 1769 |
| Version history: | 6 change(s) |
| Referenced in: | [show references] |