Using inheritance instead of delegation in digraphs

db4
Alex Chapman 2008-10-01 10:57:15 +10:00
parent 38bd87a797
commit 75780d183b
5 changed files with 10 additions and 7 deletions

View File

@ -3,7 +3,9 @@ IN: digraphs.tests
: test-digraph ( -- digraph ) : test-digraph ( -- digraph )
<digraph> <digraph>
{ { "one" 1 } { "two" 2 } { "three" 3 } { "four" 4 } { "five" 5 } } [ first2 pick add-vertex ] each { { "one" 1 } { "two" 2 } { "three" 3 } { "four" 4 } { "five" 5 } }
{ { "one" "three" } { "one" "four" } { "two" "three" } { "two" "one" } { "three" "four" } } [ first2 pick add-edge ] each ; [ 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 [ 5 ] [ test-digraph topological-sort length ] unit-test

View File

@ -1,19 +1,20 @@
! Copyright (C) 2008 Alex Chapman ! Copyright (C) 2008 Alex Chapman
! See http://factorcode.org/license.txt for BSD license. ! 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 IN: digraphs
TUPLE: digraph ; TUPLE: digraph < hashtable ;
TUPLE: vertex value edges ;
: <digraph> ( -- digraph ) : <digraph> ( -- digraph )
digraph new H{ } clone over set-delegate ; 0 digraph new [ reset-hash ] keep ;
TUPLE: vertex value edges ;
: <vertex> ( value -- vertex ) : <vertex> ( value -- vertex )
V{ } clone vertex boa ; V{ } clone vertex boa ;
: add-vertex ( key value digraph -- ) : add-vertex ( key value digraph -- )
>r <vertex> swap r> set-at ; [ <vertex> swap ] dip set-at ;
: children ( key digraph -- seq ) : children ( key digraph -- seq )
at edges>> ; at edges>> ;