1 (* Copyright (C) 1999-2006 Henry Cejtin, Matthew Fluet, Suresh
2 * Jagannathan, and Stephen Weeks.
4 * MLton is released under a BSD-style license.
5 * See the file MLton-LICENSE for details.
7 (*-------------------------------------------------------------------*)
9 (*-------------------------------------------------------------------*)
11 functor QueueLinkedList(): QUEUE_EPHEMERAL_UNBOUNDED =
14 val {error, ...} = Error.errors("queue", "linked-list")
16 structure L = MutableList
18 datatype 'a t = T of {head: 'a L.t ref,
21 fun empty() = T{head = ref(L.empty()), tail = ref(L.empty())}
23 fun isEmpty(T{head = ref l, ...}) = L.isEmpty l
25 fun enque(q as T{head, tail}, x) =
26 let val cell = L.single x
27 in (if isEmpty q then head := cell
28 else L.setTail(!tail, cell) ;
32 fun deque(q as T{head, tail}) =
33 case L.destruct(!head) of
35 | SOME(x, _) => (if L.eq(!head, !tail)
36 then (head := L.empty() ; tail := L.empty())
37 else head := L.tail(!head) ;