@@ -317,185 +317,3 @@ module Make(X : Hashtbl.HashedType)
317
317
Format. fprintf fmt " }@]"
318
318
end
319
319
320
- (* * {2 Default Table} *)
321
-
322
- module type DEFAULT = sig
323
- type key
324
-
325
- type 'a t
326
- (* * A hashtable for keys of type [key] and values of type ['a] *)
327
-
328
- val create : ?size : int -> 'a -> 'a t
329
- (* * [create d] makes a new table that maps every key to [d] by default.
330
- @param size optional size of the initial table *)
331
-
332
- val create_with : ?size : int -> (key -> 'a ) -> 'a t
333
- (* * Similar to [create d] but here [d] is a function called to obtain a
334
- new default value for each distinct key. Useful if the default
335
- value is stateful. *)
336
-
337
- val get : 'a t -> key -> 'a
338
- (* * Unfailing retrieval (possibly returns the default value) *)
339
-
340
- val set : 'a t -> key -> 'a -> unit
341
- (* * Replace the current binding for this key *)
342
-
343
- val remove : 'a t -> key -> unit
344
- (* * Remove the binding for this key. If [get tbl k] is called later, the
345
- default value for the table will be returned *)
346
-
347
- val to_seq : 'a t -> (key * 'a ) sequence
348
- (* * Pairs of [(elem, count)] for all elements whose count is positive *)
349
- end
350
-
351
- module MakeDefault (X : Hashtbl.HashedType ) = struct
352
- type key = X .t
353
-
354
- module T = Hashtbl. Make (X )
355
-
356
- type 'a t = {
357
- default : key -> 'a ;
358
- tbl : 'a T .t
359
- }
360
-
361
- let create_with ?(size =32 ) default = { default; tbl= T. create size }
362
-
363
- let create ?size d = create_with ?size (fun _ -> d)
364
-
365
- let get tbl k =
366
- try T. find tbl.tbl k
367
- with Not_found ->
368
- let v = tbl.default k in
369
- T. add tbl.tbl k v;
370
- v
371
-
372
- let set tbl k v = T. replace tbl.tbl k v
373
-
374
- let remove tbl k = T. remove tbl.tbl k
375
-
376
- let to_seq tbl k = T. iter (fun key v -> k (key,v)) tbl.tbl
377
- end
378
-
379
- (* * {2 Count occurrences using a Hashtbl} *)
380
-
381
- module type COUNTER = sig
382
- type elt
383
- (* * Elements that are to be counted *)
384
-
385
- type t
386
-
387
- val create : int -> t
388
- (* * A counter maps elements to natural numbers (the number of times this
389
- element occurred) *)
390
-
391
- val incr : t -> elt -> unit
392
- (* * Increment the counter for the given element *)
393
-
394
- val incr_by : t -> int -> elt -> unit
395
- (* * Add or remove several occurrences at once. [incr_by c x n]
396
- will add [n] occurrences of [x] if [n>0],
397
- and remove [abs n] occurrences if [n<0]. *)
398
-
399
- val get : t -> elt -> int
400
- (* * Number of occurrences for this element *)
401
-
402
- val decr : t -> elt -> unit
403
- (* * Remove one occurrence of the element
404
- @since 0.14 *)
405
-
406
- val length : t -> int
407
- (* * Number of distinct elements
408
- @since 0.14 *)
409
-
410
- val add_seq : t -> elt sequence -> unit
411
- (* * Increment each element of the sequence *)
412
-
413
- val of_seq : elt sequence -> t
414
- (* * [of_seq s] is the same as [add_seq (create ())] *)
415
-
416
- val to_seq : t -> (elt * int ) sequence
417
- (* * [to_seq tbl] returns elements of [tbl] along with their multiplicity
418
- @since 0.14 *)
419
-
420
- val add_list : t -> (elt * int ) list -> unit
421
- (* * Similar to {!add_seq}
422
- @since 0.14 *)
423
-
424
- val of_list : (elt * int ) list -> t
425
- (* * Similar to {!of_seq}
426
- @since 0.14 *)
427
-
428
- val to_list : t -> (elt * int ) list
429
- (* * @since 0.14 *)
430
- end
431
-
432
- module MakeCounter (X : Hashtbl.HashedType )
433
- : COUNTER
434
- with type elt = X. t
435
- and type t = int Hashtbl. Make (X ).t
436
- = struct
437
- type elt = X .t
438
-
439
- module T = Hashtbl. Make (X )
440
-
441
- type t = int T .t
442
-
443
- let create size = T. create size
444
-
445
- let get tbl x = try T. find tbl x with Not_found -> 0
446
-
447
- let length = T. length
448
-
449
- let incr tbl x =
450
- let n = get tbl x in
451
- T. replace tbl x (n+ 1 )
452
-
453
- let incr_by tbl n x =
454
- let n' = get tbl x in
455
- if n' + n < = 0
456
- then T. remove tbl x
457
- else T. replace tbl x (n+ n')
458
-
459
- let decr tbl x = incr_by tbl 1 x
460
-
461
- let add_seq tbl seq = seq (incr tbl)
462
-
463
- let of_seq seq =
464
- let tbl = create 32 in
465
- add_seq tbl seq;
466
- tbl
467
-
468
- let to_seq tbl yield = T. iter (fun x i -> yield (x,i)) tbl
469
-
470
- let add_list tbl l =
471
- List. iter (fun (x ,i ) -> incr_by tbl i x) l
472
-
473
- let of_list l =
474
- let tbl = create 32 in
475
- add_list tbl l;
476
- tbl
477
-
478
- let to_list tbl =
479
- T. fold (fun x i acc -> (x,i) :: acc) tbl []
480
- end
481
-
482
- (* $inject
483
- module C = MakeCounter(CCInt)
484
-
485
- let list_int = Q.(make
486
- ~print:Print.(list (pair int int))
487
- ~small:List.length
488
- ~shrink:Shrink.(list ?shrink:None)
489
- Gen.(list small_int >|= List.map (fun i->i,1))
490
- )
491
-
492
- *)
493
-
494
- (* $Q
495
- list_int (fun l -> \
496
- l |> C.of_list |> C.to_list |> List.length = \
497
- (l |> CCList.sort_uniq |> List.length))
498
- list_int (fun l -> \
499
- l |> C.of_list |> C.to_seq |> Sequence.fold (fun n(_,i)->i+n) 0 = \
500
- List.fold_left (fun n (_,_) ->n+1) 0 l)
501
- *)
0 commit comments