More data structure cleanups
parent
90287744dc
commit
52f586877f
|
@ -1,8 +1,7 @@
|
|||
! Copyright (C) 2008 Eric Mertens.
|
||||
! See http://factorcode.org/license.txt for BSD license.
|
||||
USING: accessors arrays hints kernel locals math hashtables
|
||||
assocs fry ;
|
||||
|
||||
assocs fry sequences ;
|
||||
IN: disjoint-sets
|
||||
|
||||
TUPLE: disjoint-set
|
||||
|
@ -65,6 +64,8 @@ M: disjoint-set add-atom
|
|||
[ 1 -rot counts>> set-at ]
|
||||
2tri ;
|
||||
|
||||
: add-atoms ( seq disjoint-set -- ) '[ , add-atom ] each ;
|
||||
|
||||
GENERIC: equiv-set-size ( a disjoint-set -- n )
|
||||
|
||||
M: disjoint-set equiv-set-size [ representative ] keep count ;
|
||||
|
|
|
@ -41,6 +41,13 @@ M: persistent-hash >alist [ root>> >alist% ] { } make ;
|
|||
: >persistent-hash ( assoc -- phash )
|
||||
T{ persistent-hash } swap [ spin new-at ] assoc-each ;
|
||||
|
||||
M: persistent-hash equal?
|
||||
over persistent-hash? [ assoc= ] [ 2drop f ] if ;
|
||||
|
||||
M: persistent-hash hashcode* nip assoc-size ;
|
||||
|
||||
M: persistent-hash clone ;
|
||||
|
||||
: PH{ \ } [ >persistent-hash ] parse-literal ; parsing
|
||||
|
||||
M: persistent-hash pprint-delims drop \ PH{ \ } ;
|
||||
|
|
|
@ -3,15 +3,21 @@ USING: help.markup help.syntax math sequences kernel ;
|
|||
|
||||
HELP: new-nth
|
||||
{ $values { "val" object } { "i" integer } { "seq" sequence } { "seq'" sequence } }
|
||||
{ $contract "Persistent analogue of " { $link set-nth } ". Outputs a new sequence with the " { $snippet "i" } "th element replaced by " { $snippet "val" } "." }
|
||||
{ $notes "This operation runs in " { $snippet "O(log_32 n)" } " time on " { $vocab-link "persistent.vectors" } " and " { $snippet "O(n)" } " time on all other sequences." } ;
|
||||
{ $contract "Persistent analogue of " { $link set-nth } ". Outputs a new sequence with the " { $snippet "i" } "th element replaced by " { $snippet "val" } "." } ;
|
||||
|
||||
HELP: ppush
|
||||
{ $values { "val" object } { "seq" sequence } { "seq'" sequence } }
|
||||
{ $contract "Persistent analogue of " { $link push } ". Outputs a new sequence with all elements of " { $snippet "seq" } " together with " { $snippet "val" } " added at the end." }
|
||||
{ $notes "This operation runs in amortized " { $snippet "O(1)" } " time on " { $vocab-link "persistent.vectors" } " and " { $snippet "O(n)" } " time on all other sequences." } ;
|
||||
{ $contract "Persistent analogue of " { $link push } ". Outputs a new sequence with all elements of " { $snippet "seq" } " together with " { $snippet "val" } " added at the end." } ;
|
||||
|
||||
HELP: ppop
|
||||
{ $values { "seq" sequence } { "seq'" sequence } }
|
||||
{ $contract "Persistent analogue of " { $link pop* } ". Outputs a new sequence with all elements of " { $snippet "seq" } " except for the final element." }
|
||||
{ $notes "This operation runs in amortized " { $snippet "O(1)" } " time on " { $vocab-link "persistent.vectors" } " and " { $snippet "O(n)" } " time on all other sequences." } ;
|
||||
{ $contract "Persistent analogue of " { $link pop* } ". Outputs a new sequence with all elements of " { $snippet "seq" } " except for the final element." } ;
|
||||
|
||||
ARTICLE: "persistent.sequences" "Persistent sequence protocol"
|
||||
"The persistent sequence protocol consists of the non-mutating sequence protocol words, such as " { $link length } " and " { $link nth } ", together with the following operations:"
|
||||
{ $subsection new-nth }
|
||||
{ $subsection ppush }
|
||||
{ $subsection ppop }
|
||||
"The default implementations of the above run in " { $snippet "O(n)" } " time; the " { $vocab-link "persistent.vectors" } " vocabulary provides an implementation of these operations in " { $snippet "O(1)" } " time." ;
|
||||
|
||||
ABOUT: "persistent.sequences"
|
||||
|
|
|
@ -17,12 +17,6 @@ ARTICLE: "persistent.vectors" "Persistent vectors"
|
|||
$nl
|
||||
"The class of persistent vectors:"
|
||||
{ $subsection persistent-vector }
|
||||
"Persistent vectors support the immutable sequence protocol, namely as " { $link length } " and " { $link nth } ", and so can be used with most sequence words (" { $link "sequences" } ")."
|
||||
$nl
|
||||
"In addition to standard sequence operations, persistent vectors implement efficient operations specific to them. They run in sub-linear time on persistent vectors, and degrate to linear-time algorithms on ordinary sequences:"
|
||||
{ $subsection new-nth }
|
||||
{ $subsection ppush }
|
||||
{ $subsection ppop }
|
||||
"Converting a sequence into a persistent vector:"
|
||||
{ $subsection >persistent-vector }
|
||||
"Persistent vectors have a literal syntax:"
|
||||
|
|
Loading…
Reference in New Issue