Libraryless. Click here for Pure Java version (2377L/15K).
1 | sclass GlobalID implements Comparable<GlobalID> { |
2 | // We need 76 bits for 26^16 IDs |
3 | long a; // lower 64 bits |
4 | int b; // 76-64=12 higher bits; could be short. change to short when unstructure() is smarter |
5 | |
6 | *() {} |
7 | *(S id) { |
8 | assertGlobalID(id); |
9 | BigInt value = bigint(0); |
10 | for (int i = 0; i < l(id); i++) |
11 | value = plus(mul(value, 26), charDiff(id.charAt(i), 'a')); |
12 | |
13 | ifdef GlobalID_debug |
14 | print("value in constructor: " + value); |
15 | endifdef |
16 | a = value.longValue(); |
17 | value = value.shiftRight(64); |
18 | b = value.shortValue(); |
19 | } |
20 | |
21 | toString { |
22 | BigInt value = bigint(b); |
23 | value = value.shiftLeft(32); |
24 | value = plus(value, (a >> 32) & 0xFFFFFFFFL); |
25 | value = value.shiftLeft(32); |
26 | value = plus(value, a & 0xFFFFFFFFL); |
27 | |
28 | ifdef GlobalID_debug |
29 | print("value in toString: " + value); |
30 | endifdef |
31 | |
32 | ret bigintToGlobalID(value); |
33 | } |
34 | |
35 | public bool equals(O o) { |
36 | if (!o instanceof GlobalID) false; |
37 | ret o/GlobalID.a == a && o/GlobalID.b == b; |
38 | } |
39 | |
40 | public int hashCode() { |
41 | ret (int) a; |
42 | } |
43 | |
44 | // matches ID's lexical ordering |
45 | public int compareTo(GlobalID id) { |
46 | int diff = b-id.b; |
47 | ret diff != 0 ? diff : Long.compareUnsigned(a, id.a); |
48 | } |
49 | } |
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: | 589 / 1600 |
Version history: | 15 change(s) |
Referenced in: | [show references] |