1Stdlib.Hashtbl(3) OCaml library Stdlib.Hashtbl(3)
2
3
4
6 Stdlib.Hashtbl - no description
7
9 Module Stdlib.Hashtbl
10
12 Module Hashtbl
13 : (module Stdlib__hashtbl)
14
15
16
17
18
19
20
21
22
23 Generic interface
24 type ('a, 'b) t
25
26
27 The type of hash tables from type 'a to type 'b .
28
29
30
31 val create : ?random:bool -> int -> ('a, 'b) t
32
33
34 Hashtbl.create n creates a new, empty hash table, with initial size n .
35 For best results, n should be on the order of the expected number of
36 elements that will be in the table. The table grows as needed, so n is
37 just an initial guess.
38
39 The optional random parameter (a boolean) controls whether the internal
40 organization of the hash table is randomized at each execution of
41 Hashtbl.create or deterministic over all executions.
42
43 A hash table that is created with ~random:false uses a fixed hash func‐
44 tion ( Hashtbl.hash ) to distribute keys among buckets. As a conse‐
45 quence, collisions between keys happen deterministically. In Web-fac‐
46 ing applications or other security-sensitive applications, the deter‐
47 ministic collision patterns can be exploited by a malicious user to
48 create a denial-of-service attack: the attacker sends input crafted to
49 create many collisions in the table, slowing the application down.
50
51 A hash table that is created with ~random:true uses the seeded hash
52 function Hashtbl.seeded_hash with a seed that is randomly chosen at
53 hash table creation time. In effect, the hash function used is ran‐
54 domly selected among 2^{30} different hash functions. All these hash
55 functions have different collision patterns, rendering ineffective the
56 denial-of-service attack described above. However, because of random‐
57 ization, enumerating all elements of the hash table using Hashtbl.fold
58 or Hashtbl.iter is no longer deterministic: elements are enumerated in
59 different orders at different runs of the program.
60
61 If no ~random parameter is given, hash tables are created in non-random
62 mode by default. This default can be changed either programmatically
63 by calling Hashtbl.randomize or by setting the R flag in the OCAMLRUN‐
64 PARAM environment variable.
65
66
67 Before4.00.0 the random parameter was not present and all hash tables
68 were created in non-randomized mode.
69
70
71
72
73 val clear : ('a, 'b) t -> unit
74
75 Empty a hash table. Use reset instead of clear to shrink the size of
76 the bucket table to its initial size.
77
78
79
80 val reset : ('a, 'b) t -> unit
81
82 Empty a hash table and shrink the size of the bucket table to its ini‐
83 tial size.
84
85
86 Since 4.00.0
87
88
89
90 val copy : ('a, 'b) t -> ('a, 'b) t
91
92 Return a copy of the given hashtable.
93
94
95
96 val add : ('a, 'b) t -> 'a -> 'b -> unit
97
98
99 Hashtbl.add tbl x y adds a binding of x to y in table tbl . Previous
100 bindings for x are not removed, but simply hidden. That is, after per‐
101 forming Hashtbl.remove tbl x , the previous binding for x , if any, is
102 restored. (Same behavior as with association lists.)
103
104
105
106 val find : ('a, 'b) t -> 'a -> 'b
107
108
109 Hashtbl.find tbl x returns the current binding of x in tbl , or raises
110 Not_found if no such binding exists.
111
112
113
114 val find_opt : ('a, 'b) t -> 'a -> 'b option
115
116
117 Hashtbl.find_opt tbl x returns the current binding of x in tbl , or
118 None if no such binding exists.
119
120
121 Since 4.05
122
123
124
125 val find_all : ('a, 'b) t -> 'a -> 'b list
126
127
128 Hashtbl.find_all tbl x returns the list of all data associated with x
129 in tbl . The current binding is returned first, then the previous
130 bindings, in reverse order of introduction in the table.
131
132
133
134 val mem : ('a, 'b) t -> 'a -> bool
135
136
137 Hashtbl.mem tbl x checks if x is bound in tbl .
138
139
140
141 val remove : ('a, 'b) t -> 'a -> unit
142
143
144 Hashtbl.remove tbl x removes the current binding of x in tbl , restor‐
145 ing the previous binding if it exists. It does nothing if x is not
146 bound in tbl .
147
148
149
150 val replace : ('a, 'b) t -> 'a -> 'b -> unit
151
152
153 Hashtbl.replace tbl x y replaces the current binding of x in tbl by a
154 binding of x to y . If x is unbound in tbl , a binding of x to y is
155 added to tbl . This is functionally equivalent to Hashtbl.remove tbl x
156 followed by Hashtbl.add tbl x y .
157
158
159
160 val iter : ('a -> 'b -> unit) -> ('a, 'b) t -> unit
161
162
163 Hashtbl.iter f tbl applies f to all bindings in table tbl . f receives
164 the key as first argument, and the associated value as second argument.
165 Each binding is presented exactly once to f .
166
167 The order in which the bindings are passed to f is unspecified. How‐
168 ever, if the table contains several bindings for the same key, they are
169 passed to f in reverse order of introduction, that is, the most recent
170 binding is passed first.
171
172 If the hash table was created in non-randomized mode, the order in
173 which the bindings are enumerated is reproducible between successive
174 runs of the program, and even between minor versions of OCaml. For
175 randomized hash tables, the order of enumeration is entirely random.
176
177 The behavior is not defined if the hash table is modified by f during
178 the iteration.
179
180
181
182 val filter_map_inplace : ('a -> 'b -> 'b option) -> ('a, 'b) t -> unit
183
184
185 Hashtbl.filter_map_inplace f tbl applies f to all bindings in table tbl
186 and update each binding depending on the result of f . If f returns
187 None , the binding is discarded. If it returns Some new_val , the
188 binding is update to associate the key to new_val .
189
190 Other comments for Hashtbl.iter apply as well.
191
192
193 Since 4.03.0
194
195
196
197 val fold : ('a -> 'b -> 'c -> 'c) -> ('a, 'b) t -> 'c -> 'c
198
199
200 Hashtbl.fold f tbl init computes (f kN dN ... (f k1 d1 init)...) ,
201 where k1 ... kN are the keys of all bindings in tbl , and d1 ... dN are
202 the associated values. Each binding is presented exactly once to f .
203
204 The order in which the bindings are passed to f is unspecified. How‐
205 ever, if the table contains several bindings for the same key, they are
206 passed to f in reverse order of introduction, that is, the most recent
207 binding is passed first.
208
209 If the hash table was created in non-randomized mode, the order in
210 which the bindings are enumerated is reproducible between successive
211 runs of the program, and even between minor versions of OCaml. For
212 randomized hash tables, the order of enumeration is entirely random.
213
214 The behavior is not defined if the hash table is modified by f during
215 the iteration.
216
217
218
219 val length : ('a, 'b) t -> int
220
221
222 Hashtbl.length tbl returns the number of bindings in tbl . It takes
223 constant time. Multiple bindings are counted once each, so
224 Hashtbl.length gives the number of times Hashtbl.iter calls its first
225 argument.
226
227
228
229 val randomize : unit -> unit
230
231 After a call to Hashtbl.randomize() , hash tables are created in ran‐
232 domized mode by default: Hashtbl.create returns randomized hash tables,
233 unless the ~random:false optional parameter is given. The same effect
234 can be achieved by setting the R parameter in the OCAMLRUNPARAM envi‐
235 ronment variable.
236
237 It is recommended that applications or Web frameworks that need to pro‐
238 tect themselves against the denial-of-service attack described in
239 Hashtbl.create call Hashtbl.randomize() at initialization time.
240
241 Note that once Hashtbl.randomize() was called, there is no way to
242 revert to the non-randomized default behavior of Hashtbl.create . This
243 is intentional. Non-randomized hash tables can still be created using
244 Hashtbl.create ~random:false .
245
246
247 Since 4.00.0
248
249
250
251 val is_randomized : unit -> bool
252
253 return if the tables are currently created in randomized mode by
254 default
255
256
257 Since 4.03.0
258
259
260 type statistics = {
261 num_bindings : int ; (* Number of bindings present in the table.
262 Same value as returned by Hashtbl.length .
263 *)
264 num_buckets : int ; (* Number of buckets in the table.
265 *)
266 max_bucket_length : int ; (* Maximal number of bindings per bucket.
267 *)
268 bucket_histogram : int array ; (* Histogram of bucket sizes. This
269 array histo has length max_bucket_length + 1 . The value of histo.(i)
270 is the number of buckets whose size is i .
271 *)
272 }
273
274
275 Since 4.00.0
276
277
278
279 val stats : ('a, 'b) t -> statistics
280
281
282 Hashtbl.stats tbl returns statistics about the table tbl : number of
283 buckets, size of the biggest bucket, distribution of buckets by size.
284
285
286 Since 4.00.0
287
288
289
290
291 Iterators
292 val to_seq : ('a, 'b) t -> ('a * 'b) Seq.t
293
294 Iterate on the whole table. The order in which the bindings appear in
295 the sequence is unspecified. However, if the table contains several
296 bindings for the same key, they appear in reversed order of introduc‐
297 tion, that is, the most recent binding appears first.
298
299 The behavior is not defined if the hash table is modified during the
300 iteration.
301
302
303 Since 4.07
304
305
306
307 val to_seq_keys : ('a, 'b) t -> 'a Seq.t
308
309 Same as Seq.map fst (to_seq m)
310
311
312
313 Since 4.07
314
315
316
317 val to_seq_values : ('a, 'b) t -> 'b Seq.t
318
319 Same as Seq.map snd (to_seq m)
320
321
322
323 Since 4.07
324
325
326
327 val add_seq : ('a, 'b) t -> ('a * 'b) Seq.t -> unit
328
329 Add the given bindings to the table, using Hashtbl.add
330
331
332
333 Since 4.07
334
335
336
337 val replace_seq : ('a, 'b) t -> ('a * 'b) Seq.t -> unit
338
339 Add the given bindings to the table, using Hashtbl.replace
340
341
342
343 Since 4.07
344
345
346
347 val of_seq : ('a * 'b) Seq.t -> ('a, 'b) t
348
349 Build a table from the given bindings. The bindings are added in the
350 same order they appear in the sequence, using Hashtbl.replace_seq ,
351 which means that if two pairs have the same key, only the latest one
352 will appear in the table.
353
354
355 Since 4.07
356
357
358
359
360 Functorial interface
361 The functorial interface allows the use of specific comparison and hash
362 functions, either for performance/security concerns, or because keys
363 are not hashable/comparable with the polymorphic builtins.
364
365 For instance, one might want to specialize a table for integer keys:
366 module IntHash =
367 struct
368 type t = int
369 let equal i j = i=j
370 let hash i = i land max_int
371 end
372
373 module IntHashtbl = Hashtbl.Make(IntHash)
374
375 let h = IntHashtbl.create 17 in
376 IntHashtbl.add h 12 "hello"
377
378
379 This creates a new module IntHashtbl , with a new type 'a
380 IntHashtbl.t of tables from int to 'a . In this example, h contains
381 string values so its type is string IntHashtbl.t .
382
383 Note that the new type 'a IntHashtbl.t is not compatible with the type
384 ('a,'b) Hashtbl.t of the generic interface. For example, Hashtbl.length
385 h would not type-check, you must use IntHashtbl.length .
386
387 module type HashedType = sig end
388
389
390 The input signature of the functor Hashtbl.Make .
391
392
393 module type S = sig end
394
395
396 The output signature of the functor Hashtbl.Make .
397
398
399 module Make : functor (H : HashedType) -> sig end
400
401
402 Functor building an implementation of the hashtable structure. The
403 functor Hashtbl.Make returns a structure containing a type key of keys
404 and a type 'a t of hash tables associating data of type 'a to keys of
405 type key . The operations perform similarly to those of the generic
406 interface, but use the hashing and equality functions specified in the
407 functor argument H instead of generic equality and hashing. Since the
408 hash function is not seeded, the create operation of the result struc‐
409 ture always returns non-randomized hash tables.
410
411
412 module type SeededHashedType = sig end
413
414
415 The input signature of the functor Hashtbl.MakeSeeded .
416
417
418 Since 4.00.0
419
420
421 module type SeededS = sig end
422
423
424 The output signature of the functor Hashtbl.MakeSeeded .
425
426
427 Since 4.00.0
428
429
430 module MakeSeeded : functor (H : SeededHashedType) -> sig end
431
432
433 Functor building an implementation of the hashtable structure. The
434 functor Hashtbl.MakeSeeded returns a structure containing a type key of
435 keys and a type 'a t of hash tables associating data of type 'a to keys
436 of type key . The operations perform similarly to those of the generic
437 interface, but use the seeded hashing and equality functions specified
438 in the functor argument H instead of generic equality and hashing. The
439 create operation of the result structure supports the ~random optional
440 parameter and returns randomized hash tables if ~random:true is passed
441 or if randomization is globally on (see Hashtbl.randomize ).
442
443
444 Since 4.00.0
445
446
447
448
449 The polymorphic hash functions
450 val hash : 'a -> int
451
452
453 Hashtbl.hash x associates a nonnegative integer to any value of any
454 type. It is guaranteed that if x = y or Stdlib.compare x y = 0 , then
455 hash x = hash y . Moreover, hash always terminates, even on cyclic
456 structures.
457
458
459
460 val seeded_hash : int -> 'a -> int
461
462 A variant of Hashtbl.hash that is further parameterized by an integer
463 seed.
464
465
466 Since 4.00.0
467
468
469
470 val hash_param : int -> int -> 'a -> int
471
472
473 Hashtbl.hash_param meaningful total x computes a hash value for x ,
474 with the same properties as for hash . The two extra integer parameters
475 meaningful and total give more precise control over hashing. Hashing
476 performs a breadth-first, left-to-right traversal of the structure x ,
477 stopping after meaningful meaningful nodes were encountered, or total
478 nodes (meaningful or not) were encountered. If total as specified by
479 the user exceeds a certain value, currently 256, then it is capped to
480 that value. Meaningful nodes are: integers; floating-point numbers;
481 strings; characters; booleans; and constant constructors. Larger values
482 of meaningful and total means that more nodes are taken into account to
483 compute the final hash value, and therefore collisions are less likely
484 to happen. However, hashing takes longer. The parameters meaningful
485 and total govern the tradeoff between accuracy and speed. As default
486 choices, Hashtbl.hash and Hashtbl.seeded_hash take meaningful = 10 and
487 total = 100 .
488
489
490
491 val seeded_hash_param : int -> int -> int -> 'a -> int
492
493 A variant of Hashtbl.hash_param that is further parameterized by an
494 integer seed. Usage: Hashtbl.seeded_hash_param meaningful total seed x
495 .
496
497
498 Since 4.00.0
499
500
501
502
503
504OCamldoc 2020-09-01 Stdlib.Hashtbl(3)