From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Thierry Volpiatto Newsgroups: gmane.emacs.help Subject: Re: how to access a large datastructure efficiently? Date: Thu, 04 Mar 2010 12:00:17 +0100 Organization: ThierryVolpiatto Message-ID: <87lje8z70e.fsf@tux.homenetwork> References: <873a0gshb6.fsf@tux.homenetwork> <87y6i8r1j3.fsf@tux.homenetwork> <4B8F6BBC.2000607@easy-emacs.de> NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Trace: dough.gmane.org 1267700752 15290 80.91.229.12 (4 Mar 2010 11:05:52 GMT) X-Complaints-To: usenet@dough.gmane.org NNTP-Posting-Date: Thu, 4 Mar 2010 11:05:52 +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 12:05:49 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 1Nn8sF-0006DN-6u for geh-help-gnu-emacs@m.gmane.org; Thu, 04 Mar 2010 12:05:47 +0100 Original-Received: from localhost ([127.0.0.1]:55137 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1Nn8sE-0006QQ-PT for geh-help-gnu-emacs@m.gmane.org; Thu, 04 Mar 2010 06:05:46 -0500 Original-Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1Nn8rN-0006KB-DG for help-gnu-emacs@gnu.org; Thu, 04 Mar 2010 06:04:53 -0500 Original-Received: from [140.186.70.92] (port=38858 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1Nn8rJ-0006HT-Oo for help-gnu-emacs@gnu.org; Thu, 04 Mar 2010 06:04:50 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.69) (envelope-from ) id 1Nn8rI-0000ft-VV for help-gnu-emacs@gnu.org; Thu, 04 Mar 2010 06:04:49 -0500 Original-Received: from lo.gmane.org ([80.91.229.12]:53004) by eggs.gnu.org with esmtp (Exim 4.69) (envelope-from ) id 1Nn8rI-0000fZ-Kj for help-gnu-emacs@gnu.org; Thu, 04 Mar 2010 06:04:48 -0500 Original-Received: from list by lo.gmane.org with local (Exim 4.69) (envelope-from ) id 1Nn8rF-0005iq-S9 for help-gnu-emacs@gnu.org; Thu, 04 Mar 2010 12:04:45 +0100 Original-Received: from 43.211.85-79.rev.gaoland.net ([79.85.211.43]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Thu, 04 Mar 2010 12:04:45 +0100 Original-Received: from thierry.volpiatto by 43.211.85-79.rev.gaoland.net with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Thu, 04 Mar 2010 12:04:45 +0100 X-Injected-Via-Gmane: http://gmane.org/ Original-Lines: 78 Original-X-Complaints-To: usenet@dough.gmane.org X-Gmane-NNTP-Posting-Host: 43.211.85-79.rev.gaoland.net Face: iVBORw0KGgoAAAANSUhEUgAAADAAAAAwBAMAAAClLOS0AAAAElBMVEUHBARgJRFPS0WbMBTC TSG/uJs0yyVHAAAACXBIWXMAAAsSAAALEgHS3X78AAAACXZwQWcAAAAwAAAAMADO7oxXAAABpUlE QVQ4y3VTQXKDMAyUnD7AwvQOhtxj5N4LFh9ow/+/UtlpM0BczWSS0Wola7UBeEb3+3kJ/5p9ZLz3 UI0n0J0Y4K/lh1nG24FhlJOR2LdSKtpV5gfi/Zd+NdaJBbB4D5N0JnfUIVp6obZURkJwS2a8b9uX AkgxA5gCEcknAH1s2z0DPooWmjGKcJR+HNZt+9bm0GtGRycRuZGs98Ffr02vABWGopIAhmXmxnQN BwBXZjgFLHRoqbfeYxCLWqsjLyKhbEHW+A5nEs699Q2O80ALrgHTmUTy6P0MnKyKQpKHpoNWokxs F0D5VegvWmEozyE6nuMiS98mWzlUQ9bW8qYfqOaFtzWlYCqGeIs6HEyl08jkrxUG9MF5ipXpOOpd ZMoXPPVSLWJiiwNNB+eRSyxJZmQKcacikgucnCRiHuJOLmyYJRDPwOqAvY4pA+Wy+VS8G26BAsdb Ph2Hw7ORy/H0IOG0TnaM8Cp0XrFYyU0v2z8AqkifPVbLZ0qo/gsRg60C7p+8Ov9jrUIxFde+jnAq X42hyp/c+ZcnVyUYBarrqZt3+R8450png3cWKgAAACV0RVh0ZGF0ZTpjcmVhdGUAMjAwOC0wNS0z MVQyMjoxNDoyMiswMjowMK3VBDcAAAAldEVYdGRhdGU6bW9kaWZ5ADIwMDctMTAtMjhUMTc6MDg6 NDgrMDE6MDDLdIcLAAAAEXRFWHRqcGVnOmNvbG9yc3BhY2UAMix1VZ8AAAAgdEVYdGpwZWc6c2Ft cGxpbmctZmFjdG9yADJ4MiwxeDEsMXgxSfqmtAAAAABJRU5ErkJggg== User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1.93 (gnu/linux) Cancel-Lock: sha1:VkfXGXMmvMCAWG6CBcdiL6SFOlc= 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:72360 Archived-At: Andreas Röhler writes: > Thierry Volpiatto wrote: >> Thierry Volpiatto writes: >> >>> Hi, >>> >>> Christian Wittern 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? >>> ,---- >>> | (defun closest-elm-in-seq (n seq) >>> | (let ((pair (loop with elm = n with last-elm >>> | for i in seq >>> | if (and last-elm (< last-elm elm) (> i elm)) return (list last-elm i) >>> | do (setq last-elm i)))) >>> | (if (< (- n (car pair)) (- (cadr pair) n)) >>> | (car pair) (cadr pair)))) >>> `---- >>> >>> That return the closest, but not the smaller closest, but it should be >>> easy to adapt. >> >> Case where your element is member of list, return it: >> >> ,---- >> | (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... >> > > if n is member of the seq, maybe equal-operator too > > (<= last-elm elm) > > is correct? No, in this case: if (eq i elm) return (list i) ==> (i) ; which is n and finally (car pair) ==> n > Thanks BTW, very interesting > > Andreas > > > -- Thierry Volpiatto Gpg key: http://pgp.mit.edu/