(* Copyright (C) 1999-2005, 2008 Henry Cejtin, Matthew Fluet, Suresh * Jagannathan, and Stephen Weeks. * * MLton is released under a BSD-style license. * See the file MLton-LICENSE for details. *) structure MutableQueue: sig type 'a t val new: unit -> 'a t val enque: 'a t * 'a -> unit val deque: 'a t -> 'a option end = struct datatype 'a t = T of {front: 'a list ref, back: 'a list ref} fun new () = T {front = ref [], back = ref []} fun enque (T {back, ...}, x) = back := x :: !back fun deque (T {front, back}) = case !front of [] => (case !back of [] => NONE | l => let val _ = back := [] val l = rev l in case l of [] => raise Fail "MutableQueue.deque" | x :: l => (front := l; SOME x) end) | x :: l => (front := l; SOME x) end