Import Upstream version 20180207
[hcoop/debian/mlton.git] / lib / mlton / basic / two-list-queue.sml
CommitLineData
7f918cf1
CE
1(* Copyright (C) 1999-2005 Henry Cejtin, Matthew Fluet, Suresh
2 * Jagannathan, and Stephen Weeks.
3 *
4 * MLton is released under a BSD-style license.
5 * See the file MLton-LICENSE for details.
6 *)
7
8structure TwoListQueue:> QUEUE =
9struct
10
11datatype 'a t = T of 'a list * 'a list
12
13fun foldAnyOrder (T (l, r), ac, f) =
14 List.fold (r, List.fold (l, ac, f), f)
15
16fun foldr (T (l, r), ac, f) =
17 List.foldr (l, List.fold (r, ac, f), f)
18
19fun toList q = foldr (q, [], op ::)
20
21fun deque (T (l, r)) =
22 let
23 val (l, r) = (case l of
24 [] => (rev r, [])
25 | _ => (l, r))
26 in
27 case l of
28 [] => NONE
29 | x :: l => SOME (T (l, r), x)
30 end
31
32fun empty () = T ([], [])
33
34val isEmpty =
35 fn T ([], []) => true
36 | _ => false
37
38fun enque (T (l, r), x) = T (l, x :: r)
39
40end
41
42structure Queue = TwoListQueue