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

129
LINES

< > BotCompany Repo | #1036283 // SynchronizedLongBufferStoredAsLinearInts

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

Transpiled version (10139L) is out of date.

1  
persistable sclass SynchronizedLongBufferStoredAsLinearInts is ILongBuffer {
2  
  int[] data;
3  
  int size;
4  
  
5  
  // affine conversion to long (long = int*factor+base)
6  
  long base, factor;
7  
  
8  
  *(long *base, long *factor) {}
9  
  *(long *base, long *factor, int size) { if (size != 0) data = new int[size]; }
10  
  *(long *base, long *factor, Iterable<Long> l) {
11  
    if (l cast Cl) allocate(l.size());
12  
    addAll(l);
13  
  }
14  
  
15  
  public synchronized void addRaw(int i) {
16  
    if (size >= lIntArray(data)) {
17  
      data = resizeIntArray(data, Math.max(1, toInt(Math.min(maximumSafeArraySize(), lIntArray(data)*2L))));
18  
      if (size >= data.length) fail(shortClassName(this) + " too large: " + size);
19  
    }
20  
    data[size++] = i;
21  
  }
22  
  
23  
  public synchronized void add(long i) {
24  
    addRaw(compress(i));
25  
  }
26  
  
27  
  synchronized void allocate(int n) {
28  
    data = resizeIntArray(data, max(n, size()));
29  
  }
30  
  
31  
  public synchronized void addAll(Iterable<Long> l) {
32  
    if (l != null) for (long i : l) add(i);
33  
  }
34  
  
35  
  public synchronized long[] toArray() {
36  
    if (size == 0) null;
37  
    long[] out = new[size];
38  
    for i to size:
39  
      out[i] = expand(data[i]);
40  
    ret out;
41  
  }
42  
  
43  
  /*synchronized L<Long> toList() {
44  
    ret longArrayToList(data, 0, size);
45  
  }*/
46  
  
47  
  public synchronized L<Long> asVirtualList() {
48  
    ret listFromFunction get(size);
49  
  }
50  
  
51  
  synchronized void reset { size = 0; }
52  
  void clear { reset(); }
53  
  
54  
  synchronized public int size() { ret size; }
55  
  public synchronized bool isEmpty() { ret size == 0; }
56  
  
57  
  public synchronized int getRaw(int idx) {
58  
    if (idx >= size) fail("Index out of range: " + idx + "/" + size);
59  
    ret data[idx];
60  
  }
61  
  
62  
  public synchronized long get(int idx) {
63  
    if (idx >= size) fail("Index out of range: " + idx + "/" + size);
64  
    ret expand(data[idx]);
65  
  }
66  
  
67  
  synchronized void set(int idx, long value) {
68  
    if (idx >= size) fail("Index out of range: " + idx + "/" + size);
69  
    data[idx] = compress(value);
70  
  }
71  
  
72  
  public synchronized long popLast() {
73  
    if (size == 0) fail("empty buffer");
74  
    ret expand(data[--size]);
75  
  }
76  
  
77  
  public synchronized long last() { ret expand(data[size-1]); }
78  
  synchronized long nextToLast() { ret expand(data[size-2]); }
79  
  
80  
  toString { ret squareBracket(joinWithSpace(asVirtualList())); }
81  
  
82  
  public synchronized Iterator<Long> iterator() {
83  
    ret new ItIt<Long> {
84  
      int i = 0;
85  
      
86  
      public bool hasNext() { ret i < size(); }
87  
      public Long next() {
88  
        synchronized(SynchronizedLongBufferStoredAsLinearInts.this) {
89  
          if (!hasNext()) fail("Index out of bounds: " + i);
90  
          ret expand(data[i++]);
91  
        }
92  
      }
93  
    };
94  
  }
95  
  
96  
  public synchronized void trimToSize {
97  
    data = resizeIntArray(data, size);
98  
  }
99  
  
100  
  synchronized void remove(int idx) {
101  
    arraycopy(data, idx+1, data, idx, size-1-idx);
102  
    --size;
103  
  }
104  
  
105  
  // don't rely on return value if buffer is empty
106  
  public synchronized long poll() { 
107  
    ret size == 0 ? -1 : expand(data[--size]);
108  
  }
109  
  
110  
  long expand(int value) { ret base+value*factor; }
111  
  int compress(long value) { ret toInt(ldiv_round(value-base, factor); }
112  
  
113  
  public synchronized void insertAt(int idx, long[] l) {
114  
    int n = l(l);
115  
    if (n == 0) ret;
116  
    int[] newData = new[size+n];
117  
    arraycopy(data, 0, newData, 0, idx);
118  
    for i to n:
119  
      newData[idx+i] = compress(l[i]);
120  
    arraycopy(data, idx, newData, idx+n, size-idx);
121  
    data = newData;
122  
    size = newData.length;
123  
  }
124  
  
125  
  public void integrityCheck {
126  
    assertTrue("Size positive", size >= 0);
127  
    assertTrue("Data length", l(data) >= size);
128  
  }
129  
}

Author comment

Began life as a copy of #1036243

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1036283
Snippet name: SynchronizedLongBufferStoredAsLinearInts
Eternal ID of this version: #1036283/13
Text MD5: 64502164a79b06e0c2df4f3cee805ae3
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:18:15
Source code size: 3684 bytes / 129 lines
Pitched / IR pitched: No / No
Views / Downloads: 174 / 279
Version history: 12 change(s)
Referenced in: [show references]