1 (* Copyright (C) 1999-2006, 2008 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 ArrayFiniteFunction(): ARRAY_FINITE_FUNCTION =
14 {size: int, fromInt: int -> 'a, toInt: 'a -> int}
16 fun pair({size, fromInt, toInt}: 'a1 t,
17 {size=size', fromInt=fromInt', toInt=toInt'}: 'a2 t,
20 out: 'a * ('a1 -> int) * ('a2 -> int) -> int) =
22 toInt = fn d => out(d, toInt, fn d' => size + toInt' d'),
23 fromInt = fn n => if n < size then inj(fromInt n)
24 else inj'(fromInt'(n - size))}
28 T of {domain: 'a Domain.t,
31 fun empty(domain: 'a Domain.t) =
33 array = Array.new(#size domain, NONE)}
35 fun new(domain: 'a Domain.t, x) =
37 array = Array.new(#size domain, x)}
39 fun tabulate(domain as {size, fromInt, ...}: 'a Domain.t, f) =
41 array = Array.tabulate(size, f o fromInt)}
43 fun size(T{domain={size, ...}, ...}) = size
45 fun lookup(T{domain={toInt, ...}, array}, x) = Array.sub(array, toInt x)
47 fun foreach(T{domain={fromInt, ...}, array}, f) =
48 Array.foreachIndex(array, fn (i, x) => f(fromInt i, x))
50 fun set(T{domain={toInt, ...}, array}, x, y) =
51 Array.update(array, toInt x, y)
53 fun toFunction f a = lookup(f, a)
57 structure ArrayFiniteFunction = ArrayFiniteFunction()