aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNicolas Petton2015-04-24 19:06:27 +0200
committerNicolas Petton2015-04-24 19:10:45 +0200
commitf37e265ea992f5799f1bf30a03509444c976df1d (patch)
tree18946d61e195148201b9aa18e6e6550a6bd8dd53
parent89baf163324c6820ca17e91cda9dc8b162a59eab (diff)
downloademacs-f37e265ea992f5799f1bf30a03509444c976df1d.tar.gz
emacs-f37e265ea992f5799f1bf30a03509444c976df1d.zip
Minor improvement in map-elt.
* lisp/emacs-lisp/map.el (map-elt): Do not use `ignore-errors' when doing a lookup in arrays, but check the boundaries of the array instead. * test/automated/map-tests.el: Adds a test for `map-elt' with arrays and a negative integer as key.
-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 ()