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.
8 functor CircularList(S: CIRCULAR_LIST_STRUCTS): CIRCULAR_LIST =
13 type 'a t = 'a Elt.t Pointer.t
15 val empty = Pointer.null
17 fun makeEmpty p = Pointer.clear p
19 fun isEmpty p = Pointer.isNull p
22 case Pointer.follow p of
24 | SOME d => Elt.eqPrev(d, Elt.prev d)
29 case Pointer.follow p of
30 SOME d' => Elt.insertR(d', d)
31 | NONE => (Elt.link(d, d); Pointer.:=(p, d))
34 case Pointer.follow p of
35 SOME d => Pointer.:=(p, Elt.next d)
38 fun deleteSafe(p, d) =
39 (if Elt.eqPrev(Pointer.! p, d)
40 then if isSingle p then makeEmpty p
41 else Pointer.:=(p, Elt.next d)
46 if Elt.isLinked d then deleteSafe(l, d)
47 else Error.bug "CircularList.delete"
50 if Pointer.isNull p then ()
53 val start = Pointer.! p
55 let val next = Elt.next d
57 ; if Elt.eqPrev(start, next)
64 fun deleteEach(p, f) = foreach(p, fn d => (Elt.unlink d; f d))
67 if Pointer.isNull p then Pointer.copy(p, p')
68 else if Pointer.isNull p' then ()
69 else let val e1 = Pointer.! p
70 val e1' = Pointer.! p'
72 val e2' = Elt.next e1'