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 PolyCache(): POLY_CACHE =
11 datatype ('a, 'b) t = T of {equal: ('a * 'a) -> bool,
12 elts: ('a * 'b) list ref}
14 fun fromList{equal, elements} = T{equal = equal, elts = ref elements}
16 fun new{equal} = T{equal = equal, elts = ref []}
18 fun toList(T{elts, ...}) = !elts
20 fun size c = List.length(toList c)
21 fun foreach(c, f) = List.foreach(toList c, f)
23 fun peek(T{equal, elts = ref l}, x) =
24 case List.peek(l, fn (x', _) => equal(x, x')) of
26 | SOME(_, y) => SOME y
28 fun lookup cx = valOf(peek cx)
30 fun toFunction c a = lookup(c, a)
32 fun addNew(T{elts = r as ref l, ...}, x, y) = r := (x, y) :: l
34 fun getOrAdd(c, x, th) =
36 NONE => let val y = th()
37 in addNew(c, x, y) ; y
41 fun eq(T{elts=r, ...}, T{elts=r', ...}) = r = r'
45 structure PolyCache = PolyCache()