package logtk

  1. Overview
  2. Docs
Core types and algorithms for logic

Install

Dune Dependency

Authors

Maintainers

Sources

1.6.tar.gz
md5=97cdb2f90468e9e27c7bbe3b4fb160bb
sha512=fee73369f673a91dfa9e265fc69be08b32235e10a495f3af6477d404fcd01e3452a0d012b150f3d7f97c00af2f6045019ad039164bf698f70d771231cc4efe5d

doc/src/logtk/Hashcons.ml.html

Source file Hashcons.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

(* This file is free software, part of Zipperposition. See file "license" for more details. *)

(** {1 Hashconsing} *)

module type HashedType = sig
  type t
  val equal : t -> t -> bool
  val hash : t -> int
  val tag : int -> t -> unit
end

module type S = sig
  type elt
  (** Hashconsed objects *)

  val hashcons : elt -> elt
  (** Hashcons the elements *)

  val mem : elt -> bool
  (** Is the element present in this table? *)

  val fresh_unique_id : unit -> int
  (** Unique ID that will never occur again in this table (modulo 2^63...) *)

  val stats : unit -> int*int*int*int*int*int
end

module Make(X : HashedType) = struct
  module H = Weak.Make(X)
  type elt = X.t

  let count_ = ref 0
  let tbl : H.t = H.create 4_096

  let hashcons x =
    let x' = H.merge tbl x in
    if x == x' then (
      X.tag !count_ x;
      incr count_;
    );
    x'

  let[@inline] mem x = H.mem tbl x

  let fresh_unique_id () =
    let x = !count_ in
    incr count_;
    x

  let stats () = H.stats tbl
end[@@inline]

module MakeNonWeak(X : HashedType) = struct
  module H = Hashtbl.Make(X)
  type elt = X.t

  let count_ = ref 0
  let tbl : elt H.t = H.create 1024

  let hashcons x =
    try H.find tbl x
    with Not_found ->
      X.tag !count_ x;
      incr count_;
      H.add tbl x x;
      x

  let mem x = H.mem tbl x

  let fresh_unique_id () =
    let x = !count_ in
    incr count_;
    x

  let stats () =
    let stat = H.stats tbl in
    let open Hashtbl in
    let sum_buck = Array.fold_left (+) 0 stat.bucket_histogram in
    let min_buck = Array.fold_left min max_int stat.bucket_histogram in
    stat.num_buckets, stat.num_bindings, sum_buck,
    min_buck, 0, stat.max_bucket_length
end
OCaml

Innovation. Community. Security.