Github user mateiz commented on a diff in the pull request:
https://github.com/apache/spark/pull/97#discussion_r11193261
--- Diff: python/pyspark/rdd.py ---
@@ -91,6 +92,58 @@ def __exit__(self, type, value, tb):
if _spark_stack_depth == 0:
self._context._jsc.setCallSite(None)
+class MaxHeapQ(object):
+ """
+ An implementation of MaxHeap.
+
+ """
+
+ def __init__(self):
+ # we start from q[1], this makes calculating children as trivial
as 2 * k
+ self.q = [0]
+
+ def _swim(self, k):
+ while (k > 1) and (self.q[k/2] < self.q[k]):
+ self._swap(k, k/2)
+ k = k/2
+
+ def _swap(self, i, j):
+ t = self.q[i]
+ self.q[i] = self.q[j]
+ self.q[j] = t
+
+ def _sink(self, k):
+ N=len(self.q)-1
+ while 2*k <= N:
+ j = 2*k
+ # Here we test if both children are greater than parent
+ # if not swap with larger one.
+ if j<N and self.q[j] < self.q[j+1]:
+ j = j+1
+ if(self.q[k] > self.q[j]):
+ break
+ self._swap(k, j)
+ k = j
+
+ def insert(self, value):
+ self.q.append(value)
+ self._swim(len(self.q) - 1)
+
+ def getQ(self):
+ return self.q[1:]
--- End diff --
Call this something like `getElements`, it's not clear what a `Q` would
mean and it's not really a queue either (because it's not FIFO)
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---