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

52
LINES

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

JavaX fragment (include)

static class Symbol implements CharSequence, Comparable<Symbol> {
  S text;
  MasterSymbol master;
  
  *() {}
  *(S *text, bool dummy) {} // weird signature to prevent accidental calling
  *(S *text, MasterSymbol *master) {}
  
  bool isMaster() { ret master == this; }
  final public int hashCode() { ret master.lowerCaseHash; }
  S text() { ret text; }
  final toString { ret text; }
  S lowerCase() { ret lower(text()); }
  final public bool equals(O o) {
    if (!(o instanceof Symbol)) false;
    bool result = o instanceof Symbol && master == o/Symbol.master;
    ifdef symbol_debug
      print("Symbol.equals: " + this + " vs " + o + " => " + result);
    endifdef
    ret result;
  }
  
  public int length() { ret text.length(); }
  public char charAt(int index) { ret text.charAt(index); }
  public CharSequence subSequence(int start, int end) {
    ret text.substring(start, end);
  }
  
  // This really has to be correct for TreeMap<Symbol, ...>
  // equals() <=> compareTo() == 0 or at least
  // equals() => compareTo() == 0.
  public int compareTo(Symbol s) {
    //ret lowerCase().compareTo(s.lowerCase());
    ret lowerCaseCompare(text, s.text);
  }
}

static final class MasterSymbol extends Symbol {
  //S lowerCase;
  int lowerCaseHash;
  bool dollarVar;
  
  *() {}
  *(S *text, bool dummy) {
    master = this;
    S lowerCase = lower(text);
    lowerCaseHash = lowerCase.hashCode();
    dollarVar = isDollarVar(lowerCase);
  }
  
  int lowerCaseHash() { ret lowerCaseHash; }
}

download  show line numbers  debug dex   

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

No comments. add comment

Snippet ID: #1010608
Snippet name: class Symbol + MasterSymbol v2 - a case-insensitive string with faster compare (though not with == anymore)
Eternal ID of this version: #1010608/26
Text MD5: 28da7eafffa0f143091bb74d0c99f421
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2017-12-10 22:06:45
Source code size: 1552 bytes / 52 lines
Pitched / IR pitched: No / No
Views / Downloads: 286 / 827
Version history: 25 change(s)
Referenced in: [show references]

Formerly at http://tinybrain.de/1010608 & http://1010608.tinybrain.de