Reviewed By: ngorogiannis Differential Revision: D24306054 fbshipit-source-id: 3ee9f8752master
parent
08da86ae62
commit
9a238a76da
@ -0,0 +1,43 @@
|
||||
(*
|
||||
* Copyright (c) Facebook, Inc. and its affiliates.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*)
|
||||
|
||||
open! NS0
|
||||
|
||||
(** Hash tables *)
|
||||
|
||||
include HashTable_intf
|
||||
|
||||
module Make (Key : HashedType) = struct
|
||||
include CCHashtbl.Make (Key)
|
||||
|
||||
let create ?(size = 0) () = create size
|
||||
let set tbl ~key ~data = replace tbl key data
|
||||
|
||||
let add_multi tbl ~key ~data =
|
||||
update tbl ~k:key ~f:(fun _ -> function
|
||||
| None -> Some [data] | Some datas -> Some (data :: datas) )
|
||||
|
||||
let find_exn = find
|
||||
let find = find_opt
|
||||
|
||||
let find_or_add tbl key ~default =
|
||||
let found = ref None in
|
||||
update tbl ~k:key ~f:(fun _ -> function
|
||||
| None ->
|
||||
let v = default () in
|
||||
found := Some v ;
|
||||
Some v
|
||||
| Some v ->
|
||||
found := Some v ;
|
||||
Some v ) ;
|
||||
Option.value_exn !found
|
||||
|
||||
let iteri tbl ~f = iter (fun key data -> f ~key ~data) tbl
|
||||
|
||||
let fold tbl ~init ~f =
|
||||
fold (fun key data acc -> f ~key ~data acc) tbl init
|
||||
end
|
@ -0,0 +1,13 @@
|
||||
(*
|
||||
* Copyright (c) Facebook, Inc. and its affiliates.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*)
|
||||
|
||||
open! NS0
|
||||
|
||||
(** Hash tables *)
|
||||
|
||||
include module type of HashTable_intf
|
||||
module Make (Key : HashedType) : S with type key = Key.t
|
@ -0,0 +1,23 @@
|
||||
(*
|
||||
* Copyright (c) Facebook, Inc. and its affiliates.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*)
|
||||
|
||||
open! NS0
|
||||
|
||||
module type HashedType = Stdlib.Hashtbl.HashedType
|
||||
|
||||
module type S = sig
|
||||
include CCHashtbl.S
|
||||
|
||||
val create : ?size:int -> unit -> 'a t
|
||||
val set : 'a t -> key:key -> data:'a -> unit
|
||||
val add_multi : 'a list t -> key:key -> data:'a -> unit
|
||||
val find_exn : 'a t -> key -> 'a
|
||||
val find : 'a t -> key -> 'a option
|
||||
val find_or_add : 'a t -> key -> default:(unit -> 'a) -> 'a
|
||||
val iteri : 'a t -> f:(key:key -> data:'a -> unit) -> unit
|
||||
val fold : 'a t -> init:'s -> f:(key:key -> data:'a -> 's -> 's) -> 's
|
||||
end
|
Loading…
Reference in new issue