-
Notifications
You must be signed in to change notification settings - Fork 8
/
constant.ml
71 lines (53 loc) · 1.88 KB
/
constant.ml
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
open Util
(* This module should not be opened, but be used qualified. *)
(* A constant is identified by an autonumber, scoped by compilation unit. *)
module Constant = struct
type t = Compunit.t * int
let compare (x : t) (y : t) = compare x y
end
type t = Constant.t
let counters = Compunit.IntArray.make_basic ()
let make compunit : t =
let number = Compunit.IntArray.inc counters compunit in
(compunit, number)
let remake f (c, i) = (f c, i)
(* Data associated to constants is also stored in a map whose domain is their paired identities. *)
module Map = struct
module IntMap = Map.Make (Int)
type 'a t = 'a IntMap.t Compunit.Map.t
let empty : 'a t = Compunit.Map.empty
let find_opt (i, c) m =
let open Monad.Ops (Monad.Maybe) in
let* m = Compunit.Map.find_opt i m in
IntMap.find_opt c m
let mem (i, c) m =
match Compunit.Map.find_opt i m with
| Some m -> IntMap.mem c m
| None -> false
let add (i, c) x m =
Compunit.Map.update i
(function
| None -> Some (IntMap.empty |> IntMap.add c x)
| Some m -> Some (IntMap.add c x m))
m
let update (i, c) f m =
Compunit.Map.update i
(function
| None -> Some (IntMap.update c f IntMap.empty)
| Some m -> Some (IntMap.update c f m))
m
let remove (i, c) m =
Compunit.Map.update i
(function
| None -> None
| Some m -> Some (IntMap.remove c m))
m
let iter f m = Compunit.Map.iter (fun i n -> IntMap.iter (fun c v -> f (i, c) v) n) m
let cardinal m = Compunit.Map.fold (fun _ n x -> x + IntMap.cardinal n) m 0
let to_channel_unit chan i (m : 'a t) flags =
Marshal.to_channel chan (Compunit.Map.find_opt i m : 'a IntMap.t option) flags
let from_channel_unit chan f i (m : 'a t) =
match (Marshal.from_channel chan : 'a IntMap.t option) with
| Some x -> Compunit.Map.add i (IntMap.map f x) m
| None -> m
end