]> gitweb.factorcode.org Git - factor.git/blob - core/graphs/graphs.factor
Initial import
[factor.git] / core / graphs / graphs.factor
1 ! Copyright (C) 2006, 2007 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: assocs kernel namespaces sequences ;
4 IN: graphs
5
6 SYMBOL: graph
7
8 : if-graph ( vertex edges graph quot -- )
9     over
10     [ graph swap with-variable ]
11     [ 2drop 2drop ] if ; inline
12
13 : nest ( key -- hash )
14     graph get [ drop H{ } clone ] cache ;
15
16 : add-vertex ( vertex edges graph -- )
17     [ [ dupd nest set-at ] curry* each ] if-graph ; inline
18
19 : remove-vertex ( vertex edges graph -- )
20     [ [ graph get at delete-at ] curry* each ] if-graph ; inline
21
22 SYMBOL: previous
23
24 : (closure) ( obj quot -- )
25     over previous get key? [
26         2drop
27     ] [
28         over dup previous get set-at
29         dup slip
30         [ nip (closure) ] curry assoc-each
31     ] if ; inline
32
33 : closure ( obj quot -- assoc )
34     H{ } clone [
35         previous [ (closure) ] with-variable
36     ] keep ; inline