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.

1  
sclass SynchronizedFloatBufferPresentingAsDoubles is IDoubleBuffer {
2  
  float[] data;
3  
  int size;
4  
  
5  
  *() {}
6  
  *(int size) { if (size != 0) data = new float[size]; }
7  
  *(Iterable<Double> l) { addAll(l); }
8  
  *(Cl<Double> l) { this(l(l)); addAll(l); }
9  
  *(double... data) { this.data = doubleArrayToFloatArray(data); size = l(data); }
10  
  
11  
  public synchronized void add(double 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(shortClassName(this) + " too large: " + size);
15  
    }
16  
    data[size++] = (float) i;
17  
  }
18  
  
19  
  public synchronized void addAll(Iterable<Double> l) {
20  
    if (l != null) for (double i : l) add(i);
21  
  }
22  
  
23  
  public synchronized double[] toArray() {
24  
    ret size == 0 ? null : takeFirstFromFloatArrayAsDoubleArray(data, size);
25  
  }
26  
  
27  
  double[] toArrayNonNull() {
28  
    ret unnull(toArray());
29  
  }
30  
  
31  
  synchronized L<Double> toList() {
32  
    ret floatArrayToDoubleList(data, 0, size);
33  
  }
34  
35  
  synchronized L<Double> asVirtualList() {
36  
    ret new RandomAccessAbstractList<Double> {
37  
      public int size() { ret SynchronizedFloatBufferPresentingAsDoubles.this.size(); }
38  
      public Double get(int i) { ret SynchronizedFloatBufferPresentingAsDoubles.this.get(i); }
39  
      public Double set(int i, Double val) {
40  
        synchronized(SynchronizedFloatBufferPresentingAsDoubles.this) {
41  
          Double a = get(i);
42  
          data[i] = val.floatValue();
43  
          ret a;
44  
        }
45  
      }
46  
    };
47  
  }
48  
  
49  
  synchronized void reset { size = 0; }
50  
  void clear { reset(); }
51  
  
52  
  public synchronized int size() { ret size; }
53  
  public synchronized bool isEmpty() { ret size == 0; }
54  
  
55  
  public synchronized double get(int idx) {
56  
    if (idx >= size) fail("Index out of range: " + idx + "/" + size);
57  
    ret data[idx];
58  
  }
59  
  
60  
  synchronized void set(int idx, double value) {
61  
    if (idx >= size) fail("Index out of range: " + idx + "/" + size);
62  
    data[idx] = (float) value;
63  
  }
64  
  
65  
  public synchronized double popLast() {
66  
    if (size == 0) fail("empty buffer");
67  
    ret data[--size];
68  
  }
69  
  
70  
  public synchronized double first() { ret data[0]; }
71  
  public synchronized double last() { ret data[size-1]; }
72  
  synchronized double nextToLast() { ret data[size-2]; }
73  
  
74  
  toString { ret squareBracket(joinWithSpace(toList())); }
75  
  
76  
  public Iterator<Double> iterator() {
77  
    ret new ItIt<Double> {
78  
      int i = 0;
79  
      
80  
      public bool hasNext() { ret i < size(); }
81  
      public Double next() {
82  
        synchronized(SynchronizedFloatBufferPresentingAsDoubles.this) {
83  
          //if (!hasNext()) fail("Index out of bounds: " + i);
84  
          ret (double) data[i++];
85  
        }
86  
      }
87  
    };
88  
  }
89  
  
90  
  /*public DoubleIterator doubleIterator() {
91  
    ret new DoubleIterator {
92  
      int i = 0;
93  
      
94  
      public bool hasNext() { ret i < size; }
95  
      public int next() {
96  
        //if (!hasNext()) fail("Index out of bounds: " + i);
97  
        ret data[i++];
98  
      }
99  
      toString { ret "Iterator@" + i + " over " + DoubleBuffer.this; }
100  
    };
101  
  }*/
102  
  
103  
  public synchronized void trimToSize {
104  
    data = resizeFloatArray(data, size);
105  
  }
106  
  
107  
  public synchronized int indexOf(double b) {
108  
    for i to size:
109  
      if (data[i] == (float) b)
110  
        ret i;
111  
    ret -1;
112  
  }
113  
  
114  
  synchronized double[] subArray(int start, int end) {
115  
    ret subFloatArrayAsDoubleArray(data, start, min(end, size));
116  
  }
117  
  
118  
  public synchronized void insertAt(int idx, double[] l) {
119  
    int n = l(l);
120  
    if (n == 0) ret;
121  
    float[] newData = new[size+n];
122  
    arraycopy(data, 0, newData, 0, idx);
123  
    for i to n:
124  
      newData[idx+i] = (float) l[i];
125  
    arraycopy(data, idx, newData, idx+n, size-idx);
126  
    data = newData;
127  
    size = newData.length;
128  
  }
129  
  
130  
  public void integrityCheck {
131  
    assertTrue("Size positive", size >= 0);
132  
    assertTrue("Data length", l(data) >= size);
133  
  }
134  
}

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: 125 / 241
Version history: 19 change(s)
Referenced in: [show references]