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

167
LINES

< > BotCompany Repo | #1016611 // HybridTokens - an indexed version of a token list [dev.]

JavaX fragment (include)

1  
static class HybridTokens extends AbstractList<S> {
2  
  Tokenization tok;
3  
  L<LinkedToken> pointers;
4  
  static bool debug;
5  
  static int instances;
6  
  
7  
  sclass LinkedToken {
8  
    S t;
9  
    int index = -1;
10  
    LinkedToken prev, next;
11  
    LinkedToken prevIdentical, nextIdentical;
12  
    
13  
    *() {}
14  
    *(S *t) {}
15  
  }
16  
  
17  
  sclass Tokenization {
18  
    LinkedToken first, last;
19  
    new Map<S, LinkedToken> firstByContent;
20  
    new Map<S, LinkedToken> lastByContent;
21  
    
22  
    bool contains(S t) {
23  
      ret firstByContent.get(t) != null;
24  
    }
25  
    
26  
    int countInstances(S t) {
27  
      int n = 0;
28  
      LinkedToken lt = firstByContent.get(t);
29  
      while (lt != null) {
30  
        ++n;
31  
        lt = lt.nextIdentical;
32  
      }
33  
      ret n;
34  
    }
35  
  }
36  
37  
  *() { ++instances; }
38  
  *(L<S> tokens) {
39  
    this();
40  
    tok = makeTokenization(tokens);
41  
    pointers.ensureCapacity(l(tokens));
42  
  }
43  
  
44  
  // required immutable list methods
45  
  
46  
  @Override
47  
  public S get(int i) {
48  
    int j = max(size(),
49  
  }
50  
  
51  
  @Override
52  
  public int size() {
53  
    if (last(pointers) != tok.last) {
54  
      LinkedToken lt = last(pointers);
55  
      if (lt == null) lt = tok.first;
56  
      if (lt != null)
57  
        while ((lt = lt.next) != null)
58  
          pointers.add(lt);
59  
    }
60  
    ret l(pointers);
61  
  }
62  
  
63  
  // required mutable list methods
64  
  
65  
  @Override
66  
  public S set(int i, S a) {
67  
    LinkedToken lt = get(i);
68  
    if (eq(lt.t, a)) ret;
69  
    
70  
    // Take out of old content chain
71  
    if (lt.prevIdentical == null) tok.firstByContent.remove(a);
72  
    else lt.prevIdentical.nextIdentical = lt.nextIdentical;
73  
    if (lt.nextIdentical == null) tok.lastByContent.remove(a);
74  
    else lt.nextIdentical.prevIdentical = lt.prevIdentical;
75  
    
76  
    // Add to new content chain
77  
    lt.prevIdentical = lt_findPrevIdentical(lt);
78  
    if (lt.prevIdentical == null) tok.firstByContent.put(a, lt);
79  
    lt.nextIdentical = lt_findNextIdentical(lt);
80  
    if (lt.nextIdentical == null) tok.lastByContent.put(a, lt);
81  
    
82  
    // Set new value and return old one
83  
    S prev = lt.t;
84  
    lt.t = a;
85  
    ret prev;
86  
  }
87  
  
88  
  @Override
89  
  public void add(int i, S a) {
90  
    LinkedToken lt = get(i);
91  
    if (lt == null) {
92  
      assertTrue(i == size());
93  
      lt_addToken(tok, a);
94  
    } else {
95  
      LinkedToken ltNew = new(a);
96  
      ltNew.last = lt.last;
97  
      if (lt.last != null) lt.last.next = ltNew; else tokens.first = ltNew;
98  
      ltNew.next = lt;
99  
      lt.last = ltNew;
100  
      lt_addToContentChain(lt, tok);
101  
      clearPointersFrom(i);
102  
    }
103  
  }
104  
  
105  
  @Override
106  
  public S remove(int i) {
107  
    S a = l.get(i);
108  
    index.remove(a);
109  
    l.remove(i);
110  
    ret a;
111  
  }
112  
  
113  
  // speed up methods
114  
115  
  @Override  
116  
  protected void removeRange(int fromIndex, int toIndex) {
117  
    for (int i = fromIndex; i < toIndex; i++)
118  
      unindex(i);
119  
    l.subList(fromIndex, toIndex).clear();
120  
  }
121  
  
122  
  @Override
123  
  public int indexOf(O a) {
124  
    LinkedToken lt = tok.firstByContent(a);
125  
    if (lt == null) ret -1;
126  
    makeIndex(lt);
127  
    ret lt.index;
128  
  }
129  
  
130  
  @Override
131  
  public bool contains(O a) {
132  
    ret tok.contains((S) a);
133  
  }
134  
  
135  
  @Override
136  
  public void clear() {
137  
    index.clear();
138  
    l.clear();
139  
  }
140  
  
141  
  @Override
142  
  public bool addAll(int i, Collection<? extends S> c) {
143  
    index.addAll((Collection) c);
144  
    ret l.addAll(i, c);
145  
  }
146  
  
147  
  // indexing methods
148  
  
149  
  void makeIndex(LinkedToken lt) {
150  
    if (lt.index >= 0) ret;
151  
    LinkedToken l = lt.prev;
152  
    while (l != null && l.index < 0) l = l.prev;
153  
    int index = 0;
154  
    if (l == null) l = tok.first;
155  
    while true {
156  
      l.index = index++;
157  
      if (l == lt) break;
158  
      l = l.next;
159  
    }
160  
  }
161  
  
162  
  // static methods
163  
  
164  
  static HybridTokens ensureIndexed(L<S> l) {
165  
    ret l instanceof HybridTokens ? (HybridTokens) l : new HybridTokens(l);
166  
  }
167  
}

Author comment

Began life as a copy of #1004045

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: #1016611
Snippet name: HybridTokens - an indexed version of a token list [dev.]
Eternal ID of this version: #1016611/1
Text MD5: c76ab0f64c57f386a032d7e34a394e7c
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2018-06-25 15:10:11
Source code size: 3863 bytes / 167 lines
Pitched / IR pitched: No / No
Views / Downloads: 367 / 387
Referenced in: [show references]