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

133
LINES

< > BotCompany Repo | #1036244 // SynchronizedDoubleBuffer - buffer of doubles

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

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

sclass SynchronizedDoubleBuffer is IDoubleBuffer {
  double[] data;
  int size;
  
  *() {}
  *(int size) { if (size != 0) data = new double[size]; }
  *(Iterable<Double> l) { addAll(l); }
  *(Cl<Double> l) { this(l(l)); addAll(l); }
  *(double... data) { this.data = data; size = l(data); }
  
  public synchronized void add(double i) {
    if (size >= lDoubleArray(data)) {
      data = resizeDoubleArray(data, Math.max(1, toInt(Math.min(maximumSafeArraySize(), lDoubleArray(data)*2L))));
      if (size >= data.length) fail("DoubleBuffer too large: " + size);
    }
    data[size++] = 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 : resizeDoubleArray(data, size);
  }
  
  double[] toArrayNonNull() {
    ret unnull(toArray());
  }
  
  synchronized L<Double> toList() {
    ret doubleArrayToList(data, 0, size);
  }

  synchronized L<Double> asVirtualList() {
    ret new RandomAccessAbstractList<Double> {
      public int size() { ret SynchronizedDoubleBuffer.this.size(); }
      public Double get(int i) { ret SynchronizedDoubleBuffer.this.get(i); }
      public Double set(int i, Double val) {
        synchronized(SynchronizedDoubleBuffer.this) {
          Double a = get(i);
          data[i] = val;
          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] = 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(SynchronizedDoubleBuffer.this) {
          //if (!hasNext()) fail("Index out of bounds: " + i);
          ret 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 = resizeDoubleArray(data, size);
  }
  
  public synchronized int indexOf(double b) {
    for i to size:
      if (data[i] == b)
        ret i;
    ret -1;
  }
  
  synchronized double[] subArray(int start, int end) {
    ret subDoubleArray(data, start, min(end, size));
  }
  
  public synchronized void insertAt(int idx, double[] l) {
    int n = l(l);
    if (n == 0) ret;
    double[] 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 #1030692

download  show line numbers  debug dex  old transpilations   

Travelled to 2 computer(s): elmgxqgtpvxh, mqqgnosmbjvj

No comments. add comment

Snippet ID: #1036244
Snippet name: SynchronizedDoubleBuffer - buffer of doubles
Eternal ID of this version: #1036244/11
Text MD5: bd6a78f64c3c8fcb4e6d469005ec7f0c
Transpilation MD5: 6471739f1503b52f84ca21e8c7ef711b
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:20:04
Source code size: 3806 bytes / 133 lines
Pitched / IR pitched: No / No
Views / Downloads: 117 / 181
Version history: 10 change(s)
Referenced in: [show references]