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.

sclass LongBuffer implements Iterable<Long>, ILongQueue, IntSize {
  long[] data;
  int size;
  
  *() {}
  *(int size) { if (size != 0) data = new long[size]; }
  *(Iterable<Long> l) {
    if (l cast Cl) allocate(l.size());
    addAll(l);
  }
  
  public void add(long i) {
    if (size >= lLongArray(data)) {
      data = resizeLongArray(data, Math.max(1, toInt(Math.min(maximumSafeArraySize(), lLongArray(data)*2L))));
      if (size >= data.length) fail("LongBuffer too large: " + size);
    }
    data[size++] = i;
  }
  
  void allocate(int n) {
    data = resizeLongArray(data, max(n, size()));
  }
  
  void addAll(Iterable<Long> l) {
    if (l != null) for (long i : l) add(i);
  }
  
  long[] toArray() {
    ret size == 0 ? null : resizeLongArray(data, size);
  }
  
  L<Long> toList() {
    ret longArrayToList(data, 0, size);
  }
  
  L<Long> asVirtualList() {
    ret listFromFunction get(size);
  }
  
  void reset { size = 0; }
  void clear { reset(); }
  
  public int size() { ret size; }
  public bool isEmpty() { ret size == 0; }
  
  long get(int idx) {
    if (idx >= size) fail("Index out of range: " + idx + "/" + size);
    ret data[idx];
  }
  
  void set(int idx, long value) {
    if (idx >= size) fail("Index out of range: " + idx + "/" + size);
    data[idx] = value;
  }
  
  long popLast() {
    if (size == 0) fail("empty buffer");
    ret data[--size];
  }
  
  long last() { ret data[size-1]; }
  long nextToLast() { ret data[size-2]; }
  
  toString { ret squareBracket(joinWithSpace(toList())); }
  
  public Iterator<Long> iterator() {
    ret new ItIt<Long> {
      int i = 0;
      
      public bool hasNext() { ret i < size; }
      public Long next() {
        if (!hasNext()) fail("Index out of bounds: " + i);
        ret data[i++];
      }
    };
  }
  
  void trimToSize {
    data = resizeLongArray(data, size);
  }
  
  void remove(int idx) {
    arraycopy(data, idx+1, data, idx, size-1-idx);
    --size;
  }
  
  // don't rely on return value if buffer is empty
  public long poll() { 
    ret size == 0 ? -1 : data[--size];
  }
}

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: 496 / 915
Version history: 17 change(s)
Referenced in: [show references]