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

109
LINES

< > BotCompany Repo | #1036243 // SynchronizedLongBuffer

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

Libraryless. Click here for Pure Java version (10233L/57K).

sclass SynchronizedLongBuffer is ILongBuffer {
  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 synchronized 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;
  }
  
  synchronized void allocate(int n) {
    data = resizeLongArray(data, max(n, size()));
  }
  
  public synchronized void addAll(Iterable<Long> l) {
    if (l != null) for (long i : l) add(i);
  }
  
  public synchronized long[] toArray() {
    ret size == 0 ? null : resizeLongArray(data, size);
  }
  
  synchronized L<Long> toList() {
    ret longArrayToList(data, 0, size);
  }
  
  public synchronized L<Long> asVirtualList() {
    ret listFromFunction get(size);
  }
  
  synchronized void reset { size = 0; }
  void clear { reset(); }
  
  synchronized public int size() { ret size; }
  public synchronized bool isEmpty() { ret size == 0; }
  
  public synchronized long get(int idx) {
    if (idx >= size) fail("Index out of range: " + idx + "/" + size);
    ret data[idx];
  }
  
  synchronized void set(int idx, long value) {
    if (idx >= size) fail("Index out of range: " + idx + "/" + size);
    data[idx] = value;
  }
  
  public synchronized long popLast() {
    if (size == 0) fail("empty buffer");
    ret data[--size];
  }
  
  public synchronized long last() { ret data[size-1]; }
  synchronized long nextToLast() { ret data[size-2]; }
  
  toString { ret squareBracket(joinWithSpace(toList())); }
  
  public synchronized Iterator<Long> iterator() {
    ret new ItIt<Long> {
      int i = 0;
      
      public bool hasNext() { ret i < size(); }
      public Long next() {
        synchronized(SynchronizedLongBuffer.this) {
          if (!hasNext()) fail("Index out of bounds: " + i);
          ret data[i++];
        }
      }
    };
  }
  
  public synchronized void trimToSize {
    data = resizeLongArray(data, size);
  }
  
  synchronized 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 synchronized long poll() { 
    ret size == 0 ? -1 : data[--size];
  }
  
  public synchronized void insertAt(int idx, long[] l) {
    int n = l(l);
    if (n == 0) ret;
    long[] newData = new[size+n];
    arraycopy(data, 0, newData, 0, idx);
    arraycopy(l, 0, newData, idx, n);
    arraycopy(data, idx, newData, idx+n, size-idx);
    data = newData;
    size = newData.length;
  }
  
  public void integrityCheck {
    assertTrue("Size positive", size >= 0);
    assertTrue("Data length", l(data) >= size);
  }
}

Author comment

Began life as a copy of #1029186

download  show line numbers  debug dex  old transpilations   

Travelled to 2 computer(s): elmgxqgtpvxh, mqqgnosmbjvj

No comments. add comment

Snippet ID: #1036243
Snippet name: SynchronizedLongBuffer
Eternal ID of this version: #1036243/10
Text MD5: b0c96f4dafb2cc21e5d8764c2a1595b2
Transpilation MD5: 303ef611a08d80f8a6c008492eecaf33
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2023-03-20 20:17:38
Source code size: 2965 bytes / 109 lines
Pitched / IR pitched: No / No
Views / Downloads: 172 / 292
Version history: 9 change(s)
Referenced in: [show references]