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).

// A: object type
// B: hash type (e.g. Long)
sclass UniquifyUsingHasher<A, B> {
  settable IF1<A, B> hasher;
  settable IF2<A, A, Bool> equalsPredicate = l2 eq;
  
  *(IF1<A, B> *hasher) {}
  *(IF1<A, B> *hasher, IF2<A, A, Bool> *equalsPredicate) {}
  
  new MultiMap<B, A> byHash;
  gettable new L<A> list;
  
  bool add(A a) {
    B hash = hasher.get(a);
    if (any(byHash.get(hash), x -> equalsPredicate.get(x, a)))
      false;
    byHash.put(hash, a);
    list.add(a);
    true;
  }
  
  selfType addAll(Iterable<? extends A> l) {
    fOr (A a : l)
      add(a);
    this;
  }
  
  L<A> get() { ret list; }
  
  int nHashCollisions() { ret l(keysWithMultipleValues(byHash)); }
}

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: 101 / 213
Version history: 13 change(s)
Referenced in: [show references]