]> gitweb.factorcode.org Git - factor.git/commitdiff
graphs: move to core and simplify.
authorJohn Benediktsson <mrjbq7@gmail.com>
Mon, 21 Mar 2016 17:17:29 +0000 (10:17 -0700)
committerJohn Benediktsson <mrjbq7@gmail.com>
Mon, 21 Mar 2016 17:20:38 +0000 (10:20 -0700)
1. remove add-vertex* and remove-vertex* that contradicted the documentation.
2. graphs use hash-sets of edges instead of hashtables of {edge,edge} pairs.
3. make graphs:closure work like classes:closure, use in classes.

14 files changed:
basis/compiler/crossref/crossref.factor
basis/graphs/authors.txt [deleted file]
basis/graphs/graphs-docs.factor [deleted file]
basis/graphs/graphs-tests.factor [deleted file]
basis/graphs/graphs.factor [deleted file]
basis/graphs/summary.txt [deleted file]
basis/graphs/tags.txt [deleted file]
core/classes/classes.factor
core/graphs/authors.txt [new file with mode: 0644]
core/graphs/graphs-docs.factor [new file with mode: 0644]
core/graphs/graphs-tests.factor [new file with mode: 0644]
core/graphs/graphs.factor [new file with mode: 0644]
core/graphs/summary.txt [new file with mode: 0644]
core/graphs/tags.txt [new file with mode: 0644]

index 2e81f2674ad75dfdad60ceaf4a10218b4bd45726..09f092dbcd26426ff9ff799b13d59fc3f03676e8 100644 (file)
@@ -1,6 +1,6 @@
 ! Copyright (C) 2009, 2010 Slava Pestov.
 ! See http://factorcode.org/license.txt for BSD license.
-USING: assocs combinators fry graphs grouping kernel namespaces
+USING: assocs combinators fry grouping kernel namespaces
 sequences sets stack-checker.dependencies words ;
 IN: compiler.crossref
 
@@ -58,9 +58,17 @@ generic-call-site-crossref [ H{ } clone ] initialize
     "effect-dependencies" "conditional-dependencies" "definition-dependencies"
     [ (store-dependencies) ] tri-curry@ tri-curry* tri ;
 
+: add-xref ( word dependencies crossref -- )
+    rot '[
+        swap _ [ drop H{ } clone ] cache _ swap set-at
+    ] assoc-each ;
+
+: remove-xref ( word dependencies crossref -- )
+    [ keys ] dip '[ _ at delete-at ] with each ;
+
 : (compiled-xref) ( word dependencies generic-dependencies -- )
     compiled-crossref generic-call-site-crossref
-    [ get add-vertex* ] bi-curry@ bi-curry* bi ;
+    [ get add-xref ] bi-curry@ bi-curry* bi ;
 
 : compiled-xref ( word dependencies generic-dependencies -- )
     [ only-xref ] bi@
@@ -86,7 +94,7 @@ generic-call-site-crossref [ H{ } clone ] initialize
     join-dependencies ;
 
 : (compiled-unxref) ( word dependencies variable -- )
-    get remove-vertex* ;
+    get remove-xref ;
 
 : generic-call-sites ( word -- alist )
     "generic-call-sites" word-prop 2 group ;
diff --git a/basis/graphs/authors.txt b/basis/graphs/authors.txt
deleted file mode 100644 (file)
index 1901f27..0000000
+++ /dev/null
@@ -1 +0,0 @@
-Slava Pestov
diff --git a/basis/graphs/graphs-docs.factor b/basis/graphs/graphs-docs.factor
deleted file mode 100644 (file)
index 4aa42c6..0000000
+++ /dev/null
@@ -1,32 +0,0 @@
-USING: assocs hashtables help.markup help.syntax kernel sequences ;
-IN: graphs
-
-ARTICLE: "graphs" "Directed graph utilities"
-"Words for treating associative mappings as directed graphs can be found in the " { $vocab-link "graphs" } " vocabulary. A directed graph is represented as an assoc mapping each vertex to a set of edges entering that vertex, where the set is itself an assoc, with equal keys and values."
-$nl
-"To create a new graph, just create an assoc, for example by calling " { $link <hashtable> } ". To add vertices and edges to a graph:"
-{ $subsections add-vertex }
-"To remove vertices from the graph:"
-{ $subsections remove-vertex }
-"Since graphs are represented as assocs, they can be cleared out by calling " { $link clear-assoc } "."
-$nl
-"You can perform queries on the graph:"
-{ $subsections closure }
-"Directed graphs are used to maintain cross-referencing information for " { $link "definitions" } "." ;
-
-ABOUT: "graphs"
-
-HELP: add-vertex
-{ $values { "vertex" object } { "edges" sequence } { "graph" "an assoc mapping vertices to sequences of edges" } }
-{ $description "Adds a vertex to a directed graph, with " { $snippet "edges" } "  as the outward edges from the vertex." }
-{ $side-effects "graph" } ;
-
-HELP: remove-vertex
-{ $values { "vertex" object } { "edges" sequence } { "graph" "an assoc mapping vertices to sequences of edges" } }
-{ $description "Removes a vertex from a graph, using the given edges sequence." }
-{ $notes "The " { $snippet "edges" } " sequence must equal the value passed to " { $link add-vertex } ", otherwise some vertices of the graph may continue to refer to the removed vertex." }
-{ $side-effects "graph" } ;
-
-HELP: closure
-{ $values { "vertex" object } { "quot" { $quotation ( vertex -- assoc ) } } { "assoc" "a new assoc" } }
-{ $description "Outputs a set of all vertices reachable from " { $snippet "vertex" } " via edges given by the quotation. The set always includes " { $snippet "vertex" } "." } ;
diff --git a/basis/graphs/graphs-tests.factor b/basis/graphs/graphs-tests.factor
deleted file mode 100644 (file)
index 9e06078..0000000
+++ /dev/null
@@ -1,50 +0,0 @@
-USING: graphs tools.test namespaces kernel sorting assocs ;
-
-H{ } "g" set
-{ 1 2 3 } "v" set
-
-{ } [ "v" dup get "g" get add-vertex ] unit-test
-
-{ { "v" } } [ 1 "g" get at keys ] unit-test
-
-H{
-    { 1 H{ { 1 1 } { 2 2 } } }
-    { 2 H{ { 3 3 } { 4 4 } } }
-    { 4 H{ { 4 4 } { 5 5 } } }
-} "g" set
-
-{ { 2 3 4 5 } } [
-    2 [ "g" get at ] closure keys natural-sort
-] unit-test
-
-H{ } "g" set
-
-{ } [
-    "mary"
-    H{ { "billy" "one" } { "joey" "two" } }
-    "g" get add-vertex*
-] unit-test
-
-{ H{ { "mary" "one" } } } [
-    "billy" "g" get at
-] unit-test
-
-{ } [
-    "liz"
-    H{ { "billy" "four" } { "fred" "three" } }
-    "g" get add-vertex*
-] unit-test
-
-{ H{ { "mary" "one" } { "liz" "four" } } } [
-    "billy" "g" get at
-] unit-test
-
-{ } [
-    "mary"
-    H{ { "billy" "one" } { "joey" "two" } }
-    "g" get remove-vertex*
-] unit-test
-
-{ H{ { "liz" "four" } } } [
-    "billy" "g" get at
-] unit-test
diff --git a/basis/graphs/graphs.factor b/basis/graphs/graphs.factor
deleted file mode 100644 (file)
index ce561d9..0000000
+++ /dev/null
@@ -1,45 +0,0 @@
-! Copyright (C) 2006, 2007 Slava Pestov.
-! See http://factorcode.org/license.txt for BSD license.
-USING: assocs kernel sequences sets ;
-IN: graphs
-
-<PRIVATE
-
-: if-graph ( vertex edges graph quot -- )
-    dupd [ 3drop ] if ; inline
-
-: nest ( key graph -- hash )
-    [ drop H{ } clone ] cache ; inline
-
-PRIVATE>
-
-: add-vertex ( vertex edges graph -- )
-    [ [ nest conjoin ] curry with each ] if-graph ; inline
-
-: add-vertex* ( vertex edges graph -- )
-    [
-        swapd [ [ rot ] dip nest set-at ] 2curry assoc-each
-    ] if-graph ; inline
-
-: remove-vertex ( vertex edges graph -- )
-    [ [ at delete-at ] curry with each ] if-graph ; inline
-
-: remove-vertex* ( vertex edges graph -- )
-    [
-        swapd [ [ rot ] dip at delete-at drop ] 2curry assoc-each
-    ] if-graph ; inline
-
-<PRIVATE
-
-: (closure) ( vertex assoc quot: ( vertex -- assoc ) -- )
-    2over key? [
-        3drop
-    ] [
-        2over conjoin [ dip ] keep
-        [ [ drop ] 3dip (closure) ] 2curry assoc-each
-    ] if ; inline recursive
-
-PRIVATE>
-
-: closure ( vertex quot: ( vertex -- assoc ) -- assoc )
-    H{ } clone [ swap (closure) ] keep ; inline
diff --git a/basis/graphs/summary.txt b/basis/graphs/summary.txt
deleted file mode 100644 (file)
index aced099..0000000
+++ /dev/null
@@ -1 +0,0 @@
-Directed graphs
diff --git a/basis/graphs/tags.txt b/basis/graphs/tags.txt
deleted file mode 100644 (file)
index 42d711b..0000000
+++ /dev/null
@@ -1 +0,0 @@
-collections
index f4623225b4b52d8c62ae04d5c764fa38c7a6fd64..cd07c4a65b213d50edf554e8c88310ef8f86c76b 100644 (file)
@@ -1,6 +1,6 @@
 ! Copyright (C) 2004, 2010 Slava Pestov.
 ! See http://factorcode.org/license.txt for BSD license.
-USING: accessors assocs combinators definitions kernel
+USING: accessors assocs combinators definitions graphs kernel
 make namespaces quotations sequences sets words words.symbol ;
 IN: classes
 
@@ -135,18 +135,6 @@ GENERIC: implementors ( class/classes -- seq )
 : class-usage ( class -- seq )
     update-map get at members ;
 
-<PRIVATE
-
-: (closure) ( obj set quot: ( elt -- seq ) -- )
-    2over ?adjoin [
-        [ dip ] keep [ (closure) ] 2curry each
-    ] [ 3drop ] if ; inline recursive
-
-: closure ( obj quot -- set )
-    HS{ } clone [ swap (closure) ] keep ; inline
-
-PRIVATE>
-
 : class-usages ( class -- seq )
     [ class-usage ] closure members ;
 
@@ -157,12 +145,10 @@ M: sequence implementors [ implementors ] gather ;
 <PRIVATE
 
 : update-map+ ( class -- )
-    dup class-uses update-map get
-    [ adjoin-at ] curry with each ;
+    dup class-uses update-map get add-vertex ;
 
 : update-map- ( class -- )
-    dup class-uses update-map get
-    [ at delete ] curry with each ;
+    dup class-uses update-map get remove-vertex ;
 
 : implementors-map+ ( class -- )
     [ HS{ } clone ] dip implementors-map get set-at ;
diff --git a/core/graphs/authors.txt b/core/graphs/authors.txt
new file mode 100644 (file)
index 0000000..1901f27
--- /dev/null
@@ -0,0 +1 @@
+Slava Pestov
diff --git a/core/graphs/graphs-docs.factor b/core/graphs/graphs-docs.factor
new file mode 100644 (file)
index 0000000..1c1ca4d
--- /dev/null
@@ -0,0 +1,33 @@
+USING: assocs hashtables hash-sets help.markup help.syntax
+kernel sequences sets ;
+IN: graphs
+
+ARTICLE: "graphs" "Directed graph utilities"
+"Words for treating associative mappings as directed graphs can be found in the " { $vocab-link "graphs" } " vocabulary. A directed graph is represented as an assoc mapping each vertex to a " { $link hash-set } " of edges entering that vertex."
+$nl
+"To create a new graph, just create an assoc, for example by calling " { $link <hashtable> } ". To add vertices and edges to a graph:"
+{ $subsections add-vertex }
+"To remove vertices from the graph:"
+{ $subsections remove-vertex }
+"Since graphs are represented as assocs, they can be cleared out by calling " { $link clear-assoc } "."
+$nl
+"You can perform queries on a graph:"
+{ $subsections closure }
+"Directed graphs are used to maintain cross-referencing information for " { $link "definitions" } "." ;
+
+ABOUT: "graphs"
+
+HELP: add-vertex
+{ $values { "vertex" object } { "edges" sequence } { "graph" "an " { $link assoc } " mapping vertices to " { $link hash-set } " of edges" } }
+{ $description "Adds a vertex to a directed graph, with " { $snippet "edges" } "  as the outward edges from the vertex." }
+{ $side-effects "graph" } ;
+
+HELP: remove-vertex
+{ $values { "vertex" object } { "edges" sequence } { "graph" "an " { $link assoc } " mapping vertices to " { $link unordered-set } "s of edges" } }
+{ $description "Removes a vertex from a graph, using the given edges sequence." }
+{ $notes "The " { $snippet "edges" } " sequence must have all the values passed to " { $link add-vertex } ", otherwise some vertices of the graph may continue to refer to the removed vertex." }
+{ $side-effects "graph" } ;
+
+HELP: closure
+{ $values { "vertex" object } { "quot" { $quotation ( vertex -- edges ) } } { "set" hash-set } }
+{ $description "Outputs all vertices reachable from " { $snippet "vertex" } " via edges given by the quotation. The set always includes " { $snippet "vertex" } "." } ;
diff --git a/core/graphs/graphs-tests.factor b/core/graphs/graphs-tests.factor
new file mode 100644 (file)
index 0000000..807d347
--- /dev/null
@@ -0,0 +1,18 @@
+USING: assocs graphs kernel namespaces sets sorting tools.test ;
+
+H{ } "g" set
+{ 1 2 3 } "v" set
+
+{ } [ "v" dup get "g" get add-vertex ] unit-test
+
+{ { "v" } } [ 1 "g" get at members ] unit-test
+
+H{
+    { 1 HS{ 1 2 } }
+    { 2 HS{ 3 4 } }
+    { 4 HS{ 4 5 } }
+} "g" set
+
+{ { 2 3 4 5 } } [
+    2 [ "g" get at members ] closure members natural-sort
+] unit-test
diff --git a/core/graphs/graphs.factor b/core/graphs/graphs.factor
new file mode 100644 (file)
index 0000000..098a704
--- /dev/null
@@ -0,0 +1,29 @@
+! Copyright (C) 2006, 2007 Slava Pestov.
+! See http://factorcode.org/license.txt for BSD license.
+USING: assocs kernel sequences sets ;
+IN: graphs
+
+<PRIVATE
+
+: nest ( vertex graph -- edges )
+    [ drop HS{ } clone ] cache ; inline
+
+PRIVATE>
+
+: add-vertex ( vertex edges graph -- )
+    [ nest adjoin ] curry with each ; inline
+
+: remove-vertex ( vertex edges graph -- )
+    [ at delete ] curry with each ; inline
+
+<PRIVATE
+
+ : (closure) ( vertex set quot: ( vertex -- edges ) -- )
+     2over ?adjoin [
+         [ dip ] keep [ (closure) ] 2curry each
+     ] [ 3drop ] if ; inline recursive
+
+PRIVATE>
+
+: closure ( vertex quot: ( vertex -- edges ) -- set )
+    HS{ } clone [ swap (closure) ] keep ; inline
diff --git a/core/graphs/summary.txt b/core/graphs/summary.txt
new file mode 100644 (file)
index 0000000..aced099
--- /dev/null
@@ -0,0 +1 @@
+Directed graphs
diff --git a/core/graphs/tags.txt b/core/graphs/tags.txt
new file mode 100644 (file)
index 0000000..42d711b
--- /dev/null
@@ -0,0 +1 @@
+collections