diff options
author | Waqas Hussain <waqas20@gmail.com> | 2014-04-23 11:38:34 -0400 |
---|---|---|
committer | Waqas Hussain <waqas20@gmail.com> | 2014-04-23 11:38:34 -0400 |
commit | 9c5384c1f1343bf2b8c60623f264869f4f1a0f07 (patch) | |
tree | 366cba51479d24ce0a6b6d1901e67c2bcec5928f | |
parent | b6f6c679f0ec7f0aee83e9dce2f18db4f4c166b1 (diff) | |
download | prosody-9c5384c1f1343bf2b8c60623f264869f4f1a0f07.tar.gz prosody-9c5384c1f1343bf2b8c60623f264869f4f1a0f07.zip |
util.indexedbheap: Fix a possible traceback when removing the last item.
-rw-r--r-- | util/indexedbheap.lua | 18 |
1 files changed, 11 insertions, 7 deletions
diff --git a/util/indexedbheap.lua b/util/indexedbheap.lua index 3cb03037..c60861e8 100644 --- a/util/indexedbheap.lua +++ b/util/indexedbheap.lua @@ -113,23 +113,27 @@ function indexed_heap:reprioritize(id, priority) k = _percolate_down(self.priorities, k, self.ids, self.index); end function indexed_heap:remove_index(k) - local size = #self.priorities; - local result = self.priorities[k]; + if result == nil then return; end + local result_sync = self.ids[k]; local item = self.items[result_sync]; - if result == nil then return; end - self.index[result_sync] = nil; - self.items[result_sync] = nil; + local size = #self.priorities; self.priorities[k] = self.priorities[size]; self.ids[k] = self.ids[size]; self.index[self.ids[k]] = k; + t_remove(self.priorities); t_remove(self.ids); - k = _percolate_up(self.priorities, k, self.ids, self.index); - k = _percolate_down(self.priorities, k, self.ids, self.index); + self.index[result_sync] = nil; + self.items[result_sync] = nil; + + if size > k then + k = _percolate_up(self.priorities, k, self.ids, self.index); + k = _percolate_down(self.priorities, k, self.ids, self.index); + end return result, item, result_sync; end |