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

71
LINES

< > BotCompany Repo | #1012721 // WeakCompactHashSet - might work

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

Transpiled version (3596L) is out of date.

1  
sclass WeakCompactHashSet<A> extends AbstractSet<A> {
2  
  new CompactHashSet<MyWeakReference<A>> set;
3  
  new ReferenceQueue<A> queue;
4  
  
5  
  sclass MyWeakReference<A> extends WeakReference<A> {
6  
    int hash;
7  
    
8  
    *(A o) {
9  
      super(o);
10  
      hash = o.hashCode();
11  
    }
12  
    
13  
    *(A o, ReferenceQueue<A> queue) {
14  
      super(o, queue);
15  
      hash = o.hashCode();
16  
    }
17  
    
18  
    public int hashCode() { ret hash; }
19  
    public bool equals(O o) {
20  
      ret o instanceof MyWeakReference
21  
        && eq(get(), ((MyWeakReference) o).get());
22  
    }
23  
  }
24  
  
25  
  // Make sure we get reaped once a minute (or whatever the policy is)
26  
  *() {
27  
    _registerWeakCollection(this);
28  
  }
29  
  
30  
  void reap() {
31  
    MyWeakReference ref;
32  
    while ((ref = (MyWeakReference) queue.poll()) != null)
33  
      set.removeIfSame(ref);
34  
  }
35  
  
36  
  public int size() { reap(); ret set.size(); }
37  
  
38  
  public Iterator<A> iterator() {
39  
    final Iterator<MyWeakReference<A>> it = set.iterator();
40  
    ret iteratorFromFunction(() -> {
41  
      while (it.hasNext()) {
42  
        MyWeakReference<A> ref = it.next();
43  
        A a = ref.get();
44  
        if (a != null) ret a;
45  
        // todo: clean up entry
46  
      }
47  
      null;
48  
    });
49  
  }
50  
  
51  
  public bool contains(O o) {
52  
    reap();
53  
    ret set.contains(new MyWeakReference(o));
54  
  }
55  
  
56  
  public bool add(A a) {
57  
    reap();
58  
    ret set.add(new MyWeakReference(a, queue));
59  
  }
60  
  
61  
  public bool remove(O o) {
62  
    reap();
63  
    ret set.remove(new MyWeakReference(o));
64  
  }
65  
  
66  
  A find(O o) {
67  
    reap();
68  
    MyWeakReference<A> ref = set.find(new MyWeakReference(o));
69  
    ret ref == null ? null : ref.get();
70  
  }
71  
}

Author comment

Began life as a copy of #1011596

download  show line numbers  debug dex  old transpilations   

Travelled to 13 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1012721
Snippet name: WeakCompactHashSet - might work
Eternal ID of this version: #1012721/18
Text MD5: df9fc8f818fe24ed08209d9194fdc8b3
Author: stefan
Category: javax / collections
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2021-07-11 16:11:43
Source code size: 1665 bytes / 71 lines
Pitched / IR pitched: No / No
Views / Downloads: 313 / 924
Version history: 17 change(s)
Referenced in: [show references]