Skip to content

Commit

Permalink
Make SortedArray work with arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
GarrisonJ committed Apr 30, 2024
1 parent bbd0eff commit 473b9e0
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
6 changes: 3 additions & 3 deletions lib/sorted_containers/sorted_array.rb
Original file line number Diff line number Diff line change
Expand Up @@ -526,7 +526,7 @@ def update(iterable)
#
# @return [Array] An array representation of the sorted array.
def to_a
@lists.flatten
@lists.flatten(1)
end

# Array is already sorted. Duplicates the sorted array and returns it.
Expand Down Expand Up @@ -591,7 +591,7 @@ def each(&block)
# @param value [Object] The value to bisect with.
# @return [Integer] The index where the value should be inserted.
def internal_bisect_left(array, value)
array.bsearch_index { |x| x >= value } || array.size
array.bsearch_index { |x| (x <=> value) >= 0 } || array.size
end

# Performs a right bisect on the array.
Expand All @@ -600,7 +600,7 @@ def internal_bisect_left(array, value)
# @param value [Object] The value to bisect with.
# @return [Integer] The index where the value should be inserted.
def internal_bisect_right(array, value)
array.bsearch_index { |x| x > value } || array.length
array.bsearch_index { |x| (x <=> value) == 1 } || array.length
end

# Gets the value at a given index. Supports negative indices.
Expand Down
8 changes: 8 additions & 0 deletions spec/sorted_array_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,14 @@
expect(array.to_a).to eq((1..1000).to_a)
end

it "should handle arrays of arrays" do
array = SortedContainers::SortedArray.new
array.add([1, 2])
array.add([3, 4])
array.add([5, 6])
expect(array.to_a).to eq([[1, 2], [3, 4], [5, 6]])
end

it "should return the array" do
array = SortedContainers::SortedArray.new
expect(array.add(5)).to eq(array)
Expand Down

0 comments on commit 473b9e0

Please sign in to comment.