Search code examples
copyhashtablecommon-lisp

Copy Hash Table in Lisp


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?


Solution

  • 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.