mirror of
https://github.com/c-cube/ocaml-containers.git
synced 2025-12-06 11:15:31 -05:00
update code and comments
This commit is contained in:
parent
85decd732c
commit
01f70cc802
2 changed files with 25 additions and 15 deletions
|
|
@ -181,6 +181,15 @@ module Make(O : Map.OrderedType) = struct
|
|||
| Some v1, Some v2 -> f k v1 v2)
|
||||
a b
|
||||
|
||||
let update k f m =
|
||||
let x =
|
||||
try f (Some (M.find k m))
|
||||
with Not_found -> f None
|
||||
in
|
||||
match x with
|
||||
| None -> M.remove k m
|
||||
| Some v' -> M.add k v' m
|
||||
|
||||
let choose_opt m =
|
||||
try Some (M.choose m)
|
||||
with Not_found -> None
|
||||
|
|
@ -229,15 +238,6 @@ module Make(O : Map.OrderedType) = struct
|
|||
| None -> raise Not_found
|
||||
| Some (k,v) -> k, v
|
||||
|
||||
let update k f m =
|
||||
let x =
|
||||
try f (Some (M.find k m))
|
||||
with Not_found -> f None
|
||||
in
|
||||
match x with
|
||||
| None -> M.remove k m
|
||||
| Some v' -> M.add k v' m
|
||||
|
||||
(*$= & ~printer:CCFormat.(to_string @@ Dump.(list (pair string int)))
|
||||
["a", 1; "b", 20] \
|
||||
(M.of_list ["b", 2; "c", 3] \
|
||||
|
|
@ -247,6 +247,10 @@ module Make(O : Map.OrderedType) = struct
|
|||
|> M.to_list |> List.sort CCOrd.compare)
|
||||
*)
|
||||
|
||||
(* === include M.
|
||||
This will shadow some values depending on OCaml's current version
|
||||
=== *)
|
||||
|
||||
include M
|
||||
|
||||
let get = find_opt
|
||||
|
|
@ -271,10 +275,8 @@ module Make(O : Map.OrderedType) = struct
|
|||
|
||||
let add_seq_with ~f m s =
|
||||
let combine k v = function None -> Some v | Some v0 -> Some (f k v v0) in
|
||||
let m = ref m in
|
||||
Seq.iter (fun (k,v) ->
|
||||
m := update k (combine k v) !m) s;
|
||||
!m
|
||||
Seq.fold_left
|
||||
(fun m (k,v) -> update k (combine k v) m) m s
|
||||
|
||||
let of_seq s = add_seq empty s
|
||||
let of_seq_with ~f s = add_seq_with ~f empty s
|
||||
|
|
@ -307,8 +309,7 @@ module Make(O : Map.OrderedType) = struct
|
|||
let add_list_with ~f m l =
|
||||
let combine k v = function None -> Some v | Some v0 -> Some (f k v v0) in
|
||||
List.fold_left
|
||||
(fun m (k,v) ->
|
||||
update k (combine k v) m)
|
||||
(fun m (k,v) -> update k (combine k v) m)
|
||||
m l
|
||||
|
||||
let of_list l = add_list empty l
|
||||
|
|
|
|||
|
|
@ -84,6 +84,9 @@ module type S = sig
|
|||
val add_seq_with : f:(key -> 'a -> 'a -> 'a) -> 'a t -> (key * 'a) Seq.t -> 'a t
|
||||
(** [add_seq ~f m l] adds the given seq [l] of bindings to the map [m],
|
||||
using [f] to combine values that have the same key.
|
||||
If a key occurs several times, all its bindings are combined using the
|
||||
function [f], with [f key v1 v2] being called with [v1] occurring
|
||||
later in the seq than [v2].
|
||||
@since NEXT_RELEASE *)
|
||||
|
||||
val of_seq : (key * 'a) Seq.t -> 'a t
|
||||
|
|
@ -108,6 +111,9 @@ module type S = sig
|
|||
val add_iter_with : f:(key -> 'a -> 'a -> 'a) -> 'a t -> (key * 'a) iter -> 'a t
|
||||
(** [add_iter ~f m l] adds the given iter [l] of bindings to the map [m],
|
||||
using [f] to combine values that have the same key.
|
||||
If a key occurs several times, all its bindings are combined using the
|
||||
function [f], with [f key v1 v2] being called with [v1] occurring
|
||||
later in the seq than [v2].
|
||||
@since NEXT_RELEASE *)
|
||||
|
||||
val of_iter : (key * 'a) iter -> 'a t
|
||||
|
|
@ -149,6 +155,9 @@ module type S = sig
|
|||
val add_list_with : f:(key -> 'a -> 'a -> 'a) -> 'a t -> (key * 'a) list -> 'a t
|
||||
(** [add_list ~f m l] adds the given list [l] of bindings to the map [m],
|
||||
using [f] to combine values that have the same key.
|
||||
If a key occurs several times, all its bindings are combined using the
|
||||
function [f], with [f key v1 v2] being called with [v1] occurring
|
||||
later in the seq than [v2].
|
||||
@since NEXT_RELEASE *)
|
||||
|
||||
val keys : _ t -> key iter
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue