all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: David Kastrup <dak@gnu.org>
Cc: rms@gnu.org, emacs-devel@gnu.org
Subject: Re: lists.texi
Date: Tue, 21 Jun 2005 07:13:40 +0200	[thread overview]
Message-ID: <85zmtk1ed7.fsf@lola.goethe.zz> (raw)
In-Reply-To: <200506202312.j5KNCct19091@raven.dms.auburn.edu> (Luc Teirlinck's message of "Mon, 20 Jun 2005 18:12:38 -0500 (CDT)")

Luc Teirlinck <teirllm@dms.auburn.edu> writes:

> Richard Stallman wrote:
>
>    If you want to do a little work, I am sure you could write a single
>    loop that produces the right elements in the right order.  Then you
>    could rotate it properly with a single call to setcdr followed by
>    nconc'ing the pieces in the opposite order.
>
> Unless I am completely misunderstanding things, I believe I was a
> little bit too quick to admit that my original function:
>
> (defun ring-elements (ring)
>   "Return a list of the elements of RING in order, newest first."
>   (let (lst)
>     (dotimes (var (ring-length ring))
>       (push (ring-ref ring var) lst))
>     (nreverse lst)))
>
> was quadratic.  It essentially does ring-length times an aref in
> _vector_, which unlike checking the element at an average position
> in a _list_, would not appear to be linear in the size of the
> vector.

Well, I was one of the O(n^2) criers, and I did not know that ring-ref
works via aref.  Looking at ring-ref, however, it would appear that
the O(1) constant is pretty hefty.

> Anyway, I now propose the following which avoids the nreverse and
> esentially "inlines" the `ring-ref' call, but in a way that avoids a
> lot more overhead than a simple inlining.  So I believe that it
> should be an improvement by a pretty good constant factor, which
> would not be of too much help if it really is quadratic, but why is
> it?
>
> (defun ring-elements (ring)
>   "Return a list of the elements of RING, in order, newest first."
>   (let ((start (car ring))
>   (size (ring-size ring))
>   (vect (cddr ring))
>   lst)
>     (dotimes (var (cadr ring) lst)
>       (push (aref vect (mod (+ start var) size)) lst))))

As long as you checked that the elements come out in the right order,
this would appear like a pretty good solution.

-- 
David Kastrup, Kriemhildstr. 15, 44793 Bochum

  reply	other threads:[~2005-06-21  5:13 UTC|newest]

Thread overview: 46+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-06-18 23:19 lists.texi Luc Teirlinck
2005-06-19  0:01 ` lists.texi Luc Teirlinck
2005-06-19  0:15 ` lists.texi Luc Teirlinck
2005-06-19  0:37   ` lists.texi Luc Teirlinck
2005-06-19  6:37     ` lists.texi David Kastrup
2005-06-19 15:55     ` lists.texi Richard Stallman
2005-06-19 17:47       ` lists.texi Luc Teirlinck
2005-06-20 17:52         ` lists.texi Richard Stallman
2005-06-20 23:12           ` lists.texi Luc Teirlinck
2005-06-21  5:13             ` David Kastrup [this message]
2005-06-21 15:13             ` lists.texi Richard M. Stallman
2005-06-21 16:35             ` lists.texi Thien-Thi Nguyen
2005-06-21 19:00               ` lists.texi Luc Teirlinck
2005-06-21 21:56                 ` lists.texi Thien-Thi Nguyen
2005-06-21 19:45               ` lists.texi Luc Teirlinck
2005-06-21 20:58               ` lists.texi Luc Teirlinck
2005-06-21 22:09                 ` lists.texi Thien-Thi Nguyen
2005-06-22 16:28                 ` lists.texi Juri Linkov
2005-06-22 19:27                   ` lists.texi Eli Zaretskii
2005-06-22 18:44                     ` lists.texi Luc Teirlinck
2005-06-22 20:25                     ` lists.texi Luc Teirlinck
2005-06-23 16:53                       ` lists.texi Richard M. Stallman
2005-06-24 19:02                       ` GC (was: lists.texi) Juri Linkov
2005-06-24 19:02                     ` Juri Linkov
2005-06-24 21:08                       ` Eli Zaretskii
2005-06-24 21:54                         ` Juri Linkov
2005-06-24 23:52                           ` Luc Teirlinck
2005-06-25  0:51                             ` Miles Bader
2005-06-25  9:48                           ` Eli Zaretskii
2005-06-25 11:58                             ` GC Adrian Aichner
2005-06-25 12:53                               ` GC Miles Bader
2005-06-25 21:53                                 ` GC Adrian Aichner
2005-06-26  0:02                                   ` GC Miles Bader
2005-06-26  8:20                                     ` GC Adrian Aichner
2005-06-26 18:51                                       ` GC Eli Zaretskii
2005-06-26 23:43                                         ` GC Juri Linkov
2005-06-27  5:38                                         ` GC Richard M. Stallman
2005-06-26 22:42                                       ` GC Richard M. Stallman
2005-06-25 12:15                             ` GC (was: lists.texi) Miles Bader
2005-06-25 13:10                               ` GC Gaëtan LEURENT
2005-06-25 14:48                                 ` GC Eli Zaretskii
2005-06-25 14:45                               ` GC (was: lists.texi) Eli Zaretskii
2005-06-25 16:40                               ` Richard M. Stallman
2005-06-28  4:55                             ` GC Stefan Monnier
2005-06-28 21:29                               ` GC Richard M. Stallman
2005-07-11 17:00                                 ` GC Stefan Monnier

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=85zmtk1ed7.fsf@lola.goethe.zz \
    --to=dak@gnu.org \
    --cc=emacs-devel@gnu.org \
    --cc=rms@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.