Module Core_kernel__.Hashtbl_intf

Usage

For many students of OCaml, using hashtables is complicated by the functors. Here are a few tips:

  1. For a list of hashtable functions see Core_kernel.Hashtbl_intf.S.
  2. To create a hashtable with string keys use String.Table:

    let table = String.Table.create () ~size:4 in
    List.iter ~f:(fun (key, data) -> Hashtbl.set table ~key ~data)
      [ ("A", 1); ("B", 2); ("C", 3); ];
    Hashtbl.find table "C"

    Here 4 need only be a guess at the hashtable's future size. There are other similar pre-made hashtables, e.g., Int63.Table or Host_and_port.Table.

  3. To create a hashtable with a custom key type use Hashable:

    module Key = struct
      module T = struct
        type t = String.t * Int63.t [@@deriving compare, hash, sexp]
      end
      include T
      include Hashable.Make (T)
    end
    let table = Key.Table.create () ~size:4 in
    List.iter ~f:(fun (key, data) -> Hashtbl.set table ~key ~data)
      [ (("pi", Int63.zero), 3.14159);
        (("e", Int63.minus_one), 2.71828);
        (("Euler", Int63.one), 0.577215);
      ];
    Hashtbl.find table ("pi", Int63.zero)

    Performance may improve if you define equal and hash explicitly, e.g.:

    let equal (x, y) (x', y') = String.(=) x x' && Int63.(=) y y'
    let hash (x, y) = String.hash x + Int63.hash y * 65599
module Binable = Core_kernel__.Binable0
module Hashtbl = Base.Hashtbl
module type Key_plain = Hashtbl.Key
module Hashable = Base.Hashable
module type Key = sig ... end
module type Key_binable = sig ... end
module type Creators = Hashtbl.Private.Creators_generic
module type Accessors = Hashtbl.Accessors
module type Multi = Hashtbl.Multi
type ('key, 'data, 'z) create_options_with_first_class_module = ('key'data'z) Hashtbl.create_options
type ('key, 'data, 'z) create_options_without_hashable = ('key'data'z) Hashtbl.Private.create_options_without_first_class_module
type ('key, 'data, 'z) create_options_with_hashable = ?⁠growth_allowed:Core_kernel__.Import.bool -> ?⁠size:Core_kernel__.Import.int -> hashable:'key Hashable.t -> 'z
module type For_deriving = Base.Hashtbl.For_deriving
module type S_plain = sig ... end
module type S = sig ... end
module type S_binable = sig ... end
module type Hashtbl = sig ... end