]> gitweb.factorcode.org Git - factor.git/commitdiff
sets: adding cardinality word.
authorJohn Benediktsson <mrjbq7@gmail.com>
Tue, 28 Dec 2010 03:22:36 +0000 (19:22 -0800)
committerJohn Benediktsson <mrjbq7@gmail.com>
Tue, 28 Dec 2010 03:22:36 +0000 (19:22 -0800)
basis/bit-sets/bit-sets.factor
core/hash-sets/hash-sets.factor
core/sets/sets-docs.factor
core/sets/sets-tests.factor
core/sets/sets.factor

index aa74c2b9fbda35592b56ce12d22bd8e5550a96d1..8bbd1b45a15c11f68681f7ae0111e802b47b2dca 100644 (file)
@@ -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 ;
index ac198a2ca2023a3ce4813a991fc125b3c7f9e12d..028c324f6a8164669df4f53dfa7a8de54fb76207 100644 (file)
@@ -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 ;
index bf2b6904c3dba4c5ffb2e9a51df33a2557148772..86be47ce307d9ddef75bc26e3d6b43a4708c77a0 100644 (file)
@@ -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." } ;
index 9a48acc4cfc0ef64bb85720f2e3d98a69fc2288a..e271dc3d2287fb750aa3753c03aec4457b5654f8 100644 (file)
@@ -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
index 9c1870aa2e57634feee580262f0813bf65771b93..ae15908e4cb594aed40ae53e3122bff54322ce3a 100644 (file)
@@ -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 ]