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

74
LINES

< > BotCompany Repo | #1029186 - LongBuffer

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

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

1  
sclass LongBuffer implements Iterable<Long> {
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  
  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 addAll(Iterable<Long> l) {
18  
    if (l != null) for (long i : l) add(i);
19  
  }
20  
  
21  
  long[] toArray() {
22  
    ret size == 0 ? null : resizeLongArray(data, size);
23  
  }
24  
  
25  
  L<Long> toList() {
26  
    ret longArrayToList(data, 0, size);
27  
  }
28  
  
29  
  L<Long> asVirtualList() {
30  
    ret listFromFunction get(size);
31  
  }
32  
  
33  
  void reset { size = 0; }
34  
  void clear { reset(); }
35  
  
36  
  int size() { ret size; }
37  
  bool isEmpty() { ret size == 0; }
38  
  
39  
  long get(int idx) {
40  
    if (idx >= size) fail("Index out of range: " + idx + "/" + size);
41  
    ret data[idx];
42  
  }
43  
  
44  
  void set(int idx, long value) {
45  
    if (idx >= size) fail("Index out of range: " + idx + "/" + size);
46  
    data[idx] = value;
47  
  }
48  
  
49  
  long popLast() {
50  
    if (size == 0) fail("empty buffer");
51  
    ret data[--size];
52  
  }
53  
  
54  
  long last() { ret data[size-1]; }
55  
  long nextToLast() { ret data[size-2]; }
56  
  
57  
  toString { ret squareBracket(joinWithSpace(toList())); }
58  
  
59  
  public Iterator<Long> iterator() {
60  
    ret new ItIt<Long> {
61  
      int i = 0;
62  
      
63  
      public bool hasNext() { ret i < size; }
64  
      public Long next() {
65  
        if (!hasNext()) fail("Index out of bounds: " + i);
66  
        ret data[i++];
67  
      }
68  
    };
69  
  }
70  
  
71  
  void trimToSize {
72  
    data = resizeLongArray(data, size);
73  
  }
74  
}

Author comment

Began life as a copy of #1028793

download  show line numbers  debug dex   

Travelled to 5 computer(s): bhatertpkbcr, mqqgnosmbjvj, pzhvpgtvlbxg, tvejysmllsmz, xrpafgyirdlv

No comments. add comment

Snippet ID: #1029186
Snippet name: LongBuffer
Eternal ID of this version: #1029186/10
Text MD5: d516775987ff64bac0436cf5e54dd72f
Transpilation MD5: cf26232614befda1d0a37661ff2a7a5f
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2020-08-06 18:27:00
Source code size: 1781 bytes / 74 lines
Pitched / IR pitched: No / No
Views / Downloads: 61 / 147
Version history: 9 change(s)
Referenced in: [show references]

Formerly at http://tinybrain.de/1029186 & http://1029186.tinybrain.de