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.

1  
sclass DoubleBuffer is Iterable<Double>, IntSize {
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  
  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  
  void addAll(Iterable<Double> l) {
20  
    if (l != null) for (double i : l) add(i);
21  
  }
22  
  
23  
  double[] toArray() {
24  
    ret size == 0 ? null : resizeDoubleArray(data, size);
25  
  }
26  
  
27  
  double[] toArrayNonNull() {
28  
    ret unnull(toArray());
29  
  }
30  
  
31  
  L<Double> toList() {
32  
    ret doubleArrayToList(data, 0, size);
33  
  }
34  
35  
  L<Double> asVirtualList() {
36  
    ret new RandomAccessAbstractList<Double> {
37  
      public int size() { ret size; }
38  
      public Double get(int i) { ret DoubleBuffer.this.get(i); }
39  
      public Double set(int i, Double val) {
40  
        Double 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  
52  
  double get(int idx) {
53  
    if (idx >= size) fail("Index out of range: " + idx + "/" + size);
54  
    ret data[idx];
55  
  }
56  
  
57  
  void set(int idx, double value) {
58  
    if (idx >= size) fail("Index out of range: " + idx + "/" + size);
59  
    data[idx] = value;
60  
  }
61  
  
62  
  double popLast() {
63  
    if (size == 0) fail("empty buffer");
64  
    ret data[--size];
65  
  }
66  
  
67  
  double last() { ret data[size-1]; }
68  
  double nextToLast() { ret data[size-2]; }
69  
  
70  
  toString { ret squareBracket(joinWithSpace(toList())); }
71  
  
72  
  public Iterator<Double> iterator() {
73  
    ret new ItIt<Double> {
74  
      int i = 0;
75  
      
76  
      public bool hasNext() { ret i < size; }
77  
      public Double next() {
78  
        //if (!hasNext()) fail("Index out of bounds: " + i);
79  
        ret data[i++];
80  
      }
81  
    };
82  
  }
83  
  
84  
  /*public DoubleIterator doubleIterator() {
85  
    ret new DoubleIterator {
86  
      int i = 0;
87  
      
88  
      public bool hasNext() { ret i < size; }
89  
      public int next() {
90  
        //if (!hasNext()) fail("Index out of bounds: " + i);
91  
        ret data[i++];
92  
      }
93  
      toString { ret "Iterator@" + i + " over " + DoubleBuffer.this; }
94  
    };
95  
  }*/
96  
  
97  
  void trimToSize {
98  
    data = resizeDoubleArray(data, size);
99  
  }
100  
  
101  
  int indexOf(double b) {
102  
    for i to size:
103  
      if (data[i] == b)
104  
        ret i;
105  
    ret -1;
106  
  }
107  
  
108  
  double[] subArray(int start, int end) {
109  
    ret subDoubleArray(data, start, min(end, size));
110  
  }
111  
}

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: 226 / 594
Version history: 12 change(s)
Referenced in: [show references]