stringMap.ml 1.45 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
(******************************************************************************)
(*                                                                            *)
(*                                   Menhir                                   *)
(*                                                                            *)
(*                       François Pottier, Inria Paris                        *)
(*              Yann Régis-Gianas, PPS, Université Paris Diderot              *)
(*                                                                            *)
(*  Copyright Inria. All rights reserved. This file is distributed under the  *)
(*  terms of the GNU General Public License version 2, as described in the    *)
(*  file LICENSE.                                                             *)
(*                                                                            *)
(******************************************************************************)

14 15 16
include Map.Make (String)

let cardinal s =
17
  fold (fun _ _ x -> x + 1) s 0
18 19 20

let filter pred map =
  fold (fun key value map ->
21
          if pred key value then
22
            add key value map
23
          else
24
            map) map empty
25 26 27 28

let restrict domain map =
  filter (fun k _ -> StringSet.mem k domain) map

29
let domain map =
30
  fold (fun key _ acu -> StringSet.add key acu) map StringSet.empty
31 32 33 34 35 36 37 38 39

let multiple_add k v m =
  let vs =
    try
      find k m
    with Not_found ->
      []
  in
  add k (v :: vs) m