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).

1  
sclass FloatBuffer is Iterable<Float>, IntSize {
2  
  float[] data;
3  
  int size;
4  
  
5  
  *() {}
6  
  *(int size) { if (size != 0) data = new float[size]; }
7  
  *(Iterable<Float> l) { addAll(l); }
8  
  *(Cl<Float> l) { this(l(l)); addAll(l); }
9  
  *(float... data) { this.data = data; size = l(data); }
10  
  
11  
  void add(float i) {
12  
    if (size >= lFloatArray(data)) {
13  
      data = resizeFloatArray(data, Math.max(1, toInt(Math.min(maximumSafeArraySize(), lFloatArray(data)*2L))));
14  
      if (size >= data.length) fail("FloatBuffer too large: " + size);
15  
    }
16  
    data[size++] = i;
17  
  }
18  
  
19  
  void addAll(Iterable<Float> l) {
20  
    if (l != null) for (float i : l) add(i);
21  
  }
22  
  
23  
  float[] toArray() {
24  
    ret size == 0 ? null : resizeFloatArray(data, size);
25  
  }
26  
  
27  
  float[] toArrayNonNull() {
28  
    ret unnull(toArray());
29  
  }
30  
  
31  
  L<Float> toList() {
32  
    ret floatArrayToList(data, 0, size);
33  
  }
34  
35  
  L<Float> asVirtualList() {
36  
    ret new RandomAccessAbstractList<Float> {
37  
      public int size() { ret size; }
38  
      public Float get(int i) { ret FloatBuffer.this.get(i); }
39  
      public Float set(int i, Float val) {
40  
        Float a = get(i);
41  
        data[i] = val;
42  
        ret a;
43  
      }
44  
    };
45  
  }
46  
  
47  
  void reset { size = 0; }
48  
  void clear { reset(); }
49  
  
50  
  public int size() { ret size; }
51  
  bool isEmpty() { ret size == 0; }
52  
  
53  
  float get(int idx) {
54  
    if (idx >= size) fail("Index out of range: " + idx + "/" + size);
55  
    ret data[idx];
56  
  }
57  
  
58  
  void set(int idx, float value) {
59  
    if (idx >= size) fail("Index out of range: " + idx + "/" + size);
60  
    data[idx] = value;
61  
  }
62  
  
63  
  float popLast() {
64  
    if (size == 0) fail("empty buffer");
65  
    ret data[--size];
66  
  }
67  
  
68  
  float last() { ret data[size-1]; }
69  
  float nextToLast() { ret data[size-2]; }
70  
  
71  
  toString { ret squareBracket(joinWithSpace(toList())); }
72  
  
73  
  public Iterator<Float> iterator() {
74  
    ret new ItIt<Float> {
75  
      int i = 0;
76  
      
77  
      public bool hasNext() { ret i < size; }
78  
      public Float next() {
79  
        //if (!hasNext()) fail("Index out of bounds: " + i);
80  
        ret data[i++];
81  
      }
82  
    };
83  
  }
84  
  
85  
  /*public FloatIterator floatIterator() {
86  
    ret new FloatIterator {
87  
      int i = 0;
88  
      
89  
      public bool hasNext() { ret i < size; }
90  
      public int next() {
91  
        //if (!hasNext()) fail("Index out of bounds: " + i);
92  
        ret data[i++];
93  
      }
94  
      toString { ret "Iterator@" + i + " over " + FloatBuffer.this; }
95  
    };
96  
  }*/
97  
  
98  
  void trimToSize {
99  
    data = resizeFloatArray(data, size);
100  
  }
101  
  
102  
  int indexOf(float b) {
103  
    for i to size:
104  
      if (data[i] == b)
105  
        ret i;
106  
    ret -1;
107  
  }
108  
  
109  
  float[] subArray(int start, int end) {
110  
    ret subFloatArray(data, start, min(end, size));
111  
  }
112  
}

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: 146 / 220
Referenced in: [show references]