factor/core/collections/graphs.factor

48 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: assocs hashtables kernel namespaces sequences ;
: if-graph over [ bind ] [ 2drop 2drop ] if ; inline
: nest ( key -- hash ) namespace [ drop H{ } clone ] cache ;
2006-08-15 16:29:35 -04:00
: (add-vertex) ( vertex edges -- )
dupd call [ dupd nest set-at ] 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-assoc
swap [ swap (add-vertex) ] each-with
] if-graph ;
: (remove-vertex) ( vertex graph -- ) nest delete-at ;
: remove-vertex ( vertex edges graph -- )
2006-06-10 00:53:29 -04:00
[
dupd call [ namespace at delete-at ] each-with
2006-06-10 00:53:29 -04:00
] if-graph ; inline
: in-edges ( vertex graph -- seq )
at dup [ keys ] when ;
2006-07-25 01:37:54 -04:00
SYMBOL: previous
: (closure) ( obj quot -- )
over previous get key? [
2drop
2006-07-25 01:37:54 -04:00
] [
over dup previous get set-at
2006-07-25 01:37:54 -04:00
[ 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)
previous get keys
2006-07-25 01:37:54 -04:00
] with-scope ; inline