Commit 0426bd2f authored by Attila Lendvai's avatar Attila Lendvai

Fix: earlier keys take priority in ALIST-HASH-TABLE and PLIST-HASH-TABLE

Also record tests for the new behavior.

This is a semantic change, but more in line with how alists and
plists are defined in the CLHS.

See '15.6. Association Lists' and 'property list' in the Glossary.

Reported by Christoph Arenz to alexandria-devel in
'Wrong handling of duplicate keys in alist-hash-table and plist-hash-table'.
parent 06d2d364
......@@ -88,7 +88,7 @@ TABLE."
ALIST. Hash table is initialized using the HASH-TABLE-INITARGS."
(let ((table (apply #'make-hash-table hash-table-initargs)))
(dolist (cons alist)
(setf (gethash (car cons) table) (cdr cons)))
(ensure-gethash (car cons) table (cdr cons)))
table))
(defun plist-hash-table (plist &rest hash-table-initargs)
......@@ -97,5 +97,5 @@ PLIST. Hash table is initialized using the HASH-TABLE-INITARGS."
(let ((table (apply #'make-hash-table hash-table-initargs)))
(do ((tail plist (cddr tail)))
((not tail))
(setf (gethash (car tail) table) (cadr tail)))
(ensure-gethash (car tail) table (cadr tail)))
table))
......@@ -373,6 +373,15 @@
(hash-table-test table))))
(3 (a) (b) (c) t))
(deftest alist-hash-table.duplicate-keys
(let* ((alist '((0 a) (1 b) (0 c) (1 d) (2 e)))
(table (alist-hash-table alist)))
(list (hash-table-count table)
(gethash 0 table)
(gethash 1 table)
(gethash 2 table)))
(3 (a) (b) (e)))
(deftest plist-hash-table.1
(let* ((plist '(:a 1 :b 2 :c 3))
(table (plist-hash-table plist :test 'eq)))
......@@ -386,6 +395,15 @@
(hash-table-test table))))
(3 1 2 3 nil nil t))
(deftest plist-hash-table.duplicate-keys
(let* ((plist '(:a 1 :b 2 :a 3 :b 4 :c 5))
(table (plist-hash-table plist)))
(list (hash-table-count table)
(gethash :a table)
(gethash :b table)
(gethash :c table)))
(3 1 2 5))
;;;; Functions
(deftest disjoin.1
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment