1 ! Copyright (C) 2008, 2009 Slava Pestov, Doug Coleman.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: assocs hashtables kernel sequences vectors ;
6 : adjoin ( elt seq -- ) [ delete ] [ push ] 2bi ;
8 : conjoin ( elt assoc -- ) dupd set-at ;
10 : (prune) ( elt hash vec -- )
11 3dup drop key? [ 3drop ] [
12 [ drop conjoin ] [ nip push ] 3bi
15 : prune ( seq -- newseq )
16 [ ] [ length <hashtable> ] [ length <vector> ] tri
17 [ [ (prune) ] 2curry each ] keep ;
19 : duplicates ( seq -- newseq )
20 H{ } clone [ [ key? ] [ conjoin ] 2bi ] curry filter ;
22 : gather ( seq quot -- newseq )
23 map concat prune ; inline
25 : unique ( seq -- assoc )
26 [ dup ] H{ } map>assoc ;
28 : (all-unique?) ( elt hash -- ? )
29 2dup key? [ 2drop f ] [ conjoin t ] if ;
31 : all-unique? ( seq -- ? )
32 dup length <hashtable> [ (all-unique?) ] curry all? ;
36 : tester ( seq -- quot ) unique [ key? ] curry ; inline
40 : intersect ( seq1 seq2 -- newseq )
43 : intersects? ( seq1 seq2 -- ? )
46 : diff ( seq1 seq2 -- newseq )
47 tester [ not ] compose filter ;
49 : union ( seq1 seq2 -- newseq )
52 : subset? ( seq1 seq2 -- ? )
55 : set= ( seq1 seq2 -- ? )