【问题标题】:Ocaml double hash tableOcaml 双哈希表
【发布时间】:2021-02-15 10:41:58
【问题描述】:

我一直在寻找在 Ocaml 中使用双哈希表的方法,但找不到。我想做的是:

哈希:(("a",1), ("b",2), ...) ,其中提到的所有元素都不会重复,例如“a”不会再次出现,2也不会出现。

因此,如果我找到像 [1, "b", 2, "a",...] 这样的数组,我可以用它的键或值替换那些出现的数字和字母:["a",2,"b",1,...]

谢谢!

【问题讨论】:

  • 你能详细说明什么是“双哈希表”吗?这似乎与使用双散列解决冲突不同......

标签: ocaml hashtable double-hashing


【解决方案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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-16
    相关资源
    最近更新 更多