22(* license: GNU Lesser General Public License Version 2.1 or later *)
33(* ------------------------------------------------------------------------- *)
44
5- (*
6- build: bl=0.517913 l=0.583841
7- scan: bl=0.025057 l=0.020488
8-
9- base: 0.011125
10- extend: 1.322639
11- query: 0.000045
12- optimize: 0.000187
13- ok
14- exec: 0.000044
15- *)
16-
175module Array = struct
18-
6+ (* Main code taken from OCaml-bazaar, Library General
7+ Public License version 2. *)
198 type 'a t = 'a data ref
209
2110 and 'a data =
@@ -50,13 +39,6 @@ module Array = struct
5039
5140 let reroot t = rerootk t (fun () -> () )
5241
53- let extend len t a =
54- let data = reroot t; match ! t with Array x -> x | Diff _ -> assert false in
55- let newdata = Array. make (2 * (max 1 len)) a in
56- if len > 0 then
57- Array. blit data 0 newdata 0 len;
58- ref @@ Array newdata
59-
6042 let get t i =
6143 match ! t with
6244 | Array a ->
@@ -81,6 +63,15 @@ module Array = struct
8163 | Diff _ ->
8264 assert false
8365
66+ (* New code, all bugs are mine ;-) *)
67+ let extend len t a =
68+ let data = reroot t; match ! t with Array x -> x | Diff _ -> assert false in
69+ let newdata = Array. make (2 * (max 1 len)) a in
70+ if len > 0 then
71+ Array. blit data 0 newdata 0 len;
72+ ref @@ Array newdata
73+
74+
8475 let shift_right t i len =
8576 let rec shift t j =
8677 if j < i then t
@@ -105,25 +96,6 @@ type 'a t =
10596 | BCons of 'a * 'a t
10697 [@@ deriving show ]
10798
108- (* | UndoRcons of { old_tail : 'a data; next : 'a t }
109- | UndoInsert of { old_before : 'a data; old_after : 'a data; next : 'a t } *)
110- (* and 'a t = 'a data ref *)
111-
112- let pp_pointer fmt x = Format. fprintf fmt " %x" (Obj. magic x land 0xffffff )
113- (* let rec pp pp_a fmt = function
114- | BArray -> Format.fprintf fmt "[]"
115- | BCons { head; tail; last } as x ->
116- Format.fprintf fmt "[self %a, tail %a, last %a] %a :: "
117- pp_pointer x
118- pp_pointer tail
119- pp_pointer last
120- pp_a head;
121- pp pp_a fmt tail *)
122- (* | UndoRcons { old_tail; next } -> Format.fprintf fmt "UndoRcons %a :: " pp_pointer old_tail ; pp pp_a fmt next
123- | UndoInsert { old_before; old_after; next } -> Format.fprintf fmt "UndoInsert %a - %a :: " pp_pointer old_before pp_pointer old_after; pp pp_a fmt next *)
124-
125- (* let show pp_a x = Format.asprintf "%a" (pp pp_a) x *)
126-
12799let empty () = BArray { len = 0 ; data = Array. empty () }
128100
129101let extendk len data a k =
@@ -190,15 +162,6 @@ let rec insert f x = function
190162 in
191163 aux 0
192164
193- let rec commit = function
194- | BCons (x ,xs ) -> x :: commit xs
195- | BArray { len; data } ->
196- let [@ tail_mod_cons] rec aux i =
197- if i = len then []
198- else data.(i) :: aux (i+ 1 )
199- in
200- aux 0
201-
202165type 'a scan = 'a t * int
203166let to_scan x = x, 0
204167let is_empty (x ,n ) =
@@ -211,16 +174,19 @@ let next (x,n) =
211174 | BArray { len; data } -> assert (n < len); data.(n), (x,n+ 1 )
212175 | BCons (a ,xs ) -> a, (xs,n)
213176
177+ let (* [@tail_mod_cons]*) rec to_list_aux i len data =
178+ if i = len then []
179+ else data.(i) :: to_list_aux (i+ 1 ) len data
180+
181+ let rec to_list = function
182+ | BCons (x ,xs ) -> x :: to_list xs
183+ | BArray { len; data } -> to_list_aux 0 len data
184+
214185let to_list (x ,n ) =
215- if n = 0 then commit x else
186+ if n = 0 then to_list x else
216187 match x with
217188 | BCons _ -> assert false
218- | BArray { len; data } ->
219- let [@ tail_mod_cons] rec aux i =
220- if i = len then []
221- else data.(i) :: aux (i+ 1 )
222- in
223- aux n
189+ | BArray { len; data } -> to_list_aux n len data
224190
225191let of_list l = let data = Array. of_list l in BArray { len = Array. length data; data }, 0
226192
0 commit comments