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

49
LINES

< > BotCompany Repo | #1012238 // SimpleIntList

JavaX fragment (include)

1  
sclass SimpleIntList extends RandomAccessAbstractList<Int> {
2  
  int[] data = EMPTY_ELEMENTDATA;
3  
  int size;
4  
  
5  
  private static final int[] EMPTY_ELEMENTDATA = {};
6  
7  
  public void add(int i) {
8  
    ensureCapacityInternal(size+1);
9  
    data[size++] = i;
10  
  }
11  
  
12  
  public bool add(Int i) {
13  
    add((int) i);
14  
    true;
15  
  }
16  
  
17  
  public Int set(int i, Int value) {
18  
    if (i >= size) fail("Index out of range: " + i);
19  
    int old = data[i];
20  
    data[i] = value;
21  
    ret old;
22  
  }
23  
  
24  
  public int size() { ret size; }
25  
  
26  
  public Int get(int i) {
27  
    if (i >= 0 && i < size) ret data[i];
28  
    throw new IndexOutOfBoundsException(i + "/" + size);
29  
  }
30  
  
31  
  void ensureCapacityInternal(int minCapacity) {
32  
    if (data.length < minCapacity) grow(minCapacity);
33  
  }
34  
  
35  
  private void grow(int minCapacity) {
36  
    int oldCapacity = data.length;
37  
    int newCapacity = oldCapacity + (oldCapacity >> 1);
38  
    if (newCapacity - minCapacity < 0)
39  
      newCapacity = minCapacity;
40  
    data = Arrays.copyOf(data, newCapacity);
41  
  }
42  
  
43  
  // always returns a fresh copy
44  
  int[] toIntArray() {
45  
    int[] a = new int[size];
46  
    System.arraycopy(data, 0, a, 0, size);
47  
    ret a;
48  
  }
49  
}

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1012238
Snippet name: SimpleIntList
Eternal ID of this version: #1012238/8
Text MD5: 1f06ddb709895475103ecf80b4fde86d
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2020-07-23 19:04:28
Source code size: 1196 bytes / 49 lines
Pitched / IR pitched: No / No
Views / Downloads: 515 / 1084
Version history: 7 change(s)
Referenced in: [show references]