diff --git a/extra/assoc-deques/assoc-deques-docs.factor b/extra/assoc-deques/assoc-deques-docs.factor new file mode 100644 index 0000000000..d8f305d51d --- /dev/null +++ b/extra/assoc-deques/assoc-deques-docs.factor @@ -0,0 +1,32 @@ +! Copyright (C) 2008 Doug Coleman. +! See http://factorcode.org/license.txt for BSD license. +USING: help.markup help.syntax io.streams.string ; +IN: assoc-deques + +HELP: +{ $description "Constructs a new " { $link assoc-deque } " from two existing data structures." } ; + +HELP: +{ $values + + { "unique-heap" assoc-deque } } +{ $description "Creates a new " { $link assoc-deque } " where the assoc is a hashtable and the deque is a max-heap." } ; + +HELP: +{ $values + + { "unique-heap" assoc-deque } } +{ $description "Creates a new " { $link assoc-deque } " where the assoc is a hashtable and the deque is a min-heap." } ; + +HELP: assoc-deque +{ $description "A data structure containing an assoc and a deque to get certain properties with better time constraints at the expense of more space and complexity. For instance, a hashtable and a heap can be combined into one assoc-deque to get a sorted data structure with O(1) lookup. Operations on assoc-deques should update both the assoc and the deque." } ; + +ARTICLE: "assoc-deques" "Associative deques" +"The " { $vocab-link "assoc-deques" } " vocabulary combines exists to synthesize data structures with better time properties than either of the two component data structures alone." $nl +"Associative deque constructor:" +{ $subsection } +"Unique heaps:" +{ $subsection } +{ $subsection } ; + +ABOUT: "assoc-deques" diff --git a/extra/assoc-deques/assoc-deques-tests.factor b/extra/assoc-deques/assoc-deques-tests.factor new file mode 100644 index 0000000000..fe9d8840bc --- /dev/null +++ b/extra/assoc-deques/assoc-deques-tests.factor @@ -0,0 +1,4 @@ +! Copyright (C) 2008 Doug Coleman. +! See http://factorcode.org/license.txt for BSD license. +USING: tools.test assoc-deques ; +IN: assoc-deques.tests diff --git a/extra/assoc-deques/assoc-deques.factor b/extra/assoc-deques/assoc-deques.factor new file mode 100644 index 0000000000..a23e632b8b --- /dev/null +++ b/extra/assoc-deques/assoc-deques.factor @@ -0,0 +1,31 @@ +! Copyright (C) 2008 Doug Coleman. +! See http://factorcode.org/license.txt for BSD license. +USING: accessors assocs deques hashtables heaps kernel ; +IN: assoc-deques + +TUPLE: assoc-deque assoc deque ; + +C: assoc-deque + +: ( -- unique-heap ) + H{ } clone ; + +: ( -- unique-heap ) + H{ } clone ; + +M: assoc-deque heap-push* ( value key assoc-deque -- entry ) + pick over assoc>> key? [ + 3drop f + ] [ + [ assoc>> swapd set-at ] [ deque>> heap-push* ] 3bi + ] if ; + +M: assoc-deque heap-pop ( assoc-deque -- value key ) + [ deque>> heap-pop ] keep + [ over ] dip assoc>> delete-at ; + +M: assoc-deque heap-peek ( assoc-deque -- value key ) + deque>> heap-peek ; + +M: assoc-deque heap-empty? ( assoc-deque -- value key ) + deque>> heap-empty? ; diff --git a/extra/assoc-deques/authors.txt b/extra/assoc-deques/authors.txt new file mode 100644 index 0000000000..b4bd0e7b35 --- /dev/null +++ b/extra/assoc-deques/authors.txt @@ -0,0 +1 @@ +Doug Coleman \ No newline at end of file