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

1  
sclass SynchronizedDoubleBuffer is IDoubleBuffer {
2  
  double[] data;
3  
  int size;
4  
  
5  
  *() {}
6  
  *(int size) { if (size != 0) data = new double[size]; }
7  
  *(Iterable<Double> l) { addAll(l); }
8  
  *(Cl<Double> l) { this(l(l)); addAll(l); }
9  
  *(double... data) { this.data = data; size = l(data); }
10  
  
11  
  public synchronized void add(double i) {
12  
    if (size >= lDoubleArray(data)) {
13  
      data = resizeDoubleArray(data, Math.max(1, toInt(Math.min(maximumSafeArraySize(), lDoubleArray(data)*2L))));
14  
      if (size >= data.length) fail("DoubleBuffer too large: " + size);
15  
    }
16  
    data[size++] = 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 : resizeDoubleArray(data, size);
25  
  }
26  
  
27  
  double[] toArrayNonNull() {
28  
    ret unnull(toArray());
29  
  }
30  
  
31  
  synchronized L<Double> toList() {
32  
    ret doubleArrayToList(data, 0, size);
33  
  }
34  
35  
  synchronized L<Double> asVirtualList() {
36  
    ret new RandomAccessAbstractList<Double> {
37  
      public int size() { ret SynchronizedDoubleBuffer.this.size(); }
38  
      public Double get(int i) { ret SynchronizedDoubleBuffer.this.get(i); }
39  
      public Double set(int i, Double val) {
40  
        synchronized(SynchronizedDoubleBuffer.this) {
41  
          Double a = get(i);
42  
          data[i] = val;
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] = 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(SynchronizedDoubleBuffer.this) {
83  
          //if (!hasNext()) fail("Index out of bounds: " + i);
84  
          ret 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 = resizeDoubleArray(data, size);
105  
  }
106  
  
107  
  public synchronized int indexOf(double b) {
108  
    for i to size:
109  
      if (data[i] == b)
110  
        ret i;
111  
    ret -1;
112  
  }
113  
  
114  
  synchronized double[] subArray(int start, int end) {
115  
    ret subDoubleArray(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  
    double[] newData = new[size+n];
122  
    arraycopy(data, 0, newData, 0, idx);
123  
    arraycopy(l, 0, newData, idx, n);
124  
    arraycopy(data, idx, newData, idx+n, size-idx);
125  
    data = newData;
126  
    size = newData.length;
127  
  }
128  
  
129  
  public void integrityCheck {
130  
    assertTrue("Size positive", size >= 0);
131  
    assertTrue("Data length", l(data) >= size);
132  
  }
133  
}

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: 110 / 172
Version history: 10 change(s)
Referenced in: [show references]