forked from rescript-lang/rescript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhashtblLabels.ml
130 lines (101 loc) · 4.03 KB
/
hashtblLabels.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
(**************************************************************************)
(* *)
(* OCaml *)
(* *)
(* Xavier Leroy, projet Cristal, INRIA Rocquencourt *)
(* *)
(* Copyright 1996 Institut National de Recherche en Informatique et *)
(* en Automatique. *)
(* *)
(* All rights reserved. This file is distributed under the terms of *)
(* the GNU Lesser General Public License version 2.1, with the *)
(* special exception on linking described in the file LICENSE. *)
(* *)
(**************************************************************************)
(* Hash tables *)
type ('a, 'b) t = ('a,'b) Hashtbl.t
let {create;
clear;reset;copy;add;find;find_opt;find_all;mem;remove;replace;iter;filter_map_inplace;fold;length;randomize;is_randomized;stats;hash;seeded_hash;hash_param;seeded_hash_param} = (module Hashtbl)
let add tbl ~key ~data = add tbl key data
let replace tbl ~key ~data = replace tbl key data
let iter ~f tbl = iter (fun key data -> f ~key ~data) tbl
let filter_map_inplace ~f tbl =
filter_map_inplace (fun key data -> f ~key ~data) tbl
let fold ~f tbl ~init =
fold (fun key data acc -> f ~key ~data acc) tbl init
type statistics = Hashtbl.statistics = {
num_bindings: int;
num_buckets: int;
max_bucket_length: int;
bucket_histogram: int array
}
(* Functorial interface *)
module type HashedType = Hashtbl.HashedType
module type SeededHashedType = Hashtbl.SeededHashedType
module type S =
sig
type key
and 'a t
val create : int -> 'a t
val clear : 'a t -> unit
val reset : 'a t -> unit
val copy : 'a t -> 'a t
val add : 'a t -> key:key -> data:'a -> unit
val remove : 'a t -> key -> unit
val find : 'a t -> key -> 'a
val find_opt: 'a t -> key -> 'a option
val find_all : 'a t -> key -> 'a list
val replace : 'a t -> key:key -> data:'a -> unit
val mem : 'a t -> key -> bool
val iter : f:(key:key -> data:'a -> unit) -> 'a t -> unit
val filter_map_inplace:
f:(key:key -> data:'a -> 'a option) -> 'a t -> unit
val fold :
f:(key:key -> data:'a -> 'b -> 'b) ->
'a t -> init:'b -> 'b
val length : 'a t -> int
val stats: 'a t -> statistics
end
module type SeededS =
sig
type key
and 'a t
val create : ?random:bool -> int -> 'a t
val clear : 'a t -> unit
val reset : 'a t -> unit
val copy : 'a t -> 'a t
val add : 'a t -> key:key -> data:'a -> unit
val remove : 'a t -> key -> unit
val find : 'a t -> key -> 'a
val find_opt : 'a t -> key -> 'a option
val find_all : 'a t -> key -> 'a list
val replace : 'a t -> key:key -> data:'a -> unit
val mem : 'a t -> key -> bool
val iter : f:(key:key -> data:'a -> unit) -> 'a t -> unit
val filter_map_inplace:
f:(key:key -> data:'a -> 'a option) -> 'a t -> unit
val fold :
f:(key:key -> data:'a -> 'b -> 'b) ->
'a t -> init:'b -> 'b
val length : 'a t -> int
val stats: 'a t -> statistics
end
module MakeSeeded(H: SeededHashedType): (SeededS with type key = H.t) = struct
include Hashtbl.MakeSeeded(H)
let add tbl ~key ~data = add tbl key data
let replace tbl ~key ~data = replace tbl key data
let iter ~f tbl = iter (fun key data -> f ~key ~data) tbl
let filter_map_inplace ~f tbl =
filter_map_inplace (fun key data -> f ~key ~data) tbl
let fold ~f tbl ~init =
fold (fun key data acc -> f ~key ~data acc) tbl init
end
module Make(H: HashedType): (S with type key = H.t) =
struct
include MakeSeeded(struct
type t = H.t
let equal = H.equal
let hash (_seed: int) x = H.hash x
end)
let create sz = create ~random:false sz
end