]> gitweb.factorcode.org Git - factor.git/blob - core/collections/graphs.factor
more sql changes
[factor.git] / core / collections / graphs.factor
1 ! Copyright (C) 2006 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 IN: graphs
4 USING: hashtables kernel namespaces sequences ;
5
6 : if-graph over [ bind ] [ 2drop 2drop ] if ; inline
7
8 : (add-vertex) ( vertex edges -- )
9     dupd call [ dupd nest set-hash ] each-with ; inline
10
11 : add-vertex ( vertex edges graph -- )
12     [ (add-vertex) ] if-graph ; inline
13
14 : build-graph ( seq edges graph -- )
15     [
16         namespace clear-hash
17         swap [ swap (add-vertex) ] each-with
18     ] if-graph ;
19
20 : (remove-vertex) ( vertex graph -- ) nest remove-hash ;
21
22 : remove-vertex ( vertex edges graph -- )
23     [
24         dupd call [ namespace hash ?remove-hash ] each-with
25     ] if-graph ; inline
26
27 : in-edges ( vertex graph -- seq )
28     ?hash dup [ hash-keys ] when ;
29
30 SYMBOL: previous
31
32 : (closure) ( obj quot -- )
33     over previous get hash-member? [
34         2drop
35     ] [
36         over dup previous get set-hash
37         [ call ] keep swap [ swap (closure) ] each-with
38     ] if ; inline
39
40 : closure ( obj quot -- seq )
41     [
42         H{ } clone previous set
43         (closure)
44         previous get hash-keys
45     ] with-scope ; inline