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

111
LINES

< > BotCompany Repo | #1030692 // DoubleBuffer - buffer of doubles

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

Transpiled version (9664L) is out of date.

sclass DoubleBuffer is Iterable<Double>, IntSize {
  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); }
  
  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;
  }
  
  void addAll(Iterable<Double> l) {
    if (l != null) for (double i : l) add(i);
  }
  
  double[] toArray() {
    ret size == 0 ? null : resizeDoubleArray(data, size);
  }
  
  double[] toArrayNonNull() {
    ret unnull(toArray());
  }
  
  L<Double> toList() {
    ret doubleArrayToList(data, 0, size);
  }

  L<Double> asVirtualList() {
    ret new RandomAccessAbstractList<Double> {
      public int size() { ret size; }
      public Double get(int i) { ret DoubleBuffer.this.get(i); }
      public Double set(int i, Double val) {
        Double a = get(i);
        data[i] = val;
        ret a;
      }
    };
  }
  
  void reset { size = 0; }
  void clear { reset(); }
  
  public int size() { ret size; }

  double get(int idx) {
    if (idx >= size) fail("Index out of range: " + idx + "/" + size);
    ret data[idx];
  }
  
  void set(int idx, double value) {
    if (idx >= size) fail("Index out of range: " + idx + "/" + size);
    data[idx] = value;
  }
  
  double popLast() {
    if (size == 0) fail("empty buffer");
    ret data[--size];
  }
  
  double last() { ret data[size-1]; }
  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() {
        //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; }
    };
  }*/
  
  void trimToSize {
    data = resizeDoubleArray(data, size);
  }
  
  int indexOf(double b) {
    for i to size:
      if (data[i] == b)
        ret i;
    ret -1;
  }
  
  double[] subArray(int start, int end) {
    ret subDoubleArray(data, start, min(end, size));
  }
}

Author comment

Began life as a copy of #1030688

download  show line numbers  debug dex  old transpilations   

Travelled to 7 computer(s): bhatertpkbcr, ekrmjmnbrukm, elmgxqgtpvxh, mowyntqkapby, mqqgnosmbjvj, pyentgdyhuwx, vouqrxazstgt

No comments. add comment

Snippet ID: #1030692
Snippet name: DoubleBuffer - buffer of doubles
Eternal ID of this version: #1030692/13
Text MD5: 7aaa402c2190af2d8c626e299d51c6fa
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-11-30 18:31:11
Source code size: 2788 bytes / 111 lines
Pitched / IR pitched: No / No
Views / Downloads: 214 / 577
Version history: 12 change(s)
Referenced in: [show references]