Transpiled version (10025L) is out of date.
1 | // Has long indices! |
2 | persistable sclass SimpleCircularBuffer<A> is Iterable<A>, IntSize { |
3 | A[] buffer; |
4 | long base; // elements dropped due to overcapacity |
5 | int size; // elements actually contained |
6 | |
7 | *(int capacity) { |
8 | buffer = (A[]) new O[capacity]; |
9 | } |
10 | |
11 | synchronized void add(A a) { |
12 | if (size == buffer.length) { |
13 | --size; |
14 | ++base; |
15 | } |
16 | buffer[(int) ((base+size) % buffer.length)] = a; |
17 | ++size; |
18 | } |
19 | |
20 | synchronized A get(long pos) { |
21 | if (pos < base || pos >= base+size) null; |
22 | ret buffer[(int) (pos % buffer.length)]; |
23 | } |
24 | |
25 | synchronized A getFromBase(long pos) { |
26 | ret get(pos+base); |
27 | } |
28 | |
29 | public synchronized int size() { |
30 | ret size; |
31 | } |
32 | |
33 | public Iterator<A> iterator() { |
34 | ret new ItIt<A> { |
35 | long i; |
36 | public bool hasNext() { ret i < size(); } |
37 | public A next() { ret get(base + (i++)); } |
38 | }; |
39 | } |
40 | |
41 | synchronized int capacity() { |
42 | ret buffer.length; |
43 | } |
44 | |
45 | synchronized bool isFull() { |
46 | ret size() == capacity(); |
47 | } |
48 | |
49 | public bool isEmpty() { |
50 | ret size() == 0; |
51 | } |
52 | |
53 | synchronized long getBase aka base() { |
54 | ret base; |
55 | } |
56 | |
57 | // pop first actually contained element |
58 | synchronized A popFirst aka remove() { |
59 | if (isEmpty()) null; |
60 | A a = get(base); |
61 | --size; |
62 | ++base; |
63 | ret a; |
64 | } |
65 | |
66 | synchronized A nextToLast() { ret get(base+size-2); } |
67 | synchronized A last() { ret get(base+size-1); } |
68 | synchronized A first() { ret get(base); } |
69 | |
70 | L<A> asList() { |
71 | ret main asList(this); |
72 | } |
73 | } |
download show line numbers debug dex old transpilations
Travelled to 6 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt
No comments. add comment
Snippet ID: | #1025789 |
Snippet name: | SimpleCircularBuffer (synchronized) |
Eternal ID of this version: | #1025789/25 |
Text MD5: | f012b243829fb1036f45bd91b51a7010 |
Author: | stefan |
Category: | javax |
Type: | JavaX fragment (include) |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2022-12-20 23:10:09 |
Source code size: | 1570 bytes / 73 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 330 / 884 |
Version history: | 24 change(s) |
Referenced in: | [show references] |