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

69
LINES

< > BotCompany Repo | #1029225 // ChunkedLongList - stored in fixed-size chunks

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

Libraryless. Click here for Pure Java version (2816L/18K).

1  
final sclass ChunkedLongList extends RandomAccessAbstractList<Long> {
2  
  int chunkShift = 16; // => chunkSize = 65536
3  
  new ArrayList<long[]> chunks;
4  
  int actualChunks; // roughly keep track of chunk list's internal capacity so we can shrink in time
5  
  int size;
6  
  
7  
  public void add(long i) {
8  
    ensureCapacityInternal(size+1);
9  
    set(size, i);
10  
    ++size;
11  
  }
12  
  
13  
  public Long set(int i, Long value) {
14  
    ret set(i, (long) value);
15  
  }
16  
  
17  
  public long set(int i, long value) {
18  
    long[] chunk = chunks.get(i >> chunkShift);
19  
    int idx = i & (chunkSize()-1);
20  
    long old = chunk[idx];
21  
    chunk[idx] = value;
22  
    ret old;
23  
  }
24  
  
25  
  public bool add(Long i) {
26  
    add((long) i);
27  
    true;
28  
  }
29  
  
30  
  public int size() { ret size; }
31  
  
32  
  public Long get(int i) {
33  
    ret getPrimitive(i);
34  
  }
35  
  
36  
  public long getPrimitive(int i) {
37  
    if (i >= 0 && i < size) ret chunks.get(i >> chunkShift)[i & (chunkSize()-1)];
38  
    throw new IndexOutOfBoundsException(i + "/" + size);
39  
  }
40  
  
41  
  void ensureCapacityInternal(int minCapacity) {
42  
    while (l(chunks) << chunkShift < minCapacity) {
43  
      chunks.add(new long[chunkSize()]);
44  
      actualChunks = max(actualChunks, l(chunks));
45  
    }
46  
  }
47  
  
48  
  long[] toLongArray() {
49  
    long[] a = new long[size];
50  
    int i = 0;
51  
    for (long[] chunk : chunks) {
52  
      System.arraycopy(chunk, 0, a, i, min(chunk.length, size-i));
53  
      i += chunk.length;
54  
    }
55  
    ret a;
56  
  }
57  
  
58  
  int chunkSize() { ret 1 << chunkShift; }
59  
  
60  
  void truncateAt(int idx) {
61  
    size = idx;
62  
    while (size <= (l(chunks)-1) << chunkShift)
63  
      popLast(chunks);
64  
    if (l(chunks)*2 < actualChunks) {
65  
      chunks.trimToSize();
66  
      actualChunks = l(chunks);
67  
    }
68  
  }
69  
}

Author comment

Began life as a copy of #1028525

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1029225
Snippet name: ChunkedLongList - stored in fixed-size chunks
Eternal ID of this version: #1029225/1
Text MD5: 9b83abab903d87763e78754ea9029460
Transpilation MD5: 86cb0b7cd6455f8dc09962fd49ebdc33
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2020-07-26 13:37:27
Source code size: 1742 bytes / 69 lines
Pitched / IR pitched: No / No
Views / Downloads: 141 / 387
Referenced in: [show references]