1 ! Copyright (C) 2006 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
4 USING: hashtables kernel namespaces sequences ;
6 : if-graph over [ bind ] [ 2drop 2drop ] if ; inline
8 : (add-vertex) ( vertex edges -- )
9 dupd call [ dupd nest set-hash ] each-with ; inline
11 : add-vertex ( vertex edges graph -- )
12 [ (add-vertex) ] if-graph ; inline
14 : build-graph ( seq edges graph -- )
17 swap [ swap (add-vertex) ] each-with
20 : (remove-vertex) ( vertex graph -- ) nest remove-hash ;
22 : remove-vertex ( vertex edges graph -- )
24 dupd call [ namespace hash ?remove-hash ] each-with
27 : in-edges ( vertex graph -- seq )
28 ?hash dup [ hash-keys ] when ;
32 : (closure) ( obj quot -- )
33 over previous get hash-member? [
36 over dup previous get set-hash
37 [ call ] keep swap [ swap (closure) ] each-with
40 : closure ( obj quot -- seq )
42 H{ } clone previous set
44 previous get hash-keys