mirror of
https://github.com/c-cube/ocaml-containers.git
synced 2025-12-06 11:15:31 -05:00
remove CCList.Idx, rename its functions to toplevel
This commit is contained in:
parent
bf609e7f04
commit
ad1513e36c
4 changed files with 90 additions and 94 deletions
|
|
@ -718,8 +718,7 @@ let inter ?(eq=(=)) l1 l2 =
|
||||||
inter [1;2;4] [2;3;4;5] = [2;4]
|
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
|
let r = ref 0 in
|
||||||
map
|
map
|
||||||
(fun x ->
|
(fun x ->
|
||||||
|
|
@ -727,17 +726,17 @@ module Idx = struct
|
||||||
incr r; y
|
incr r; y
|
||||||
) l
|
) l
|
||||||
|
|
||||||
(*$T
|
(*$T
|
||||||
Idx.mapi (fun i x -> i*x) [10;10;10] = [0;10;20]
|
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
|
let rec aux f i l = match l with
|
||||||
| [] -> ()
|
| [] -> ()
|
||||||
| x::l' -> f i x; aux f (i+1) l'
|
| x::l' -> f i x; aux f (i+1) l'
|
||||||
in aux f 0 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
|
let rec foldi f acc i l = match l with
|
||||||
| [] -> acc
|
| [] -> acc
|
||||||
| x::l' ->
|
| x::l' ->
|
||||||
|
|
@ -746,23 +745,23 @@ module Idx = struct
|
||||||
in
|
in
|
||||||
foldi f acc 0 l
|
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
|
| [] -> raise Not_found
|
||||||
| x::_ when i=0 -> x
|
| x::_ when i=0 -> x
|
||||||
| _::l' -> get_exn l' (i-1)
|
| _::l' -> get_at_idx_exn (i-1) l'
|
||||||
|
|
||||||
let get l i =
|
let get_at_idx i l =
|
||||||
try Some (get_exn l i)
|
try Some (get_at_idx_exn i l)
|
||||||
with Not_found -> None
|
with Not_found -> None
|
||||||
|
|
||||||
(*$T
|
(*$T
|
||||||
Idx.get (range 0 10) 0 = Some 0
|
get_at_idx 0 (range 0 10) = Some 0
|
||||||
Idx.get (range 0 10) 5 = Some 5
|
get_at_idx 5 (range 0 10) = Some 5
|
||||||
Idx.get (range 0 10) 11 = None
|
get_at_idx 11 (range 0 10) = None
|
||||||
Idx.get [] 0 = 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
|
let rec aux l acc i = match l with
|
||||||
| [] -> l0
|
| [] -> l0
|
||||||
| _::l' when i=0 -> List.rev_append acc (x::l')
|
| _::l' when i=0 -> List.rev_append acc (x::l')
|
||||||
|
|
@ -771,13 +770,13 @@ module Idx = struct
|
||||||
in
|
in
|
||||||
aux l0 [] i
|
aux l0 [] i
|
||||||
|
|
||||||
(*$T
|
(*$T
|
||||||
Idx.set [1;2;3] 0 10 = [10;2;3]
|
set_at_idx 0 10 [1;2;3] = [10;2;3]
|
||||||
Idx.set [1;2;3] 4 10 = [1;2;3]
|
set_at_idx 4 10 [1;2;3] = [1;2;3]
|
||||||
Idx.set [1;2;3] 1 10 = [1;10;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
|
let rec aux l acc i x = match l with
|
||||||
| [] -> List.rev_append acc [x]
|
| [] -> List.rev_append acc [x]
|
||||||
| y::l' when i=0 -> List.rev_append acc (x::y::l')
|
| y::l' when i=0 -> List.rev_append acc (x::y::l')
|
||||||
|
|
@ -786,13 +785,13 @@ module Idx = struct
|
||||||
in
|
in
|
||||||
aux l [] i x
|
aux l [] i x
|
||||||
|
|
||||||
(*$T
|
(*$T
|
||||||
Idx.insert [1;2;3] 0 10 = [10;1;2;3]
|
insert_at_idx 0 10 [1;2;3] = [10;1;2;3]
|
||||||
Idx.insert [1;2;3] 4 10 = [1;2;3;10]
|
insert_at_idx 4 10 [1;2;3] = [1;2;3;10]
|
||||||
Idx.insert [1;2;3] 1 10 = [1;10;2;3]
|
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
|
let rec aux l acc i = match l with
|
||||||
| [] -> l0
|
| [] -> l0
|
||||||
| _::l' when i=0 -> List.rev_append acc l'
|
| _::l' when i=0 -> List.rev_append acc l'
|
||||||
|
|
@ -801,12 +800,11 @@ module Idx = struct
|
||||||
in
|
in
|
||||||
aux l0 [] i
|
aux l0 [] i
|
||||||
|
|
||||||
(*$T
|
(*$T
|
||||||
Idx.remove [1;2;3;4] 0 = [2;3;4]
|
remove_at_idx 0 [1;2;3;4] = [2;3;4]
|
||||||
Idx.remove [1;2;3;4] 3 = [1;2;3]
|
remove_at_idx 3 [1;2;3;4] = [1;2;3]
|
||||||
Idx.remove [1;2;3;4] 5 = [1;2;3;4]
|
remove_at_idx 5 [1;2;3;4] = [1;2;3;4]
|
||||||
*)
|
*)
|
||||||
end
|
|
||||||
|
|
||||||
let range_by ~step i j =
|
let range_by ~step i j =
|
||||||
let rec range i j acc =
|
let rec range i j acc =
|
||||||
|
|
|
||||||
|
|
@ -224,32 +224,30 @@ val group_succ : ?eq:('a -> 'a -> bool) -> 'a list -> 'a list list
|
||||||
|
|
||||||
(** {2 Indices} *)
|
(** {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
|
val foldi : ('b -> int -> 'a -> 'b) -> 'b -> 'a t -> 'b
|
||||||
(** Fold on list, with index *)
|
(** 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
|
val get_at_idx_exn : int -> 'a t -> 'a
|
||||||
(** Get the i-th element, or
|
(** Get the i-th element, or
|
||||||
@raise Not_found if the index is invalid *)
|
@raise Not_found if the index is invalid *)
|
||||||
|
|
||||||
val set : 'a t -> int -> 'a -> 'a t
|
val set_at_idx : int -> 'a -> 'a t -> 'a t
|
||||||
(** Set i-th element (removes the old one), or does nothing if
|
(** Set i-th element (removes the old one), or does nothing if
|
||||||
index is too high *)
|
index is too high *)
|
||||||
|
|
||||||
val insert : 'a t -> int -> 'a -> 'a t
|
val insert_at_idx : int -> 'a -> 'a t -> 'a t
|
||||||
(** Insert at i-th position, between the two existing elements. If the
|
(** Insert at i-th position, between the two existing elements. If the
|
||||||
index is too high, append at the end of the list *)
|
index is too high, append at the end of the list *)
|
||||||
|
|
||||||
val remove : 'a t -> int -> 'a t
|
val remove_at_idx : int -> 'a t -> 'a t
|
||||||
(** Remove element at given index. Does nothing if the index is
|
(** Remove element at given index. Does nothing if the index is
|
||||||
too high. *)
|
too high. *)
|
||||||
end
|
|
||||||
|
|
||||||
(** {2 Set Operators}
|
(** {2 Set Operators}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -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 *)
|
(* example from https://en.wikipedia.org/wiki/Strongly_connected_component *)
|
||||||
(*$R
|
(*$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
|
let graph = of_list
|
||||||
[ "a", "b"
|
[ "a", "b"
|
||||||
; "b", "e"
|
; "b", "e"
|
||||||
|
|
|
||||||
|
|
@ -65,7 +65,7 @@ let rec set l i v = match l with
|
||||||
(*$Q & ~small:List.length
|
(*$Q & ~small:List.length
|
||||||
Q.(list small_int) (fun l -> \
|
Q.(list small_int) (fun l -> \
|
||||||
let l1 = of_list l in \
|
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))
|
|> List.for_all (fun (i,x) -> get_exn l1 i = x))
|
||||||
*)
|
*)
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue