package core-and-more

  1. Overview
  2. Docs
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source

Source file HashtblWrapper.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
open Util
open Core

module type ImperativeDict =
sig
  type key
  type 'a t

  val create : int -> 'a t
  val empty : unit -> 'a t
  val find_opt : key -> 'a t -> 'a option
  val add: key -> 'a -> 'a t -> unit
  val fold : (key -> 'a -> 'b -> 'b) -> 'a t -> 'b -> 'b
  val fold2 : (key -> 'a -> key -> 'b -> 'c -> 'c) -> 'a t -> 'b t -> 'c -> 'c
  val iter: (key -> 'a -> unit) -> 'a t -> unit
  val copy: 'a t -> 'a t
  val find_or_add : key -> (unit -> 'a) -> 'a t -> 'a
  (*val add : elt -> t -> unit
  val remove : elt -> t -> unit
  val size : t -> int
  val is_empty : t -> bool
  val contains : elt -> t -> bool
  val fold : (elt -> 'b -> 'b) -> t -> 'b -> 'b
  val fold2 : (elt -> elt -> 'a -> 'a) -> t -> t -> 'a -> 'a
  val as_list : t -> elt list
  val iter : (elt -> unit) -> t -> unit
  val union : t -> t -> t
  val pp : (Format.formatter -> elt -> unit) -> Format.formatter -> t -> unit
    val update : (elt option -> unit) -> elt -> t -> unit*)
end

module Make(D : Data) =
struct
  module D = struct
    type t = D.t
    [@@deriving ord, show, hash]

    let sexp_of_t _ = failwith "cant call"
  end

  type key = D.t
  type 'a t = (key,'a) Hashtbl.t

  let create size =
    Hashtbl.create ~size (module D)

  let empty _ =
    Hashtbl.create (module D)

  let add
      (k:key)
      (v:'a)
      (s:'a t)
    : unit =
    Hashtbl.set ~key:k ~data:v s

  let find_or_add
      (k:key)
      (default:unit -> 'a)
      (s:'a t)
    : 'a =
    Hashtbl.find_or_add ~default s k

  let update
      (k:key)
      (update:'a option -> 'a)
      (d:'a t)
    : unit =
    Hashtbl.update
      ~f:update
      d
      k

  let size
      (s:'a t)
    : int =
    Hashtbl.length s

  let is_empty
      (s:'a t)
    : bool =
    Hashtbl.is_empty s

  let contains_key
      (key:D.t)
      (s:'a t)
    : bool =
    Hashtbl.mem s key

  let find_opt
      (key:D.t)
      (d:'a t)
    : 'a option =
    Hashtbl.find d key

  let fold
      (type a)
      (type b)
      (f:key -> a -> b -> b)
      (s:a t)
      (init:b)
    : b =
    Hashtbl.fold
      ~f:(fun ~key ~data acc -> f key data acc)
      ~init
      s

  let fold2 f a b x =
    let fold2' ka va x = fold (f ka va) b x in
    fold fold2' a x

  let iter f s =
    fold (fun k v () -> f k v) s ()

  let as_kvp_list d =
    fold
      (fun k v l -> (k,v)::l)
      d
      []

  let pp
      (type a)
      (k_pp:Format.formatter -> key -> unit)
      (v_pp:Format.formatter -> a -> unit)
      (f:Format.formatter)
      (d:a t)
    : unit =
    let rec pp_kvp f kvp =
      begin match kvp with
        | [] -> ()
        | [(k,v)] ->
          Format.fprintf
            f
            "(%a -> %a)"
            k_pp
            k
            v_pp
            v
        | (k,v)::l ->
          Format.fprintf
            f
            "(%a -> %a);%a"
            k_pp
            k
            v_pp
            v
            pp_kvp
            l

      end
    in
    let kvp = as_kvp_list d in
    Format.fprintf
      f
      "[";
    pp_kvp f kvp;
    Format.fprintf
      f
      "]"

  (*let exists f s =
    Hash_set.exists
      ~f
      s

  let as_list s =
    Hash_set.to_list
      s

  let union s1 s2 =
    Hash_set.union s1 s2

  let pp k_pp f s =
    Format.fprintf
      f
      "[";
    iter
      (fun k -> k_pp f k)
      s;
    Format.fprintf
      f
      "]"*)

  let copy
      (s:'a t)
    : 'a t =
    Hashtbl.copy s
end