Debian Codegraph @

kernel/parray.ml

raw symbols SCIP
1 (************************************************************************)
2 (* * The Rocq Prover / The Rocq Development Team *)
3 (* v * Copyright INRIA, CNRS and contributors *)
4 (* <O___,, * (see version control and CREDITS file for authors & dates) *)
5 (* \VV/ **************************************************************)
6 (* // * This file is distributed under the terms of the *)
7 (* * GNU Lesser General Public License Version 2.1 *)
8 (* * (see LICENSE file for the text of the license) *)
9 (************************************************************************)
10
11 (** Uniform Arrays: non-flat arrays (even floats are boxed, i.e., doesn't use
12 {!Obj.double_array_tag}) *)
13 module UArray :
14 sig
15 type 'a t
16 val empty : 'a t
17 val unsafe_get : 'a t -> int -> 'a
18 val unsafe_set : 'a t -> int -> 'a -> unit
19 val length : 'a t -> int
20 val make : int -> 'a -> 'a t
21 val copy : 'a t -> 'a t
22 val of_array : 'a array -> 'a t
23 val to_array : 'a t -> 'a array
24 (* 'a should not be float (no Obj.double_tag) *)
25 val unsafe_of_obj : Obj.t -> 'a t
26 end =
27 struct
28 type 'a t = Obj.t array
29 (** Guaranteed to be a non-flat array and no funny business with write
30 barriers because of the opacity of Obj.t. *)
31
32 let empty = [||]
33
34 let length (v : 'a t) = Array.length v
35
36 let of_array v =
37 if (Obj.tag (Obj.repr v) == Obj.double_array_tag) then begin
38 let n = Array.length v in
39 (* Ensure that we initialize it with a non-float *)
40 let ans = Array.make n (Obj.repr ()) in
41 for i = 0 to n - 1 do
42 Array.unsafe_set ans i (Obj.repr (Array.unsafe_get v i))
43 done;
44 ans
45 end else
46 (Obj.magic (Array.copy v))
47
48 let obj_is_float x = Obj.tag x == Obj.double_tag
49
50 let to_array (type a) (v : a t) : a array =
51 let () = assert (not (Array.exists obj_is_float v)) in
52 Obj.magic (Array.copy v)
53
54 let unsafe_of_obj (type a) (v : Obj.t) =
55 let () = assert (Obj.tag v == 0) in
56 (Obj.obj v : a t)
57
58 let unsafe_get = Obj.magic Array.unsafe_get
59 let unsafe_set = Obj.magic Array.unsafe_set
60
61 let make (type a) n (x : a) : a t =
62 (* Ensure that we initialize it with a non-float *)
63 let ans = Array.make n (Obj.repr ()) in
64 let () = Array.fill ans 0 n (Obj.repr x) in
65 ans
66
67 let copy = Array.copy
68
69 end
70
71 let max_array_length32 = 4194303
72
73 let max_length = Uint63.of_int max_array_length32
74
75 let length_to_int i = snd (Uint63.to_int2 i)
76
77 let trunc_size n =
78 if Uint63.le Uint63.zero n && Uint63.lt n (Uint63.of_int max_array_length32) then
79 length_to_int n
80 else max_array_length32
81
82 type 'a t = ('a kind) ref
83 and 'a kind =
84 | Array of 'a UArray.t * 'a
85 | Updated of int * 'a * 'a t
86
87 let unsafe_of_obj t def = ref (Array (UArray.unsafe_of_obj t, def))
88 let of_array t def = ref (Array (UArray.of_array t, def))
89
90 let rec rerootk t k =
91 match !t with
92 | Array (a, _) -> k a
93 | Updated (i, v, p) ->
94 let k' a =
95 let v' = UArray.unsafe_get a i in
96 UArray.unsafe_set a i v;
97 t := !p; (* i.e., Array (a, def) *)
98 p := Updated (i, v', t);
99 k a in
100 rerootk p k'
101
102 let reroot t = rerootk t (fun a -> a)
103
104 let length_int p =
105 UArray.length (reroot p)
106
107 let length p = Uint63.of_int @@ length_int p
108
109 let get p n =
110 let t = reroot p in
111 let l = UArray.length t in
112 if Uint63.le Uint63.zero n && Uint63.lt n (Uint63.of_int l) then
113 UArray.unsafe_get t (length_to_int n)
114 else
115 match !p with
116 | Array (_, def) -> def
117 | Updated _ -> assert false
118
119 let set p n e =
120 let a = reroot p in
121 let l = Uint63.of_int (UArray.length a) in
122 if Uint63.le Uint63.zero n && Uint63.lt n l then
123 let i = length_to_int n in
124 let v' = UArray.unsafe_get a i in
125 UArray.unsafe_set a i e;
126 let t = ref !p in (* i.e., Array (a, def) *)
127 p := Updated (i, v', t);
128 t
129 else p
130
131 let default p =
132 let _ = reroot p in
133 match !p with
134 | Array (_,def) -> def
135 | Updated _ -> assert false
136
137 let make_int n def =
138 ref (Array (UArray.make n def, def))
139
140 let make n def = make_int (trunc_size n) def
141
142 let uinit n f =
143 if Int.equal n 0 then UArray.empty
144 else begin
145 let t = UArray.make n (f 0) in
146 for i = 1 to n - 1 do
147 UArray.unsafe_set t i (f i)
148 done;
149 t
150 end
151
152 let init n f def =
153 let n = trunc_size n in
154 let t = uinit n f in
155 ref (Array (t, def))
156
157 let to_array p =
158 let _ = reroot p in
159 match !p with
160 | Array (t,def) -> UArray.to_array t, def
161 | Updated _ -> assert false
162
163 let copy p =
164 let _ = reroot p in
165 match !p with
166 | Array (t, def) -> ref (Array (UArray.copy t, def))
167 | Updated _ -> assert false
168
169 (* Higher order combinators: the callback may update the underlying
170 array requiring a reroot between each call. To avoid doing n
171 reroots (-> O(n^2)), we copy if we have to reroot again. *)
172
173 let is_rooted p = match !p with
174 | Array _ -> true
175 | Updated _ -> false
176
177 type 'a cache = {
178 orig : 'a t;
179 mutable self : 'a UArray.t;
180 mutable rerooted_again : bool;
181 }
182
183 let make_cache p = {
184 orig = p;
185 self = reroot p;
186 rerooted_again = false;
187 }
188
189 let uget_cache cache i =
190 let () = if not cache.rerooted_again && not (is_rooted cache.orig)
191 then begin
192 cache.self <- UArray.copy (reroot cache.orig);
193 cache.rerooted_again <- true
194 end
195 in
196 UArray.unsafe_get cache.self i
197
198 let map f p =
199 let t = make_cache p in
200 let len = UArray.length t.self in
201 let res = uinit len (fun i -> f (uget_cache t i)) in
202 let def = f (default p) in
203 ref (Array (res, def))
204
205 let fold_left f x p =
206 let r = ref x in
207 let t = make_cache p in
208 let len = UArray.length t.self in
209 for i = 0 to len - 1 do
210 r := f !r (uget_cache t i)
211 done;
212 f !r (default p)
213
214 let fold_left2 f a p1 p2 =
215 let r = ref a in
216 let t1 = make_cache p1 in
217 let len = UArray.length t1.self in
218 let t2 = make_cache p2 in
219 if UArray.length t2.self <> len then invalid_arg "Parray.fold_left2";
220 for i = 0 to len - 1 do
221 let v1 = uget_cache t1 i in
222 let v2 = uget_cache t2 i in
223 r := f !r v1 v2
224 done;
225 f !r (default p1) (default p2)