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

72
LINES

< > BotCompany Repo | #1016606 // LinkedTokenList Spike [OK]

JavaX source code (desktop) [tags: use-pretranspiled] - run with: x30.jar

Download Jar. Uses 3874K of libraries. Click here for Pure Java version (7817L/58K).

1  
!7
2  
3  
sclass LinkedToken {
4  
  S t;
5  
  LinkedToken prev, next;
6  
  LinkedToken prevIdentical, nextIdentical;
7  
  
8  
  *() {}
9  
  *(S *t) {}
10  
}
11  
12  
sclass Tokenization {
13  
  LinkedToken first, last;
14  
  new Map<S, LinkedToken> firstByContent;
15  
  new Map<S, LinkedToken> lastByContent;
16  
  
17  
  bool contains(S t) {
18  
    ret firstByContent.get(t) != null;
19  
  }
20  
  
21  
  int countInstances(S t) {
22  
    int n = 0;
23  
    LinkedToken lt = firstByContent.get(t);
24  
    while (lt != null) {
25  
      ++n;
26  
      lt = lt.nextIdentical;
27  
    }
28  
    ret n;
29  
  }
30  
}
31  
32  
static Tokenization makeTokenization(L<S> tokens) {
33  
  new Tokenization tok;
34  
  for (S t : tokens) {
35  
    LinkedToken lt = new(t);
36  
    lt.t = t;
37  
    if (tok.first == null)
38  
      tok.first = lt;
39  
    else {
40  
      lt.prev = tok.last;
41  
      tok.last.next = lt;
42  
    }
43  
    tok.last = lt;
44  
    lt.prevIdentical = tok.lastByContent.get(t);
45  
    if (lt.prevIdentical == null)
46  
      tok.firstByContent.put(t, lt);
47  
    else {
48  
      lt.prevIdentical.nextIdentical = lt;
49  
    }
50  
    tok.lastByContent.put(t, lt);
51  
  }
52  
  ret tok;
53  
}
54  
55  
p-experiment {
56  
  S text = "hello hello";
57  
  L<S> tokens = javaTok(text);
58  
  
59  
  Tokenization tok = makeTokenization(tokens);
60  
  
61  
  printStruct(tok);
62  
  
63  
  assertVerbose(true);
64  
  assertEquals(true, tok.contains("hello"));
65  
  assertEquals(true, tok.contains(" "));
66  
  assertEquals(false, tok.contains("x"));
67  
  assertEquals(2, tok.countInstances("hello"));
68  
  assertEquals(1, tok.countInstances(" "));
69  
  assertEquals(0, tok.countInstances("x"));
70  
  
71  
  print("OK!");
72  
}

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1016606
Snippet name: LinkedTokenList Spike [OK]
Eternal ID of this version: #1016606/10
Text MD5: f8e5c9664d43f394b5001f2dfe05f70e
Transpilation MD5: f077080717a15cfba02374be0b0ef28f
Author: stefan
Category: javax / parsing
Type: JavaX source code (desktop)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2018-06-25 14:39:19
Source code size: 1547 bytes / 72 lines
Pitched / IR pitched: No / No
Views / Downloads: 315 / 770
Version history: 9 change(s)
Referenced in: [show references]