diff options
Diffstat (limited to 'dynarray.sml')
-rw-r--r-- | dynarray.sml | 28 |
1 files changed, 23 insertions, 5 deletions
diff --git a/dynarray.sml b/dynarray.sml index 0c1cd7f..b052989 100644 --- a/dynarray.sml +++ b/dynarray.sml @@ -1,12 +1,10 @@ structure Dynarray: DYNARRAY = struct type 'a t = (int * 'a option Array.array) ref - exception OutOfBounds - fun create n = ref (0, Array.array (n, NONE)) - fun create0 () = create 10 + fun create0 () = create 10 fun length dynarr = let @@ -37,7 +35,7 @@ structure Dynarray: DYNARRAY = struct val (len, arr) = !dynarr in if n >= len then - raise OutOfBounds + raise Subscript else valOf $ Array.sub (arr, n) end @@ -47,9 +45,29 @@ structure Dynarray: DYNARRAY = struct val (len, arr) = !dynarr in if n >= len then - raise OutOfBounds + raise Subscript else Array.update (arr, n, SOME v) end + fun reset dynarr = + let + val (_, arr) = !dynarr + in + dynarr := (0, arr) + end + + fun toVec dynarr = + let + val (len, arr) = !dynarr + + fun arr2list idx acc = + if idx = len then + rev acc + else + arr2list (idx + 1) (valOf (Array.sub (arr, idx)) :: acc) + val l = arr2list 0 [] + in + Vector.fromList l + end end |