Libraryless. Click here for Pure Java version (9152L/50K).
1 | // AppendableChain has one "smart" head element (with size counter |
2 | // and pointer to the chain's last element), all the other nodes are |
3 | // maximally simple (MinimalChain). |
4 | // This allows O(1) front insertion, front removal and back insertion |
5 | // (not removal at the back though) which is fine for what I need this |
6 | // for (event queues). |
7 | // |
8 | // Stefan Reich, Oct 21 |
9 | |
10 | sclass AppendableChain<A> extends MinimalChain<A> is Iterable<A>, IntSize { |
11 | MinimalChain<A> last; // pointer to last element in chain (which may be us) |
12 | gettable int size; // total length of chain |
13 | |
14 | *() {} // only used internally |
15 | *(A *element) { size = 1; last = this; } |
16 | |
17 | // intermediate constructor called by itemPlusChain() |
18 | *(A *element, AppendableChain<A> *next) { |
19 | if (next == null) ret; |
20 | |
21 | new MinimalChain<A> b; |
22 | b.element = next.element; |
23 | b.next = next.next; |
24 | this.next = b; |
25 | last = next.last; |
26 | size = next.size+1; |
27 | } |
28 | |
29 | toString { ret str(toList()); } |
30 | |
31 | // append at the end |
32 | bool add(A a) { |
33 | MinimalChain newLast = new MinimalChain(a); |
34 | last.next = newLast; |
35 | last = newLast; |
36 | ++size; |
37 | true; |
38 | } |
39 | |
40 | // drop first element |
41 | AppendableChain<A> popFirst() { |
42 | if (next == null) null; |
43 | element = next.element; |
44 | if (last == next) last = this; |
45 | next = next.next; |
46 | --size; |
47 | this; |
48 | } |
49 | |
50 | ArrayList<A> toList() { |
51 | ArrayList<A> l = emptyList(size); |
52 | MinimalChain<A> c = this; |
53 | while (c != null) { |
54 | l.add(c.element); |
55 | c = c.next; |
56 | } |
57 | ret l; |
58 | } |
59 | |
60 | //public Iterator<A> iterator() { ret toList().iterator(); } |
61 | |
62 | class ACIt > ItIt<A> { |
63 | MinimalChain<A> c = AppendableChain.this; |
64 | |
65 | public bool hasNext() { |
66 | ret c != null; |
67 | } |
68 | |
69 | public A next() { |
70 | var a = c.element; |
71 | c = c.next; |
72 | ret a; |
73 | } |
74 | } |
75 | |
76 | public ItIt<A> iterator() { |
77 | ret new ACIt; |
78 | } |
79 | } |
Began life as a copy of #1027968
download show line numbers debug dex old transpilations
Travelled to 4 computer(s): bhatertpkbcr, mowyntqkapby, mqqgnosmbjvj, wnsclhtenguj
No comments. add comment
Snippet ID: | #1032858 |
Snippet name: | AppendableChain - singly-linked cons list (forward connected) with a pointer to the end so you can append cheaply |
Eternal ID of this version: | #1032858/22 |
Text MD5: | fece5fa7a6eb51a45bd6b0112313d864 |
Transpilation MD5: | 3751c0557823aaf817656fbe42f85aa8 |
Author: | stefan |
Category: | javax |
Type: | JavaX fragment (include) |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2022-07-19 19:34:05 |
Source code size: | 1937 bytes / 79 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 324 / 551 |
Version history: | 21 change(s) |
Referenced in: | [show references] |