Libraryless. Click here for Pure Java version (3800L/21K).
sclass WeakCompactLinkedHashSet<A> extends AbstractSet<A> { new CompactLinkedHashSet<MyWeakReference<A>> set; new ReferenceQueue<A> queue; sclass MyWeakReference<A> extends WeakReference<A> { int hash; *(A o) { super(o); hash = o.hashCode(); } *(A o, ReferenceQueue<A> queue) { super(o, queue); hash = o.hashCode(); } public int hashCode() { ret hash; } public bool equals(O o) { ret o instanceof MyWeakReference && eq(get(), ((MyWeakReference) o).get()); } } // Make sure we get reaped once a minute (or whatever the policy is) *() { _registerWeakCollection(this); } void reap() { MyWeakReference ref; while ((ref = (MyWeakReference) queue.poll()) != null) set.removeIfSame(ref); } public int size() { reap(); ret set.size(); } public Iterator<A> iterator() { final Iterator<MyWeakReference<A>> it = set.iterator(); ret iteratorFromFunction(() -> { while (it.hasNext()) { MyWeakReference<A> ref = it.next(); A a = ref.get(); if (a != null) ret a; // todo: clean up entry } null; }); } public bool contains(O o) { reap(); ret set.contains(new MyWeakReference(o)); } public bool add(A a) { reap(); ret set.add(new MyWeakReference(a, queue)); } public bool remove(O o) { reap(); ret set.remove(new MyWeakReference(o)); } A find(O o) { reap(); MyWeakReference<A> ref = set.find(new MyWeakReference(o)); ret ref == null ? null : ref.get(); } }
Began life as a copy of #1012721
download show line numbers debug dex old transpilations
Travelled to 3 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx
No comments. add comment
Snippet ID: | #1032095 |
Snippet name: | WeakCompactLinkedHashSet [should work] |
Eternal ID of this version: | #1032095/3 |
Text MD5: | ccb6625bcb0aec45681d49eb49620f33 |
Transpilation MD5: | 42ced2d54936f16de10d76ce420286b5 |
Author: | stefan |
Category: | javax / collections |
Type: | JavaX fragment (include) |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2021-08-11 14:42:16 |
Source code size: | 1677 bytes / 71 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 144 / 289 |
Version history: | 2 change(s) |
Referenced in: | [show references] |