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

83
LINES

< > BotCompany Repo | #1029186 - LongBuffer

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

Libraryless. Click here for Pure Java version (4937L/28K).

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

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/13
Text MD5: 578728dc6324b344ae77167fc012b005
Transpilation MD5: 0a57ffb7f6660e8cbaa00b7870826c59
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-01-22 14:59:57
Source code size: 2019 bytes / 83 lines
Pitched / IR pitched: No / No
Views / Downloads: 197 / 480
Version history: 12 change(s)
Referenced in: [show references]