emacs-diffs
[Top][All Lists]
Advanced

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

[Emacs-diffs] map d75151a 3/4: Do not signal an error when trying to del


From: Nicolas Petton
Subject: [Emacs-diffs] map d75151a 3/4: Do not signal an error when trying to delete a key from an array
Date: Fri, 24 Apr 2015 17:40:28 +0000

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

    Do not signal an error when trying to delete a key from an array
    
    * lisp/emacs-lisp/map.el (map-delete): When map is an array, check if
    the key is present to avoid signaling an error.
    
    * test/automated/map-tests.el: Add a test for deleting non-existing
    keys from maps.
---
 lisp/emacs-lisp/map.el      |   11 +++++++++--
 test/automated/map-tests.el |    5 ++++-
 2 files changed, 13 insertions(+), 3 deletions(-)

diff --git a/lisp/emacs-lisp/map.el b/lisp/emacs-lisp/map.el
index 621c37f..087ab28 100644
--- a/lisp/emacs-lisp/map.el
+++ b/lisp/emacs-lisp/map.el
@@ -73,7 +73,7 @@ If MAP is an array, store nil at the index KEY."
      (map--dispatch (m ,map m)
        :list (setq ,map (map--delete-alist m ,key))
        :hash-table (remhash ,key m)
-       :array (aset m ,key nil))))
+       :array (map--delete-array m ,key))))
 
 (defun map-nested-elt (map keys &optional default)
   "Travserse MAP using KEYS and return the looked up value or DEFAULT if nil.
@@ -261,13 +261,20 @@ form.
              (seq-elt map key))
         default)))
 
-
 (defun map--delete-alist (map key)
   "Return MAP with KEY removed."
   (seq-remove (lambda (pair)
                 (equal key (car pair)))
               map))
 
+(defun map--delete-array (map key)
+  "Set nil in the array MAP at the index KEY if present and return MAP."
+  (let ((len (seq-length map)))
+    (and (>= key 0)
+         (<= key len)
+         (aset m key nil)))
+  map)
+
 (defun map--into-hash-table (map)
   "Convert MAP into a hash-table."
   (let ((ht (make-hash-table :size (map-length map)
diff --git a/test/automated/map-tests.el b/test/automated/map-tests.el
index f41cd70..5201116 100644
--- a/test/automated/map-tests.el
+++ b/test/automated/map-tests.el
@@ -96,7 +96,10 @@ Evaluate BODY for each created map.
 (ert-deftest test-map-delete ()
   (with-maps-do map
     (map-delete map 1)
-    (assert (null (map-elt map 1)))))
+    (assert (null (map-elt map 1))))
+  (with-maps-do map
+    (map-delete map -2)
+    (assert (null (map-elt map -2)))))
 
 (ert-deftest test-map-delete-return-value ()
   (let ((ht (make-hash-table)))



reply via email to

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