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 | 0f7452e6a709099cc722d9e7b6484b2cb12a417d (patch) | |
tree | 366cba51479d24ce0a6b6d1901e67c2bcec5928f /util | |
parent | 9ac3675494a9bf1c40781ef2c7954ba9c1993049 (diff) | |
download | prosody-0f7452e6a709099cc722d9e7b6484b2cb12a417d.tar.gz prosody-0f7452e6a709099cc722d9e7b6484b2cb12a417d.zip |
util.indexedbheap: Fix a possible traceback when removing the last item.
Diffstat (limited to 'util')
-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 |