emacs-devel
[Top][All Lists]
Advanced

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

Re: Merging nth, aref, and elt.


From: Artur Malabarba
Subject: Re: Merging nth, aref, and elt.
Date: Sat, 13 Dec 2014 04:09:51 +0000

Here's an initial diff for hash-tables. Should it use `get' instead of `elt'?

    (fns.c) elt: Also work on hash-tables

    (lisp/emacs-lisp/gv.el): Fix setf on `elt'

    (bytecode.c) exec_byte_code: Add nil arg to Felt call.

3 files changed, 26 insertions(+), 14 deletions(-)
 lisp/emacs-lisp/gv.el |  7 ++++---
 src/bytecode.c        |  2 +-
 src/fns.c             | 31 +++++++++++++++++++++----------

    Modified   lisp/emacs-lisp/gv.el
diff --git a/lisp/emacs-lisp/gv.el b/lisp/emacs-lisp/gv.el
index a0f92a5..11c35a4 100644
--- a/lisp/emacs-lisp/gv.el
+++ b/lisp/emacs-lisp/gv.el
@@ -302,7 +302,8 @@ The return value is the last VAL in the list.
 (gv-define-setter cdar (val x) `(setcdr (car ,x) ,val))
 (gv-define-setter cddr (val x) `(setcdr (cdr ,x) ,val))
-(gv-define-setter elt (store seq n)
-  `(if (listp ,seq) (setcar (nthcdr ,n ,seq) ,store)
-     (aset ,seq ,n ,store)))
+(gv-define-setter elt (store set index)
+  `(if (listp ,set) (setcar (nthcdr ,index ,set) ,store)
+     (if (arrayp ,set) (aset ,set ,index ,store)
+       (puthash ,index ,store ,set))))
 (gv-define-simple-setter get put)
 (gv-define-setter gethash (val k h &optional _d) `(puthash ,k ,val ,h))
    Modified   src/bytecode.c
diff --git a/src/bytecode.c b/src/bytecode.c
index d3c8b47..456f23d 100644
--- a/src/bytecode.c
+++ b/src/bytecode.c
@@ -1843,5 +1843,5 @@ exec_byte_code (Lisp_Object bytestr, Lisp_Object
vector, Lisp_Object maxdepth,
         BEFORE_POTENTIAL_GC ();
         v1 = POP;
-        TOP = Felt (TOP, v1);
+        TOP = Felt (TOP, v1, Qnil);
         AFTER_POTENTIAL_GC ();
           }
    Modified   src/fns.c
diff --git a/src/fns.c b/src/fns.c
index e891fdb..aabe757 100644
--- a/src/fns.c
+++ b/src/fns.c
@@ -1355,15 +1355,26 @@ N counts from zero.  If LIST is not that long,
nil is returned.  */)
 }

-DEFUN ("elt", Felt, Selt, 2, 2, 0,
-       doc: /* Return element of SEQUENCE at index N.  */)
-  (register Lisp_Object sequence, Lisp_Object n)
-{
-  CHECK_NUMBER (n);
-  if (CONSP (sequence) || NILP (sequence))
-    return Fcar (Fnthcdr (n, sequence));
+DEFUN ("elt", Felt, Selt, 2, 3, 0,
+       doc: /* Return element of SET at INDEX.
+If SET is a list or an array, INDEX is a number and DFLT is ignored.
+If SET is a hash table, INDEX is a key.  If INDEX is not found, return
+DFLT which defaults to nil.  */)
+  (register Lisp_Object set, Lisp_Object index, Lisp_Object dflt)
+{
+  if (CONSP (set) || NILP (set))
+    {
+      CHECK_NUMBER (index);
+      return Fcar (Fnthcdr (index, set));
+    }
+
+  if (ARRAYP (set))
+    {
+      CHECK_NUMBER (index);
+      return Faref (set, index);
+    }

-  /* Faref signals a "not array" error, so check here.  */
-  CHECK_ARRAY (sequence, Qsequencep);
-  return Faref (sequence, n);
+  /* Fgethash signals a "not hash-table" error, so check here.  */
+  CHECK_TYPE (HASH_TABLE_P (set), Qsequencep, set);
+  return Fgethash (index, set, dflt);
 }



reply via email to

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