all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: pjb@informatimago.com (Pascal J. Bourguignon)
To: help-gnu-emacs@gnu.org
Subject: Re: how to access a large datastructure efficiently?
Date: Thu, 04 Mar 2010 21:22:16 +0100	[thread overview]
Message-ID: <87hbovn8g7.fsf@informatimago.com> (raw)
In-Reply-To: mailman.2247.1267667449.14305.help-gnu-emacs@gnu.org

Christian Wittern <cwittern@gmail.com> writes:

> Hi there,
>
> Here is the problem I am trying to solve:
>
> I have a large list of items which I want to access.  The items are in 
> sequential order, but many are missing in between, like:
>
> (1 8 17 23 25 34 45 47 50)  [in reality, there is a value associated 
> with this, but I took it out for simplicity]
>
> Now when I am trying to access with a key that is not in the list, I 
> want to have the one with the closest smaller key returned, so for 6 
> and 7 this would be 1, but for 8 and 9 this would be 8.
>
> Since the list will have thousands of elements, I do not want to simply 
> loop through it but am looking for better ways to do this in Emacs lisp.  
> Any ideas how to achieve this?

Why do you have a list?

If you had a vector, you could do a dichotomy, and find it in O(log(n)).

Or, if you need to insert or remove elements between searches, as the
others have advised, use a binary tree, preferablemente a balanced
binary tree.  I prefer the left-leaning red-black trees over the avl
trees. 



-- 
__Pascal Bourguignon__
http://www.informatimago.com


  parent reply	other threads:[~2010-03-04 20:22 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <mailman.2247.1267667449.14305.help-gnu-emacs@gnu.org>
2010-03-04 10:36 ` how to access a large datastructure efficiently? Alan Mackenzie
2010-03-04 20:22 ` Pascal J. Bourguignon [this message]
2010-03-05  0:29 ` Stefan Monnier
2010-03-04  1:50 Christian Wittern
2010-03-04  6:59 ` Thierry Volpiatto
2010-03-04  7:25   ` Thierry Volpiatto
2010-03-04  8:13     ` Andreas Röhler
2010-03-04 11:00       ` Thierry Volpiatto
2010-03-04 15:49         ` Andreas Röhler
2010-03-04 16:09           ` Thierry Volpiatto
2010-03-04  8:15     ` Christian Wittern
2010-03-04 10:24       ` Thierry Volpiatto
2010-03-04 15:01       ` Thien-Thi Nguyen
2010-03-04 16:49       ` Andreas Politz

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=87hbovn8g7.fsf@informatimago.com \
    --to=pjb@informatimago.com \
    --cc=help-gnu-emacs@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.