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

106
LINES

< > BotCompany Repo | #1024895 // IndexedList2 subclassing ArrayList (doesn't work)

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

Libraryless. Click here for Pure Java version (1888L/12K).

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

Author comment

Began life as a copy of #1004045

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1024895
Snippet name: IndexedList2 subclassing ArrayList (doesn't work)
Eternal ID of this version: #1024895/11
Text MD5: 0661b63ccde69a5a83e07260b57dc165
Transpilation MD5: 3c0d5ddecdf612f1871583db56042459
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2019-08-29 11:48:50
Source code size: 1985 bytes / 106 lines
Pitched / IR pitched: No / No
Views / Downloads: 202 / 482
Version history: 10 change(s)
Referenced in: [show references]