Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

73
LINES

< > BotCompany Repo | #1027809 // TreeSetWithDuplicates

JavaX fragment (include) [tags: use-pretranspiled]

Libraryless. Click here for Pure Java version (3826L/22K).

1  
// TreeSet variant allowing multiple elements which are considered
2  
// equal by the comparator but not by eq().
3  
// All operations O(log n) with n being the number of _different_ elements.
4  
sclass TreeSetWithDuplicates<A> extends AbstractSet<A> {
5  
  NavigableSet<CompactLinkedHashSet<A>> sets;
6  
  Comparator<A> comparator;
7  
  int size;
8  
  
9  
  *() { this((Comparator<A>) null); }
10  
  
11  
  *(Comparator<A> *comparator) {
12  
    sets = new TreeSet<CompactLinkedHashSet<A>>(
13  
      comparator == null
14  
        ? (setA, setB) -> cmp(main first(setA), main first(setB))
15  
        : (setA, setB) -> comparator.compare(main first(setA), main first(setB))
16  
    );
17  
  }
18  
  protected *(NavigableSet<CompactLinkedHashSet<A>> *sets) {}
19  
  
20  
  /*swappable*/ CompactLinkedHashSet<A> makeInnerSet() {
21  
    ret new CompactLinkedHashSet;
22  
  }
23  
  
24  
  public bool add(A a) {
25  
    var probe = probe(a);
26  
    CompactLinkedHashSet<A> found = navigableSet_find(sets, probe);
27  
    if (found != null) {
28  
      if (!found.add(a)) false;
29  
    } else
30  
      sets.add(probe);
31  
    ++size;
32  
    true;
33  
  }
34  
  
35  
  public bool remove(O a) {
36  
    CompactLinkedHashSet<A> newSet = makeInnerSet();
37  
    newSet.add((A) a);
38  
    CompactLinkedHashSet<A> found = navigableSet_find(sets, newSet);
39  
    if (found == null) false;
40  
    if (l(found) == 1)
41  
      sets.remove(found);
42  
    else
43  
      found.remove(a);
44  
    --size;
45  
    true;
46  
  }
47  
  
48  
  A first() {
49  
    ret main first(main first(sets));
50  
  }
51  
  
52  
  A last() {
53  
    var last = sets.last();
54  
    ret last?.last();
55  
  }
56  
  
57  
  public ItIt<A> iterator() {
58  
    ret nestedIterator(sets, methodLambda0 iterator);
59  
  }
60  
  
61  
  public int size() { ret size; }
62  
  
63  
  // may return null
64  
  Set<A> tiedForFirst() { ret main first(sets); }
65  
  
66  
  CompactLinkedHashSet<A> probe(O a) {
67  
    ret addAndReturnCollection(makeInnerSet(), (A) a);
68  
  }
69  
  
70  
  public bool contains(O a) {
71  
    ret navigableSet_find(sets, probe(a)) != null;
72  
  }
73  
}

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: 300 / 1208
Version history: 42 change(s)
Referenced in: [show references]