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 QueueEphemeral(): BASIC_QUEUE =
14 val {error, ...} = Error.errors("queue", "ephemeral")
16 structure L = MutableList
18 datatype 'a t = T of {head: 'a L.t ref,
21 fun destruct(q as T{head, tail}) =
22 case L.destruct(!head) of
25 (if L.eqTail(!head, !tail)
26 then (head := L.empty() ; tail := L.empty())
27 else head := L.tail(!head) ;
30 fun empty() = T{head = ref(L.empty()), tail = ref(L.empty())}
32 fun isEmpty(T{head, ...}) = L.isEmpty(!head)
34 fun enque(q as T{head, tail}, x) =
35 (let val cell = L.cons(x, L.empty())
36 in (if isEmpty q then head := cell
37 else L.setTail(!tail, cell) ;