我一直在寻找一种在 Ocaml 中使用双哈希表的方法,但找不到。我想做的是:
Hash: (("a",1), ("b",2), ...)
,其中提到的所有元素都不会重复,例如“a”不会再次出现,2 也不会出现。
因此,如果我找到一个像我这样的数组,[1, "b", 2, "a",...]
我可以用它的键或值替换那些出现的数字和字母:["a",2,"b",1,...]
.
谢谢!
我一直在寻找一种在 Ocaml 中使用双哈希表的方法,但找不到。我想做的是:
Hash: (("a",1), ("b",2), ...)
,其中提到的所有元素都不会重复,例如“a”不会再次出现,2 也不会出现。
因此,如果我找到一个像我这样的数组,[1, "b", 2, "a",...]
我可以用它的键或值替换那些出现的数字和字母:["a",2,"b",1,...]
.
谢谢!
库容器数据具有 CCBijections。
如果你想要一个可变版本,你可以将两个哈希表与
module Table: sig
type (!'left, !'right) t
(* or before OCaml 4.12:
type (!'left, !'right) t
*)
val add: 'left ->'right -> ('left,'right) t -> unit
...
end = struct
type ('a,'b) t = {left:('a,'b) Hashtbl.t; right:('b,'a) Hashtbl.t }
let add left right tbl =
Hashtbl.add tbl.left left right;
Hashtbl.add tbl.right right left
...
end