remove CCList.Idx, rename its functions to toplevel

This commit is contained in:
Simon Cruanes 2016-11-03 21:57:24 +01:00
parent bf609e7f04
commit ad1513e36c
4 changed files with 90 additions and 94 deletions

View file

@ -718,8 +718,7 @@ let inter ?(eq=(=)) l1 l2 =
inter [1;2;4] [2;3;4;5] = [2;4]
*)
module Idx = struct
let mapi f l =
let mapi f l =
let r = ref 0 in
map
(fun x ->
@ -727,17 +726,17 @@ module Idx = struct
incr r; y
) l
(*$T
Idx.mapi (fun i x -> i*x) [10;10;10] = [0;10;20]
*)
(*$T
mapi (fun i x -> i*x) [10;10;10] = [0;10;20]
*)
let iteri f l =
let iteri f l =
let rec aux f i l = match l with
| [] -> ()
| x::l' -> f i x; aux f (i+1) l'
in aux f 0 l
let foldi f acc l =
let foldi f acc l =
let rec foldi f acc i l = match l with
| [] -> acc
| x::l' ->
@ -746,23 +745,23 @@ module Idx = struct
in
foldi f acc 0 l
let rec get_exn l i = match l with
let rec get_at_idx_exn i l = match l with
| [] -> raise Not_found
| x::_ when i=0 -> x
| _::l' -> get_exn l' (i-1)
| _::l' -> get_at_idx_exn (i-1) l'
let get l i =
try Some (get_exn l i)
let get_at_idx i l =
try Some (get_at_idx_exn i l)
with Not_found -> None
(*$T
Idx.get (range 0 10) 0 = Some 0
Idx.get (range 0 10) 5 = Some 5
Idx.get (range 0 10) 11 = None
Idx.get [] 0 = None
*)
(*$T
get_at_idx 0 (range 0 10) = Some 0
get_at_idx 5 (range 0 10) = Some 5
get_at_idx 11 (range 0 10) = None
get_at_idx 0 [] = None
*)
let set l0 i x =
let set_at_idx i x l0 =
let rec aux l acc i = match l with
| [] -> l0
| _::l' when i=0 -> List.rev_append acc (x::l')
@ -771,13 +770,13 @@ module Idx = struct
in
aux l0 [] i
(*$T
Idx.set [1;2;3] 0 10 = [10;2;3]
Idx.set [1;2;3] 4 10 = [1;2;3]
Idx.set [1;2;3] 1 10 = [1;10;3]
(*$T
set_at_idx 0 10 [1;2;3] = [10;2;3]
set_at_idx 4 10 [1;2;3] = [1;2;3]
set_at_idx 1 10 [1;2;3] = [1;10;3]
*)
let insert l i x =
let insert_at_idx i x l =
let rec aux l acc i x = match l with
| [] -> List.rev_append acc [x]
| y::l' when i=0 -> List.rev_append acc (x::y::l')
@ -786,13 +785,13 @@ module Idx = struct
in
aux l [] i x
(*$T
Idx.insert [1;2;3] 0 10 = [10;1;2;3]
Idx.insert [1;2;3] 4 10 = [1;2;3;10]
Idx.insert [1;2;3] 1 10 = [1;10;2;3]
(*$T
insert_at_idx 0 10 [1;2;3] = [10;1;2;3]
insert_at_idx 4 10 [1;2;3] = [1;2;3;10]
insert_at_idx 1 10 [1;2;3] = [1;10;2;3]
*)
let remove l0 i =
let remove_at_idx i l0 =
let rec aux l acc i = match l with
| [] -> l0
| _::l' when i=0 -> List.rev_append acc l'
@ -801,12 +800,11 @@ module Idx = struct
in
aux l0 [] i
(*$T
Idx.remove [1;2;3;4] 0 = [2;3;4]
Idx.remove [1;2;3;4] 3 = [1;2;3]
Idx.remove [1;2;3;4] 5 = [1;2;3;4]
*)
end
(*$T
remove_at_idx 0 [1;2;3;4] = [2;3;4]
remove_at_idx 3 [1;2;3;4] = [1;2;3]
remove_at_idx 5 [1;2;3;4] = [1;2;3;4]
*)
let range_by ~step i j =
let rec range i j acc =

View file

@ -224,32 +224,30 @@ val group_succ : ?eq:('a -> 'a -> bool) -> 'a list -> 'a list list
(** {2 Indices} *)
module Idx : sig
val mapi : (int -> 'a -> 'b) -> 'a t -> 'b t
val mapi : (int -> 'a -> 'b) -> 'a t -> 'b t
val iteri : (int -> 'a -> unit) -> 'a t -> unit
val iteri : (int -> 'a -> unit) -> 'a t -> unit
val foldi : ('b -> int -> 'a -> 'b) -> 'b -> 'a t -> 'b
(** Fold on list, with index *)
val foldi : ('b -> int -> 'a -> 'b) -> 'b -> 'a t -> 'b
(** Fold on list, with index *)
val get : 'a t -> int -> 'a option
val get_at_idx : int -> 'a t -> 'a option
val get_exn : 'a t -> int -> 'a
(** Get the i-th element, or
val get_at_idx_exn : int -> 'a t -> 'a
(** Get the i-th element, or
@raise Not_found if the index is invalid *)
val set : 'a t -> int -> 'a -> 'a t
(** Set i-th element (removes the old one), or does nothing if
val set_at_idx : int -> 'a -> 'a t -> 'a t
(** Set i-th element (removes the old one), or does nothing if
index is too high *)
val insert : 'a t -> int -> 'a -> 'a t
(** Insert at i-th position, between the two existing elements. If the
val insert_at_idx : int -> 'a -> 'a t -> 'a t
(** Insert at i-th position, between the two existing elements. If the
index is too high, append at the end of the list *)
val remove : 'a t -> int -> 'a t
(** Remove element at given index. Does nothing if the index is
val remove_at_idx : int -> 'a t -> 'a t
(** Remove element at given index. Does nothing if the index is
too high. *)
end
(** {2 Set Operators}

View file

@ -501,7 +501,7 @@ let scc ?(tbl=mk_table 128) ~graph seq = SCC.explore ~tbl ~graph seq
(* example from https://en.wikipedia.org/wiki/Strongly_connected_component *)
(*$R
let set_eq ?(eq=(=)) l1 l2 = CCList.Set.subset ~eq l1 l2 && CCList.Set.subset ~eq l2 l1 in
let set_eq ?(eq=(=)) l1 l2 = CCList.subset ~eq l1 l2 && CCList.subset ~eq l2 l1 in
let graph = of_list
[ "a", "b"
; "b", "e"

View file

@ -65,7 +65,7 @@ let rec set l i v = match l with
(*$Q & ~small:List.length
Q.(list small_int) (fun l -> \
let l1 = of_list l in \
CCList.Idx.mapi (fun i x -> i,x) l \
CCList.mapi (fun i x -> i,x) l \
|> List.for_all (fun (i,x) -> get_exn l1 i = x))
*)