Libraryless. Click here for Pure Java version (2260L/14K).
1 | // optimized for search - O(1) for searching for a token |
2 | // set is O(m) with m = number of occurrences of token |
3 | // This could be improved to O(log m) by using a TreeSet instead of the L in the index |
4 | final sclass TokenIndexedList2 extends RandomAccessAbstractList<S> { |
5 | new Map<S, L<Token>> index; // tokens are in order |
6 | new L<Token> list; |
7 | |
8 | sclass Token { |
9 | int i; // index in list |
10 | S s; // actual token |
11 | } |
12 | |
13 | *() {} |
14 | *(Collection<S> l) { addAll(l); } |
15 | |
16 | public S get(int i) { ret list.get(i).s; } |
17 | public int size() { ret list.size(); } |
18 | |
19 | public S set(int i, S s) { |
20 | Token t = list.get(i); |
21 | S old = t.s; |
22 | if (eq(old, s)) ret old; |
23 | removeFromIdx(t); |
24 | t.s = s; |
25 | addToIdx(t); |
26 | ret old; |
27 | } |
28 | |
29 | public bool add(S s) { |
30 | ++modCount; |
31 | new Token t; |
32 | t.s = s; |
33 | t.i = size(); |
34 | list.add(t); |
35 | addToIdx(t); |
36 | true; |
37 | } |
38 | |
39 | public void add(int i, S s) { |
40 | ++modCount; |
41 | new Token t; |
42 | t.s = s; |
43 | t.i = i; |
44 | list.add(i, t); |
45 | reorder(i+1); |
46 | addToIdx(t); |
47 | } |
48 | |
49 | public S remove(int i) { |
50 | ++modCount; |
51 | Token t = list.get(i); |
52 | removeFromIdx(t); |
53 | list.remove(i); |
54 | reorder(i); |
55 | ret t.s; |
56 | } |
57 | |
58 | void reorder(int fromIdx) { |
59 | int n = size(); |
60 | for (int i = fromIdx; i < n; i++) |
61 | list.get(i).i = i; |
62 | } |
63 | |
64 | void removeFromIdx(Token t) { |
65 | L<Token> idx = index.get(t.s); |
66 | int i = Collections.binarySearch(idx, t, comparator()); |
67 | idx.remove(i); |
68 | if (empty(idx)) index.remove(t.s); |
69 | } |
70 | |
71 | void addToIdx(Token t) { |
72 | L<Token> idx = index.get(t.s); |
73 | if (idx == null) |
74 | index.put(t.s, idx = new L); |
75 | int i = -1-Collections.binarySearch(idx, t, comparator()); |
76 | idx.add(i, t); |
77 | } |
78 | |
79 | Comparator<Token> comparator() { |
80 | ret (Comparator<Token>) (a, b) -> b.i-a.i; |
81 | } |
82 | |
83 | public int indexOf(S s) { |
84 | L<Token> l = index.get(s); |
85 | ret l == null ? -1 : l.get(0).i; |
86 | } |
87 | |
88 | public bool contains(O s) { |
89 | ret index.containsKey(s); |
90 | } |
91 | |
92 | public void clear() { |
93 | ++modCount; |
94 | index.clear(); |
95 | list.clear(); |
96 | } |
97 | |
98 | protected void removeRange(int fromIndex, int toIndex) { |
99 | if (fromIndex == toIndex) ret; |
100 | ++modCount; |
101 | |
102 | // TODO: this can be optimized |
103 | for (int i = fromIndex; i < toIndex; i++) |
104 | removeFromIdx(list.get(i)); |
105 | |
106 | list.subList(fromIndex, toIndex).clear(); |
107 | reorder(fromIndex); |
108 | } |
109 | } |
Began life as a copy of #1025480
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: | #1025481 |
Snippet name: | TokenIndexedList2 [OK but usually not fast. use TokenIndexedList3 instead] |
Eternal ID of this version: | #1025481/23 |
Text MD5: | 087fdd028073c70b764bd130ddc53f96 |
Transpilation MD5: | 0c26db5b0c5a4ae3724d3383d9b0875b |
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-01 18:39:36 |
Source code size: | 2454 bytes / 109 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 268 / 761 |
Version history: | 22 change(s) |
Referenced in: | [show references] |