Add >biassoc word, and define new-assoc on biassocs so that assoc-map and such can produce biassocs when given biassocs as inputs
parent
1360e5d1e0
commit
7512ec1110
|
@ -16,13 +16,22 @@ HELP: once-at
|
|||
{ $values { "value" object } { "key" object } { "assoc" assoc } }
|
||||
{ $description "If the assoc does not contain the given key, adds the key/value pair to the assoc, otherwise does nothing." } ;
|
||||
|
||||
HELP: >biassoc
|
||||
{ $values { "assoc" assoc } { "biassoc" biassoc } }
|
||||
{ $description "Costructs a new biassoc with the same key/value pairs as the given assoc." } ;
|
||||
|
||||
ARTICLE: "biassocs" "Bidirectional assocs"
|
||||
"A " { $emphasis "bidirectional assoc" } " combines a pair of assocs to form a data structure where both normal assoc opeartions (eg, " { $link at } "), as well as " { $link "assocs-values" } " (eg, " { $link value-at } ") run in sub-linear time."
|
||||
$nl
|
||||
"Bidirectional assocs implement the entire assoc protocol with the exception of " { $link delete-at } ". Duplicate values are allowed, however value lookups with " { $link value-at } " only return the first key that a given value was stored with."
|
||||
"Bidirectional assocs implement the entire " { $link "assoc-protocol" } " with the exception of " { $link delete-at } ". Duplicate values are allowed, however value lookups with " { $link value-at } " only return the first key that a given value was stored with."
|
||||
$nl
|
||||
"The class of biassocs:"
|
||||
{ $subsection biassoc }
|
||||
{ $subsection biassoc? }
|
||||
"Creating new biassocs:"
|
||||
{ $subsection <biassoc> }
|
||||
{ $subsection <bihash> } ;
|
||||
{ $subsection <bihash> }
|
||||
"Converting existing assocs to biassocs:"
|
||||
{ $subsection >biassoc } ;
|
||||
|
||||
ABOUT: "biassocs"
|
||||
|
|
|
@ -20,3 +20,13 @@ USING: biassocs assocs namespaces tools.test ;
|
|||
[ 2 ] [ 1 "h" get value-at ] unit-test
|
||||
|
||||
[ 2 ] [ "h" get assoc-size ] unit-test
|
||||
|
||||
H{ { "a" "A" } { "b" "B" } } "a" set
|
||||
|
||||
[ ] [ "a" get >biassoc "b" set ] unit-test
|
||||
|
||||
[ t ] [ "b" get biassoc? ] unit-test
|
||||
|
||||
[ "A" ] [ "a" "b" get at ] unit-test
|
||||
|
||||
[ "a" ] [ "A" "b" get value-at ] unit-test
|
|
@ -1,6 +1,6 @@
|
|||
! Copyright (C) 2008 Slava Pestov.
|
||||
! Copyright (C) 2008, 2009 Slava Pestov.
|
||||
! See http://factorcode.org/license.txt for BSD license.
|
||||
USING: kernel assocs accessors summary ;
|
||||
USING: kernel assocs accessors summary hashtables ;
|
||||
IN: biassocs
|
||||
|
||||
TUPLE: biassoc from to ;
|
||||
|
@ -37,4 +37,10 @@ M: biassoc >alist
|
|||
M: biassoc clear-assoc
|
||||
[ from>> clear-assoc ] [ to>> clear-assoc ] bi ;
|
||||
|
||||
M: biassoc new-assoc
|
||||
drop [ <hashtable> ] [ <hashtable> ] bi biassoc boa ;
|
||||
|
||||
INSTANCE: biassoc assoc
|
||||
|
||||
: >biassoc ( assoc -- biassoc )
|
||||
T{ biassoc } assoc-clone-like ;
|
Loading…
Reference in New Issue