1 | ifndef SymbolAsString |
2 | sclass symbol_Tester { // compares by text (case-sensitive)
|
3 | Symbol symbol; |
4 | |
5 | *() {}
|
6 | *(Symbol *symbol) {}
|
7 | |
8 | public int hashCode() { ret symbol.text.hashCode(); }
|
9 | public bool equals(O o) {
|
10 | if (!o instanceof symbol_Tester) false; |
11 | ret eq(symbol.text, o/symbol_Tester.symbol.text); |
12 | } |
13 | |
14 | toString { ret str(symbol); }
|
15 | } |
16 | |
17 | sclass symbol_MasterTester { // compares by text (case-insensitive)
|
18 | MasterSymbol symbol; |
19 | |
20 | *() {}
|
21 | *(MasterSymbol *symbol) {}
|
22 | |
23 | public int hashCode() { ret symbol.lowerCase.hashCode(); }
|
24 | public bool equals(O o) {
|
25 | if (!o instanceof symbol_MasterTester) false; |
26 | ret eq(symbol.lowerCase, o/symbol_MasterTester.symbol.lowerCase); |
27 | } |
28 | } |
29 | |
30 | static WeakHashMap<symbol_Tester, Bool> symbol_map = unsyncedWeakHashMap(); |
31 | static WeakHashMap<symbol_MasterTester, Bool> masterSymbol_map = unsyncedWeakHashMap(); |
32 | endifndef |
33 | |
34 | static Symbol symbol(S s) {
|
35 | ifdef SymbolAsString |
36 | ret s; |
37 | endifdef |
38 | ifndef SymbolAsString |
39 | if (s == null) null; |
40 | synchronized(symbol_map) {
|
41 | // This is a trick that works because of how WeakHashMap compares keys. |
42 | symbol_MasterTester masterTester = symbol_MasterTester(new MasterSymbol(s, true)); |
43 | O e = call(masterSymbol_map, 'getEntry, masterTester); |
44 | symbol_MasterTester existingMaster = e == null ? null : ((WeakReference<symbol_MasterTester>) e).get(); |
45 | if (existingMaster == null) |
46 | masterSymbol_map.put(existingMaster = masterTester, true); |
47 | ifdef symbol_debug |
48 | if (eqic(s, "Java")) |
49 | print("existing master for " + s + ": " + existingMaster);
|
50 | endifdef |
51 | |
52 | // second lookup after master found |
53 | bool isEq = eq(s, existingMaster.symbol.text); |
54 | Symbol symbol = isEq ? existingMaster.symbol : new Symbol(s, existingMaster.symbol); |
55 | ifdef symbol_debug |
56 | //print("existing master text: " + existingMaster.symbol.text + ", eq=" + isEq + " => " + symbol);
|
57 | endifdef |
58 | symbol_Tester tester = symbol_Tester(symbol); |
59 | e = call(symbol_map, 'getEntry, tester); |
60 | symbol_Tester sym = e == null ? null : ((WeakReference<symbol_Tester>) e).get(); |
61 | if (sym == null) |
62 | symbol_map.put(sym = tester, true); |
63 | ifdef symbol_debug |
64 | if (eqic(s, "Java")) |
65 | print("Symbol for " + s + ": " + symbolToStringFull(sym.symbol);
|
66 | endifdef |
67 | |
68 | ret sym.symbol; |
69 | } |
70 | endifndef |
71 | } |
72 | |
73 | static Symbol symbol(CharSequence s) {
|
74 | if (s == null) null; |
75 | ifdef SymbolAsString |
76 | ret str(s); |
77 | endifdef |
78 | ifndef SymbolAsString |
79 | if (s instanceof Symbol) ret (Symbol) s; |
80 | if (s instanceof S) ret symbol((S) s); |
81 | ret symbol(str(s)); |
82 | endifndef |
83 | } |
84 | |
85 | static Symbol symbol(O o) {
|
86 | ret symbol((CharSequence) o); |
87 | } |
Began life as a copy of #1012686
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: | #1012712 |
| Snippet name: | symbol - make Symbol from String or CharSequence - case-insensitive version with MasterSymbol and testers [buggy] |
| Eternal ID of this version: | #1012712/1 |
| Text MD5: | 1962d0000fa8112069133ab81e4fec99 |
| Author: | stefan |
| Category: | javax |
| Type: | JavaX fragment (include) |
| Public (visible to everyone): | Yes |
| Archived (hidden from active list): | No |
| Created/modified: | 2017-12-10 20:42:05 |
| Source code size: | 2734 bytes / 87 lines |
| Pitched / IR pitched: | No / No |
| Views / Downloads: | 688 / 745 |
| Referenced in: | [show references] |