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

64
LINES

< > BotCompany Repo | #1032058 // TreeSetWithDuplicates (backup)

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

Libraryless. Click here for Pure Java version (3867L/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<Set<A>> internal;
6  
  Comparator<A> comparator;
7  
  int size;
8  
  
9  
  *() { internal = new TreeSet; }
10  
  *(Comparator<A> *comparator) {
11  
    internal = new TreeSet<Set<A>>((setA, setB) -> {
12  
      ret comparator.compare(main first(setA), main first(setB));
13  
    });
14  
  }
15  
  protected *(NavigableSet<Set<A>> *internal) {}
16  
  
17  
  // Now using CompactLinkedHashSet
18  
  swappable Set<A> makeInternalSet() { ret new CompactLinkedHashSet; }
19  
  
20  
  public bool add(A a) {
21  
    Set<A> newSet = addAndReturnCollection(makeInternalSet(), a);
22  
    Set<A> found = navigableSet_find(internal, newSet);
23  
    if (found != null) {
24  
      if (!found.add(a)) false;
25  
    } else
26  
      internal.add(newSet);
27  
    ++size;
28  
    true;
29  
  }
30  
  
31  
  public bool remove(O a) {
32  
    Set<A> newSet = addAndReturnCollection(makeInternalSet(), (A) a);
33  
    Set<A> found = navigableSet_find(internal, newSet);
34  
    if (found == null) false;
35  
    if (l(found) == 1)
36  
      internal.remove(found);
37  
    else
38  
      found.remove(a);
39  
    --size;
40  
    true;
41  
  }
42  
  
43  
  A first() {
44  
    ret main first(main first(internal));
45  
  }
46  
  
47  
  A last() {
48  
    ret main last(main last(internal));
49  
  }
50  
  
51  
  public ItIt<A> iterator() {
52  
    ret nestedIterator(internal, methodLambda0 iterator);
53  
  }
54  
  
55  
  public int size() { ret size; }
56  
  
57  
  // may return null
58  
  Set<A> tiedForFirst() { ret main first(internal); }
59  
  
60  
  public bool contains(O a) {
61  
    Set<A> newSet = addAndReturnCollection(makeInternalSet(), (A) a);
62  
    ret navigableSet_find(internal, newSet) != null;
63  
  }
64  
}

Author comment

Began life as a copy of #1027809

download  show line numbers  debug dex  old transpilations   

Travelled to 3 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx

No comments. add comment

Snippet ID: #1032058
Snippet name: TreeSetWithDuplicates (backup)
Eternal ID of this version: #1032058/1
Text MD5: 02a1d6d6280d8f46bd192e16a36e9119
Transpilation MD5: 01cfada5d54f96c07ea8f29ac07f5e90
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2021-08-11 08:26:34
Source code size: 1804 bytes / 64 lines
Pitched / IR pitched: No / No
Views / Downloads: 132 / 183
Referenced in: [show references]