@@ -8,4 +8,98 @@ open! Stdlib
8
8
9
9
[@@@ ocaml.nolabels]
10
10
11
- include Iarray
11
+ (* CR mshinwell: Change to "include Iarray"; we can't do this at present
12
+ as it requires referencing Stdlib__Iarray which will work under the make
13
+ build but not under dune. *)
14
+
15
+ (* CR aspectorzabusky: This needs a copyright header; should I copy [array.ml]? *)
16
+
17
+ (* An alias for the type of immutable arrays. *)
18
+ type +'a t = 'a iarray
19
+
20
+ (* Array operations *)
21
+
22
+ external length : 'a iarray -> int = " %array_length"
23
+ external get : 'a iarray -> int -> 'a = " %array_safe_get"
24
+ external ( .:( ) ) : 'a iarray -> int -> 'a = " %array_safe_get"
25
+ external unsafe_get : 'a iarray -> int -> 'a = " %array_unsafe_get"
26
+
27
+ (* Immutable and mutable arrays have the same runtime representation; we
28
+ construct immutable arrays by constructing mutable arrays and then blindly
29
+ casting them to become immutable. This is safe here because:
30
+ 1. None of these functions mutate their array inputs;
31
+ 2. None of these functions hold on to their array inputs; and
32
+ 3. All of these functions return fresh arrays if they return an array. *)
33
+ let init =
34
+ Obj. magic (Array. init : int -> (int -> 'a ) -> 'a array )
35
+ let append =
36
+ Obj. magic (Array. append : 'a array -> 'a array -> 'a array )
37
+ let concat =
38
+ Obj. magic (Array. concat : 'a array list -> 'a array )
39
+ let sub =
40
+ Obj. magic (Array. sub : 'a array -> int -> int -> 'a array )
41
+ let to_list =
42
+ Obj. magic (Array. to_list : 'a array -> 'a list )
43
+ let of_list =
44
+ Obj. magic (Array. of_list : 'a list -> 'a array )
45
+ let iter =
46
+ Obj. magic (Array. iter : ('a -> unit ) -> 'a array -> unit )
47
+ let iteri =
48
+ Obj. magic (Array. iteri : (int -> 'a -> unit ) -> 'a array -> unit )
49
+ let map =
50
+ Obj. magic (Array. map : ('a -> 'b ) -> 'a array -> 'b array )
51
+ let mapi =
52
+ Obj. magic (Array. mapi : (int -> 'a -> 'b ) -> 'a array -> 'b array )
53
+ let fold_left =
54
+ Obj. magic (Array. fold_left : ('a -> 'b -> 'a ) -> 'a -> 'b array -> 'a)
55
+ let fold_left_map =
56
+ Obj. magic (Array. fold_left_map :
57
+ ('a -> 'b -> 'a * 'c) -> 'a -> 'b array -> 'a * 'c array )
58
+ let fold_right =
59
+ Obj. magic (Array. fold_right : ('b -> 'a -> 'a ) -> 'b array -> 'a -> 'a)
60
+ let iter2 =
61
+ Obj. magic (Array. iter2 : ('a -> 'b -> unit ) -> 'a array -> 'b array -> unit )
62
+ let map2 =
63
+ Obj. magic (Array. map2 : ('a -> 'b -> 'c ) -> 'a array -> 'b array -> 'c array )
64
+ let for_all =
65
+ Obj. magic (Array. for_all : ('a -> bool ) -> 'a array -> bool )
66
+ let exists =
67
+ Obj. magic (Array. exists : ('a -> bool ) -> 'a array -> bool )
68
+ let for_all2 =
69
+ Obj. magic (Array. for_all2 : ('a -> 'b -> bool ) -> 'a array -> 'b array -> bool )
70
+ let exists2 =
71
+ Obj. magic (Array. exists2 : ('a -> 'b -> bool ) -> 'a array -> 'b array -> bool )
72
+ let mem =
73
+ Obj. magic (Array. mem : 'a -> 'a array -> bool )
74
+ let memq =
75
+ Obj. magic (Array. memq : 'a -> 'a array -> bool )
76
+ let find_opt =
77
+ Obj. magic (Array. find_opt : ('a -> bool ) -> 'a array -> 'a option )
78
+ let find_map =
79
+ Obj. magic (Array. find_map : ('a -> 'b option ) -> 'a array -> 'b option )
80
+ let split =
81
+ Obj. magic (Array. split : ('a * 'b ) array -> 'a array * 'b array )
82
+ let combine =
83
+ Obj. magic (Array. combine : 'a array -> 'b array -> ('a * 'b ) array )
84
+ let to_seq =
85
+ Obj. magic (Array. to_seq : 'a array -> 'a Seq.t )
86
+ let to_seqi =
87
+ Obj. magic (Array. to_seqi : 'a array -> (int * 'a ) Seq. t)
88
+ let of_seq =
89
+ Obj. magic (Array. of_seq : 'a Seq.t -> 'a array )
90
+
91
+ (* Only safe if the array isn't used after this call *)
92
+ let unsafe_of_array : 'a array -> 'a iarray = Obj. magic
93
+
94
+ (* Must be fully applied due to the value restriction *)
95
+ let lift_sort sorter cmp iarr =
96
+ let arr = Array. of_iarray iarr in
97
+ sorter cmp arr;
98
+ unsafe_of_array arr
99
+
100
+ let sort cmp iarr = lift_sort Array. sort cmp iarr
101
+ let stable_sort cmp iarr = lift_sort Array. stable_sort cmp iarr
102
+ let fast_sort cmp iarr = lift_sort Array. fast_sort cmp iarr
103
+
104
+ let to_array = Array. of_iarray
105
+ let of_array = Array. to_iarray
0 commit comments