Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

54
LINES

< > BotCompany Repo | #1010608 // class Symbol [CharSequence comparable with ==] + MasterSymbol v2 - a case-insensitive string with faster compare (though not with == anymore)

JavaX fragment (include)

1  
set flag SymbolWithMasterSymbol.
2  
3  
static class Symbol implements CharSequence, Comparable<Symbol> {
4  
  S text;
5  
  MasterSymbol master;
6  
  
7  
  *() {}
8  
  *(S *text, bool dummy) {} // weird signature to prevent accidental calling
9  
  *(S *text, MasterSymbol *master) {}
10  
  
11  
  bool isMaster() { ret master == this; }
12  
  final public int hashCode() { ret master.lowerCaseHash; }
13  
  S text() { ret text; }
14  
  final toString { ret text; }
15  
  S lowerCase() { ret lower(text()); }
16  
  final public bool equals(O o) {
17  
    if (!(o instanceof Symbol)) false;
18  
    bool result = o instanceof Symbol && master == o/Symbol.master;
19  
    ifdef symbol_debug
20  
      print("Symbol.equals: " + this + " vs " + o + " => " + result);
21  
    endifdef
22  
    ret result;
23  
  }
24  
  
25  
  public int length() { ret text.length(); }
26  
  public char charAt(int index) { ret text.charAt(index); }
27  
  public CharSequence subSequence(int start, int end) {
28  
    ret text.substring(start, end);
29  
  }
30  
  
31  
  // This really has to be correct for TreeMap<Symbol, ...>
32  
  // equals() <=> compareTo() == 0 or at least
33  
  // equals() => compareTo() == 0.
34  
  public int compareTo(Symbol s) {
35  
    //ret lowerCase().compareTo(s.lowerCase());
36  
    ret lowerCaseCompare(text, s.text);
37  
  }
38  
}
39  
40  
static final class MasterSymbol extends Symbol {
41  
  //S lowerCase;
42  
  int lowerCaseHash;
43  
  bool dollarVar;
44  
  
45  
  *() {}
46  
  *(S *text, bool dummy) {
47  
    master = this;
48  
    S lowerCase = lower(text);
49  
    lowerCaseHash = lowerCase.hashCode();
50  
    dollarVar = isDollarVar(lowerCase);
51  
  }
52  
  
53  
  int lowerCaseHash() { ret lowerCaseHash; }
54  
}

download  show line numbers  debug dex  old transpilations   

Travelled to 14 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1010608
Snippet name: class Symbol [CharSequence comparable with ==] + MasterSymbol v2 - a case-insensitive string with faster compare (though not with == anymore)
Eternal ID of this version: #1010608/28
Text MD5: 8e070907420aa9097a8639f4eabaac4e
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2021-09-08 03:49:25
Source code size: 1588 bytes / 54 lines
Pitched / IR pitched: No / No
Views / Downloads: 613 / 1327
Version history: 27 change(s)
Referenced in: [show references]