Libraryless. Click here for Pure Java version (10233L/57K).
1 | sclass SynchronizedLongBuffer is ILongBuffer {
|
2 | long[] data; |
3 | int size; |
4 | |
5 | *() {}
|
6 | *(int size) { if (size != 0) data = new long[size]; }
|
7 | *(Iterable<Long> l) {
|
8 | if (l cast Cl) allocate(l.size()); |
9 | addAll(l); |
10 | } |
11 | |
12 | public synchronized void add(long i) {
|
13 | if (size >= lLongArray(data)) {
|
14 | data = resizeLongArray(data, Math.max(1, toInt(Math.min(maximumSafeArraySize(), lLongArray(data)*2L)))); |
15 | if (size >= data.length) fail("LongBuffer too large: " + size);
|
16 | } |
17 | data[size++] = i; |
18 | } |
19 | |
20 | synchronized void allocate(int n) {
|
21 | data = resizeLongArray(data, max(n, size())); |
22 | } |
23 | |
24 | public synchronized void addAll(Iterable<Long> l) {
|
25 | if (l != null) for (long i : l) add(i); |
26 | } |
27 | |
28 | public synchronized long[] toArray() {
|
29 | ret size == 0 ? null : resizeLongArray(data, size); |
30 | } |
31 | |
32 | synchronized L<Long> toList() {
|
33 | ret longArrayToList(data, 0, size); |
34 | } |
35 | |
36 | public synchronized L<Long> asVirtualList() {
|
37 | ret listFromFunction get(size); |
38 | } |
39 | |
40 | synchronized void reset { size = 0; }
|
41 | void clear { reset(); }
|
42 | |
43 | synchronized public int size() { ret size; }
|
44 | public synchronized bool isEmpty() { ret size == 0; }
|
45 | |
46 | public synchronized long get(int idx) {
|
47 | if (idx >= size) fail("Index out of range: " + idx + "/" + size);
|
48 | ret data[idx]; |
49 | } |
50 | |
51 | synchronized void set(int idx, long value) {
|
52 | if (idx >= size) fail("Index out of range: " + idx + "/" + size);
|
53 | data[idx] = value; |
54 | } |
55 | |
56 | public synchronized long popLast() {
|
57 | if (size == 0) fail("empty buffer");
|
58 | ret data[--size]; |
59 | } |
60 | |
61 | public synchronized long last() { ret data[size-1]; }
|
62 | synchronized long nextToLast() { ret data[size-2]; }
|
63 | |
64 | toString { ret squareBracket(joinWithSpace(toList())); }
|
65 | |
66 | public synchronized Iterator<Long> iterator() {
|
67 | ret new ItIt<Long> {
|
68 | int i = 0; |
69 | |
70 | public bool hasNext() { ret i < size(); }
|
71 | public Long next() {
|
72 | synchronized(SynchronizedLongBuffer.this) {
|
73 | if (!hasNext()) fail("Index out of bounds: " + i);
|
74 | ret data[i++]; |
75 | } |
76 | } |
77 | }; |
78 | } |
79 | |
80 | public synchronized void trimToSize {
|
81 | data = resizeLongArray(data, size); |
82 | } |
83 | |
84 | synchronized void remove(int idx) {
|
85 | arraycopy(data, idx+1, data, idx, size-1-idx); |
86 | --size; |
87 | } |
88 | |
89 | // don't rely on return value if buffer is empty |
90 | public synchronized long poll() {
|
91 | ret size == 0 ? -1 : data[--size]; |
92 | } |
93 | |
94 | public synchronized void insertAt(int idx, long[] l) {
|
95 | int n = l(l); |
96 | if (n == 0) ret; |
97 | long[] newData = new[size+n]; |
98 | arraycopy(data, 0, newData, 0, idx); |
99 | arraycopy(l, 0, newData, idx, n); |
100 | arraycopy(data, idx, newData, idx+n, size-idx); |
101 | data = newData; |
102 | size = newData.length; |
103 | } |
104 | |
105 | public void integrityCheck {
|
106 | assertTrue("Size positive", size >= 0);
|
107 | assertTrue("Data length", l(data) >= size);
|
108 | } |
109 | } |
Began life as a copy of #1029186
download show line numbers debug dex old transpilations
Travelled to 2 computer(s): elmgxqgtpvxh, mqqgnosmbjvj
No comments. add comment
| Snippet ID: | #1036243 |
| Snippet name: | SynchronizedLongBuffer |
| Eternal ID of this version: | #1036243/10 |
| Text MD5: | b0c96f4dafb2cc21e5d8764c2a1595b2 |
| Transpilation MD5: | 303ef611a08d80f8a6c008492eecaf33 |
| 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:38 |
| Source code size: | 2965 bytes / 109 lines |
| Pitched / IR pitched: | No / No |
| Views / Downloads: | 850 / 1118 |
| Version history: | 9 change(s) |
| Referenced in: | [show references] |