sets: adding cardinality word.
parent
160016dab9
commit
47faf5f6ae
|
@ -1,6 +1,7 @@
|
|||
! Copyright (C) 2009 Slava Pestov.
|
||||
! See http://factorcode.org/license.txt for BSD license.
|
||||
USING: kernel accessors sequences byte-arrays bit-arrays math hints sets ;
|
||||
USING: kernel accessors sequences byte-arrays bit-arrays math
|
||||
math.bitwise hints sets ;
|
||||
IN: bit-sets
|
||||
|
||||
TUPLE: bit-set { table bit-array read-only } ;
|
||||
|
@ -84,3 +85,6 @@ M: bit-set set-like
|
|||
|
||||
M: bit-set clone
|
||||
table>> clone bit-set boa ;
|
||||
|
||||
M: bit-set cardinality
|
||||
table>> bit-array>integer bit-count ;
|
||||
|
|
|
@ -19,6 +19,7 @@ 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: hash-set null? table>> assoc-empty? ;
|
||||
M: hash-set cardinality table>> assoc-size ;
|
||||
|
||||
M: sequence fast-set <hash-set> ;
|
||||
M: f fast-set drop H{ } clone hash-set boa ;
|
||||
|
|
|
@ -184,3 +184,7 @@ HELP: without
|
|||
HELP: null?
|
||||
{ $values { "set" set } { "?" "a boolean" } }
|
||||
{ $description "Tests whether the given set is empty. This outputs " { $snippet "t" } " when given a null set of any type." } ;
|
||||
|
||||
HELP: cardinality
|
||||
{ $values { "set" set } { "n" "a non-negative integer" } }
|
||||
{ $description "Returns the number of elements in the set. All sets support this operation." } ;
|
||||
|
|
|
@ -64,3 +64,7 @@ IN: sets.tests
|
|||
|
||||
[ t ] [ f null? ] unit-test
|
||||
[ f ] [ { 4 } null? ] unit-test
|
||||
|
||||
[ 0 ] [ f cardinality ] unit-test
|
||||
[ 0 ] [ { } cardinality ] unit-test
|
||||
[ 1 ] [ HS{ 1 } cardinality ] unit-test
|
||||
|
|
|
@ -22,12 +22,17 @@ GENERIC: set= ( set1 set2 -- ? )
|
|||
GENERIC: duplicates ( set -- seq )
|
||||
GENERIC: all-unique? ( set -- ? )
|
||||
GENERIC: null? ( set -- ? )
|
||||
GENERIC: cardinality ( set -- n )
|
||||
|
||||
M: f cardinality drop 0 ;
|
||||
|
||||
! Defaults for some methods.
|
||||
! Override them for efficiency
|
||||
|
||||
M: set null? members null? ; inline
|
||||
|
||||
M: set cardinality members length ;
|
||||
|
||||
M: set set-like drop ; inline
|
||||
|
||||
M: set union
|
||||
|
@ -54,7 +59,7 @@ M: set intersects?
|
|||
|
||||
M: set subset?
|
||||
sequence/tester all? ;
|
||||
|
||||
|
||||
M: set set=
|
||||
2dup subset? [ swap subset? ] [ 2drop f ] if ;
|
||||
|
||||
|
@ -94,10 +99,13 @@ M: sequence set-like
|
|||
|
||||
M: sequence members
|
||||
[ pruned ] keep like ;
|
||||
|
||||
|
||||
M: sequence null?
|
||||
empty? ; inline
|
||||
|
||||
M: sequence cardinality
|
||||
length ;
|
||||
|
||||
: combine ( sets -- set )
|
||||
[ f ]
|
||||
[ [ [ members ] map concat ] [ first ] bi set-like ]
|
||||
|
|
Loading…
Reference in New Issue