Libraryless. Click here for Pure Java version (3826L/22K).
// TreeSet variant allowing multiple elements which are considered // equal by the comparator but not by eq(). // All operations O(log n) with n being the number of _different_ elements. sclass TreeSetWithDuplicates<A> extends AbstractSet<A> { NavigableSet<CompactLinkedHashSet<A>> sets; Comparator<A> comparator; int size; *() { this((Comparator<A>) null); } *(Comparator<A> *comparator) { sets = new TreeSet<CompactLinkedHashSet<A>>( comparator == null ? (setA, setB) -> cmp(main first(setA), main first(setB)) : (setA, setB) -> comparator.compare(main first(setA), main first(setB)) ); } protected *(NavigableSet<CompactLinkedHashSet<A>> *sets) {} /*swappable*/ CompactLinkedHashSet<A> makeInnerSet() { ret new CompactLinkedHashSet; } public bool add(A a) { var probe = probe(a); CompactLinkedHashSet<A> found = navigableSet_find(sets, probe); if (found != null) { if (!found.add(a)) false; } else sets.add(probe); ++size; true; } public bool remove(O a) { CompactLinkedHashSet<A> newSet = makeInnerSet(); newSet.add((A) a); CompactLinkedHashSet<A> found = navigableSet_find(sets, newSet); if (found == null) false; if (l(found) == 1) sets.remove(found); else found.remove(a); --size; true; } A first() { ret main first(main first(sets)); } A last() { var last = sets.last(); ret last?.last(); } public ItIt<A> iterator() { ret nestedIterator(sets, methodLambda0 iterator); } public int size() { ret size; } // may return null Set<A> tiedForFirst() { ret main first(sets); } CompactLinkedHashSet<A> probe(O a) { ret addAndReturnCollection(makeInnerSet(), (A) a); } public bool contains(O a) { ret navigableSet_find(sets, probe(a)) != null; } }
download show line numbers debug dex old transpilations
Travelled to 9 computer(s): bhatertpkbcr, ekrmjmnbrukm, mowyntqkapby, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt, xrpafgyirdlv
No comments. add comment
Snippet ID: | #1027809 |
Snippet name: | TreeSetWithDuplicates |
Eternal ID of this version: | #1027809/43 |
Text MD5: | 0e014ec5aaa230f4c5c89f6c82082dee |
Transpilation MD5: | 0e0581bf4279dca609461cadd0f3637e |
Author: | stefan |
Category: | javax |
Type: | JavaX fragment (include) |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2021-08-11 09:11:55 |
Source code size: | 1944 bytes / 73 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 360 / 1288 |
Version history: | 42 change(s) |
Referenced in: | #1032058 - TreeSetWithDuplicates (backup) #1034167 - Standard Classes + Interfaces (LIVE, continuation of #1003674) |