在Lisp中复制哈希表 [英] Copy Hash Table in Lisp

查看:79
本文介绍了在Lisp中复制哈希表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近一直在使用Common Lisp中的哈希表。我一直想知道如何制作包含与第一个相同的所有值的哈希表的单独副本。有官方的方法吗?如果没有,您可以举一个使用maphash的例子吗?

I have recently been working with hash tables in Common Lisp. I have been wondering how to make a separate copy of a hash table containing all the same values as the first. Is there an official way to do this? If not, can you give me an example using maphash?

推荐答案

clhs 没有列出复制表函数,我认为maphash是可行的方法。

As clhs does not list a copy table function I'd assume that maphash is the way to go.

(defun copy-table (table)
  (let ((new-table (make-hash-table
                    :test (hash-table-test table)
                    :size (hash-table-size table))))
    (maphash #'(lambda(key value)
                 (setf (gethash key new-table) value))
             table)
    new-table))

(let ((table (make-hash-table)))
  (mapcar #'(lambda(arg argg)
              (setf (gethash arg table) argg))
          '(1 2 3 4) '(a b c d))
  (format t "~a~%" table)
  (format t "~a~%" (copy-table table)))

#<HASH-TABLE :TEST EQL :COUNT 4 {10063C7F13}>
#<HASH-TABLE :TEST EQL :COUNT 4 {10063C86D3}>

此函数未考虑哈希表的特殊配置,但以示例为例

This function however does not take special configurations of the hashtable into account, but it should suffice as an example.

这篇关于在Lisp中复制哈希表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆