Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
gares committed Sep 18, 2024
1 parent 9d99e96 commit 7fcd8db
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
15 changes: 10 additions & 5 deletions src/bl.ml
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ let rec remove f = function
aux 0

let rec insert f x = function
| BCons (head, tail) when f head <= 0 -> BCons (head, BCons(x,tail))
| BCons (head, tail) when f head > 0 -> BCons (head, BCons(x,tail))
| BCons (head, tail) -> BCons (head, insert f x tail)
| BArray { len; data } ->
let rec aux i =
Expand Down Expand Up @@ -174,19 +174,24 @@ let next (x,n) =
| BArray { len; data } -> assert(n < len); data.(n), (x,n+1)
| BCons (a,xs) -> a, (xs,n)

let(*[@tail_mod_cons]*) rec to_list_aux i len data =
(* ocaml >= 4.14
let[@tail_mod_cons] rec to_list_aux i len data =
if i = len then []
else data.(i) :: to_list_aux (i+1) len data
else data.(i) :: to_list_aux (i+1) len data *)

let rec to_list_aux i len data acc =
if i = len then List.rev acc
else to_list_aux (i+1) len data (data.(i) :: acc)

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

let to_list (x,n) =
if n = 0 then to_list x else
match x with
| BCons _ -> assert false
| BArray { len; data } -> to_list_aux n len data
| BArray { len; data } -> to_list_aux n len data []

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

Expand Down
7 changes: 3 additions & 4 deletions src/bl.mli
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,20 @@
(* license: GNU Lesser General Public License Version 2.1 or later *)
(* ------------------------------------------------------------------------- *)

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

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

(* These are O(1) *)
val empty : unit -> 'a t
val cons : 'a -> 'a t -> 'a t
val rcons : 'a -> 'a t -> 'a t

val replace : ('a -> bool) -> 'a -> 'a t -> 'a t
val remove : ('a -> bool) -> 'a t -> 'a t

(* [insert f x l] inserts before y in l if f y > 0 *)
val insert : ('a -> int) -> 'a -> 'a t -> 'a t
val remove : ('a -> bool) -> 'a t -> 'a t

Expand Down

0 comments on commit 7fcd8db

Please sign in to comment.