From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Christian Wittern Newsgroups: gmane.emacs.help Subject: Re: how to access a large datastructure efficiently? Date: Thu, 4 Mar 2010 08:15:47 +0000 (UTC) Message-ID: References: <873a0gshb6.fsf@tux.homenetwork> <87y6i8r1j3.fsf@tux.homenetwork> NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Trace: dough.gmane.org 1267690596 12067 80.91.229.12 (4 Mar 2010 08:16:36 GMT) X-Complaints-To: usenet@dough.gmane.org NNTP-Posting-Date: Thu, 4 Mar 2010 08:16:36 +0000 (UTC) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Thu Mar 04 09:16:32 2010 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([199.232.76.165]) by lo.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1Nn6ES-0006hA-96 for geh-help-gnu-emacs@m.gmane.org; Thu, 04 Mar 2010 09:16:32 +0100 Original-Received: from localhost ([127.0.0.1]:42320 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1Nn6ER-0008FM-4o for geh-help-gnu-emacs@m.gmane.org; Thu, 04 Mar 2010 03:16:31 -0500 Original-Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1Nn6Dy-0008E2-G6 for help-gnu-emacs@gnu.org; Thu, 04 Mar 2010 03:16:02 -0500 Original-Received: from [140.186.70.92] (port=35996 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1Nn6Dx-0008DI-Hf for help-gnu-emacs@gnu.org; Thu, 04 Mar 2010 03:16:02 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.69) (envelope-from ) id 1Nn6Dw-0006st-Nz for help-gnu-emacs@gnu.org; Thu, 04 Mar 2010 03:16:01 -0500 Original-Received: from lo.gmane.org ([80.91.229.12]:45105) by eggs.gnu.org with esmtp (Exim 4.69) (envelope-from ) id 1Nn6Dw-0006sW-EO for help-gnu-emacs@gnu.org; Thu, 04 Mar 2010 03:16:00 -0500 Original-Received: from list by lo.gmane.org with local (Exim 4.69) (envelope-from ) id 1Nn6Du-0006RD-5C for help-gnu-emacs@gnu.org; Thu, 04 Mar 2010 09:15:58 +0100 Original-Received: from chw.zinbun.kyoto-u.ac.jp ([130.54.104.146]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Thu, 04 Mar 2010 09:15:58 +0100 Original-Received: from cwittern by chw.zinbun.kyoto-u.ac.jp with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Thu, 04 Mar 2010 09:15:58 +0100 X-Injected-Via-Gmane: http://gmane.org/ Original-Lines: 32 Original-X-Complaints-To: usenet@dough.gmane.org X-Gmane-NNTP-Posting-Host: sea.gmane.org User-Agent: Loom/3.14 (http://gmane.org/) X-Loom-IP: 130.54.104.146 (Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.6; en-US; rv:1.9.0.17) Gecko/2009122115 Firefox/3.0.17 GTB6) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 3) X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.help:72356 Archived-At: Thierry Volpiatto gmail.com> writes: > >> 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] > > ,---- > | (defun closest-elm-in-seq (n seq) > | (let ((pair (loop with elm = n with last-elm > | for i in seq > | if (eq i elm) return (list i) > | else if (and last-elm (< last-elm elm) (> i elm)) return (list last-elm i) > | do (setq last-elm i)))) > | (if (> (length pair) 1) > | (if (< (- n (car pair)) (- (cadr pair) n)) > | (car pair) (cadr pair)) > | (car pair)))) > `---- > For the smallest just return the car... > This seems to do what I need, thanks! Now I have to see how that performs on the real data. I was hoping there would be a method that did not involve loops, but some kind of binary search. Would it be possible to use a hash-table here? Christian