Skip to content

Commit 7fcd8db

Browse files
committed
cleanup
1 parent 9d99e96 commit 7fcd8db

File tree

2 files changed

+13
-9
lines changed

2 files changed

+13
-9
lines changed

src/bl.ml

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ let rec remove f = function
138138
aux 0
139139

140140
let rec insert f x = function
141-
| BCons (head, tail) when f head <= 0 -> BCons (head, BCons(x,tail))
141+
| BCons (head, tail) when f head > 0 -> BCons (head, BCons(x,tail))
142142
| BCons (head, tail) -> BCons (head, insert f x tail)
143143
| BArray { len; data } ->
144144
let rec aux i =
@@ -174,19 +174,24 @@ let next (x,n) =
174174
| BArray { len; data } -> assert(n < len); data.(n), (x,n+1)
175175
| BCons (a,xs) -> a, (xs,n)
176176

177-
let(*[@tail_mod_cons]*) rec to_list_aux i len data =
177+
(* ocaml >= 4.14
178+
let[@tail_mod_cons] rec to_list_aux i len data =
178179
if i = len then []
179-
else data.(i) :: to_list_aux (i+1) len data
180+
else data.(i) :: to_list_aux (i+1) len data *)
181+
182+
let rec to_list_aux i len data acc =
183+
if i = len then List.rev acc
184+
else to_list_aux (i+1) len data (data.(i) :: acc)
180185

181186
let rec to_list = function
182187
| BCons(x,xs) -> x :: to_list xs
183-
| BArray { len; data } -> to_list_aux 0 len data
188+
| BArray { len; data } -> to_list_aux 0 len data []
184189

185190
let to_list (x,n) =
186191
if n = 0 then to_list x else
187192
match x with
188193
| BCons _ -> assert false
189-
| BArray { len; data } -> to_list_aux n len data
194+
| BArray { len; data } -> to_list_aux n len data []
190195

191196
let of_list l = let data = Array.of_list l in BArray { len = Array.length data; data }, 0
192197

src/bl.mli

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,20 @@
22
(* license: GNU Lesser General Public License Version 2.1 or later *)
33
(* ------------------------------------------------------------------------- *)
44

5-
(* These lists become functional only after a call to commit. Before that
6-
they are imperative, keeping a pointer to an old "copy" must be avoided.
7-
Before calling commit rcons is O(1) time and space. *)
5+
(* functional lists with O(1) rcons *)
86

97
type 'a t
108
val pp : (Format.formatter -> 'a -> unit) -> Format.formatter -> 'a t -> unit
119
val show : (Format.formatter -> 'a -> unit) -> 'a t -> string
1210

13-
(* These are O(1) *)
1411
val empty : unit -> 'a t
1512
val cons : 'a -> 'a t -> 'a t
1613
val rcons : 'a -> 'a t -> 'a t
1714

1815
val replace : ('a -> bool) -> 'a -> 'a t -> 'a t
1916
val remove : ('a -> bool) -> 'a t -> 'a t
17+
18+
(* [insert f x l] inserts before y in l if f y > 0 *)
2019
val insert : ('a -> int) -> 'a -> 'a t -> 'a t
2120
val remove : ('a -> bool) -> 'a t -> 'a t
2221

0 commit comments

Comments
 (0)