The Fifo
structure provides a functional queue data structure,
which are implemented as a pair of stacks (lists) representing the
front and rear of the queue. Single-threaded enqueuing and
dequeuing operations will have amortized constant time.
Synopsis
signature FIFO
structure Fifo :> FIFO
Interface
type 'a fifo
exception Dequeue
val empty : 'a fifo
val isEmpty : 'a fifo -> bool
val enqueue : 'a fifo * 'a -> 'a fifo
val dequeue : 'a fifo -> 'a fifo * 'a
val next : 'a fifo -> ('a * 'a fifo) option
val delete : ('a fifo * ('a -> bool)) -> 'a fifo
val head : 'a fifo -> 'a
val peek : 'a fifo -> 'a option
val length : 'a fifo -> int
val contents : 'a fifo -> 'a list
val app : ('a -> unit) -> 'a fifo -> unit
val map : ('a -> 'b) -> 'a fifo -> 'b fifo
val foldl : ('a * 'b -> 'b) -> 'b -> 'a fifo -> 'b
val foldr : ('a * 'b -> 'b) -> 'b -> 'a fifo -> 'b
Description
type 'a fifo
-
The type constructor for functional queues.
exception Dequeue
-
This exceptions is raised when the
dequeue
function is applied to an empty queue. val empty : 'a fifo
-
The empty queue.
val isEmpty : 'a fifo -> bool
-
ifEmpty q
returns true if the queue is empty. val enqueue : 'a fifo * 'a -> 'a fifo
-
enqueue (q, x)
returns a queue withx
added to the end. val dequeue : 'a fifo -> 'a fifo * 'a
-
dequeue q
returns a pair(q', x)
, wherex
was the first element inq
andq'
is the queue withx
removed. This function raises theDequeue
exception if it is called on an empty queue. val next : 'a fifo -> ('a * 'a fifo) option
-
next q
returnsSOME(q', x)
, wherex
was the first element inq
andq'
is the queue withx
removed, orNONE
ifq
is empty. val delete : ('a fifo * ('a -> bool)) -> 'a fifo
-
delete (q, pred)
removes those items fromq
for which the functionpred
returnstrue
and returns the resulting queue. val head : 'a fifo -> 'a
-
head q
returns the first element ofq
or raises the exceptionDequeue
ifq
is empty. val peek : 'a fifo -> 'a option
-
peek q
returnsSOME x
, wherex
is the first element ofq
, orNONE
ifq
is empty. val length : 'a fifo -> int
-
length q
returns the number of elements in the queue. val contents : 'a fifo -> 'a list
-
contents q
returns the contents ofq
as a list. val app : ('a -> unit) -> 'a fifo -> unit
-
app f q
applies the functionf
to the elements ofq
. This expression is equivalent toList.app f (contents q)
val map : ('a -> 'b) -> 'a fifo -> 'b fifo
-
map f q
returns the queue that results from mapping the functionf
across the elements of the queue. val foldl : ('a * 'b -> 'b) -> 'b -> 'a fifo -> 'b
-
foldl f init q
folds the functionf
over the elements ofq
from front to back. This expression is equivalent toList.foldl f init (contents q)
val foldr : ('a * 'b -> 'b) -> 'b -> 'a fifo -> 'b
-
foldr f init q
folds the functionf
over the elements ofq
from back to front. This expression is equivalent toList.foldr f init (contents q)