Elisp: Test Hash Tables Equality 🚀

By Xah Lee. Date: .
(defun xah-hashtable-equal (HashTable1 HashTable2)
  "Return t if two hashtable are same, else nil.
Same means
• count of keys is same
• keys are same by `equal'
• values are same by `equal'

URL `http://xahlee.info/emacs/emacs/elisp_hash_table_equality.html'
Version: 2024-04-20 2024-04-30"
  (interactive)
  (let ((xuniq :3tFcNwZR2Y))
    (catch 'TAG
      (when (not (eq (hash-table-count HashTable1) (hash-table-count HashTable2))) (throw 'TAG nil))
      (maphash
       (lambda (kk vv)
         (when (equal xuniq (gethash kk HashTable2 xuniq)) (throw 'TAG nil))
         (when (not (equal vv (gethash kk HashTable2))) (throw 'TAG nil)))
       HashTable1)
      t)))

test:


(setq x-a
      #s(hash-table
         size 30
         test equal
         data (
               "aa" 3
               "bb" 9
               "cc" 5 )))

(setq x-b
      #s(hash-table
         size 30
         test equal
         data (
               "bb" 9
               "aa" 3
               "cc" 5 )))

(xah-print-hash x-a)
(xah-print-hash x-b)

(xah-hashtable-equal x-a x-b )

Emacs Lisp Hash Table