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

112
LINES

< > BotCompany Repo | #1036156 // FloatBuffer - buffer of floats

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

Libraryless. Click here for Pure Java version (9446L/52K).

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

  L<Float> asVirtualList() {
    ret new RandomAccessAbstractList<Float> {
      public int size() { ret size; }
      public Float get(int i) { ret FloatBuffer.this.get(i); }
      public Float set(int i, Float val) {
        Float a = get(i);
        data[i] = val;
        ret a;
      }
    };
  }
  
  void reset { size = 0; }
  void clear { reset(); }
  
  public int size() { ret size; }
  bool isEmpty() { ret size == 0; }
  
  float get(int idx) {
    if (idx >= size) fail("Index out of range: " + idx + "/" + size);
    ret data[idx];
  }
  
  void set(int idx, float value) {
    if (idx >= size) fail("Index out of range: " + idx + "/" + size);
    data[idx] = value;
  }
  
  float popLast() {
    if (size == 0) fail("empty buffer");
    ret data[--size];
  }
  
  float last() { ret data[size-1]; }
  float nextToLast() { ret data[size-2]; }
  
  toString { ret squareBracket(joinWithSpace(toList())); }
  
  public Iterator<Float> iterator() {
    ret new ItIt<Float> {
      int i = 0;
      
      public bool hasNext() { ret i < size; }
      public Float next() {
        //if (!hasNext()) fail("Index out of bounds: " + i);
        ret data[i++];
      }
    };
  }
  
  /*public FloatIterator floatIterator() {
    ret new FloatIterator {
      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 " + FloatBuffer.this; }
    };
  }*/
  
  void trimToSize {
    data = resizeFloatArray(data, size);
  }
  
  int indexOf(float b) {
    for i to size:
      if (data[i] == b)
        ret i;
    ret -1;
  }
  
  float[] subArray(int start, int end) {
    ret subFloatArray(data, start, min(end, size));
  }
}

Author comment

Began life as a copy of #1030692

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1036156
Snippet name: FloatBuffer - buffer of floats
Eternal ID of this version: #1036156/1
Text MD5: 34265e29f846545050e9a6cee7b37a4e
Transpilation MD5: d47fc82acb131067479a6be29c4ec97f
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-10-01 09:46:24
Source code size: 2785 bytes / 112 lines
Pitched / IR pitched: No / No
Views / Downloads: 145 / 219
Referenced in: [show references]