Skip to content

Commit

Permalink
Fix delete_at bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
GarrisonJ committed Apr 24, 2024
1 parent 1e9c298 commit d194627
Showing 1 changed file with 22 additions and 14 deletions.
36 changes: 22 additions & 14 deletions lib/sorted_containers/sorted_array.rb
Original file line number Diff line number Diff line change
Expand Up @@ -558,41 +558,49 @@ def expand(pos)
#
# @param pos [Integer] The index of the sublist.
# @param idx [Integer] The index of the value to delete.
# @return [Object] The value that was deleted.
# rubocop:disable Metrics/AbcSize
# rubocop:disable Metrics/MethodLength
def internal_delete(pos, idx)
@lists[pos].delete_at(idx)
list = @lists[pos]
value = list.delete_at(idx)
@size -= 1
return unless @lists[pos].size < @load_factor >> 1

@maxes[pos] = @lists[pos].last
len_list = list.length

if @index.size.positive?
child = @offset + pos
while child.positive?
@index[child] -= 1
child = (child - 1) >> 1
if len_list > (@load_factor >> 1)
@maxes[pos] = list.last

if @index.size.positive?
child = @offset + pos
while child.positive?
@index[child] -= 1
child = (child - 1) >> 1
end
@index[0] -= 1
end
elsif @lists.length > 1
if pos.zero?
pos += 1
end
@index[0] -= 1
elsif @lists.size > 1
pos += 1 if pos.zero?

prev = pos - 1
@lists[prev].concat(@lists[pos])
@lists[prev].concat(list)
@maxes[prev] = @lists[prev].last

@lists.delete_at(pos)
@maxes.delete_at(pos)
@index.clear

expand(prev)
elsif @lists[pos].size.positive?
@maxes[pos] = @lists[pos].last
elsif len_list.positive?
@maxes[pos] = list.last
else
@lists.delete_at(pos)
@maxes.delete_at(pos)
@index.clear
end
value
end
# rubocop:enable Metrics/AbcSize
# rubocop:enable Metrics/MethodLength
Expand Down

0 comments on commit d194627

Please sign in to comment.