Libraryless. Click here for Pure Java version (59L/1K).
1 | final sclass SimpleStack<A> extends RandomAccessAbstractList<A> { |
2 | new ArrayList<A> l; |
3 | int peak; |
4 | |
5 | public A get(int i) { ret l.get(i); } |
6 | public int size() { ret l.size(); } |
7 | |
8 | public A set(int i, A a) { ret l.set(i, a); } |
9 | public void add(int i, A a) { l.add(i, a); checkSize(); } |
10 | public A remove(int i) { A a = l.remove(i); checkSize(); ret a; } |
11 | |
12 | void checkSize { |
13 | int n = size(); |
14 | if (n > peak) peak = n; // record new peak size (~actual capacity) |
15 | if (n*2 < peak) { // shrink physically when below half of peak |
16 | ifdef SimpleStack_debug |
17 | print("Shrinking SimpleStack: " + peak + " => " + n); |
18 | endifdef |
19 | trimToSize(l); |
20 | peak = n; |
21 | } |
22 | } |
23 | } |
download show line numbers debug dex old transpilations
Travelled to 7 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt, xrpafgyirdlv
No comments. add comment
Snippet ID: | #1029154 |
Snippet name: | SimpleStack - shrinks array when shrunk logically. Note: only made for adding and removing from the end, other operations may be slow |
Eternal ID of this version: | #1029154/4 |
Text MD5: | 8720ecaec9d09aab7724a0a1cf536334 |
Transpilation MD5: | a1709c2825b789261ce944dfb4ea37bc |
Author: | stefan |
Category: | javax |
Type: | JavaX fragment (include) |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2020-07-26 03:11:04 |
Source code size: | 715 bytes / 23 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 221 / 518 |
Version history: | 3 change(s) |
Referenced in: | [show references] |