]> gitweb.factorcode.org Git - factor.git/commitdiff
heaps: add comments referring to pypy's heapq.py 2105/head
authorJon Harper <jon.harper87@gmail.com>
Thu, 10 Jan 2019 20:50:50 +0000 (21:50 +0100)
committerJon Harper <jon.harper87@gmail.com>
Thu, 10 Jan 2019 20:52:16 +0000 (21:52 +0100)
basis/heaps/heaps.factor

index 192b50e94a54512c425a7ae0581a8b6c5544ab7c..75406d76604bdd8e842b0c8f4dbad0586441ad1f 100644 (file)
@@ -6,6 +6,9 @@ kernel.private locals math math.order math.private sequences
 sequences.private summary vectors ;
 IN: heaps
 
+! names and optimizations copied from pypy's heapq.py,
+! refer to it from in depth explanations of the optimizations.
+
 GENERIC: heap-push* ( value key heap -- entry )
 GENERIC: heap-peek ( heap -- value key )
 GENERIC: heap-pop* ( heap -- )
@@ -86,6 +89,7 @@ M: heap heap-peek ( heap -- value key )
 
 <PRIVATE
 
+! called bubble-up in the litterature... but we keep pypy's name.
 :: sift-down ( heap from to -- )
     heap data>>      :> data
     to data data-nth :> tmp
@@ -115,6 +119,11 @@ M: heap heap-push*
 
 <PRIVATE
 
+! called bubble-down in the litterature... but we keep pypy's name.
+! A quote from pypy's implementation:
+! > We *could* break out of the loop as soon as we find a pos where newitem <=
+! > both its children, but turns out that's not a good idea [...]
+! Indeed the code is 33% slower if we remove this optmization.
 :: sift-up ( heap n -- )
     heap data>>     :> data
     data length     :> end