emacs-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Emacs-diffs] map f37e265 1/4: Minor improvement in map-elt.


From: Nicolas Petton
Subject: [Emacs-diffs] map f37e265 1/4: Minor improvement in map-elt.
Date: Fri, 24 Apr 2015 17:40:27 +0000

branch: map
commit f37e265ea992f5799f1bf30a03509444c976df1d
Author: Nicolas Petton <address@hidden>
Commit: Nicolas Petton <address@hidden>

    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.
---
 lisp/emacs-lisp/map.el      |   13 +++++++++++--
 test/automated/map-tests.el |    1 +
 2 files changed, 12 insertions(+), 2 deletions(-)

diff --git a/lisp/emacs-lisp/map.el b/lisp/emacs-lisp/map.el
index 06fd7ad..2c95f35 100644
--- a/lisp/emacs-lisp/map.el
+++ b/lisp/emacs-lisp/map.el
@@ -48,11 +48,11 @@
   "Perform a lookup in MAP of KEY and return its associated value.
 If KEY is not found, return DEFAULT which defaults to nil.
 
-If MAP is a list, `assoc' is used to lookup KEY."
+If MAP is a list, `equal' is used to lookup KEY."
   (map--dispatch map
     :list (or (cdr (assoc key map)) default)
     :hash-table (gethash key map default)
-    :array (or (ignore-errors (elt map key)) default)))
+    :array (map--elt-array map key default)))
 
 (defmacro map-put (map key value)
   "In MAP, associate KEY with VALUE and return MAP.
@@ -252,6 +252,15 @@ form.
                   (setq index (1+ index))))
               map)))
 
+(defun map--elt-array (map key &optional default)
+  "Return the element of the arary MAP at the index KEY, or DEFAULT if nil."
+  (let ((len (seq-length map)))
+    (or (and (>= key 0)
+             (<= key len)
+             (seq-elt map key))
+        default)))
+
+
 (defun map--delete-alist (map key)
   "Return MAP with KEY removed."
   (seq-remove (lambda (pair)
diff --git a/test/automated/map-tests.el b/test/automated/map-tests.el
index 9a0d99e..f41cd70 100644
--- a/test/automated/map-tests.el
+++ b/test/automated/map-tests.el
@@ -59,6 +59,7 @@ Evaluate BODY for each created map.
     (assert (= 3 (map-elt map 0)))
     (assert (= 4 (map-elt map 1)))
     (assert (= 5 (map-elt map 2)))
+    (assert (null (map-elt map -1)))
     (assert (null (map-elt map 4)))))
 
 (ert-deftest test-map-elt-default ()



reply via email to

[Prev in Thread] Current Thread [Next in Thread]