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

134
LINES

< > BotCompany Repo | #1036276 // SynchronizedFloatBufferPresentingAsDoubles - buffer of floats, exposed as doubles

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

Transpiled version (10327L) is out of date.

sclass SynchronizedFloatBufferPresentingAsDoubles is IDoubleBuffer {
  float[] data;
  int size;
  
  *() {}
  *(int size) { if (size != 0) data = new float[size]; }
  *(Iterable<Double> l) { addAll(l); }
  *(Cl<Double> l) { this(l(l)); addAll(l); }
  *(double... data) { this.data = doubleArrayToFloatArray(data); size = l(data); }
  
  public synchronized void add(double i) {
    if (size >= lFloatArray(data)) {
      data = resizeFloatArray(data, Math.max(1, toInt(Math.min(maximumSafeArraySize(), lFloatArray(data)*2L))));
      if (size >= data.length) fail(shortClassName(this) + " too large: " + size);
    }
    data[size++] = (float) i;
  }
  
  public synchronized void addAll(Iterable<Double> l) {
    if (l != null) for (double i : l) add(i);
  }
  
  public synchronized double[] toArray() {
    ret size == 0 ? null : takeFirstFromFloatArrayAsDoubleArray(data, size);
  }
  
  double[] toArrayNonNull() {
    ret unnull(toArray());
  }
  
  synchronized L<Double> toList() {
    ret floatArrayToDoubleList(data, 0, size);
  }

  synchronized L<Double> asVirtualList() {
    ret new RandomAccessAbstractList<Double> {
      public int size() { ret SynchronizedFloatBufferPresentingAsDoubles.this.size(); }
      public Double get(int i) { ret SynchronizedFloatBufferPresentingAsDoubles.this.get(i); }
      public Double set(int i, Double val) {
        synchronized(SynchronizedFloatBufferPresentingAsDoubles.this) {
          Double a = get(i);
          data[i] = val.floatValue();
          ret a;
        }
      }
    };
  }
  
  synchronized void reset { size = 0; }
  void clear { reset(); }
  
  public synchronized int size() { ret size; }
  public synchronized bool isEmpty() { ret size == 0; }
  
  public synchronized double get(int idx) {
    if (idx >= size) fail("Index out of range: " + idx + "/" + size);
    ret data[idx];
  }
  
  synchronized void set(int idx, double value) {
    if (idx >= size) fail("Index out of range: " + idx + "/" + size);
    data[idx] = (float) value;
  }
  
  public synchronized double popLast() {
    if (size == 0) fail("empty buffer");
    ret data[--size];
  }
  
  public synchronized double first() { ret data[0]; }
  public synchronized double last() { ret data[size-1]; }
  synchronized double nextToLast() { ret data[size-2]; }
  
  toString { ret squareBracket(joinWithSpace(toList())); }
  
  public Iterator<Double> iterator() {
    ret new ItIt<Double> {
      int i = 0;
      
      public bool hasNext() { ret i < size(); }
      public Double next() {
        synchronized(SynchronizedFloatBufferPresentingAsDoubles.this) {
          //if (!hasNext()) fail("Index out of bounds: " + i);
          ret (double) data[i++];
        }
      }
    };
  }
  
  /*public DoubleIterator doubleIterator() {
    ret new DoubleIterator {
      int i = 0;
      
      public bool hasNext() { ret i < size; }
      public int next() {
        //if (!hasNext()) fail("Index out of bounds: " + i);
        ret data[i++];
      }
      toString { ret "Iterator@" + i + " over " + DoubleBuffer.this; }
    };
  }*/
  
  public synchronized void trimToSize {
    data = resizeFloatArray(data, size);
  }
  
  public synchronized int indexOf(double b) {
    for i to size:
      if (data[i] == (float) b)
        ret i;
    ret -1;
  }
  
  synchronized double[] subArray(int start, int end) {
    ret subFloatArrayAsDoubleArray(data, start, min(end, size));
  }
  
  public synchronized void insertAt(int idx, double[] l) {
    int n = l(l);
    if (n == 0) ret;
    float[] newData = new[size+n];
    arraycopy(data, 0, newData, 0, idx);
    for i to n:
      newData[idx+i] = (float) 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 #1036244

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1036276
Snippet name: SynchronizedFloatBufferPresentingAsDoubles - buffer of floats, exposed as doubles
Eternal ID of this version: #1036276/20
Text MD5: 39125aeafbda1bfc5e5753c6f37803dd
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:51
Source code size: 4023 bytes / 134 lines
Pitched / IR pitched: No / No
Views / Downloads: 102 / 210
Version history: 19 change(s)
Referenced in: [show references]