Module type Hashtbl.Creators

type ('a, 'b) t

Creators

val create : ?growth_allowed:bool -> ?size:int -> (module Base__Hashtbl_intf.Key.S with type t = 'a) -> ('a'b) t

The module you pass to create must have a type that is hashable, sexpable, and comparable.

Example:

        Hashtbl.create (module Int);;
        - : (int, '_a) Hashtbl.t = <abstr>;;
val of_alist : ?growth_allowed:bool -> ?size:int -> (module Base__Hashtbl_intf.Key.S with type t = 'a) -> ('a * 'b) list -> [ `Ok of ('a'b) t | `Duplicate_key of 'a ]

Example:

         Hashtbl.of_alist (module Int) [(3, "something"); (2, "whatever")]
         - : [ `Duplicate_key of int | `Ok of (int, string) Hashtbl.t ] = `Ok <abstr>
val of_alist_report_all_dups : ?growth_allowed:bool -> ?size:int -> (module Base__Hashtbl_intf.Key.S with type t = 'a) -> ('a * 'b) list -> [ `Ok of ('a'b) t | `Duplicate_keys of 'a list ]

Whereas of_alist will report Duplicate_key no matter how many dups there are in your list, of_alist_report_all_dups will report each and every duplicate entry.

For example:

        Hashtbl.of_alist (module Int) [(1, "foo"); (1, "bar"); (2, "foo"); (2, "bar")];;
        - : [ `Duplicate_key of int | `Ok of (int, string) Hashtbl.t ] = `Duplicate_key 1

        Hashtbl.of_alist_report_all_dups (module Int) [(1, "foo"); (1, "bar"); (2, "foo"); (2, "bar")];;
        - : [ `Duplicate_keys of int list | `Ok of (int, string) Hashtbl.t ] = `Duplicate_keys [1; 2]
val of_alist_or_error : ?growth_allowed:bool -> ?size:int -> (module Base__Hashtbl_intf.Key.S with type t = 'a) -> ('a * 'b) list -> ('a'b) t Or_error.t
val of_alist_exn : ?growth_allowed:bool -> ?size:int -> (module Base__Hashtbl_intf.Key.S with type t = 'a) -> ('a * 'b) list -> ('a'b) t
val of_alist_multi : ?growth_allowed:bool -> ?size:int -> (module Base__Hashtbl_intf.Key.S with type t = 'a) -> ('a * 'b) list -> ('a'b list) t

Creates a "multi" hashtable, i.e., a hashtable where each key points to a list potentially containing multiple values. So instead of short-circuiting with a `Duplicate_key variant on duplicates, as in of_alist, of_alist_multi folds those values into a list for the given key:

      let h = Hashtbl.of_alist_multi (module Int) [(1, "a"); (1, "b"); (2, "c"); (2, "d")];;
      val h : (int, string list) Hashtbl.t = <abstr>

      Hashtbl.find_exn h 1;;
      - : string list = ["b"; "a"]
val create_mapped : ?growth_allowed:bool -> ?size:int -> (module Base__Hashtbl_intf.Key.S with type t = 'a) -> get_key:('r -> 'a) -> get_data:('r -> 'b) -> 'r list -> [ `Ok of ('a'b) t | `Duplicate_keys of 'a list ]

Applies the get_key and get_data functions to the 'r list to create the initial keys and values, respectively, for the new hashtable.

create_mapped get_key get_data [x1;...;xn]
= of_alist [get_key x1, get_data x1; ...; get_key xn, get_data xn]

Example:

        let h =
          Hashtbl.create_mapped (module Int)
            ~get_key:(fun x -> x)
            ~get_data:(fun x -> x + 1)
           [1; 2; 3];;
        val h : [ `Duplicate_keys of int list | `Ok of (int, int) Hashtbl.t ] = `Ok <abstr>

        let h =
          match h with
          | `Ok x -> x
          | `Duplicate_keys _ -> failwith ""
        in
        Hashtbl.find_exn h 1;;
        - : int = 2
val create_with_key : ?growth_allowed:bool -> ?size:int -> (module Base__Hashtbl_intf.Key.S with type t = 'a) -> get_key:('r -> 'a) -> 'r list -> [ `Ok of ('a'r) t | `Duplicate_keys of 'a list ]
create_with_key ~get_key [x1;...;xn]
= of_alist [get_key x1, x1; ...; get_key xn, xn] 
val create_with_key_or_error : ?growth_allowed:bool -> ?size:int -> (module Base__Hashtbl_intf.Key.S with type t = 'a) -> get_key:('r -> 'a) -> 'r list -> ('a'r) t Or_error.t
val create_with_key_exn : ?growth_allowed:bool -> ?size:int -> (module Base__Hashtbl_intf.Key.S with type t = 'a) -> get_key:('r -> 'a) -> 'r list -> ('a'r) t
val group : ?growth_allowed:bool -> ?size:int -> (module Base__Hashtbl_intf.Key.S with type t = 'a) -> get_key:('r -> 'a) -> get_data:('r -> 'b) -> combine:('b -> 'b -> 'b) -> 'r list -> ('a'b) t

Like create_mapped, applies the get_key and get_data functions to the 'r list to create the initial keys and values, respectively, for the new hashtable -- and then, like add_multi, folds together values belonging to the same keys. Here, though, the function used for the folding is given by combine (instead of just being a cons).

Example:

         Hashtbl.group (module Int)
           ~get_key:(fun x -> x / 2)
           ~get_data:(fun x -> x)
           ~combine:(fun x y -> x * y)
            [ 1; 2; 3; 4]
         |> Hashtbl.to_alist;;
         - : (int * int) list = [(2, 4); (1, 6); (0, 1)]