factor/core/hash-sets/hash-sets.factor

28 lines
822 B
Factor
Raw Normal View History

2010-02-26 11:01:57 -05:00
! Copyright (C) 2010 Daniel Ehrenberg
! See http://factorcode.org/license.txt for BSD license.
USING: accessors assocs hashtables kernel new-sets
sequences parser ;
QUALIFIED: sets
IN: hash-sets
! In a better implementation, less memory would be used
TUPLE: hash-set { table hashtable read-only } ;
: <hash-set> ( members -- hash-set )
[ dup ] H{ } map>assoc hash-set boa ;
INSTANCE: hash-set set
M: hash-set in? table>> key? ; inline
M: hash-set adjoin table>> dupd set-at ; inline
M: hash-set delete table>> delete-at ; inline
M: hash-set members table>> keys ; inline
M: hash-set set-like
drop dup hash-set? [ members <hash-set> ] unless ;
M: hash-set clone
table>> clone hash-set boa ;
M: sequence fast-set <hash-set> ;
M: sequence duplicates
HS{ } clone [ [ in? ] [ adjoin ] 2bi ] curry filter ;