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

156
LINES

< > BotCompany Repo | #1025805 // TokenIndexedList3 with lazy reordering [dev. - probably the idea doesn't help much]

JavaX fragment (include)

1  
// optimized for search - O(1) for searching for a token
2  
// set is O(log m) with m = number of occurrences of token
3  
final sclass TokenIndexedList3 extends RandomAccessAbstractList<S> implements IContentsIndexedList<S>, IContentsIndexedList2<S> {
4  
  final new HashMap<S, TreeSet<Token>> index; // tokens by contents, sorted by index
5  
  final new ArrayList<Token> list;
6  
  int reorderFrom = 0;
7  
8  
  final sclass Token extends HasIndex {
9  
    S s; // actual token
10  
    
11  
    toString { ret "Token " + quote(s) + "@" + idx; }
12  
  }
13  
  
14  
  *() {}
15  
  *(Collection<S> l) { addAll(l); }
16  
  
17  
  public S get(int i) { ret list.get(i).s; }
18  
  public int size() { ret list.size(); }
19  
  
20  
  public S set(int i, S s) {
21  
    Token t = list.get(i);
22  
    S old = t.s;
23  
    if (eq(old, s)) ret old;
24  
    removeFromIdx(t);
25  
    t.s = s;
26  
    addToIdx(t);
27  
    ret old;
28  
  }
29  
  
30  
  public bool add(S s) {
31  
    ++modCount;
32  
    new Token t;
33  
    t.s = s;
34  
    t.idx = size();
35  
    list.add(t);
36  
    addToIdx(t);
37  
    true;
38  
  }
39  
  
40  
  public void add(int i, S s) {
41  
    ++modCount;
42  
    new Token t;
43  
    t.s = s;
44  
    t.idx = i;
45  
    list.add(i, t);
46  
    reorder(i+1);
47  
    addToIdx(t);
48  
  }
49  
  
50  
  public bool addAll(int i, Collection<? extends S> l) {
51  
    int n = l.size();
52  
    if (n == 0) false;
53  
    ++modCount;
54  
    L<Token> l2 = emptyList(n);
55  
    int j = i;
56  
    for (S s : l) {
57  
      new Token t;
58  
      t.s = s;
59  
      t.idx = j++;
60  
      l2.add(t);
61  
    }
62  
    list.addAll(i, l2);
63  
    reorder(i+n);
64  
    for (Token t : l2)
65  
      addToIdx(t);
66  
    true;
67  
  }
68  
  
69  
  public S remove(int i) {
70  
    ++modCount;
71  
    Token t = list.get(i);
72  
    removeFromIdx(t);
73  
    list.remove(i);
74  
    reorder(i);
75  
    ret t.s;
76  
  }
77  
  
78  
  void reorder(int fromIdx) {
79  
    if (reorderFrom < 0 || reorderFrom < fromIdx) reorderFrom = fromIdx;
80  
  }
81  
  
82  
  void ensureReordered {
83  
    ensureReorderedTo(size()-1);
84  
  }
85  
  
86  
  // to = inclusive
87  
  void ensureReorderedTo(int to) {
88  
    if (reorderFrom > to) ret;
89  
    for (int i = reorderFrom; i <= to; i++)
90  
      list.get(i).idx = i;
91  
    reorderFrom = to;
92  
  }
93  
  
94  
  void removeFromIdx(Token t) {
95  
    TreeSet<Token> idx = index.get(t.s);
96  
    idx.remove(t);
97  
    if (idx.isEmpty()) index.remove(t.s);
98  
  }
99  
  
100  
  void addToIdx(Token t) {
101  
    TreeSet<Token> idx = index.get(t.s);
102  
    if (idx == null)
103  
      index.put(t.s, idx = new TreeSet);
104  
    idx.add(t);
105  
  }
106  
  
107  
  @Override
108  
  public int indexOf(O s) {
109  
    TreeSet<Token> l = index.get(s);
110  
    if (l == null) ret -1;
111  
    Token t = first(l);
112  
    ensureReorderedTo(t.idx);
113  
    ret t.idx;
114  
  }
115  
  
116  
  @Override
117  
  public int lastIndexOf(O s) {
118  
    TreeSet<Token> l = index.get(s);
119  
    ret l == null ? -1 : last(l).idx;
120  
  }
121  
  
122  
  @Override
123  
  public bool contains(O s) {
124  
    ret index.containsKey(s);
125  
  }
126  
  
127  
  public void clear() {
128  
    ++modCount;
129  
    index.clear();
130  
    list.clear();
131  
  }
132  
  
133  
  protected void removeRange(int fromIndex, int toIndex) {
134  
    if (fromIndex == toIndex) ret;
135  
    ++modCount;
136  
    
137  
    for (int i = fromIndex; i < toIndex; i++)
138  
      removeFromIdx(list.get(i));
139  
      
140  
    list.subList(fromIndex, toIndex).clear();
141  
    reorder(fromIndex);
142  
  }
143  
  
144  
  public int[] indicesOf(O o) {
145  
    TreeSet<Token> idx = index.get(o);
146  
    if (idx == null) ret emptyIntArray();
147  
    int[] a = new int[idx.size()];
148  
    int i = 0;
149  
    for (Token t : idx) a[i++] = t.idx;
150  
    ret a;
151  
  }
152  
  
153  
  public TreeSet<HasIndex> indicesOf_treeSetOfHasIndex(O o) {
154  
    ret (TreeSet) index.get(o);
155  
  }
156  
}

Author comment

Began life as a copy of #1025510

download  show line numbers  debug dex  old transpilations   

Travelled to 6 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1025805
Snippet name: TokenIndexedList3 with lazy reordering [dev. - probably the idea doesn't help much]
Eternal ID of this version: #1025805/10
Text MD5: c54974be1b49ebe1ff9db5e57101599f
Author: stefan
Category: javax / image analysis
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2019-10-22 12:18:23
Source code size: 3517 bytes / 156 lines
Pitched / IR pitched: No / No
Views / Downloads: 206 / 268
Version history: 9 change(s)
Referenced in: [show references]