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

49
LINES

< > BotCompany Repo | #1012268 // GlobalID - more compact representation of a 16-character global ID

JavaX fragment (include) [tags: use-pretranspiled]

Libraryless. Click here for Pure Java version (2377L/15K).

sclass GlobalID implements Comparable<GlobalID> {
  // We need 76 bits for 26^16 IDs
  long a; // lower 64 bits
  int b;  // 76-64=12 higher bits; could be short. change to short when unstructure() is smarter
  
  *() {}
  *(S id) {
    assertGlobalID(id);
    BigInt value = bigint(0);
    for (int i = 0; i < l(id); i++)
      value = plus(mul(value, 26), charDiff(id.charAt(i), 'a'));
      
    ifdef GlobalID_debug
    print("value in constructor: " + value);
    endifdef
    a = value.longValue();
    value = value.shiftRight(64);
    b = value.shortValue();
  }
  
  toString {
    BigInt value = bigint(b);
    value = value.shiftLeft(32);
    value = plus(value, (a >> 32) & 0xFFFFFFFFL);
    value = value.shiftLeft(32);
    value = plus(value, a & 0xFFFFFFFFL);
    
    ifdef GlobalID_debug
    print("value in toString: " + value);
    endifdef
    
    ret bigintToGlobalID(value);
  }
  
  public bool equals(O o) {
    if (!o instanceof GlobalID) false;
    ret o/GlobalID.a == a && o/GlobalID.b == b;
  }
  
  public int hashCode() {
    ret (int) a;
  }
  
  // matches ID's lexical ordering
  public int compareTo(GlobalID id) {
    int diff = b-id.b;
    ret diff != 0 ? diff : Long.compareUnsigned(a, id.a);
  }
}

download  show line numbers  debug dex  old transpilations   

Travelled to 18 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, ekrmjmnbrukm, gwrvuhgaqvyk, irmadwmeruwu, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt, whxojlpjdney, xrpafgyirdlv

No comments. add comment

Snippet ID: #1012268
Snippet name: GlobalID - more compact representation of a 16-character global ID
Eternal ID of this version: #1012268/16
Text MD5: f6d2335ac0ba70e8b492a00085f4c3df
Transpilation MD5: 104fe86d90782d65bd47b685c9d9f40a
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2019-11-27 13:47:44
Source code size: 1284 bytes / 49 lines
Pitched / IR pitched: No / No
Views / Downloads: 499 / 1479
Version history: 15 change(s)
Referenced in: #1034167 - Standard Classes + Interfaces (LIVE, continuation of #1003674)