]> gitweb.factorcode.org Git - factor.git/commitdiff
Moving new-sets and hash-sets to core
authorDaniel Ehrenberg <littledan@Macintosh-122.local>
Fri, 26 Feb 2010 16:01:57 +0000 (11:01 -0500)
committerDaniel Ehrenberg <littledan@Macintosh-122.local>
Fri, 26 Feb 2010 16:01:57 +0000 (11:01 -0500)
basis/new-sets/new-sets-tests.factor [deleted file]
basis/new-sets/new-sets.factor [deleted file]
basis/prettyprint/backend/backend.factor
core/bootstrap/syntax.factor
core/hash-sets/hash-sets-tests.factor [new file with mode: 0644]
core/hash-sets/hash-sets.factor [new file with mode: 0644]
core/new-sets/new-sets-tests.factor [new file with mode: 0644]
core/new-sets/new-sets.factor [new file with mode: 0644]
core/syntax/syntax.factor

diff --git a/basis/new-sets/new-sets-tests.factor b/basis/new-sets/new-sets-tests.factor
deleted file mode 100644 (file)
index bd77761..0000000
+++ /dev/null
@@ -1,42 +0,0 @@
-! Copyright (C) 2010 Daniel Ehrenberg
-! See http://factorcode.org/license.txt for BSD license.
-USING: new-sets tools.test kernel prettyprint hash-sets sorting ;
-IN: new-sets.tests
-
-[ { } ] [ { } { } intersect  ] unit-test
-[ { 2 3 } ] [ { 1 2 3 } { 2 3 4 } intersect ] unit-test
-
-[ { } ] [ { } { } diff ] unit-test
-[ { 1 } ] [ { 1 2 3 } { 2 3 4 } diff ] unit-test
-
-[ { } ] [ { } { } union ] unit-test
-[ { 1 2 3 4 } ] [ { 1 2 3 } { 2 3 4 } union ] unit-test
-
-[ t ] [ { 1 2 } { 1 3 } intersects? ] unit-test
-
-[ f ] [ { 4 2 } { 1 3 } intersects? ] unit-test
-
-[ f ] [ { } { 1 } intersects? ] unit-test
-
-[ f ] [ { 1 } { } intersects? ] unit-test
-
-[ t ] [ 4 { 2 4 5 } in? ] unit-test
-[ f ] [ 1 { 2 4 5 } in? ] unit-test
-
-[ V{ 1 2 3 } ] [ 3 V{ 1 2 } clone [ adjoin ] keep ] unit-test
-[ V{ 1 2 } ] [ 2 V{ 1 2 } clone [ adjoin ] keep ] unit-test
-[ V{ 1 2 } ] [ 3 V{ 1 2 } clone [ delete ] keep ] unit-test
-[ V{ 2 } ] [ 1 V{ 1 2 } clone [ delete ] keep ] unit-test
-
-[ t ] [ { 1 2 3 } { 2 1 3 } set= ] unit-test
-[ f ] [ { 2 3 } { 1 2 3 } set= ] unit-test
-[ f ] [ { 1 2 3 } { 2 3 } set= ] unit-test
-
-[ { 1 } ] [ { 1 } members ] unit-test
-
-[ { 1 2 3 } ] [ { 1 1 1 2 2 3 3 3 3 3 } dup set-like natural-sort ] unit-test
-[ { 1 2 3 } ] [ HS{ 1 2 3 } { } set-like natural-sort ] unit-test
-
-[ HS{ 1 2 3 } ] [ { 1 2 3 } fast-set ] unit-test
-
-[ { 1 2 3 } ] [ { { 1 } { 2 } { 1 3 } } combine ] unit-test
diff --git a/basis/new-sets/new-sets.factor b/basis/new-sets/new-sets.factor
deleted file mode 100644 (file)
index 435c245..0000000
+++ /dev/null
@@ -1,68 +0,0 @@
-! Copyright (C) 2010 Daniel Ehrenberg
-! See http://factorcode.org/license.txt for BSD license.
-USING: accessors assocs hashtables kernel
-math sequences parser prettyprint.custom ;
-FROM: sets => prune ;
-IN: new-sets
-! The vocab is called new-sets for now, but only until it gets into core
-! All the code here is in the style that could be put in core
-
-! Set protocol
-MIXIN: set
-GENERIC: adjoin ( elt set -- )
-GENERIC: in? ( elt set -- ? )
-GENERIC: delete ( elt set -- )
-GENERIC: set-like ( set exemplar -- set' )
-GENERIC: fast-set ( set -- set' )
-GENERIC: members ( set -- sequence )
-GENERIC: union ( set1 set2 -- set )
-GENERIC: intersect ( set1 set2 -- set )
-GENERIC: intersects? ( set1 set2 -- ? )
-GENERIC: diff ( set1 set2 -- set )
-GENERIC: subset? ( set1 set2 -- ? )
-GENERIC: set= ( set1 set2 -- ? )
-
-! Defaults for some methods.
-! Override them for efficiency
-
-M: set union
-    [ [ members ] bi@ append ] keep set-like ;
-
-<PRIVATE
-
-: sequence/tester ( set1 set2 -- set1' quot )
-    [ members ] [ fast-set [ in? ] curry ] bi* ; inline
-
-PRIVATE>
-
-M: set intersect
-    [ sequence/tester filter ] keep set-like ;
-
-M: set diff
-    [ sequence/tester [ not ] compose filter ] keep set-like ;
-
-M: set intersects?
-    sequence/tester any? ;
-
-M: set subset?
-    sequence/tester all? ;
-    
-M: set set=
-    2dup subset? [ swap subset? ] [ 2drop f ] if ;
-
-M: set fast-set ;
-
-! Sequences are sets
-INSTANCE: sequence set
-M: sequence in? member? ; inline
-M: sequence adjoin [ delete ] [ push ] 2bi ;
-M: sequence delete remove! drop ; inline
-M: sequence set-like
-    [ dup sequence? [ prune ] [ members ] if ] dip like ;
-M: sequence members fast-set members ;
-
-USE: vocabs.loader
-"hash-sets" require
-
-: combine ( sets -- set )
-    f [ union ] reduce ;
index 11d97a5118dc8b690e8fb994c138326c9ae70a93..aead51a4e3ec8e4ab740aef872257448e49d58f7 100644 (file)
@@ -6,7 +6,8 @@ combinators continuations effects generic hashtables io
 io.pathnames io.styles kernel make math math.order math.parser
 namespaces prettyprint.config prettyprint.custom
 prettyprint.sections prettyprint.stylesheet quotations sbufs
-sequences strings vectors words words.symbol ;
+sequences strings vectors words words.symbol hash-sets ;
+FROM: new-sets => members ;
 IN: prettyprint.backend
 
 M: effect pprint* effect>string "(" ")" surround text ;
@@ -187,6 +188,7 @@ M: hashtable pprint-delims drop \ H{ \ } ;
 M: tuple pprint-delims drop \ T{ \ } ;
 M: wrapper pprint-delims drop \ W{ \ } ;
 M: callstack pprint-delims drop \ CS{ \ } ;
+M: hash-set pprint-delims drop \ HS{ \ } ;
 
 M: object >pprint-sequence ;
 M: vector >pprint-sequence ;
@@ -195,6 +197,7 @@ M: callable >pprint-sequence ;
 M: hashtable >pprint-sequence >alist ;
 M: wrapper >pprint-sequence wrapped>> 1array ;
 M: callstack >pprint-sequence callstack>array ;
+M: hash-set >pprint-sequence members ;
 
 : class-slot-sequence ( class slots -- sequence )
     [ 1array ] [ [ f 2array ] dip append ] if-empty ;
@@ -226,6 +229,7 @@ M: byte-vector pprint* pprint-object ;
 M: hashtable pprint* pprint-object ;
 M: curry pprint* pprint-object ;
 M: compose pprint* pprint-object ;
+M: hash-set pprint* pprint-object ;
 
 M: wrapper pprint*
     {
index 1870f4ac1bc5ad5e2a1ed33cddd20601a0c2f99b..c13f9f9026a1c78c35a51358e0ba306ff35746a7 100644 (file)
@@ -29,6 +29,7 @@ IN: bootstrap.syntax
         "HEX:"
         "HOOK:"
         "H{"
+        "HS{"
         "IN:"
         "INSTANCE:"
         "M:"
diff --git a/core/hash-sets/hash-sets-tests.factor b/core/hash-sets/hash-sets-tests.factor
new file mode 100644 (file)
index 0000000..2eef2bd
--- /dev/null
@@ -0,0 +1,33 @@
+! Copyright (C) 2010 Daniel Ehrenberg
+! See http://factorcode.org/license.txt for BSD license.
+USING: new-sets tools.test kernel sorting prettyprint hash-sets ;
+IN: hash-sets.tests
+
+[ { 1 2 3 } ] [ HS{ 1 2 3 } members natural-sort ] unit-test
+
+[ "HS{ 1 2 3 4 }" ] [ HS{ 1 2 3 4 } unparse ] unit-test
+
+[ t ] [ 1 HS{ 0 1 2 } in? ] unit-test
+[ f ] [ 3 HS{ 0 1 2 } in? ] unit-test
+[ HS{ 1 2 3 } ] [ 3 HS{ 1 2 } clone [ adjoin ] keep ] unit-test
+[ HS{ 1 2 } ] [ 2 HS{ 1 2 } clone [ adjoin ] keep ] unit-test
+[ HS{ 1 2 3 } ] [ 4 HS{ 1 2 3 } clone [ delete ] keep ] unit-test
+[ HS{ 1 2 } ] [ 3 HS{ 1 2 3 } clone [ delete ] keep ] unit-test
+[ HS{ 1 2 } ] [ HS{ 1 2 } fast-set ] unit-test
+[ { 1 2 } ] [ HS{ 1 2 } members natural-sort ] unit-test
+
+[ HS{ 1 2 3 4 } ] [ HS{ 1 2 3 } HS{ 2 3 4 } union ] unit-test
+[ HS{ 2 3 } ] [ HS{ 1 2 3 } HS{ 2 3 4 } intersect ] unit-test
+[ t ] [ HS{ 1 2 3 } HS{ 2 3 4 } intersects? ] unit-test
+[ f ] [ HS{ 1 } HS{ 2 3 4 } intersects? ] unit-test
+[ f ] [ HS{ 1 } HS{ 2 3 4 } subset? ] unit-test
+[ f ] [ HS{ 1 2 3 } HS{ 2 3 4 } subset? ] unit-test
+[ t ] [ HS{ 2 3 } HS{ 2 3 4 } subset? ] unit-test
+[ t ] [ HS{ } HS{ 2 3 4 } subset? ] unit-test
+[ HS{ 1 } ] [ HS{ 1 2 3 } HS{ 2 3 4 } diff ] unit-test
+[ t ] [ HS{ 1 2 3 } HS{ 2 1 3 } set= ] unit-test
+[ t ] [ HS{ 1 2 3 } HS{ 2 1 3 } = ] unit-test
+[ f ] [ HS{ 2 3 } HS{ 2 1 3 } set= ] unit-test
+[ f ] [ HS{ 1 2 3 } HS{ 2 3 } set= ] unit-test
+
+[ HS{ 1 2 } HS{ 1 2 3 } ] [ HS{ 1 2 } clone dup clone [ 3 swap adjoin ] keep ] unit-test
diff --git a/core/hash-sets/hash-sets.factor b/core/hash-sets/hash-sets.factor
new file mode 100644 (file)
index 0000000..34af2f5
--- /dev/null
@@ -0,0 +1,24 @@
+! 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> ;
diff --git a/core/new-sets/new-sets-tests.factor b/core/new-sets/new-sets-tests.factor
new file mode 100644 (file)
index 0000000..bd77761
--- /dev/null
@@ -0,0 +1,42 @@
+! Copyright (C) 2010 Daniel Ehrenberg
+! See http://factorcode.org/license.txt for BSD license.
+USING: new-sets tools.test kernel prettyprint hash-sets sorting ;
+IN: new-sets.tests
+
+[ { } ] [ { } { } intersect  ] unit-test
+[ { 2 3 } ] [ { 1 2 3 } { 2 3 4 } intersect ] unit-test
+
+[ { } ] [ { } { } diff ] unit-test
+[ { 1 } ] [ { 1 2 3 } { 2 3 4 } diff ] unit-test
+
+[ { } ] [ { } { } union ] unit-test
+[ { 1 2 3 4 } ] [ { 1 2 3 } { 2 3 4 } union ] unit-test
+
+[ t ] [ { 1 2 } { 1 3 } intersects? ] unit-test
+
+[ f ] [ { 4 2 } { 1 3 } intersects? ] unit-test
+
+[ f ] [ { } { 1 } intersects? ] unit-test
+
+[ f ] [ { 1 } { } intersects? ] unit-test
+
+[ t ] [ 4 { 2 4 5 } in? ] unit-test
+[ f ] [ 1 { 2 4 5 } in? ] unit-test
+
+[ V{ 1 2 3 } ] [ 3 V{ 1 2 } clone [ adjoin ] keep ] unit-test
+[ V{ 1 2 } ] [ 2 V{ 1 2 } clone [ adjoin ] keep ] unit-test
+[ V{ 1 2 } ] [ 3 V{ 1 2 } clone [ delete ] keep ] unit-test
+[ V{ 2 } ] [ 1 V{ 1 2 } clone [ delete ] keep ] unit-test
+
+[ t ] [ { 1 2 3 } { 2 1 3 } set= ] unit-test
+[ f ] [ { 2 3 } { 1 2 3 } set= ] unit-test
+[ f ] [ { 1 2 3 } { 2 3 } set= ] unit-test
+
+[ { 1 } ] [ { 1 } members ] unit-test
+
+[ { 1 2 3 } ] [ { 1 1 1 2 2 3 3 3 3 3 } dup set-like natural-sort ] unit-test
+[ { 1 2 3 } ] [ HS{ 1 2 3 } { } set-like natural-sort ] unit-test
+
+[ HS{ 1 2 3 } ] [ { 1 2 3 } fast-set ] unit-test
+
+[ { 1 2 3 } ] [ { { 1 } { 2 } { 1 3 } } combine ] unit-test
diff --git a/core/new-sets/new-sets.factor b/core/new-sets/new-sets.factor
new file mode 100644 (file)
index 0000000..d0541d9
--- /dev/null
@@ -0,0 +1,68 @@
+! Copyright (C) 2010 Daniel Ehrenberg
+! See http://factorcode.org/license.txt for BSD license.
+USING: accessors assocs hashtables kernel
+math sequences ;
+FROM: sets => prune ;
+IN: new-sets
+! The vocab is called new-sets for now, but only until it gets into core
+! All the code here is in the style that could be put in core
+
+! Set protocol
+MIXIN: set
+GENERIC: adjoin ( elt set -- )
+GENERIC: in? ( elt set -- ? )
+GENERIC: delete ( elt set -- )
+GENERIC: set-like ( set exemplar -- set' )
+GENERIC: fast-set ( set -- set' )
+GENERIC: members ( set -- sequence )
+GENERIC: union ( set1 set2 -- set )
+GENERIC: intersect ( set1 set2 -- set )
+GENERIC: intersects? ( set1 set2 -- ? )
+GENERIC: diff ( set1 set2 -- set )
+GENERIC: subset? ( set1 set2 -- ? )
+GENERIC: set= ( set1 set2 -- ? )
+
+! Defaults for some methods.
+! Override them for efficiency
+
+M: set union
+    [ [ members ] bi@ append ] keep set-like ;
+
+<PRIVATE
+
+: sequence/tester ( set1 set2 -- set1' quot )
+    [ members ] [ fast-set [ in? ] curry ] bi* ; inline
+
+PRIVATE>
+
+M: set intersect
+    [ sequence/tester filter ] keep set-like ;
+
+M: set diff
+    [ sequence/tester [ not ] compose filter ] keep set-like ;
+
+M: set intersects?
+    sequence/tester any? ;
+
+M: set subset?
+    sequence/tester all? ;
+    
+M: set set=
+    2dup subset? [ swap subset? ] [ 2drop f ] if ;
+
+M: set fast-set ;
+
+! Sequences are sets
+INSTANCE: sequence set
+M: sequence in? member? ; inline
+M: sequence adjoin [ delete ] [ push ] 2bi ;
+M: sequence delete remove! drop ; inline
+M: sequence set-like
+    [ dup sequence? [ prune ] [ members ] if ] dip like ;
+M: sequence members fast-set members ;
+
+USE: vocabs.loader
+"hash-sets" require
+
+: combine ( sets -- set )
+    f [ union ] reduce ;
index 0b5b32e289174a7336a8d64382c104f76af644e4..77ef643fe2a28ae08a813c5ce6a114b7f09d917f 100644 (file)
@@ -8,7 +8,7 @@ generic.standard generic.hook generic.math generic.parser classes
 io.pathnames vocabs vocabs.parser classes.parser classes.union
 classes.intersection classes.mixin classes.predicate
 classes.singleton classes.tuple.parser compiler.units
-combinators effects.parser slots ;
+combinators effects.parser slots hash-sets ;
 IN: bootstrap.syntax
 
 ! These words are defined as a top-level form, instead of with
@@ -104,6 +104,7 @@ IN: bootstrap.syntax
     "H{" [ \ } [ >hashtable ] parse-literal ] define-core-syntax
     "T{" [ parse-tuple-literal suffix! ] define-core-syntax
     "W{" [ \ } [ first <wrapper> ] parse-literal ] define-core-syntax
+    "HS{" [ \ } [ <hash-set> ] parse-literal ] define-core-syntax
 
     "POSTPONE:" [ scan-word suffix! ] define-core-syntax
     "\\" [ scan-word <wrapper> suffix! ] define-core-syntax