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

129
LINES

< > BotCompany Repo | #1036283 // SynchronizedLongBufferStoredAsLinearInts

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

Transpiled version (10139L) is out of date.

persistable sclass SynchronizedLongBufferStoredAsLinearInts is ILongBuffer {
  int[] data;
  int size;
  
  // affine conversion to long (long = int*factor+base)
  long base, factor;
  
  *(long *base, long *factor) {}
  *(long *base, long *factor, int size) { if (size != 0) data = new int[size]; }
  *(long *base, long *factor, Iterable<Long> l) {
    if (l cast Cl) allocate(l.size());
    addAll(l);
  }
  
  public synchronized void addRaw(int i) {
    if (size >= lIntArray(data)) {
      data = resizeIntArray(data, Math.max(1, toInt(Math.min(maximumSafeArraySize(), lIntArray(data)*2L))));
      if (size >= data.length) fail(shortClassName(this) + " too large: " + size);
    }
    data[size++] = i;
  }
  
  public synchronized void add(long i) {
    addRaw(compress(i));
  }
  
  synchronized void allocate(int n) {
    data = resizeIntArray(data, max(n, size()));
  }
  
  public synchronized void addAll(Iterable<Long> l) {
    if (l != null) for (long i : l) add(i);
  }
  
  public synchronized long[] toArray() {
    if (size == 0) null;
    long[] out = new[size];
    for i to size:
      out[i] = expand(data[i]);
    ret out;
  }
  
  /*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 int getRaw(int idx) {
    if (idx >= size) fail("Index out of range: " + idx + "/" + size);
    ret data[idx];
  }
  
  public synchronized long get(int idx) {
    if (idx >= size) fail("Index out of range: " + idx + "/" + size);
    ret expand(data[idx]);
  }
  
  synchronized void set(int idx, long value) {
    if (idx >= size) fail("Index out of range: " + idx + "/" + size);
    data[idx] = compress(value);
  }
  
  public synchronized long popLast() {
    if (size == 0) fail("empty buffer");
    ret expand(data[--size]);
  }
  
  public synchronized long last() { ret expand(data[size-1]); }
  synchronized long nextToLast() { ret expand(data[size-2]); }
  
  toString { ret squareBracket(joinWithSpace(asVirtualList())); }
  
  public synchronized Iterator<Long> iterator() {
    ret new ItIt<Long> {
      int i = 0;
      
      public bool hasNext() { ret i < size(); }
      public Long next() {
        synchronized(SynchronizedLongBufferStoredAsLinearInts.this) {
          if (!hasNext()) fail("Index out of bounds: " + i);
          ret expand(data[i++]);
        }
      }
    };
  }
  
  public synchronized void trimToSize {
    data = resizeIntArray(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 : expand(data[--size]);
  }
  
  long expand(int value) { ret base+value*factor; }
  int compress(long value) { ret toInt(ldiv_round(value-base, factor); }
  
  public synchronized void insertAt(int idx, long[] l) {
    int n = l(l);
    if (n == 0) ret;
    int[] newData = new[size+n];
    arraycopy(data, 0, newData, 0, idx);
    for i to n:
      newData[idx+i] = compress(l[i]);
    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 #1036243

download  show line numbers  debug dex  old transpilations   

Travelled to 3 computer(s): elmgxqgtpvxh, mowyntqkapby, mqqgnosmbjvj

No comments. add comment

Snippet ID: #1036283
Snippet name: SynchronizedLongBufferStoredAsLinearInts
Eternal ID of this version: #1036283/13
Text MD5: 64502164a79b06e0c2df4f3cee805ae3
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:18:15
Source code size: 3684 bytes / 129 lines
Pitched / IR pitched: No / No
Views / Downloads: 92 / 176
Version history: 12 change(s)
Referenced in: [show references]