Libraryless. Click here for Pure Java version (10074L/55K).
1 | sclass CircularFifoBuffer<E> is Iterable<E>, IntSize {
|
2 | int capacity; |
3 | long base; |
4 | ArrayDeque<E> storage; |
5 | |
6 | public CircularFifoBuffer(int capacity) {
|
7 | this.capacity = capacity; |
8 | storage = new ArrayDeque<E>(capacity); |
9 | } |
10 | |
11 | /** |
12 | * Adds the given element to the buffer. If the buffer is full, the least recently added element is discarded so |
13 | * that a new element can be inserted. |
14 | */ |
15 | public void add(E e) {
|
16 | if (isFull()) remove(); |
17 | storage.addLast(e); |
18 | } |
19 | |
20 | /** |
21 | * Removes and returns the least recently inserted element from this buffer. |
22 | */ |
23 | public E remove() {
|
24 | ++base; |
25 | return storage.removeFirst(); |
26 | } |
27 | |
28 | /** |
29 | * Returns true iff the buffers remaining capacity is 0. |
30 | */ |
31 | public boolean isFull() {
|
32 | return storage.size() == capacity; |
33 | } |
34 | |
35 | /** |
36 | * Returns true iff the buffers size is 0. |
37 | */ |
38 | public boolean isEmpty() {
|
39 | return storage.isEmpty(); |
40 | } |
41 | |
42 | /** |
43 | * Returns the number of elements in the buffer. |
44 | */ |
45 | public int size() {
|
46 | return storage.size(); |
47 | } |
48 | |
49 | public Object[] toArray() {
|
50 | return storage.toArray(); |
51 | } |
52 | |
53 | public<T> T[] toArray(T[] a) {
|
54 | return storage.toArray(a); |
55 | } |
56 | |
57 | public List<E> asList() {
|
58 | return new ArrayList<E>(storage); |
59 | } |
60 | |
61 | public ArrayDeque getBackingStore() {
|
62 | return storage; |
63 | } |
64 | |
65 | public Iterator<E> iterator() {
|
66 | return storage.iterator(); |
67 | } |
68 | |
69 | public E last() {
|
70 | ret storage.peekLast(); |
71 | } |
72 | |
73 | void removeToSize(int size) {
|
74 | while (size() > max(size, 0)) remove(); |
75 | } |
76 | |
77 | // how many elements were discarded |
78 | long getBase() { ret base; }
|
79 | |
80 | int getCapacity() { ret capacity; }
|
81 | } |
from https://raw.githubusercontent.com/OliverColeman/ahni/master/src/com/ojcoleman/ahni/util/CircularFifoBuffer.java
download show line numbers debug dex old transpilations
Travelled to 13 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt
No comments. add comment
| Snippet ID: | #1000774 |
| Snippet name: | CircularFifoBuffer |
| Eternal ID of this version: | #1000774/7 |
| Text MD5: | 328851bf6ac32c141f72e56f5ee033d8 |
| Transpilation MD5: | aaaaa41f993396d293e3b71d7e3542c5 |
| Author: | stefan |
| Category: | |
| Type: | JavaX fragment (include) |
| Public (visible to everyone): | Yes |
| Archived (hidden from active list): | No |
| Created/modified: | 2022-12-12 02:16:55 |
| Source code size: | 1720 bytes / 81 lines |
| Pitched / IR pitched: | No / No |
| Views / Downloads: | 1078 / 2147 |
| Version history: | 6 change(s) |
| Referenced in: | [show references] |