aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--lisp/emacs-lisp/map.el13
-rw-r--r--test/automated/map-tests.el1
2 files changed, 12 insertions, 2 deletions
diff --git a/lisp/emacs-lisp/map.el b/lisp/emacs-lisp/map.el
index 06fd7ad2957..2c95f35569c 100644
--- a/lisp/emacs-lisp/map.el
+++ b/lisp/emacs-lisp/map.el
@@ -48,11 +48,11 @@
48 "Perform a lookup in MAP of KEY and return its associated value. 48 "Perform a lookup in MAP of KEY and return its associated value.
49If KEY is not found, return DEFAULT which defaults to nil. 49If KEY is not found, return DEFAULT which defaults to nil.
50 50
51If MAP is a list, `assoc' is used to lookup KEY." 51If MAP is a list, `equal' is used to lookup KEY."
52 (map--dispatch map 52 (map--dispatch map
53 :list (or (cdr (assoc key map)) default) 53 :list (or (cdr (assoc key map)) default)
54 :hash-table (gethash key map default) 54 :hash-table (gethash key map default)
55 :array (or (ignore-errors (elt map key)) default))) 55 :array (map--elt-array map key default)))
56 56
57(defmacro map-put (map key value) 57(defmacro map-put (map key value)
58 "In MAP, associate KEY with VALUE and return MAP. 58 "In MAP, associate KEY with VALUE and return MAP.
@@ -252,6 +252,15 @@ form.
252 (setq index (1+ index)))) 252 (setq index (1+ index))))
253 map))) 253 map)))
254 254
255(defun map--elt-array (map key &optional default)
256 "Return the element of the arary MAP at the index KEY, or DEFAULT if nil."
257 (let ((len (seq-length map)))
258 (or (and (>= key 0)
259 (<= key len)
260 (seq-elt map key))
261 default)))
262
263
255(defun map--delete-alist (map key) 264(defun map--delete-alist (map key)
256 "Return MAP with KEY removed." 265 "Return MAP with KEY removed."
257 (seq-remove (lambda (pair) 266 (seq-remove (lambda (pair)
diff --git a/test/automated/map-tests.el b/test/automated/map-tests.el
index 9a0d99e04de..f41cd70c4c5 100644
--- a/test/automated/map-tests.el
+++ b/test/automated/map-tests.el
@@ -59,6 +59,7 @@ Evaluate BODY for each created map.
59 (assert (= 3 (map-elt map 0))) 59 (assert (= 3 (map-elt map 0)))
60 (assert (= 4 (map-elt map 1))) 60 (assert (= 4 (map-elt map 1)))
61 (assert (= 5 (map-elt map 2))) 61 (assert (= 5 (map-elt map 2)))
62 (assert (null (map-elt map -1)))
62 (assert (null (map-elt map 4))))) 63 (assert (null (map-elt map 4)))))
63 64
64(ert-deftest test-map-elt-default () 65(ert-deftest test-map-elt-default ()