File: Multimap.ml

package info (click to toggle)
ocamlformat 0.28.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 14,436 kB
  • sloc: ml: 63,321; pascal: 4,769; lisp: 229; sh: 217; makefile: 121
file content (42 lines) | stat: -rw-r--r-- 1,659 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
(**************************************************************************)
(*                                                                        *)
(*                              OCamlFormat                               *)
(*                                                                        *)
(*            Copyright (c) Facebook, Inc. and its affiliates.            *)
(*                                                                        *)
(*      This source code is licensed under the MIT license found in       *)
(*      the LICENSE file in the root directory of this source tree.       *)
(*                                                                        *)
(**************************************************************************)

type ('key, 'value, 'cmp) t = ('key, 'value list, 'cmp) Map.t

module M (K : sig
  type t

  type comparator_witness
end) =
struct
  type nonrec 'v t = (K.t, 'v, K.comparator_witness) t
end

let update_multi map ~src ~dst ~f =
  Option.fold (Map.find map src) ~init:(Map.remove map src)
    ~f:(fun new_map src_data ->
      Map.update new_map dst ~f:(fun dst_data ->
          Option.fold dst_data ~init:src_data ~f ) )

let change_multi map key data =
  Map.change map key ~f:(function _ -> Some data)

let partition_multi map ~src ~dst ~f =
  let move, dontmove = List.partition_tf (Map.find_multi map src) ~f in
  let map =
    List.fold_left ~init:map (List.rev move) ~f:(fun map data ->
        Map.add_multi map ~key:dst ~data )
  in
  change_multi map src dontmove

let filter map ~f = Map.map map ~f:(List.filter ~f)

let to_list map = Map.to_alist map |> List.concat_map ~f:snd