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

31
LINES

< > BotCompany Repo | #1034799 // UniquifyUsingHasher

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

Libraryless. Click here for Pure Java version (22420L/137K).

1  
// A: object type
2  
// B: hash type (e.g. Long)
3  
sclass UniquifyUsingHasher<A, B> {
4  
  settable IF1<A, B> hasher;
5  
  settable IF2<A, A, Bool> equalsPredicate = l2 eq;
6  
  
7  
  *(IF1<A, B> *hasher) {}
8  
  *(IF1<A, B> *hasher, IF2<A, A, Bool> *equalsPredicate) {}
9  
  
10  
  new MultiMap<B, A> byHash;
11  
  gettable new L<A> list;
12  
  
13  
  bool add(A a) {
14  
    B hash = hasher.get(a);
15  
    if (any(byHash.get(hash), x -> equalsPredicate.get(x, a)))
16  
      false;
17  
    byHash.put(hash, a);
18  
    list.add(a);
19  
    true;
20  
  }
21  
  
22  
  selfType addAll(Iterable<? extends A> l) {
23  
    fOr (A a : l)
24  
      add(a);
25  
    this;
26  
  }
27  
  
28  
  L<A> get() { ret list; }
29  
  
30  
  int nHashCollisions() { ret l(keysWithMultipleValues(byHash)); }
31  
}

download  show line numbers  debug dex  old transpilations   

Travelled to 4 computer(s): bhatertpkbcr, ekrmjmnbrukm, mowyntqkapby, mqqgnosmbjvj

No comments. add comment

Snippet ID: #1034799
Snippet name: UniquifyUsingHasher
Eternal ID of this version: #1034799/14
Text MD5: 0b4bef4590d303036475602271aeb29f
Transpilation MD5: e44bbc43cb950d42e2122655f5a31502
Author: stefan
Category: javax / imaging
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-08-02 19:23:34
Source code size: 715 bytes / 31 lines
Pitched / IR pitched: No / No
Views / Downloads: 105 / 219
Version history: 13 change(s)
Referenced in: [show references]