factor/library/collections/graphs.factor

46 lines
1.1 KiB
Factor
Raw Normal View History

! Copyright (C) 2006 Slava Pestov.
! See http://factorcode.org/license.txt for BSD license.
IN: graphs
USING: hashtables kernel namespaces sequences ;
: if-graph over [ bind ] [ 2drop 2drop ] if ; inline
2006-08-15 16:29:35 -04:00
: (add-vertex) ( vertex edges -- )
dupd call [ dupd nest set-hash ] each-with ; inline
2006-08-15 16:29:35 -04:00
: add-vertex ( vertex edges graph -- )
[ (add-vertex) ] if-graph ; inline
2006-08-15 16:29:35 -04:00
: build-graph ( seq edges graph -- )
[
namespace clear-hash
swap [ swap (add-vertex) ] each-with
] if-graph ;
: (remove-vertex) ( vertex graph -- ) nest remove-hash ;
: remove-vertex ( vertex edges graph -- )
2006-06-10 00:53:29 -04:00
[
dupd call [ namespace hash ?remove-hash ] each-with
] if-graph ; inline
: in-edges ( vertex graph -- seq )
?hash dup [ hash-keys ] when ;
2006-07-25 01:37:54 -04:00
SYMBOL: previous
: (closure) ( obj quot -- )
over previous get hash-member? [
2drop
2006-07-25 01:37:54 -04:00
] [
over dup previous get set-hash
[ call ] keep swap [ swap (closure) ] each-with
] if ; inline
2006-08-15 16:29:35 -04:00
: closure ( obj quot -- seq )
[
2006-07-25 01:37:54 -04:00
H{ } clone previous set
(closure)
2006-07-25 01:37:54 -04:00
previous get hash-keys
] with-scope ; inline