all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* bug#63671: Add function to test equality of hash tables
@ 2023-05-23 19:32 Joseph Turner via Bug reports for GNU Emacs, the Swiss army knife of text editors
  2023-05-24  9:27 ` Mattias Engdegård
  0 siblings, 1 reply; 11+ messages in thread
From: Joseph Turner via Bug reports for GNU Emacs, the Swiss army knife of text editors @ 2023-05-23 19:32 UTC (permalink / raw)
  To: 63671

Hello!

Would y'all be open to adding something like this?

(defun hash-equal (hash1 hash2)
  "Return non-nil when the contents of HASH1 and HASH2 are equal.
Table values are compared using `equal' unless they are both hash
tables themselves, in which case `hash-equal' is used.
Does not compare equality predicates."
  (and (= (hash-table-count hash1)
          (hash-table-count hash2))
       (catch 'flag (maphash (lambda (key hash1-value)
                               (let ((hash2-value (gethash key hash2)))
                                 (or (if (and (hash-table-p hash1-value)
                                              (hash-table-p hash2-value))
                                         (hash-equal hash1-value hash2-value)
                                       (equal hash1-value hash2-value))
                                     (throw 'flag nil))))
                             hash1)
              t)))

Rudimentary test:

(let ((hash1 (make-hash-table))
      (hash2 (make-hash-table))
      (hash3 (make-hash-table))
      (hash4 (make-hash-table)))
  (puthash 'foo "foo" hash1)
  (puthash 'foo "foo" hash2)
  (puthash 'bar "foo" hash3)
  (puthash 'bar "foo" hash4)
  (puthash 'baz hash3 hash1)
  (puthash 'baz hash4 hash2)
  (hash-equal hash1 hash2))

We could use hash-table-test to compare predicates, perhaps
dependent on the presence of a 'compare-tests flag?

Best,

Joseph





^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2023-09-11 18:39 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-05-23 19:32 bug#63671: Add function to test equality of hash tables Joseph Turner via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-05-24  9:27 ` Mattias Engdegård
2023-05-24 17:27   ` Joseph Turner via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-05-24 20:01     ` Ihor Radchenko
2023-05-24 20:34     ` Mattias Engdegård
2023-05-25  2:44       ` Joseph Turner via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-05-25  6:31         ` Ihor Radchenko
2023-05-25  7:22           ` Joseph Turner via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-05-25  7:22           ` Joseph Turner via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-05-25  8:18           ` Mattias Engdegård
2023-09-11 18:39             ` Stefan Kangas

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.