Import Debian changes 20180207-1
[hcoop/debian/mlton.git] / basis-library / arrays-and-vectors / array.sml
CommitLineData
7f918cf1
CE
1(* Copyright (C) 2017 Matthew Fluet.
2 * Copyright (C) 1999-2008 Henry Cejtin, Matthew Fluet, Suresh
3 * Jagannathan, and Stephen Weeks.
4 * Copyright (C) 1997-2000 NEC Research Institute.
5 *
6 * MLton is released under a BSD-style license.
7 * See the file MLton-LICENSE for details.
8 *)
9
10structure Array: ARRAY_EXTRA =
11 struct
12 structure A = Sequence (Primitive.Array)
13 open A
14
15 val op +? = Int.+?
16 val op < = Int.<
17 val op <= = Int.<=
18
19 fun wrap2 f = fn (i, x) => f (SeqIndex.toIntUnsafe i, x)
20
21 type 'a array = 'a array
22 type 'a vector = 'a Vector.vector
23
24 structure ArraySlice =
25 struct
26 open Slice
27 val vector = Primitive.Array.Slice.vector
28 val copyVec = Vector.VectorSlice.copy
29 val unsafeCopyVec = Vector.VectorSlice.unsafeCopy
30 fun modifyi f sl = Primitive.Array.Slice.modifyi (wrap2 f) sl
31 val modify = Primitive.Array.Slice.modify
32 end
33
34 val array = new
35 val unsafeArray = unsafeNew
36 val vector = Primitive.Array.vector
37 val copyVec = Vector.copy
38 val unsafeCopyVec = Vector.unsafeCopy
39 fun modifyi f sl = Primitive.Array.modifyi (wrap2 f) sl
40 val modify = Primitive.Array.modify
41
42 structure Raw = Primitive.Array.Raw
43 structure Raw =
44 struct
45 type 'a rawarr = 'a Raw.rawarr
46
47 fun length a =
48 if Primitive.Controls.safe
49 then (SeqIndex.toInt (Raw.length a))
50 handle Overflow => raise Fail "Raw.length"
51 else SeqIndex.toIntUnsafe (Raw.length a)
52
53 fun alloc n = Raw.alloc (SeqIndex.fromIntForLength n)
54 fun unsafeAlloc n = Raw.unsafeAlloc (SeqIndex.fromIntUnsafe n)
55
56 val uninitIsNop = Raw.uninitIsNop
57 fun unsafeUninit (a, i) =
58 Raw.unsafeUninit (a, SeqIndex.fromIntUnsafe i)
59 fun uninit (a, i) =
60 if Primitive.Controls.safe
61 then let
62 val i =
63 (SeqIndex.fromInt i)
64 handle Overflow => raise Subscript
65 in
66 Raw.uninit (a, i)
67 end
68 else unsafeUninit (a, i)
69
70 val unsafeToArray = Primitive.Array.Raw.unsafeToArray
71 end
72 end
73
74structure ArraySlice: ARRAY_SLICE_EXTRA = Array.ArraySlice
75
76structure ArrayGlobal: ARRAY_GLOBAL = Array
77open ArrayGlobal