Using inheritance instead of delegation in digraphs
parent
38bd87a797
commit
75780d183b
|
@ -3,7 +3,9 @@ IN: digraphs.tests
|
|||
|
||||
: test-digraph ( -- digraph )
|
||||
<digraph>
|
||||
{ { "one" 1 } { "two" 2 } { "three" 3 } { "four" 4 } { "five" 5 } } [ first2 pick add-vertex ] each
|
||||
{ { "one" "three" } { "one" "four" } { "two" "three" } { "two" "one" } { "three" "four" } } [ first2 pick add-edge ] each ;
|
||||
{ { "one" 1 } { "two" 2 } { "three" 3 } { "four" 4 } { "five" 5 } }
|
||||
[ first2 pick add-vertex ] each
|
||||
{ { "one" "three" } { "one" "four" } { "two" "three" } { "two" "one" } { "three" "four" } }
|
||||
[ first2 pick add-edge ] each ;
|
||||
|
||||
[ 5 ] [ test-digraph topological-sort length ] unit-test
|
|
@ -1,19 +1,20 @@
|
|||
! Copyright (C) 2008 Alex Chapman
|
||||
! See http://factorcode.org/license.txt for BSD license.
|
||||
USING: accessors assocs kernel sequences vectors ;
|
||||
USING: accessors assocs hashtables hashtables.private kernel sequences vectors ;
|
||||
IN: digraphs
|
||||
|
||||
TUPLE: digraph ;
|
||||
TUPLE: vertex value edges ;
|
||||
TUPLE: digraph < hashtable ;
|
||||
|
||||
: <digraph> ( -- digraph )
|
||||
digraph new H{ } clone over set-delegate ;
|
||||
0 digraph new [ reset-hash ] keep ;
|
||||
|
||||
TUPLE: vertex value edges ;
|
||||
|
||||
: <vertex> ( value -- vertex )
|
||||
V{ } clone vertex boa ;
|
||||
|
||||
: add-vertex ( key value digraph -- )
|
||||
>r <vertex> swap r> set-at ;
|
||||
[ <vertex> swap ] dip set-at ;
|
||||
|
||||
: children ( key digraph -- seq )
|
||||
at edges>> ;
|
Loading…
Reference in New Issue