Final changes for deques rename
parent
1e1da73309
commit
7e098265ef
|
@ -1 +0,0 @@
|
|||
Slava Pestov
|
|
@ -1,89 +0,0 @@
|
|||
IN: dequeues
|
||||
USING: help.markup help.syntax kernel ;
|
||||
|
||||
ARTICLE: "dequeues" "Dequeues"
|
||||
"A dequeue is a data structure with constant-time insertion and removal of elements at both ends. Dequeue operations are defined in the " { $vocab-link "dequeues" } " vocabulary."
|
||||
$nl
|
||||
"Dequeues must be instances of a mixin class:"
|
||||
{ $subsection dequeue }
|
||||
"Dequeues must implement a protocol."
|
||||
$nl
|
||||
"Querying the dequeue:"
|
||||
{ $subsection peek-front }
|
||||
{ $subsection peek-back }
|
||||
{ $subsection dequeue-length }
|
||||
{ $subsection dequeue-member? }
|
||||
"Adding and removing elements:"
|
||||
{ $subsection push-front* }
|
||||
{ $subsection push-back* }
|
||||
{ $subsection pop-front* }
|
||||
{ $subsection pop-back* }
|
||||
{ $subsection clear-dequeue }
|
||||
"Working with node objects output by " { $link push-front* } " and " { $link push-back* } ":"
|
||||
{ $subsection delete-node }
|
||||
{ $subsection node-value }
|
||||
"Utility operations built in terms of the above:"
|
||||
{ $subsection dequeue-empty? }
|
||||
{ $subsection push-front }
|
||||
{ $subsection push-all-front }
|
||||
{ $subsection push-back }
|
||||
{ $subsection push-all-back }
|
||||
{ $subsection pop-front }
|
||||
{ $subsection pop-back }
|
||||
{ $subsection slurp-dequeue }
|
||||
"When using a dequeue as a queue, the convention is to queue elements with " { $link push-front } " and dequeue them with " { $link pop-back } "." ;
|
||||
|
||||
ABOUT: "dequeues"
|
||||
|
||||
HELP: dequeue-empty?
|
||||
{ $values { "dequeue" { $link dequeue } } { "?" "a boolean" } }
|
||||
{ $description "Returns true if a dequeue is empty." }
|
||||
{ $notes "This operation is O(1)." } ;
|
||||
|
||||
HELP: push-front
|
||||
{ $values { "obj" object } { "dequeue" dequeue } }
|
||||
{ $description "Push the object onto the front of the dequeue." }
|
||||
{ $notes "This operation is O(1)." } ;
|
||||
|
||||
HELP: push-front*
|
||||
{ $values { "obj" object } { "dequeue" dequeue } { "node" "a node" } }
|
||||
{ $description "Push the object onto the front of the dequeue and return the newly created node." }
|
||||
{ $notes "This operation is O(1)." } ;
|
||||
|
||||
HELP: push-back
|
||||
{ $values { "obj" object } { "dequeue" dequeue } }
|
||||
{ $description "Push the object onto the back of the dequeue." }
|
||||
{ $notes "This operation is O(1)." } ;
|
||||
|
||||
HELP: push-back*
|
||||
{ $values { "obj" object } { "dequeue" dequeue } { "node" "a node" } }
|
||||
{ $description "Push the object onto the back of the dequeue and return the newly created node." }
|
||||
{ $notes "This operation is O(1)." } ;
|
||||
|
||||
HELP: peek-front
|
||||
{ $values { "dequeue" dequeue } { "obj" object } }
|
||||
{ $description "Returns the object at the front of the dequeue." } ;
|
||||
|
||||
HELP: pop-front
|
||||
{ $values { "dequeue" dequeue } { "obj" object } }
|
||||
{ $description "Pop the object off the front of the dequeue and return the object." }
|
||||
{ $notes "This operation is O(1)." } ;
|
||||
|
||||
HELP: pop-front*
|
||||
{ $values { "dequeue" dequeue } }
|
||||
{ $description "Pop the object off the front of the dequeue." }
|
||||
{ $notes "This operation is O(1)." } ;
|
||||
|
||||
HELP: peek-back
|
||||
{ $values { "dequeue" dequeue } { "obj" object } }
|
||||
{ $description "Returns the object at the back of the dequeue." } ;
|
||||
|
||||
HELP: pop-back
|
||||
{ $values { "dequeue" dequeue } { "obj" object } }
|
||||
{ $description "Pop the object off the back of the dequeue and return the object." }
|
||||
{ $notes "This operation is O(1)." } ;
|
||||
|
||||
HELP: pop-back*
|
||||
{ $values { "dequeue" dequeue } }
|
||||
{ $description "Pop the object off the back of the dequeue." }
|
||||
{ $notes "This operation is O(1)." } ;
|
|
@ -1,43 +0,0 @@
|
|||
! Copyright (C) 2008 Slava Pestov.
|
||||
! See http://factorcode.org/license.txt for BSD license.
|
||||
USING: kernel sequences math ;
|
||||
IN: dequeues
|
||||
|
||||
GENERIC: push-front* ( obj dequeue -- node )
|
||||
GENERIC: push-back* ( obj dequeue -- node )
|
||||
GENERIC: peek-front ( dequeue -- obj )
|
||||
GENERIC: peek-back ( dequeue -- obj )
|
||||
GENERIC: pop-front* ( dequeue -- )
|
||||
GENERIC: pop-back* ( dequeue -- )
|
||||
GENERIC: delete-node ( node dequeue -- )
|
||||
GENERIC: dequeue-length ( dequeue -- n )
|
||||
GENERIC: dequeue-member? ( value dequeue -- ? )
|
||||
GENERIC: clear-dequeue ( dequeue -- )
|
||||
GENERIC: node-value ( node -- value )
|
||||
|
||||
: dequeue-empty? ( dequeue -- ? )
|
||||
dequeue-length zero? ;
|
||||
|
||||
: push-front ( obj dequeue -- )
|
||||
push-front* drop ;
|
||||
|
||||
: push-all-front ( seq dequeue -- )
|
||||
[ push-front ] curry each ;
|
||||
|
||||
: push-back ( obj dequeue -- )
|
||||
push-back* drop ;
|
||||
|
||||
: push-all-back ( seq dequeue -- )
|
||||
[ push-back ] curry each ;
|
||||
|
||||
: pop-front ( dequeue -- obj )
|
||||
[ peek-front ] [ pop-front* ] bi ;
|
||||
|
||||
: pop-back ( dequeue -- obj )
|
||||
[ peek-back ] [ pop-back* ] bi ;
|
||||
|
||||
: slurp-dequeue ( dequeue quot -- )
|
||||
[ drop [ dequeue-empty? not ] curry ]
|
||||
[ [ pop-back ] prepose curry ] 2bi [ ] while ; inline
|
||||
|
||||
MIXIN: dequeue
|
|
@ -1 +0,0 @@
|
|||
Double-ended queue protocol and common operations
|
|
@ -1 +0,0 @@
|
|||
collections
|
|
@ -2,7 +2,7 @@ USING: help.markup help.syntax kernel sequences ;
|
|||
IN: persistent.deques
|
||||
|
||||
ARTICLE: "persistent.deques" "Persistent deques"
|
||||
"A deque is a data structure that can be used as both a queue and a stack. That is, there are two ends, the left and the right, and values can be pushed onto and popped off of both ends. These operations take O(1) amortized time and space in a normal usage pattern."
|
||||
"A deque is a data structure that can be used as both a queue and a stack. That is, there are two ends, the front and the back, and values can be pushed onto and popped off of both ends. These operations take O(1) amortized time and space in a normal usage pattern."
|
||||
$nl
|
||||
"This vocabulary provides a deque implementation which is persistent and purely functional: old versions of deques are not modified by operations. Instead, each push and pop operation creates a new deque based off the old one."
|
||||
$nl
|
||||
|
@ -14,10 +14,10 @@ $nl
|
|||
"To test if a deque is empty:"
|
||||
{ $subsection deque-empty? }
|
||||
"To manipulate deques:"
|
||||
{ $subsection push-left }
|
||||
{ $subsection push-right }
|
||||
{ $subsection pop-left }
|
||||
{ $subsection pop-right }
|
||||
{ $subsection push-front }
|
||||
{ $subsection push-back }
|
||||
{ $subsection pop-front }
|
||||
{ $subsection pop-back }
|
||||
{ $subsection deque>sequence } ;
|
||||
|
||||
HELP: deque
|
||||
|
@ -29,28 +29,28 @@ HELP: <deque>
|
|||
|
||||
HELP: sequence>deque
|
||||
{ $values { "sequence" sequence } { "deque" deque } }
|
||||
{ $description "Given a sequence, creates a deque containing those elements in the order such that the beginning of the sequence is on the left and the end is on the right." } ;
|
||||
{ $description "Given a sequence, creates a deque containing those elements in the order such that the beginning of the sequence is on the front and the end is on the back." } ;
|
||||
|
||||
HELP: deque>sequence
|
||||
{ $values { "deque" deque } { "sequence" sequence } }
|
||||
{ $description "Given a deque, creates a sequence containing those elements, such that the left side of the deque is the beginning of the sequence." } ;
|
||||
{ $description "Given a deque, creates a sequence containing those elements, such that the front side of the deque is the beginning of the sequence." } ;
|
||||
|
||||
HELP: deque-empty?
|
||||
{ $values { "deque" deque } { "?" "t/f" } }
|
||||
{ $description "Returns true if the deque is empty. This takes constant time." } ;
|
||||
|
||||
HELP: push-left
|
||||
HELP: push-front
|
||||
{ $values { "deque" deque } { "item" object } { "newdeque" deque } }
|
||||
{ $description "Creates a new deque with the given object pushed onto the left side. This takes constant time." } ;
|
||||
{ $description "Creates a new deque with the given object pushed onto the front side. This takes constant time." } ;
|
||||
|
||||
HELP: push-right
|
||||
HELP: push-back
|
||||
{ $values { "deque" deque } { "item" object } { "newdeque" deque } }
|
||||
{ $description "Creates a new deque with the given object pushed onto the right side. This takes constant time." } ;
|
||||
{ $description "Creates a new deque with the given object pushed onto the back side. This takes constant time." } ;
|
||||
|
||||
HELP: pop-left
|
||||
HELP: pop-front
|
||||
{ $values { "deque" object } { "item" object } { "newdeque" deque } }
|
||||
{ $description "Creates a new deque with the leftmost item removed. This takes amortized constant time with single-threaded access." } ;
|
||||
{ $description "Creates a new deque with the frontmost item removed. This takes amortized constant time with single-threaded access." } ;
|
||||
|
||||
HELP: pop-right
|
||||
HELP: pop-back
|
||||
{ $values { "deque" object } { "item" object } { "newdeque" deque } }
|
||||
{ $description "Creates a new deque with the rightmost item removed. This takes amortized constant time with single-threaded access." } ;
|
||||
{ $description "Creates a new deque with the backmost item removed. This takes amortized constant time with single-threaded access." } ;
|
||||
|
|
|
@ -1 +0,0 @@
|
|||
Slava Pestov
|
|
@ -1,21 +0,0 @@
|
|||
IN: search-dequeues
|
||||
USING: help.markup help.syntax kernel dlists hashtables
|
||||
dequeues assocs ;
|
||||
|
||||
ARTICLE: "search-dequeues" "Search dequeues"
|
||||
"A search dequeue is a data structure with constant-time insertion and removal of elements at both ends, and constant-time membership tests. Inserting an element more than once has no effect. Search dequeues implement all dequeue operations in terms of an underlying dequeue, and membership testing with " { $link dequeue-member? } " is implemented with an underlying assoc. Search dequeues are defined in the " { $vocab-link "search-dequeues" } " vocabulary."
|
||||
$nl
|
||||
"Creating a search dequeue:"
|
||||
{ $subsection <search-dequeue> }
|
||||
"Default implementation:"
|
||||
{ $subsection <hashed-dlist> } ;
|
||||
|
||||
ABOUT: "search-dequeues"
|
||||
|
||||
HELP: <search-dequeue> ( assoc dequeue -- search-dequeue )
|
||||
{ $values { "assoc" assoc } { "dequeue" dequeue } { "search-dequeue" search-dequeue } }
|
||||
{ $description "Creates a new " { $link search-dequeue } "." } ;
|
||||
|
||||
HELP: <hashed-dlist> ( -- search-dequeue )
|
||||
{ $values { "search-dequeue" search-dequeue } }
|
||||
{ $description "Creates a new " { $link search-dequeue } " backed by a " { $link dlist } ", with a " { $link hashtable } " for fast membership tests." } ;
|
|
@ -1,35 +0,0 @@
|
|||
IN: search-dequeues.tests
|
||||
USING: search-dequeues tools.test namespaces
|
||||
kernel sequences words dequeues vocabs ;
|
||||
|
||||
<hashed-dlist> "h" set
|
||||
|
||||
[ t ] [ "h" get dequeue-empty? ] unit-test
|
||||
|
||||
[ ] [ 3 "h" get push-front* "1" set ] unit-test
|
||||
[ ] [ 1 "h" get push-front ] unit-test
|
||||
[ ] [ 3 "h" get push-front* "2" set ] unit-test
|
||||
[ ] [ 3 "h" get push-front* "3" set ] unit-test
|
||||
[ ] [ 7 "h" get push-front ] unit-test
|
||||
|
||||
[ t ] [ "1" get "2" get eq? ] unit-test
|
||||
[ t ] [ "2" get "3" get eq? ] unit-test
|
||||
|
||||
[ 3 ] [ "h" get dequeue-length ] unit-test
|
||||
[ t ] [ 7 "h" get dequeue-member? ] unit-test
|
||||
|
||||
[ 3 ] [ "1" get node-value ] unit-test
|
||||
[ ] [ "1" get "h" get delete-node ] unit-test
|
||||
|
||||
[ 2 ] [ "h" get dequeue-length ] unit-test
|
||||
[ 1 ] [ "h" get pop-back ] unit-test
|
||||
[ 7 ] [ "h" get pop-back ] unit-test
|
||||
|
||||
[ f ] [ 7 "h" get dequeue-member? ] unit-test
|
||||
|
||||
[ ] [
|
||||
<hashed-dlist>
|
||||
[ all-words swap [ push-front ] curry each ]
|
||||
[ [ drop ] slurp-dequeue ]
|
||||
bi
|
||||
] unit-test
|
|
@ -1,53 +0,0 @@
|
|||
! Copyright (C) 2008 Slava Pestov.
|
||||
! See http://factorcode.org/license.txt for BSD license.
|
||||
USING: accessors kernel assocs dequeues dlists hashtables ;
|
||||
IN: search-dequeues
|
||||
|
||||
TUPLE: search-dequeue assoc dequeue ;
|
||||
|
||||
C: <search-dequeue> search-dequeue
|
||||
|
||||
: <hashed-dlist> ( -- search-dequeue )
|
||||
0 <hashtable> <dlist> <search-dequeue> ;
|
||||
|
||||
M: search-dequeue dequeue-length dequeue>> dequeue-length ;
|
||||
|
||||
M: search-dequeue peek-front dequeue>> peek-front ;
|
||||
|
||||
M: search-dequeue peek-back dequeue>> peek-back ;
|
||||
|
||||
M: search-dequeue push-front*
|
||||
2dup assoc>> at* [ 2nip ] [
|
||||
drop
|
||||
[ dequeue>> push-front* ] [ assoc>> ] 2bi
|
||||
[ 2drop ] [ set-at ] 3bi
|
||||
] if ;
|
||||
|
||||
M: search-dequeue push-back*
|
||||
2dup assoc>> at* [ 2nip ] [
|
||||
drop
|
||||
[ dequeue>> push-back* ] [ assoc>> ] 2bi
|
||||
[ 2drop ] [ set-at ] 3bi
|
||||
] if ;
|
||||
|
||||
M: search-dequeue pop-front*
|
||||
[ [ dequeue>> peek-front ] [ assoc>> ] bi delete-at ]
|
||||
[ dequeue>> pop-front* ]
|
||||
bi ;
|
||||
|
||||
M: search-dequeue pop-back*
|
||||
[ [ dequeue>> peek-back ] [ assoc>> ] bi delete-at ]
|
||||
[ dequeue>> pop-back* ]
|
||||
bi ;
|
||||
|
||||
M: search-dequeue delete-node
|
||||
[ dequeue>> delete-node ]
|
||||
[ [ node-value ] [ assoc>> ] bi* delete-at ] 2bi ;
|
||||
|
||||
M: search-dequeue clear-dequeue
|
||||
[ dequeue>> clear-dequeue ] [ assoc>> clear-assoc ] bi ;
|
||||
|
||||
M: search-dequeue dequeue-member?
|
||||
assoc>> key? ;
|
||||
|
||||
INSTANCE: search-dequeue dequeue
|
|
@ -1 +0,0 @@
|
|||
Double-ended queues with sub-linear membership testing
|
|
@ -1 +0,0 @@
|
|||
collections
|
Loading…
Reference in New Issue