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

91
LINES

< > BotCompany Repo | #1029186 // LongBuffer

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

Transpiled version (9632L) is out of date.

1  
sclass LongBuffer implements Iterable<Long>, ILongQueue, IntSize {
2  
  long[] data;
3  
  int size;
4  
  
5  
  *() {}
6  
  *(int size) { if (size != 0) data = new long[size]; }
7  
  *(Iterable<Long> l) {
8  
    if (l cast Cl) allocate(l.size());
9  
    addAll(l);
10  
  }
11  
  
12  
  public void add(long i) {
13  
    if (size >= lLongArray(data)) {
14  
      data = resizeLongArray(data, Math.max(1, toInt(Math.min(maximumSafeArraySize(), lLongArray(data)*2L))));
15  
      if (size >= data.length) fail("LongBuffer too large: " + size);
16  
    }
17  
    data[size++] = i;
18  
  }
19  
  
20  
  void allocate(int n) {
21  
    data = resizeLongArray(data, max(n, size()));
22  
  }
23  
  
24  
  void addAll(Iterable<Long> l) {
25  
    if (l != null) for (long i : l) add(i);
26  
  }
27  
  
28  
  long[] toArray() {
29  
    ret size == 0 ? null : resizeLongArray(data, size);
30  
  }
31  
  
32  
  L<Long> toList() {
33  
    ret longArrayToList(data, 0, size);
34  
  }
35  
  
36  
  L<Long> asVirtualList() {
37  
    ret listFromFunction get(size);
38  
  }
39  
  
40  
  void reset { size = 0; }
41  
  void clear { reset(); }
42  
  
43  
  public int size() { ret size; }
44  
  public bool isEmpty() { ret size == 0; }
45  
  
46  
  long get(int idx) {
47  
    if (idx >= size) fail("Index out of range: " + idx + "/" + size);
48  
    ret data[idx];
49  
  }
50  
  
51  
  void set(int idx, long value) {
52  
    if (idx >= size) fail("Index out of range: " + idx + "/" + size);
53  
    data[idx] = value;
54  
  }
55  
  
56  
  long popLast() {
57  
    if (size == 0) fail("empty buffer");
58  
    ret data[--size];
59  
  }
60  
  
61  
  long last() { ret data[size-1]; }
62  
  long nextToLast() { ret data[size-2]; }
63  
  
64  
  toString { ret squareBracket(joinWithSpace(toList())); }
65  
  
66  
  public Iterator<Long> iterator() {
67  
    ret new ItIt<Long> {
68  
      int i = 0;
69  
      
70  
      public bool hasNext() { ret i < size; }
71  
      public Long next() {
72  
        if (!hasNext()) fail("Index out of bounds: " + i);
73  
        ret data[i++];
74  
      }
75  
    };
76  
  }
77  
  
78  
  void trimToSize {
79  
    data = resizeLongArray(data, size);
80  
  }
81  
  
82  
  void remove(int idx) {
83  
    arraycopy(data, idx+1, data, idx, size-1-idx);
84  
    --size;
85  
  }
86  
  
87  
  // don't rely on return value if buffer is empty
88  
  public long poll() { 
89  
    ret size == 0 ? -1 : data[--size];
90  
  }
91  
}

Author comment

Began life as a copy of #1028793

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: #1029186
Snippet name: LongBuffer
Eternal ID of this version: #1029186/18
Text MD5: 50a7ed7df4233a3afeb3550e403fdb5d
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-10-31 21:29:19
Source code size: 2171 bytes / 91 lines
Pitched / IR pitched: No / No
Views / Downloads: 497 / 916
Version history: 17 change(s)
Referenced in: [show references]