From f38294b17301be7b7c3f0da0498cf6ea62f107dd Mon Sep 17 00:00:00 2001 From: Slava Kim Date: Fri, 21 Feb 2014 18:19:13 -0800 Subject: [PATCH] Binary-Heap: update in-place --- packages/binary-heap/max-heap.js | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/packages/binary-heap/max-heap.js b/packages/binary-heap/max-heap.js index 137d7271b4..ccc4ff3f36 100644 --- a/packages/binary-heap/max-heap.js +++ b/packages/binary-heap/max-heap.js @@ -130,14 +130,16 @@ _.extend(MaxHeap.prototype, { if (self.has(id)) { if (self.get(id) === value) return; - else { - self.remove(id); - } - } - self._heapIdx.set(id, self._heap.length); - self._heap.push({ id: id, value: value }); - self._upHeap(self._heap.length - 1); + var idx = self._heapIdx.get(id); + self._heap[idx].value = value; + self._upHeap(idx); + self._downHeap(idx); + } else { + self._heapIdx.set(id, self._heap.length); + self._heap.push({ id: id, value: value }); + self._upHeap(self._heap.length - 1); + } }, remove: function (id) { var self = this;