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

108
LINES

< > BotCompany Repo | #1004042 // IndexedList - use ContentsIndexedList instead

JavaX fragment (include) [tags: use-pretranspiled]

Transpiled version (4767L) is out of date.

1  
static class IndexedList<A> extends AbstractList<A> {
2  
  new ArrayList<A> l;
3  
  MultiMap<A, Int> index;
4  
  static bool debug;
5  
  
6  
  *() {}
7  
  *(L<A> x) {
8  
    l.ensureCapacity(l(x));
9  
    addAll(x);
10  
  }
11  
  
12  
  // required immutable list methods
13  
  
14  
  @Override
15  
  public A get(int i) {
16  
    ret l.get(i);
17  
  }
18  
  
19  
  @Override
20  
  public int size() {
21  
    ret l.size();
22  
  }
23  
  
24  
  // required mutable list methods
25  
  
26  
  @Override
27  
  public A set(int i, A a) {
28  
    unindex(i);
29  
    A prev = l.get(i);
30  
    l.set(i, a);
31  
    index(i);
32  
    ret prev;
33  
  }
34  
  
35  
  @Override
36  
  public void add(int i, A a) {
37  
    if (i != l.size()) {
38  
      l.add(i, a);
39  
      index = null;
40  
    } else {
41  
      l.add(i, a);
42  
      index(i);
43  
    }
44  
  }
45  
  
46  
  @Override
47  
  public A remove(int i) {
48  
    A a = l.get(i);
49  
    l.remove(i);
50  
    index = null;
51  
    ret a;
52  
  }
53  
  
54  
  // speed up methods
55  
56  
  @Override  
57  
  protected void removeRange(int fromIndex, int toIndex) {
58  
    l.subList(fromIndex, toIndex).clear();
59  
    index = null;
60  
  }
61  
  
62  
  public int indexOf(O a) {
63  
    ensureIndexed();
64  
    L<Int> positions = index.get((A) a);
65  
    int n = l(positions);
66  
    int min = -1;
67  
    if (n != 0) {
68  
      min = positions.get(0);
69  
      for (int i = 1; i < n; i++) {
70  
        int p = positions.get(i);
71  
        if (p < min) min = p;
72  
      }
73  
    }
74  
    if (debug) print("IndexedList.indexOf " + a + " => " + min);
75  
    ret min;
76  
  }
77  
  
78  
  @Override
79  
  public bool contains(O a) {
80  
    ensureIndexed();
81  
    bool b = index.containsKey((A) a);
82  
    if (debug) print("IndexedList.contains " + a + " => " + b);
83  
    ret b;
84  
  }
85  
  
86  
  // indexing methods
87  
  
88  
  void unindex(int i) {
89  
    if (index != null)
90  
      index.remove(l.get(i), i);
91  
  }
92  
  
93  
  void index(int i) {
94  
    if (index == null)
95  
      ensureIndexed();
96  
    else
97  
      index.put(l.get(i), i);
98  
  }
99  
  
100  
  void ensureIndexed() {
101  
    if (index == null) {
102  
      index = new MultiMap;
103  
      int n = size();
104  
      for (int i = 0; i < n; i++)
105  
        index.put(l.get(i), i);
106  
    }
107  
  }
108  
}

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1004042
Snippet name: IndexedList - use ContentsIndexedList instead
Eternal ID of this version: #1004042/2
Text MD5: 654743973be99cb1e16940a41e94da66
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-01-15 01:39:32
Source code size: 2049 bytes / 108 lines
Pitched / IR pitched: No / No
Views / Downloads: 621 / 1260
Version history: 1 change(s)
Referenced in: [show references]