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

105
LINES

< > BotCompany Repo | #1004045 // IndexedList2 - maintains only a multiset of all elements

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

Transpiled version (5212L) is out of date.

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

Author comment

Began life as a copy of #1004042

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1004045
Snippet name: IndexedList2 - maintains only a multiset of all elements
Eternal ID of this version: #1004045/3
Text MD5: 0dbf53d10fec0a72e972b5ad7b79d843
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:45
Source code size: 1947 bytes / 105 lines
Pitched / IR pitched: No / No
Views / Downloads: 771 / 4958
Version history: 2 change(s)
Referenced in: [show references]