Path: csiph.com!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail From: Cem Karan Newsgroups: comp.lang.python Subject: Re: Heap Implementation Date: Thu, 11 Feb 2016 06:06:00 -0500 Lines: 67 Message-ID: References: <56AD3D83.2050308@mail.de> <7C522D08-9D73-48D2-A71D-F1D1D34C02A5@gmail.com> <56BB8005.8090708@mail.de> Mime-Version: 1.0 (Mac OS X Mail 6.6 \(1510\)) Content-Type: text/plain; charset=windows-1252 Content-Transfer-Encoding: quoted-printable X-Trace: news.uni-berlin.de iw03a7PHnufHpJMpSZboBQQXDQl8P+/gHZu5UD5KjQIg== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'modify': 0.04; 'pop': 0.05; 'attributes': 0.07; 'lately': 0.07; 'wrapper': 0.07; 'cc:addr:python-list': 0.09; 'calculating': 0.09; 'called.': 0.09; 'deadline,': 0.09; 'exceeds': 0.09; 'it;': 0.09; 'marking': 0.09; 'python': 0.10; 'pushed': 0.13; 'benefit.': 0.16; 'callables': 0.16; 'canceled': 0.16; 'heap': 0.16; 'objects?': 0.16; 'received:192.168.1.4': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'row': 0.16; 'valid.': 0.16; 'wrote:': 0.16; 'memory': 0.17; 'comparing': 0.18; 'result,': 0.18; 'library': 0.20; 'math': 0.20; 'cc:addr:python.org': 0.20; 'cc:2**1': 0.22; 'defined': 0.23; 'feb': 0.23; 'thanks,': 0.24; 'cc:addr:gmail.com': 0.24; 'header:In-Reply-To:1': 0.24; "i've": 0.25; 'handling': 0.27; 'switch': 0.27; 'least': 0.27; '(e.g.,': 0.27; 'cancel': 0.27; 'executing': 0.27; 'tend': 0.27; 'function': 0.28; 'looks': 0.29; 'calculated': 0.29; 'concern': 0.29; 'directly,': 0.29; 'once.': 0.29; 'queue': 0.29; 'objects': 0.29; 'code': 0.30; 'push': 0.30; 'certain': 0.31; 'another': 0.32; 'expensive': 0.32; 'though,': 0.32; 'useful': 0.33; 'problem': 0.33; 'optimize': 0.33; "i'll": 0.33; 'message-id:@gmail.com': 0.34; 'changing': 0.34; 'equal': 0.34; 'handle': 0.34; 'received:google.com': 0.35; 'so,': 0.35; 'could': 0.35; 'fresh': 0.35; 'quite': 0.35; "isn't": 0.35; 'item': 0.35; 'sometimes': 0.35; 'but': 0.36; 'should': 0.36; 'located': 0.36; 'there': 0.36; 'received:209.85': 0.36; 'possible': 0.36; '(and': 0.36; 'cases': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'being': 0.37; 'hundreds': 0.37; 'busy': 0.38; 'difference': 0.38; 'itself': 0.38; "won't": 0.38; 'received:209': 0.38; 'received:209.85.220': 0.38; 'why': 0.39; 'does': 0.39; 'received:192': 0.39; 'well.': 0.40; 'where': 0.40; 'some': 0.40; 'future': 0.60; 'your': 0.60; 'behavior': 0.61; 'header:Message-Id:1': 0.61; 'impact': 0.61; 'here.': 0.62; 'charset:windows-1252': 0.62; 'more': 0.63; 'times': 0.63; 'talking': 0.67; 'finally': 0.70; 'future,': 0.70; 'useful.': 0.72; 'client-side': 0.84; 'pondered': 0.84; 'simulations': 0.84; 'simulation': 0.91; 'why?': 0.91; 'good,': 0.93 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=content-type:mime-version:subject:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to; bh=2m94rpaoJKfwItzMR2ZTC74CqNlEWsw2PcfX0nsjslg=; b=Fvxgcc8tvcYryPvqb/3JHAps4G4z0h8owiYv6H65qTfcDJQH3o124oJJE+H7p+oRnd eJJhRJ6OZwY1L5sDS7VVzOzc/zS/Drs/zUa5lbwrch/qnBAzdPLfMDcI8MMJ2Emy1AaF 4Nw6OLT1w5KP8jA+oOSsVqhA04b1L2xC3eoLGuufxlcKUGIaFX+rcgSVDKp1MnnC/5h1 KSIGh+1EhD/n4lvon3mbZOR0m/rQ29YC8TlnTTB+qIAY2C1Wbq1j1WCOrNFk8IuywR83 D+ZilhH3vksVoviw+faZQejQjUpRHK4+e1aMWTIXK+hkimIMV8s9dFVxhV1gZzC1YTpc o0qQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:content-type:mime-version:subject:from :in-reply-to:date:cc:content-transfer-encoding:message-id:references :to; bh=2m94rpaoJKfwItzMR2ZTC74CqNlEWsw2PcfX0nsjslg=; b=hrI5KZwKZVC3IV+Ci1+xvdbtqgpU+pMT2vD46hiGinKDwXYtH5UDuy+wC2No8nyLBL xquNopAuOZh6zyXG2wvRCuuvOhUupXiIs3K321tGWRxlK9OI4q8uJlPj92zbtv0IVYzA ZH/GA1OuHBL0O3dwZ9dlflD6zZEo95qEh0FfKOL3Tk7vs0/PV01d6hMP+Z0FIv0jYD/X 0qpxWWncLu4+rh7aw73tvvb8MWWxA+QfmGLMGhGGSwzboWbsCJVvqJQfkdnmpmtbNx61 tkpB35Np8HfaspLr85B0xbiBzNp3KeaEyANXxXNxSXiX8bv59wt/mxANuGDrNMcE8M0U M6jg== X-Gm-Message-State: AG10YOSjnl3bQFGJRTDVVKVG3ihawP/3wtaFqsrRJAMxFtjDVLJFhqedq0FS7t71ZdVj/g== X-Received: by 10.55.72.12 with SMTP id v12mr55065190qka.28.1455188758289; Thu, 11 Feb 2016 03:05:58 -0800 (PST) In-Reply-To: <56BB8005.8090708@mail.de> X-Mailer: Apple Mail (2.1510) X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.21rc2 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:102802 On Feb 10, 2016, at 1:23 PM, "Sven R. Kunze" wrote: > Hi Cem, >=20 > On 08.02.2016 02:37, Cem Karan wrote: >> My apologies for not writing sooner, but work has been quite busy = lately (and likely will be for some time to come). >=20 > no problem here. :) >=20 >> I read your approach, and it looks pretty good, but there may be one = issue with it; how do you handle the same item being pushed into the = heap more than once? In my simple simulator, I'll push the same object = into my event queue multiple times in a row. The priority is the moment = in the future when the object will be called. As a result, items don't = have unique priorities. I know that there are methods of handling this = from the client-side (tuples with unique counters come to mind), but if = your library can handle it directly, then that could be useful to others = as well. >=20 > I've pondered about that in the early design phase. I considered it a = slowdown for my use-case without benefit. >=20 > Why? Because I always push a fresh object ALTHOUGH it might be equal = comparing attributes (priority, deadline, etc.). >=20 >=20 > That's the reason why I need to ask again: why pushing the same item = on a heap? >=20 >=20 > Are we talking about function objects? If so, then your concern is = valid. Would you accept a solution that would involve wrapping the = function in another object carrying the priority? Would you prefer a = wrapper that's defined by xheap itself so you can just use it? Yes. I use priority queues for event loops. The items I push in are = callables (sometimes callbacks, sometimes objects with __call__()) and = the priority is the simulation date that they should be called. I push = the same item multiple times in a row because it will modify itself by = the call (e.g., the location of an actor is calculated by its velocity = and the date). There are certain calls that I tend to push in all at = once because the math for calculating when the event should occur is = somewhat expensive to calculate, and always returns multiple dates at = once. =20 That is also why deleting or changing events can be useful; I know that = at least some of those events will be canceled in the future, which = makes deleting useful. Note that it is also possible to cancel an event = by marking it as cancelled, and then simply not executing it when you = pop it off the queue, but I've found that there are a few cases in my = simulations where the number of dead events that are in the queue = exceeds the number of live events, which does have an impact on memory = and operational speed (maintaining the heap invariant). There isn't = much difference though, but I need FAST code to deal with size of my = simulations (thousands to tens of thousands of actors, over hundreds of = millions of simulations, which is why I finally had to give up on python = and switch to pure C). Having a wrapper defined by xheap would be ideal; I suspect that I won't = be the only one that needs to deal with this, so having it centrally = located would be best. It may also make it possible for you to optimize = xheap's behavior in some way. Thanks, Cem Karan=