emacs-diffs
[Top][All Lists]
Advanced

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

[Emacs-diffs] trunk r117111: * src/fns.c (Fnreverse): Allow vectors and


From: Dmitry Antipov
Subject: [Emacs-diffs] trunk r117111: * src/fns.c (Fnreverse): Allow vectors and bool vectors.
Date: Thu, 15 May 2014 15:00:32 +0000
User-agent: Bazaar (2.6b2)

------------------------------------------------------------
revno: 117111
revision-id: address@hidden
parent: address@hidden
committer: Dmitry Antipov <address@hidden>
branch nick: trunk
timestamp: Thu 2014-05-15 18:59:02 +0400
message:
  * src/fns.c (Fnreverse): Allow vectors and bool vectors.
  * doc/lispref/lists.texi (Building Cons Cells and Lists): Remove
  description of `nreverse' and generalize it...
  * doc/lispref/sequences.texi (Sequences): ...for sequences here.
  * tests/automated/fns-tests.el (fns-tests-nreverse)
  (fns-tests-nreverse-bool-vector): New tests.
modified:
  doc/lispref/ChangeLog          changelog-20091113204419-o5vbwnq5f7feedwu-6155
  doc/lispref/lists.texi         lists.texi-20091113204419-o5vbwnq5f7feedwu-6192
  doc/lispref/sequences.texi     
sequences.texi-20091113204419-o5vbwnq5f7feedwu-6209
  src/ChangeLog                  changelog-20091113204419-o5vbwnq5f7feedwu-1438
  src/fns.c                      fns.c-20091113204419-o5vbwnq5f7feedwu-203
  test/ChangeLog                 changelog-20091113204419-o5vbwnq5f7feedwu-8588
  test/automated/fns-tests.el    fnstests.el-20140515083159-ls2r7gfl9o74ajzm-1
=== modified file 'doc/lispref/ChangeLog'
--- a/doc/lispref/ChangeLog     2014-05-15 06:01:46 +0000
+++ b/doc/lispref/ChangeLog     2014-05-15 14:59:02 +0000
@@ -1,7 +1,7 @@
 2014-05-15  Dmitry Antipov  <address@hidden>
 
        * lists.texi (Building Cons Cells and Lists): Remove
-       description of `reverse' and generalize it...
+       description of `reverse' and `'nreverse' to generalize them...
        * sequences.texi (Sequences): ...for sequences here.
 
 2014-05-14  Glenn Morris  <address@hidden>

=== modified file 'doc/lispref/lists.texi'
--- a/doc/lispref/lists.texi    2014-05-15 06:01:46 +0000
+++ b/doc/lispref/lists.texi    2014-05-15 14:59:02 +0000
@@ -1124,58 +1124,6 @@
 @end smallexample
 @end defun
 
address@hidden nreverse list
address@hidden reversing a list
-  This function reverses the order of the elements of @var{list}.
-Unlike @code{reverse}, @code{nreverse} alters its argument by reversing
-the @sc{cdr}s in the cons cells forming the list.  The cons cell that
-used to be the last one in @var{list} becomes the first cons cell of the
-value.
-
-  For example:
-
address@hidden
address@hidden
-(setq x '(a b c))
-     @result{} (a b c)
address@hidden group
address@hidden
-x
-     @result{} (a b c)
-(nreverse x)
-     @result{} (c b a)
address@hidden group
address@hidden
-;; @r{The cons cell that was first is now last.}
-x
-     @result{} (a)
address@hidden group
address@hidden example
-
-  To avoid confusion, we usually store the result of @code{nreverse}
-back in the same variable which held the original list:
-
address@hidden
-(setq x (nreverse x))
address@hidden example
-
-  Here is the @code{nreverse} of our favorite example, @code{(a b c)},
-presented graphically:
-
address@hidden
address@hidden
address@hidden list head:}                       @r{Reversed list:}
- -------------        -------------        ------------
-| car  | cdr  |      | car  | cdr  |      | car | cdr  |
-|   a  |  nil |<--   |   b  |   o  |<--   |   c |   o  |
-|      |      |   |  |      |   |  |   |  |     |   |  |
- -------------    |   --------- | -    |   -------- | -
-                  |             |      |            |
-                   -------------        ------------
address@hidden group
address@hidden smallexample
address@hidden defun
-
 @defun sort list predicate
 @cindex stable sort
 @cindex sorting lists

=== modified file 'doc/lispref/sequences.texi'
--- a/doc/lispref/sequences.texi        2014-05-15 06:01:46 +0000
+++ b/doc/lispref/sequences.texi        2014-05-15 14:59:02 +0000
@@ -260,6 +260,75 @@
 @end example
 @end defun
 
address@hidden nreverse seq
address@hidden reversing a list
address@hidden reversing a vector
+  This function reverses the order of the elements of @var{seq}.
+If @var{seq} is a list, @code{nreverse} alters its by reversing the @sc{cdr}s
+in the cons cells.  The cons cell that used to be the last one in @var{seq}
+becomes the first cons cell of the value.  If @var{seq} is a vector or
+bool vector, its items are placed in the same vector in a reversed order.
+
+  For example:
+
address@hidden
address@hidden
+(setq x '(a b c))
+     @result{} (a b c)
address@hidden group
address@hidden
+x
+     @result{} (a b c)
+(nreverse x)
+     @result{} (c b a)
address@hidden group
address@hidden
+;; @r{The cons cell that was first is now last.}
+x
+     @result{} (a)
address@hidden group
address@hidden example
+
+  To avoid confusion, we usually store the result of @code{nreverse}
+back in the same variable which held the original list:
+
address@hidden
+(setq x (nreverse x))
address@hidden example
+
+  Here is the @code{nreverse} of our favorite example, @code{(a b c)},
+presented graphically:
+
address@hidden
address@hidden
address@hidden list head:}                       @r{Reversed list:}
+ -------------        -------------        ------------
+| car  | cdr  |      | car  | cdr  |      | car | cdr  |
+|   a  |  nil |<--   |   b  |   o  |<--   |   c |   o  |
+|      |      |   |  |      |   |  |   |  |     |   |  |
+ -------------    |   --------- | -    |   -------- | -
+                  |             |      |            |
+                   -------------        ------------
address@hidden group
address@hidden smallexample
+
+  For the vector, it is even simpler because you don't need setq:
+
address@hidden
+(setq x [1 2 3 4])
+     @result{} [1 2 3 4]
+(nreverse x)
+     @result{} [4 3 2 1]
+x
+     @result{} [4 3 2 1]
address@hidden example
+
+Note that unlike @code{reverse}, this function doesn't work with strings.
+Although you can alter string data by using @code{aset}, it is strongly
+encouraged to treat strings as immutable.
+
address@hidden defun
+
 @node Arrays
 @section Arrays
 @cindex array

=== modified file 'src/ChangeLog'
--- a/src/ChangeLog     2014-05-15 06:01:46 +0000
+++ b/src/ChangeLog     2014-05-15 14:59:02 +0000
@@ -1,6 +1,7 @@
 2014-05-15  Dmitry Antipov  <address@hidden>
 
        * fns.c (Freverse): Allow vectors, bool vectors and strings.
+       (Fnreverse): Allow vectors and bool vectors.
 
 2014-05-14  Dmitry Antipov  <address@hidden>
 

=== modified file 'src/fns.c'
--- a/src/fns.c 2014-05-15 06:01:46 +0000
+++ b/src/fns.c 2014-05-15 14:59:02 +0000
@@ -1697,25 +1697,55 @@
 }
 
 DEFUN ("nreverse", Fnreverse, Snreverse, 1, 1, 0,
-       doc: /* Reverse LIST by modifying cdr pointers.
-Return the reversed list.  Expects a properly nil-terminated list.  */)
-  (Lisp_Object list)
+       doc: /* Reverse order of items in a list or vector SEQ.
+If SEQ is a list, it should be nil-terminated, and reversed
+by modifying cdr pointers.  Return the reversed SEQ.
+
+Note that unlike `reverse', this function doesn't work with strings.
+It is strongly encouraged to treat them as immutable.  */)
+  (Lisp_Object seq)
 {
-  register Lisp_Object prev, tail, next;
-
-  if (NILP (list)) return list;
-  prev = Qnil;
-  tail = list;
-  while (!NILP (tail))
-    {
-      QUIT;
-      CHECK_LIST_CONS (tail, tail);
-      next = XCDR (tail);
-      Fsetcdr (tail, prev);
-      prev = tail;
-      tail = next;
-    }
-  return prev;
+  if (NILP (seq))
+    return seq;
+  else if (CONSP (seq))
+    {
+      Lisp_Object prev, tail, next;
+
+      for (prev = Qnil, tail = seq; !NILP (tail); tail = next)
+       {
+         QUIT;
+         CHECK_LIST_CONS (tail, tail);
+         next = XCDR (tail);
+         Fsetcdr (tail, prev);
+         prev = tail;
+       }
+      seq = prev;
+    }
+  else if (VECTORP (seq))
+    {
+      ptrdiff_t i, size = ASIZE (seq);
+
+      for (i = 0; i < size / 2; i++)
+       {
+         Lisp_Object tem = AREF (seq, i);
+         ASET (seq, i, AREF (seq, size - i - 1));
+         ASET (seq, size - i - 1, tem);
+       }
+    }
+  else if (BOOL_VECTOR_P (seq))
+    {
+      ptrdiff_t i, size = bool_vector_size (seq);
+
+      for (i = 0; i < size / 2; i++)
+       {
+         bool tem = bool_vector_bitref (seq, i);
+         bool_vector_set (seq, i, bool_vector_bitref (seq, size - i - 1));
+         bool_vector_set (seq, size - i - 1, tem);
+       }
+    }
+  else
+    wrong_type_argument (Qarrayp, seq);
+  return seq;
 }
 
 DEFUN ("reverse", Freverse, Sreverse, 1, 1, 0,

=== modified file 'test/ChangeLog'
--- a/test/ChangeLog    2014-05-15 10:06:45 +0000
+++ b/test/ChangeLog    2014-05-15 14:59:02 +0000
@@ -1,6 +1,8 @@
 2014-05-15  Dmitry Antipov  <address@hidden>
 
        * automated/fns-tests.el: New file.
+       * automated/fns-tests.el (fns-tests-nreverse)
+       (fns-tests-nreverse-bool-vector): New tests.
 
 2014-05-08  Glenn Morris  <address@hidden>
 

=== modified file 'test/automated/fns-tests.el'
--- a/test/automated/fns-tests.el       2014-05-15 10:06:45 +0000
+++ b/test/automated/fns-tests.el       2014-05-15 14:59:02 +0000
@@ -28,12 +28,44 @@
   (should-error (reverse))
   (should-error (reverse 1))
   (should-error (reverse (make-char-table 'foo)))
+  (should (equal [] (reverse [])))
+  (should (equal [0] (reverse [0])))
   (should (equal [1 2 3 4] (reverse (reverse [1 2 3 4]))))
+  (should (equal '(a b c d) (reverse (reverse '(a b c d)))))
   (should (equal "xyzzy" (reverse (reverse "xyzzy"))))
   (should (equal "こんにちは / コンニチハ" (reverse (reverse "こんにちは / コンニチハ")))))
 
+(ert-deftest fns-tests-nreverse ()
+  (should-error (nreverse))
+  (should-error (nreverse 1))
+  (should-error (nreverse (make-char-table 'foo)))
+  (should-error (nreverse "xyzzy"))
+  (let ((A []))
+    (nreverse A)
+    (should (equal A [])))
+  (let ((A [0]))
+    (nreverse A)
+    (should (equal A [0])))
+  (let ((A [1 2 3 4]))
+    (nreverse A)
+    (should (equal A [4 3 2 1])))
+  (let ((A [1 2 3 4]))
+    (nreverse A)
+    (nreverse A)
+    (should (equal A [1 2 3 4])))
+  (let* ((A [1 2 3 4])
+        (B (nreverse (nreverse A))))
+    (should (equal A B))))
+
 (ert-deftest fns-tests-reverse-bool-vector ()
   (let ((A (make-bool-vector 10 nil)))
     (dotimes (i 5) (aset A i t))
     (should (equal [nil nil nil nil nil t t t t t] (vconcat (reverse A))))
     (should (equal A (reverse (reverse A))))))
+
+(ert-deftest fns-tests-nreverse-bool-vector ()
+  (let ((A (make-bool-vector 10 nil)))
+    (dotimes (i 5) (aset A i t))
+    (nreverse A)
+    (should (equal [nil nil nil nil nil t t t t t] (vconcat A)))
+    (should (equal [t t t t t nil nil nil nil nil] (vconcat (nreverse A))))))


reply via email to

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