Libraryless. Click here for Pure Java version (4330L/26K).
// -has fast nextElement() and prevElement() // -design allows for more functions like reordering the list // -Saves up to 34% in space over LinkedHashSet // (e.g. 22% for a set of 1,000 Ints) sclass CompactLinkedHashSet<A> extends AbstractSet<A> { new UnsynchronizedCompactHashSet<Entry<A>> entries; Entry<A> head, tail; sclass Entry<A> { A value; Entry<A> prev, next; public int hashCode() { ret _hashCode(value); } // "magic" equals function for CompactHashSet lookup without temp object public bool equals(O o) { ret o == this || eq(value, o); } } public bool add(A a) { if (entries.contains(a)) false; new Entry<A> n; n.value = a; n.prev = tail; if (tail != null) tail.next = n; tail = n; if (head == null) head = n; entries.add(n); true; } public bool remove(O a) { ret remove(entries.find(a)); } public bool remove(Entry<A> node) { if (node == null) false; if (node.next != null) node.next.prev = node.prev; else tail = node.prev; if (node.prev != null) node.prev.next = node.next; else head = node.next; entries.remove(node); true; } public int size() { ret entries.size(); } public ItIt<A> iterator() { ret new ItIt<A> { Entry<A> entry = head, prev = null; public bool hasNext() { ret entry != null; } public A next() { A a = entry.value; prev = entry; entry = entry.next; ret a; } // untested public void remove() { if (prev == null) throw new IllegalStateException; CompactLinkedHashSet.this.remove(prev); prev = null; } }; } public void clear() { entries.clear(); head = tail = null; } public bool contains(O a) { ret entries.contains(a); } public A find(O o) { Entry<A> e = entries.find(o); ret e?.value; } public A prevElement(A a) { Entry<A> e = entries.find(a); if (e == null || e.prev == null) null; ret e.prev.value; } public A nextElement(A a) { Entry<A> e = entries.find(a); if (e == null || e.next == null) null; ret e.next.value; } public A first() { ret head?.value; } public A last() { ret tail?.value; } bool removeIfSame(O o) { A value = find(o); if (value == o) { remove(value); true; } false; } }
Began life as a copy of #1031507
download show line numbers debug dex old transpilations
Travelled to 5 computer(s): bhatertpkbcr, ekrmjmnbrukm, mowyntqkapby, mqqgnosmbjvj, pyentgdyhuwx
No comments. add comment
Snippet ID: | #1031946 |
Snippet name: | CompactLinkedHashSet [OK] |
Eternal ID of this version: | #1031946/16 |
Text MD5: | cbaa73f46d330710f4bfb651fab4ddf2 |
Transpilation MD5: | b16fdec6f444e88dfd79492617f205dd |
Author: | stefan |
Category: | javax |
Type: | JavaX fragment (include) |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2021-09-23 05:29:08 |
Source code size: | 2520 bytes / 106 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 256 / 975 |
Version history: | 15 change(s) |
Referenced in: | [show references] |